feat(tools): add read-only "git" toolset for structured repository inspection #3637
Let's just continue. @aheritier @Sayt-0
forbidigo bans context.Background() in tests repo-wide; t.Context() is required (see .golangci.yml). Applies to all 10 occurrences in this file (lines 52, 64, 76, 82, 97, 109, 122, 134, 141, 149). The linter only reports 3 of them because of max-same-issues: 3, so a local golangci-lint run after the fix is worth double-checking.
modernize flags this: strings.IndexByte + slice can be simplified with strings.Cut:
func firstLine(msg string) string {
first, _, _ := strings.Cut(strings.TrimSpace(msg), "\n")
return first
}
Inconsistency: ro is only used for git_branches, which as a result is the only tool without a Title ("Git Branches"). Either reuse ro for all five tools and set Title separately, or drop the variable and inline tools.ToolAnnotations{ReadOnlyHint: true, Title: "Git Branches"} like the other four.
limit has a default but no upper bound: limit: 1000000 on a large repository produces an unbounded string injected into the model context. A hard cap (for example 200, mirroring the maxBlameLines approach) would keep output size predictable.
With a hard 400-line cutoff and no range parameter, lines beyond 400 are unreachable ("who touched line 1200?" cannot be answered). Optional start_line / end_line args would fix this and reduce output size for targeted questions. Fine as a follow-up if preferred; worth noting in the docs page either way.
weight: 130 collides with docs/tools/mcp/index.md, making the sidebar order between the two pages non-deterministic. Existing pages use unique weights (scheduler took 135 to slot between 130 and 140). 25 is free and matches the placement next to filesystem / shell in the configuration index table; 125 works too.
Registration itself looks right (alphabetical, config ignored since the toolset has no options). Reminder tied to the general review: "git" also needs to be added to the Toolset.type enum in agent-schema.json, which is the one registration point not covered by the drift-guard test.
Summary
Useful addition: structured, read-only git context without the writable shell surface or a git binary, and the implementation follows the builtin-toolset patterns closely (creator + catalog + drift-guard, annotations, instructions, docs page, atomic conventional commits). Two blocking items prevent approval as-is; both are quick fixes. Inline comments follow.
Blocking
| # | Finding | Where | Impact |
| --- | --- | --- | --- |
| 1 | golangci-lint fails on the branch: 10x forbidigo (context.Background() in tests is banned, use t.Context()) and 1x modernize (strings.Cut). Note: the linter output shows only 3 of the 10 forbidigo hits because of max-same-issues: 3. | pkg/tools/builtin/git/git_test.go, pkg/tools/builtin/git/git.go | The ci / lint job will fail. The green checks currently on the PR are docs-only workflows; the Go CI has not run yet. |
| 2 | agent-schema.json is not updated: the Toolset.type enum does not include "git". | agent-schema.json (Toolset.type enum, near the scheduler / rag entries) | Editors validating configs against the schema flag type: git as invalid, and any future docs example using git fails pkg/config/schema_test.go. The catalog drift-guard was handled, but this second registration point was missed. |
Non-blocking suggestions
git_status on a freshly initialized repository (unborn HEAD) prints On branch (detached HEAD), and git_log returns Error: reading log: reference not found. Friendlier handling ("no commits yet") would help agents on brand-new repos.Worktree().Status() rehashes the whole worktree (slow on large repos) and Blame cost grows with history x file size; maxBlameLines truncates output only after the full computation.DetectDotGit walks up parent directories. When the working directory is not itself a repository but an ancestor is (for example a dotfiles-tracked home directory), git_show / git_blame expose the ancestor repository's full history and file contents. Worth an explicit note in the docs page, since filesystem confinement (allow/deny lists) does not apply here.git_log path filter, git_show with an explicit and an invalid ref, git_blame with rev, the 400-line truncation path, and CreateToolSet.Verdict
Request changes for items 1 and 2; everything else is optional. With those fixed, the toolset looks good to land as a read-only v1.
Done.
Done.
Done.
Done.
Done.
Done.
Done.
@Sayt-0 Thank you for the review - the reproduction detail (including the max-same-issues: 3 note, which would have had me chasing 3 of 10) made this quick to act on. I have pushed an update addressing both blocking items and all four suggestions.
Blocking
1. golangci-lint on the branch. Fixed both:
- forbidigo: replaced all 10 context.Background() occurrences in git_test.go with t.Context(). As you predicted the linter only showed 3, so I fixed them from the file rather than the report - and the context import became orphaned as a result, which I removed.
- modernize: firstLine now uses strings.Cut exactly as suggested.
2. agent-schema.json not updated. Added "git" to the toolset type enum - and to the anyOf branch for types requiring no extra fields, which is the part I would otherwise have missed. #3670 showed that adding only to the primary enum still fails with Must validate at least one schema (anyOf), so both registration points are covered.
Suggestions (all taken)
3. Unborn HEAD. A freshly initialized repo now reports On branch (no commits yet) from git_status, and git_log returns No commits yet. instead of Error: reading log: reference not found (checked via plumbing.ErrReferenceNotFound). Covered by TestGitUnbornHead.
4. limit upper bound. Capped at 200 (maxLogLimit), mirroring the maxBlameLines approach, and the parameter description now says so. TestGitLogLimitIsCapped asserts limit: 1000000 returns exactly 200 lines.
5. ro inconsistency. Dropped the variable and inlined tools.ToolAnnotations{ReadOnlyHint: true, Title: "Git Branches"} so all five tools are consistent and git_branches now has a Title.
6. Docs caveats. Added both:
- A warning that DetectDotGit walks up parent directories, so a non-repo working directory under a tracked ancestor (e.g. dotfiles in $HOME) resolves to that ancestor and exposes its history — and that the filesystem allow/deny lists do not apply. That one is worth having in writing; thank you for catching it.
- A performance note that Status() rehashes the worktree and Blame scales with history × file size, with the explicit point that maxBlameLines truncates only after the computation.
7. Test coverage gaps. Added tests for git_log path filter, git_show with an explicit ref and an invalid ref, git_blame with rev, the blame truncation path, and CreateToolSet (working-directory wiring).
8. Docs weight. Moved to 125 to resolve the collision with docs/tools/mcp/index.md (130).
On start_line / end_line for blame: I have left that out of v1 as you suggested, and noted the 400-line cutoff in the docs. Happy to add the range parameters in this PR if you would rather have them now.
Do let me know if any change is required please.
Fixed malformed agent-schema.json @Sayt-0.
looks good now thank you !
Approved these changes.
Sayt-0
dwin-gharibi
Closes #3636
---
Summary
Adds a new built-in toolset,
git, giving an agent structured, read-only access to the working repository: status, log, branches, commit details, and blame. Implemented withgo-git(already a direct dependency), so it needs nogitbinary and is fully unit-testable.Tools:
git_status,git_log,git_branches,git_show,git_blame.Motivation
Coding agents constantly need git context. Today the only option is the
shelltool with rawgit, which forces the model to parse free-form porcelain, exposes the full writable shell surface, and depends on agitbinary. A dedicated read-only toolset returns clean structured output, is safe by construction, and works withoutshellor the binary.What changed
| File | Change |
| --- | --- |
|
pkg/tools/builtin/git/git.go| New. The toolset: 5 read-only tools over go-git. ||
pkg/tools/builtin/git/git_test.go| New. Tests build throwaway repos with go-git and exercise every tool. ||
pkg/teamloader/toolsets/toolsets.go| Register thegitcreator. ||
pkg/teamloader/toolsets/catalog.go| Add thegitcatalog entry (required by the drift-guard test). ||
docs/configuration/tools/index.md| Addgitto the built-in toolsets table. ||
docs/tools/git/index.md| New. Dedicated docs page. |Design
add/commit/checkout— write ops are stateful, overlapshell, and raise permission questions; deferred to keep v1 safe.WorkingDirwithDetectDotGit.Example output
Testing
git_status(changed + clean),git_log(order +limit),git_branches(current marked),git_show(metadata + stats),git_blame(authorship), plus not-a-repo and missing-path error paths, and the tool-count/interface check.TestBuiltinToolsetsCatalogMatchesRegistrypasses withgitin both registry and catalog.All pass;
gofmt -landgo vetclean. (CI runstask lint/task test.)Scope / non-goals
git_showcovers commit-level diffs. Both are noted as possible future work.