Phase 1 of Nearle Buddy: an agent names a tool, and the registry decides whether that is allowed, whether the arguments make sense, who is asking, and what gets recorded — then runs a handler a person wrote and tested. No agent gets raw table access. The usual argument for tools over generated SQL is safety; here there is a harder one. The fields on this backend do not mean what their names say, and it is measured: orders.deliverystatus is an empty string on all 181 rows of tenant 1147, orders.orderstatus never carries the six middle delivery stages, deliveries.ridername holds statuses as often as names, deliverytype is empty on every row in production. A model writing SQL gets each of those wrong with no error — it reports a cancel rate from a column of empty strings and nobody can tell. A model calling a tool cannot, because the correction lives in the handler beside the measurement that justified it. Call does five things in order: find the tool, check the agent's allow-list, validate arguments, confirm the caller is scoped to something, run the handler — writing exactly one audit row whatever happens, refusals included. A trail of successes answers "did anything try to read another tenant?" with silence, which reads the same as no. The model has no say in whose data is read. stuck_orders has no tenantid field on its schema — absent, not rejected — and the tenant comes from the session claims added in the previous commit. Arguments the tool did not declare are dropped rather than passed on, so a model sending a `where` clause gets it discarded. stuck_orders: deliveries a rider was given and has not accepted, ten minutes for a look, twenty-five for somebody now. Derived from assigntime and orderstatus, so it does not depend on anyone having been watching. Carries the wait in minutes, what to do, where to check it, and what it covered. A capped answer says so — an empty result and a truncated one look identical to a model and it will call both "none". The audit sink writes to the log for now; a database sink is phase 8. Nothing calls the registry yet: the loop and the model gateway are phase 2. 37 tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Fiesta backend (nearle)
The Go/Fiber API behind the Nearle Daily merchant console, the customer app,
the rider app and the in-store POS terminals. Postgres (nearledb) for
tenants, stores, products, stock and orders; a separate pgvector database for
the global product catalogue; Redis for POS presence; MQTT for the tills.
This page is the map. Each section says what a thing is, how to use it, and where the detail lives.
Run it
export PATH="$PATH:$HOME/go/bin" # Go 1.24 lives there on the dev Macs
docker compose -f docker-compose.local.yml up -d # postgres :5433, pgvector :5434, redis :6379
go run . # APP_ENV unset → .env.local, listens on :1122
go test ./...
An empty database is not enough — startup runs migrations that assume the
live schema. init/README.md explains loading a schema dump first.
Startup prints what it loaded and where it is pointed:
config: loaded .env.local
config: APP_ENV=local, listening on :1122, database nearle@localhost:5433/nearledb
scan: product search uses openai/all-minilm # or: EMBEDDING_PROVIDER not set, text-only
Configuration
Everything comes from environment variables, read once by config.Load().
| You want to… | Do this |
|---|---|
| Run locally | Nothing — .env.local is loaded by default |
| Run against production settings | APP_ENV=production go run . (⚠ every write is real) |
| See every variable and what it does | .env.example |
| Add a new setting | Add it to config.Config + Load() + .env.example, and to the cluster (nearle-config ConfigMap or app-secrets Secret in namespace nearle) — a variable in the file and not in the cluster is unset in production |
| Find out why it won't boot | Read the message — it lists every missing variable at once |
Precedence is real environment > .env.<APP_ENV> > .env. The container gets
no .env file at all (.dockerignore); the Dockerfile sets
APP_ENV=production and the values come from Kubernetes.
Full detail, including the committed-credentials situation:
docs/ENVIRONMENT.md.
Layout
main.go boot: config → databases → migrations → routes → MQTT → listen
config/ env-file loading, typed Config, validation
db/ Postgres (nearledb + catalogue), Redis, S3 image store
facade/ wires repositories → services → controllers (add new modules here)
routes/ one file per module; /live/api/v1/web/... (console) and /v1/mob/... (apps)
controllers/ HTTP in, HTTP out — parse, call the service, shape the envelope
services/ the rules; no SQL, no HTTP
repositories/ the SQL; nothing else
models/ request/response and table shapes
messaging/ MQTT ingest from tills, console live stream
utils/ small shared helpers (tokens, geo, embeddings, geocoding)
docs/ integration specs for the frontends and handoff notes
scratch/ one-off read/verify tools run with `go run ./scratch/<name>`
init/ schema/seed for the local database
Every response uses the same envelope:
{ "code": 200, "status": true, "message": "…", "details": … }.
Business outcomes ("out of stock", "not registered") are 200s with a reason
in the body; HTTP errors mean the request could not be served at all.
Adding an endpoint
- Model the request/response in
models/. - Repository method(s) in
repositories/— SQL only, take acontext.Context, returnerror. - Service in
services/— the rules, with sentinel errors (ErrXxxBadRequest,ErrXxxNotFound) the controller can map to statuses. - Controller in
controllers/—BodyParser/Query, call the service, map sentinel errors to 400/404/503, everything else to 500. - Routes file in
routes/, registered inroutes/routes.go. - Wire it in
facade/container.go. - Test the service with a fake repository (see
services/scan_test.gofor the pattern) — no database needed.
services/scanService.go + controllers/scanController.go are a complete,
current example of all seven.
Features with their own docs
| Feature | For | Doc |
|---|---|---|
| Environment & deployment | everyone | docs/ENVIRONMENT.md |
| Scan-to-order (camera → product → nearest store with stock) | mobile app | docs/SCAN_TO_ORDER.md |
| Catalogue import into a store | console | docs/CATALOGUE_IMPORT_INTEGRATION.md |
| POS terminal ingest, login, API | POS / tills | docs/POS_*.md |
| Access-control audit and what is still open | everyone | docs/SECURITY_HANDOFF.md |
Things to know before you get surprised
- There is no auth layer.
customerid/tenantidin a request are trusted.docs/SECURITY_HANDOFF.md§1 is the standing issue. - Login errors mean what they say.
409 Invalid Email= the query ran and matched nobody.500 Login is temporarily unavailable= the database could not answer (it used to be reported as Invalid Email; seeservices/userService.golookupLogin). - Stock is a ledger. Live stock is always
SUM(in) − SUM(out)ofproductstocksat an outlet, never a stored number. Filter on the same expression you display (services/productVisibility.goexplains why). - The catalogue is a different database and must never be reached
through the
nearledbhandle. Its per-brand tables are discovered frominformation_schema; brands appear and columns vary. - Catalogue ids are not stable across re-scrapes;
imageidis the durable key (models.Products.Imageid). - Migrations run on boot and are guarded by
IF NOT EXISTS/ schema checks, not a version table. Read the comments inmain.gobefore adding one — several have bitten before. - One MQTT client id per replica. A second connection with the same id
evicts the first. Never run a local process with the production
MQTT_URL. scratch/tools read production when run with.env.production, and most are read-only. Two are not —termbackfillandcataloguefactsbackfillrepair rows that no endpoint can reach. Both default to a dry run that prints every change and write only when passedapply, and both print the SQL to undo themselves afterwards. A new tool that writes follows that shape or it does not write.
Operations cheat-sheet (Kubernetes, namespace nearle)
kubectl get pods -n nearle # fiesta-0/1/2 (StatefulSet)
kubectl logs -n nearle fiesta-0 | grep -E 'config:|scan:|pos:'
kubectl exec -n nearle fiesta-0 -- env | grep EMBEDDING_
kubectl set env statefulset/fiesta -n nearle KEY=value # adds a var and rolls the pods
kubectl rollout status statefulset/fiesta -n nearle
The embedding model behind scan-to-order is served by the cluster's Ollama
(ollama.krow.svc.cluster.local:11434); docs/SCAN_TO_ORDER.md has the
exact settings and why that model.