# Local Setup

## Prerequisites

- Go 1.22+
- Make
- Optional: PM2
- Optional: Docker (for Redis via compose)
- Redis at `redis://localhost:6379` for `/readyz` when running outside compose (config default)
- MongoDB at `mongodb://localhost:27018` (workspace compose; database `adpilot_code_parser`)
- `GIT_WORKSPACE_PATH` directory (default `./data/repos`; created on startup if missing; `/readyz` requires it to exist and be readable)

## Environment

Copy the template and adjust paths if needed:

```sh
cd adpilot-indexing-code-parser.com
cp env.example .env
```

| Variable | Purpose |
| --- | --- |
| `GIT_WORKSPACE_PATH` | Repo Sync clone directory; `/readyz` checks it exists and is readable (`./data/repos`) |
| `MONGO_URI` | MongoDB connection (default `mongodb://localhost:27018/adpilot_code_parser`) |
| `MONGODB_DATABASE` | Parser DB (`adpilot_code_parser`) |
| `MONGO_ARTIFACT_MAX_PART_MB` | Max BSON size per stored graph chunk (default `10`; MongoDB hard limit is 16 MB) |
| `DOCS_MONGODB_DATABASE` | Docs DB for finalization reads (`repo_docs`) |
| `INDEXING_RUNS_DATABASE` | Fan-in counters (`adpilot_repo_sync`) |
| `REDIS_URL` | Redis connection; `/readyz` checks PING (default `redis://localhost:6379`) |
| `REDIS_STREAM_MAX_RETRIES` | DLQ threshold for stream consumers (default `5`) |
| `PARSER_WORKERS` | Parse worker pool size (defaults to CPU count) |
| `PARSER_VERSION` | Emitted on merged `graph.artifact.ready` |

Point `GIT_WORKSPACE_PATH` at the directory Repo Sync writes clones to so the parser can read source files from `files.changed` events.

## Run One Service

```sh
cd adpilot-indexing-code-parser.com
make tidy
make dev
```

When running with `make dev`, start Redis locally (see Docker Compose below or use Repo Sync's Redis).

## Docker Compose

Self-contained stack with Redis 7:

```sh
cd adpilot-indexing-code-parser.com
docker compose up --build -d
curl -sf http://localhost:6002/healthz
curl -sf http://localhost:6002/readyz
docker compose down
```

The app service waits for Redis health before starting and uses `REDIS_URL=redis://redis:6379` inside the compose network.

### Shared Redis with Repo Sync

Host port `6379` conflicts if both compose files expose Redis. Options:

1. **Code Parser stack only** — use `docker compose up` in this repo.
2. **Repo Sync Redis only** — start Redis from repo-sync, run code-parser on the host:

```sh
cd ../adpilot-indexing-repo-sync.com
docker compose up redis -d

cd ../adpilot-indexing-code-parser.com
REDIS_URL=redis://localhost:6379 make dev
```

Do not start both Redis services on host port `6379` at the same time.

## Build And Test

```sh
make build
make test
make test-integration   # requires Redis; see docs/testing.md
make lint
```

## Run With PM2

```sh
pm2 start ecosystem.config.json
pm2 logs code-parser-service
pm2 stop ecosystem.config.json
```

## Verify

```sh
curl http://localhost:6002/healthz
curl http://localhost:6002/readyz
curl http://localhost:6002/version
```

`/readyz` returns 200 when Redis responds to PING, MongoDB responds to ping, and the git workspace is readable.

```sh
# After a sync + parse cycle (requires workspace Mongo on 27018)
mongosh "mongodb://localhost:27018/adpilot_code_parser" --eval 'db.code_graph_artifacts.countDocuments({})'
mongosh "mongodb://localhost:27018/adpilot_code_parser" --eval 'db.snapshot_graphs.find().limit(1).pretty()'
```

### Debug APIs (after a parse job runs)

```sh
# Replace job_id / artifact_id with values from logs or parse output
curl http://localhost:6002/parse-jobs/job_snap_01_<hash>
curl http://localhost:6002/artifacts/art_snap_01_<hash>
curl http://localhost:6002/diagnostics/job_snap_01_<hash>
```

See [`api.md`](api.md) for response shapes.

### Integration tests with compose

```sh
docker compose up --build -d
REDIS_URL=redis://localhost:6379 make test-integration
docker compose down
```

## Notes

- Full wiring lives in `internal/app/app.go` (Redis client, consumer, publisher, Mongo store, parser service).
- Event pipeline documented in [`events.md`](events.md).
