KakeiBuddy Developer Guide

This guide is for developers setting up, debugging, or extending KakeiBuddy.

Prerequisites

Backend Setup

cd backend
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --reload

The backend starts at http://localhost:8000.

Useful URLs:

On startup, the backend:

AI Coach Dependencies

backend/requirements.txt is enough to run the API with fallback coach suggestions. To use the trained DDPG policy instead of fallback rules, install the AI dependency set:

cd backend
pip install -r requirements-ai.txt

The trained policy file is expected at:

backend/ai/ddpg_budget_policy.zip

If the model or AI dependencies are unavailable, /coach/suggestions still returns suggestions with fallback_used: true.

Mobile Setup

cd mobile
npm install
npm start

Available scripts:

Command Purpose
npm start Starts Expo development server
npm run android Runs Android native app
npm run ios Runs iOS native app
npm run web Starts Expo web
npm run lint Runs Expo lint
npm run reset-project Expo starter reset script, avoid unless intentionally resetting app structure

Environment Variables

Backend

The backend loads .env.local from the backend/ working directory.

Variable Purpose Default
DATABASE_URL Full database URL. Render Postgres URLs are normalized to asyncpg. Local SQLite
DB_HOST PostgreSQL host when not using DATABASE_URL. None
DB_USER PostgreSQL user. postgres
DB_PASSWORD PostgreSQL password. None
DB_PASSWORD_FILE File containing PostgreSQL password. None
DB_NAME PostgreSQL database name. kakeibuddy
DB_PORT PostgreSQL port. 5432
ADMIN_SECRET_KEY FastAdmin secret key. gwapoak in local code default
ADMIN_PASSWORD Seeded admin password for user admin. changeme
RESEARCH_EXPORT_TOKEN Required header token for admin research export. None

Mobile

Variable Purpose
EXPO_PUBLIC_API_URL Preferred backend base URL, for example http://localhost:8000
EXPO_PUBLIC_RESEARCH_TRACK Default research track, usually tale or gala

If EXPO_PUBLIC_API_URL is not set, mobile/utils/api.js tries Expo host discovery, Android emulator 10.0.2.2, and localhost.

Local Data Model

The mobile app uses Expo SQLite database kakeibuddy.db. Important local tables include:

Local category seed data is inserted during initDB.

Backend Structure

Path Responsibility
backend/main.py FastAPI app, startup seeding, middleware, route registration
backend/routes/ HTTP endpoints
backend/services/ Business logic
backend/models/ SQLAlchemy models
backend/schemas/ Pydantic request and response schemas
backend/db/ Database connection and schema migration helpers
backend/ai/ AI policy inference wrapper and model artifact
backend/admin/ FastAdmin model registration

Mobile Structure

Path Responsibility
mobile/app/ Expo Router screens and layouts
mobile/app/(tabs)/ Main app tabs
mobile/components/ Reusable UI components
mobile/utils/ API, notifications, local storage, research, game, and budget helpers
mobile/utils/database/ SQLite create, select, sync, and destroy helpers
mobile/assets/ App icons and images
mobile/constants/ Shared styling constants

Data Flow

  1. Users create local budget data in the mobile app.
  2. Mobile SQLite stores transactions and summaries on device.
  3. Local utilities compute monthly metrics, challenge metrics, and dashboard values.
  4. When Sync Mode or research flows require backend communication, the app calls FastAPI through apiFetch.
  5. Backend persists aggregate summaries, challenge progress, consent, research responses, and events.
  6. Coach suggestions are requested from /coach/suggestions.

Common Development Workflows

Run backend and mobile together:

cd backend
uvicorn main:app --reload

In another terminal:

cd mobile
npm start

Run mobile lint:

cd mobile
npm run lint

Run the model smoke test:

cd model
pip install -r requirements.txt
python test_infer.py

The standalone model/requirements.txt may install heavier ML packages. For normal backend development, prefer backend/requirements-ai.txt.

Coding Notes