|
| 1 | +# AI Agent Instructions for Kubo |
| 2 | + |
| 3 | +This file provides instructions for AI coding agents working on the [Kubo](https://github.com/ipfs/kubo) codebase (the Go implementation of IPFS). Follow the [Developer Guide](docs/developer-guide.md) for full details. |
| 4 | + |
| 5 | +## Quick Reference |
| 6 | + |
| 7 | +| Task | Command | |
| 8 | +|-------------------|----------------------------------------------------------| |
| 9 | +| Tidy deps | `make mod_tidy` (run first if `go.mod` changed) | |
| 10 | +| Build | `make build` | |
| 11 | +| Unit tests | `go test ./... -run TestName -v` | |
| 12 | +| Integration tests | `make build && go test ./test/cli/... -run TestName -v` | |
| 13 | +| Lint | `make -O test_go_lint` | |
| 14 | +| Format | `go fmt ./...` | |
| 15 | + |
| 16 | +## Project Overview |
| 17 | + |
| 18 | +Kubo is the reference implementation of IPFS in Go. Most IPFS protocol logic lives in [boxo](https://github.com/ipfs/boxo) (the IPFS SDK); kubo wires it together and exposes it via CLI and HTTP RPC API. If a change belongs in the protocol layer, it likely belongs in boxo, not here. |
| 19 | + |
| 20 | +Key directories: |
| 21 | + |
| 22 | +| Directory | Purpose | |
| 23 | +|--------------------|----------------------------------------------------------| |
| 24 | +| `cmd/ipfs/` | CLI entry point and binary | |
| 25 | +| `core/` | core IPFS node implementation | |
| 26 | +| `core/commands/` | CLI command definitions | |
| 27 | +| `core/coreapi/` | Go API implementation | |
| 28 | +| `client/rpc/` | HTTP RPC client | |
| 29 | +| `plugin/` | plugin system | |
| 30 | +| `repo/` | repository management | |
| 31 | +| `test/cli/` | Go-based CLI integration tests (preferred for new tests) | |
| 32 | +| `test/sharness/` | legacy shell-based integration tests | |
| 33 | +| `docs/` | documentation | |
| 34 | + |
| 35 | +Other key external dependencies: [go-libp2p](https://github.com/libp2p/go-libp2p) (networking), [go-libp2p-kad-dht](https://github.com/libp2p/go-libp2p-kad-dht) (DHT). |
| 36 | + |
| 37 | +## Go Style |
| 38 | + |
| 39 | +Follow these Go style references: |
| 40 | + |
| 41 | +- [Go Code Review Comments](https://go.dev/wiki/CodeReviewComments) |
| 42 | +- [Google Go Style Decisions](https://google.github.io/styleguide/go/decisions) |
| 43 | + |
| 44 | +Specific conventions for this project: |
| 45 | + |
| 46 | +- check the Go version in `go.mod` and use idiomatic features available at that version |
| 47 | +- readability over micro-optimization: clear code is more important than saving microseconds |
| 48 | +- prefer standard library functions and utilities over writing your own |
| 49 | +- use early returns and indent the error flow, not the happy path |
| 50 | +- use `slices.Contains`, `slices.DeleteFunc`, and the `maps` package instead of manual loops |
| 51 | +- preallocate slices and maps when the size is known: `make([]T, 0, n)` |
| 52 | +- use `map[K]struct{}` for sets, not `map[K]bool` |
| 53 | +- receiver names: single-letter abbreviations matching the type (e.g., `s *Server`, `c *Client`) |
| 54 | +- run `go fmt` after modifying Go source files, never indent manually |
| 55 | + |
| 56 | +### Error Handling |
| 57 | + |
| 58 | +- wrap errors with `fmt.Errorf("context: %w", err)`, never discard errors silently |
| 59 | +- use `errors.Is` / `errors.As` for error checking, not string comparison |
| 60 | +- never use `panic` in library code; only in `main` or test helpers |
| 61 | +- return `nil` explicitly for the error value on success paths |
| 62 | + |
| 63 | +### Canonical Examples |
| 64 | + |
| 65 | +When adding or modifying code, follow the patterns established in these files: |
| 66 | + |
| 67 | +- CLI command structure: `core/commands/dag/dag.go` |
| 68 | +- CLI integration test: `test/cli/dag_test.go` |
| 69 | +- Test harness usage: `test/cli/harness/` package |
| 70 | + |
| 71 | +## Building |
| 72 | + |
| 73 | +Always run commands from the repository root. |
| 74 | + |
| 75 | +```bash |
| 76 | +make mod_tidy # update go.mod/go.sum (use this instead of go mod tidy) |
| 77 | +make build # build the ipfs binary to cmd/ipfs/ipfs |
| 78 | +make install # install to $GOPATH/bin |
| 79 | +make -O test_go_lint # run linter (use this instead of golangci-lint directly) |
| 80 | +``` |
| 81 | + |
| 82 | +If you modify `go.mod` (add/remove/update dependencies), you must run `make mod_tidy` first, before building or testing. Use `make mod_tidy` instead of `go mod tidy` directly, as the project has multiple `go.mod` files. |
| 83 | + |
| 84 | +If you modify any `.go` files outside of `test/`, you must run `make build` before running integration tests. |
| 85 | + |
| 86 | +## Testing |
| 87 | + |
| 88 | +The full test suite is composed of several targets: |
| 89 | + |
| 90 | +| Make target | What it runs | |
| 91 | +|----------------------|-----------------------------------------------------------------------| |
| 92 | +| `make test` | all tests (`test_go_fmt` + `test_unit` + `test_cli` + `test_sharness`) | |
| 93 | +| `make test_short` | fast subset (`test_go_fmt` + `test_unit`) | |
| 94 | +| `make test_unit` | unit tests with coverage (excludes `test/cli`) | |
| 95 | +| `make test_cli` | CLI integration tests (requires `make build` first) | |
| 96 | +| `make test_sharness` | legacy shell-based integration tests | |
| 97 | +| `make test_go_fmt` | checks Go source formatting | |
| 98 | +| `make -O test_go_lint` | runs `golangci-lint` | |
| 99 | + |
| 100 | +During development, prefer running a specific test rather than the full suite: |
| 101 | + |
| 102 | +```bash |
| 103 | +# run a single unit test |
| 104 | +go test ./core/... -run TestSpecificUnit -v |
| 105 | + |
| 106 | +# run a single CLI integration test (requires make build first) |
| 107 | +go test ./test/cli/... -run TestSpecificCLI -v |
| 108 | +``` |
| 109 | + |
| 110 | +### Environment Setup for Integration Tests |
| 111 | + |
| 112 | +Before running `test_cli` or `test_sharness`, set these environment variables from the repo root: |
| 113 | + |
| 114 | +```bash |
| 115 | +export PATH="$PWD/cmd/ipfs:$PATH" |
| 116 | +export IPFS_PATH="$(mktemp -d)" |
| 117 | +``` |
| 118 | + |
| 119 | +- `PATH`: integration tests use the `ipfs` binary from `PATH`, not Go source directly |
| 120 | +- `IPFS_PATH`: isolates test data from `~/.ipfs` or other running nodes |
| 121 | + |
| 122 | +If you see "version (N) is lower than repos (M)", the `ipfs` binary in `PATH` is outdated. Rebuild with `make build` and verify `PATH`. |
| 123 | + |
| 124 | +### Running Sharness Tests |
| 125 | + |
| 126 | +Sharness tests are legacy shell-based tests. Run individual tests with a timeout: |
| 127 | + |
| 128 | +```bash |
| 129 | +cd test/sharness && timeout 60s ./t0080-repo.sh |
| 130 | +``` |
| 131 | + |
| 132 | +To investigate a failing test, pass `-v` for verbose output. In this mode, daemons spawned by the test are not shut down automatically and must be killed manually afterwards. |
| 133 | + |
| 134 | +### Cleaning Up Stale Daemons |
| 135 | + |
| 136 | +Before running `test/cli` or `test/sharness`, stop any stale `ipfs daemon` processes owned by the current user. Leftover daemons hold locks and bind ports, causing test failures: |
| 137 | + |
| 138 | +```bash |
| 139 | +pkill -f "ipfs daemon" |
| 140 | +``` |
| 141 | + |
| 142 | +### Writing Tests |
| 143 | + |
| 144 | +- all new integration tests go in `test/cli/`, not `test/sharness/` |
| 145 | +- if a `test/sharness` test needs significant changes, remove it and add a replacement in `test/cli/` |
| 146 | +- use [testify](https://github.com/stretchr/testify) for assertions (already a dependency) |
| 147 | +- for Go 1.25+, use `testing/synctest` when testing concurrent code (goroutines, channels, timers) |
| 148 | +- reuse existing `.car` fixtures in `test/cli/fixtures/` when possible; only add new fixtures when the test requires data not covered by existing ones |
| 149 | +- always re-run modified tests locally before submitting to confirm they pass |
| 150 | +- avoid emojis in test names and test log output |
| 151 | + |
| 152 | +## Before Submitting |
| 153 | + |
| 154 | +Run these steps in order before considering work complete: |
| 155 | + |
| 156 | +1. `make mod_tidy` (if `go.mod` changed) |
| 157 | +2. `go fmt ./...` |
| 158 | +3. `make build` (if non-test `.go` files changed) |
| 159 | +4. `make -O test_go_lint` |
| 160 | +5. `go test ./...` (or the relevant subset) |
| 161 | + |
| 162 | +## Documentation and Commit Messages |
| 163 | + |
| 164 | +- after editing CLI help text in `core/commands/`, verify width: `go test ./test/cli/... -run TestCommandDocsWidth` |
| 165 | +- config options are documented in `docs/config.md` |
| 166 | +- changelogs in `docs/changelogs/`: only edit the Table of Contents and the Highlights section; the Changelog and Contributors sections are auto-generated and must not be modified |
| 167 | +- follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) |
| 168 | +- keep commit titles short and messages terse |
| 169 | + |
| 170 | +## Writing Style |
| 171 | + |
| 172 | +When writing docs, comments, and commit messages: |
| 173 | + |
| 174 | +- avoid emojis in code, comments, and log output |
| 175 | +- keep an empty line before lists in markdown |
| 176 | +- use backticks around CLI commands, paths, environment variables, and config options |
| 177 | + |
| 178 | +## PR Guidelines |
| 179 | + |
| 180 | +- explain what changed and why in the PR description |
| 181 | +- include test coverage for new functionality and bug fixes |
| 182 | +- run `make -O test_go_lint` and fix any lint issues before submitting |
| 183 | +- verify that `go test ./...` passes locally |
| 184 | +- when modifying `test/sharness` tests significantly, migrate them to `test/cli` instead |
| 185 | +- end the PR description with a `## References` section listing related context, one link per line |
| 186 | +- if the PR closes an issue in `ipfs/kubo`, each closing reference should be a bullet starting with `Closes`: |
| 187 | + |
| 188 | +```markdown |
| 189 | +## References |
| 190 | + |
| 191 | +- Closes https://github.com/ipfs/kubo/issues/1234 |
| 192 | +- Closes https://github.com/ipfs/kubo/issues/5678 |
| 193 | +- https://discuss.ipfs.tech/t/related-topic/999 |
| 194 | +``` |
| 195 | + |
| 196 | +## Scope and Safety |
| 197 | + |
| 198 | +Do not modify or touch: |
| 199 | + |
| 200 | +- files under `test/sharness/lib/` (third-party sharness test framework) |
| 201 | +- CI workflows in `.github/` unless explicitly asked |
| 202 | +- auto-generated sections in `docs/changelogs/` (Changelog and Contributors are generated; only TOC and Highlights are human-edited) |
| 203 | + |
| 204 | +Do not run without being asked: |
| 205 | + |
| 206 | +- `make test` or `make test_sharness` (full suite is slow; prefer targeted tests) |
| 207 | +- `ipfs daemon` without a timeout |
| 208 | + |
| 209 | +## Running the Daemon |
| 210 | + |
| 211 | +Always run the daemon with a timeout or shut it down promptly: |
| 212 | + |
| 213 | +```bash |
| 214 | +timeout 60s ipfs daemon # auto-kill after 60s |
| 215 | +ipfs shutdown # graceful shutdown via API |
| 216 | +``` |
| 217 | + |
| 218 | +Kill dangling daemons before re-running tests: `pkill -f "ipfs daemon"` |
0 commit comments