Skip to content

fix(git): append missing patterns to .gitignore on github init (#458) - #461

Merged
vybe merged 2 commits into
mainfrom
feature/458-gitignore-merge
Apr 22, 2026
Merged

vybe merged 2 commits into
mainfrom
feature/458-gitignore-merge

Conversation

@vybe

@vybe vybe commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • initialize_git_in_container used cat > .gitignore <<EOF (truncate-and-write) with a shell/cache-only pattern list, so any workspace .gitignore was clobbered and .env / .mcp.json (written by inject_credentials) leaked on the initial commit to GitHub.
  • Replaces the write with an append-if-missing merge (touch .gitignore + grep -qxF <p> .gitignore || echo <p> >> .gitignore per pattern). Idempotent; preserves any pre-existing workspace rules.
  • Adds just the three patterns the bug report named — .env, .env.*, .mcp.json — alongside the existing 8 shell/cache entries. Runs for both /home/developer and the legacy /home/developer/workspace path (previously skipped).

Replaces PR #460 (auto-closed when the prior branch was deleted). That PR took a broader deny-list + base64 scaffolding; this is the narrow scope the bug report asked for.

Changes

  • src/backend/services/git_service.py — new _GITIGNORE_PATTERNS tuple + _build_gitignore_merge_command() helper, call site hoisted out of the /home/developer-only guard.
  • tests/unit/test_github_init_gitignore.py — 2 tests that run the real bash command against a temp dir: (1) pre-existing .gitignore preserved; (2) fresh agent gets .env / .env.* / .mcp.json ignored.
  • docs/memory/feature-flows/github-repo-initialization.md — code snippet, expected-state checklist, and bug-fix note updated.

Test plan

  • pytest tests/unit/test_github_init_gitignore.py -v — both new tests pass.
  • pytest tests/unit/test_github_init_push.py -v — pre-existing 4 tests still pass.
  • Manual: deploy an agent → inject_credentials writes .env and .mcp.json → initialize_github_sync → inspect the "Initial commit from Trinity Agent" on GitHub, verify .env and .mcp.json are NOT in the tree, .gitignore contains both patterns.
  • Manual (preservation): pre-seed /home/developer/.gitignore with node_modules/\nbuild/\n → initialize_github_sync → verify those lines survived and the three credential patterns are appended.

Closes #458

Generated with Claude Code

vybe and others added 2 commits April 22, 2026 16:12
initialize_git_in_container wrote `.gitignore` with `cat > .gitignore`
(truncate-and-write) containing only shell/cache entries. Two
consequences:

1. Any workspace-supplied `.gitignore` (e.g. the one `/trinity:onboard`
   writes) was clobbered before `git add .`, so even a correct
   workspace deny-list gave no protection.
2. The hardcoded list did not contain `.env` or `.mcp.json`. Agents
   that had credentials materialized via `inject_credentials` after
   deploy leaked both files on the "Initial commit from Trinity Agent"
   push. Force-pushing the branch hides it from `main` but leaves
   retrievable orphan commits for ~30 days — a forced credential
   rotation event on a private repo and catastrophic on a public one.

The `/home/developer/workspace` legacy path skipped the `.gitignore`
step entirely, so legacy agents leaked everything in the tree.

Fix (src/backend/services/git_service.py):

- New `_GITIGNORE_PATTERNS` tuple: the prior 8 shell/cache entries
  (.bash_logout, .bashrc, .profile, .bash_history, .cache/, .local/,
  .npm/, .ssh/) plus the three files the bug report named — `.env`,
  `.env.*`, `.mcp.json`. Deliberately narrow; broader deny-lists
  (*.pem, credentials.json, etc.) are not added here.
- New `_build_gitignore_merge_command(git_dir)` helper: emits
  `touch .gitignore && (grep -qxF -- <p> .gitignore || echo <p> >> .gitignore)`
  per pattern, so pre-existing entries (user-supplied or from a prior
  run) are preserved. Idempotent — second run is a no-op. `shlex.quote`
  handles outer/inner quoting.
- Call site hoisted out of the `if git_dir == "/home/developer":`
  guard so the legacy `/home/developer/workspace` path is protected too.

Regression tests (tests/unit/test_github_init_gitignore.py):

- `test_preserves_preexisting_gitignore`: a workspace `.gitignore`
  with user rules (node_modules/, build/, *.log) survives the merge
  and the three credential patterns are present afterward.
- `test_fresh_agent_ignores_env_and_mcp_json`: with no pre-existing
  `.gitignore`, the merged file contains `.env`, `.env.*`, `.mcp.json`.

Both tests execute the real bash command against a temp directory
(via subprocess), so shell-quoting bugs surface here rather than in
production.

Closes #458

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Single-line Recent Updates row pointing at github-repo-initialization.md,
which was updated in 543d45f.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

bug: initialize_github_sync commits .env and .mcp.json despite .gitignore

1 participant