Build, extend, and debug Kasal. This guide covers local setup, the backend and frontend architecture rules, and how tests and CI check your change.
- Components you'll touch
- Before you begin
- Run the stack locally
- Authentication in local development
- Choose a database
- Developer architecture overview
- Backend architecture
- Frontend architecture
- End-to-end flow
- Test and lint your change
- Frontend (React SPA): UI, designer, monitoring
- API (FastAPI): REST endpoints and validation
- Services: Orchestration and business logic
- Repositories: DB and external I/O (Databricks, Vector, MLflow)
- Execution: Kasal's own agent runtime (
services/execution/) behind three paths: Chat, Agent Builder and Flow Builder - Data & Storage: SQLAlchemy models/sessions, embeddings, volumes
Tools and versions you need before running the stack.
- Python 3.11 (the backend pins
>=3.11,<3.12) and uv. Dependencies are declared insrc/backend/pyproject.tomland pinned insrc/backend/uv.lock; there is norequirements.txt. - Node.js 22 (what CI uses) and npm.
- SQLite needs nothing extra and is what
run.shuses by default. PostgreSQL is optional locally. - Databricks access if you exercise Databricks features.
Install the backend dependencies and start the server from src/backend:
cd src/backend
uv sync --frozen # creates src/backend/.venv from exactly what uv.lock pins
./run.sh # SQLite; ./run.sh postgres for PostgreSQLAlways pass --frozen. A plain uv sync re-resolves against whatever package index your machine is configured for and can rewrite uv.lock with that index's URLs; --frozen installs the committed lock as it is. run.sh and CI both use uv sync --frozen.
To change a dependency, edit src/backend/pyproject.toml, run uv lock, check that the uv.lock diff only points at public registries, then run uv sync --frozen. Never edit uv.lock by hand.
run.sh does the following, in order:
- Stops Kasal processes left over from a previous run of this checkout: the
uvicornreload parent, orphaned crew and flow subprocesses, and theirmultiprocessingresource trackers. It only touches processes whose working directory is insidesrc/backend. - Checks the port (
KASAL_PORT, default8000). If something else still holds it,run.shprints the owning PID and command and refuses to start, unlessKASAL_KILL_PORT_OWNER=true, in which case it sends that process SIGTERM. - Runs
uv sync --frozen. If the sync fails, it prints the error and continues with the existing.venv, or exits if there is none. - Exports
LOCAL_DEV_AUTH=true(unless you set it), then starts.venv/bin/uvicorn src.main:app --reload --reload-dir srconKASAL_BIND_HOST(default127.0.0.1).
Binding to anything other than loopback prints a warning, because with LOCAL_DEV_AUTH on, anyone who can reach the port acts as the development user. Run ./run.sh -h for the logging flags (-q, -v, -d, --no-console, --no-file) and the KASAL_LOG_* variables.
Start the frontend in a second terminal:
cd src/frontend
npm ci
npm start # Vite dev server on http://localhost:3000In development the frontend calls the backend directly at http://localhost:<port>/api/v1 (src/frontend/src/shared/api/client.ts), and the Vite /api proxy targets the same port. The port comes from VITE_KASAL_PORT, else KASAL_PORT, else 8000 (src/frontend/vite.config.ts), so if you move the backend with KASAL_PORT, start the frontend with the same value:
KASAL_PORT=8001 npm startInteractive API docs are served at /api-docs on the backend. For every environment variable, see the configuration reference.
There are three ways to start Kasal, and they don't share defaults. Use run.sh for development.
src/backend/run.sh |
src/entrypoint.py |
kasal (pip package, packaging/kasal/cli.py) |
|
|---|---|---|---|
| Intended use | Local development | Production / Databricks Apps entry point (src/app.yaml runs it) |
Trying Kasal from an installed wheel |
| Bind host | KASAL_BIND_HOST, default 127.0.0.1; warns on any other host |
KASAL_BIND_HOST if set; otherwise 0.0.0.0 inside Databricks Apps and 127.0.0.1 elsewhere |
--host, default 127.0.0.1 |
| Port | KASAL_PORT, default 8000 |
--port; default DATABRICKS_APP_PORT inside Databricks Apps, else 8000 |
--port, default 8000 |
LOCAL_DEV_AUTH |
Set to true unless you set it |
Set to true by --environment dev unless you set it; otherwise not set, and refused inside Apps |
Not set; export it yourself, or API calls return 401 (see the pip package guide) |
| Default database | SQLite, src/backend/app.db (./run.sh postgres for PostgreSQL) |
SQLite, src/kasal.db; inside Apps, the attached Lakebase resource |
SQLite, ~/.kasal/kasal.db; any DATABASE_TYPE or SQLITE_DB_PATH you export wins |
| Frontend | None; run the Vite dev server on port 3000 | Serves the built src/frontend_static |
Serves the UI bundled in the wheel |
| Dependencies | Runs uv sync --frozen first |
Uses the current interpreter | Installed with the wheel |
| Reload on code change | Yes (--reload --reload-dir src) |
With --reload |
No |
src/entrypoint.py flags: --db-type sqlite|postgres and --db-url (a postgresql:// URL is rewritten to postgresql+asyncpg://, and DATABASE_TYPE is exported so the crew and flow subprocesses agree), --port, --reload, --debug and --environment dev|prod (dev behaves like run.sh: KASAL_DEPLOYMENT_MODE=local and LOCAL_DEV_AUTH=true). For day-to-day development, run.sh is still the simplest option.
Every API call needs an identity. Inside Databricks Apps it comes from the platform proxy, through the X-Forwarded-Email, X-Forwarded-User and X-Forwarded-Access-Token headers. A request with no identity gets 401; the backend no longer assumes a default user.
Locally there is no proxy, so Kasal offers an opt-in development identity:
LOCAL_DEV_AUTH=truemakesLocalDevAuthMiddleware(src/backend/src/main.py) addX-Forwarded-Email: dev@localhostto any request that has no identity header.- The dev frontend sends its own
X-Forwarded-Email(VITE_DEV_USER_EMAIL), which always wins. run.shsetsLOCAL_DEV_AUTH=truefor you. If you startuvicornorsrc/entrypoint.pyyourself, export it first, or every API call returns 401.- It is refused in production: when
DATABRICKS_APP_NAMEis set orENVIRONMENTisproduction, the flag is ignored and an error is logged.python src/entrypoint.py --environment devturns it on, the same asrun.sh.
LOCAL_DEV_AUTH is read with os.getenv, so putting it in a .env file has no effect. Export it in your shell.
To start the backend without run.sh:
cd src/backend
export LOCAL_DEV_AUTH=true
.venv/bin/uvicorn src.main:app --host 127.0.0.1 --port 8000For the full auth model, see the security reference and the API endpoints reference.
The backend, alembic and python run_seeders.py all read src/backend/src/config/settings.py, so they share one default: SQLite at the absolute path src/backend/app.db, whatever directory you run them from. run.sh changes into src/backend itself and no longer sets SQLITE_DB_PATH. The other entry points keep their own files:
| Entry point | Default database | SQLite file |
|---|---|---|
./run.sh, uvicorn, alembic, python run_seeders.py |
SQLite (DATABASE_TYPE defaults to sqlite) |
src/backend/app.db |
python src/entrypoint.py |
SQLite | src/kasal.db |
kasal (pip package) |
SQLite | ~/.kasal/kasal.db |
For the other ways the launchers differ, see Compare the launchers.
On a new database, start the app once. ./run.sh (or any other launcher; see compare the launchers) runs init_db() at startup, which builds the whole schema, and then seeds it in the background while AUTO_SEED_DATABASE is on. Nothing else is needed.
To build and seed a database without starting the server, for example a scratch file for a test, run the seeders script. It runs init_db() first (idempotent, so it is safe on an existing database), then every seeder:
cd src/backend
export DATABASE_TYPE=sqlite SQLITE_DB_PATH=/tmp/kasal-scratch.db
uv run --frozen python run_seeders.pyThe script exits 0 only when every seeder succeeded. If any seeder fails it still runs the rest, then logs SEEDING FAILED with the name and error of each failed seeder and exits 1. No seeder is optional: each one writes rows the app or its bundled examples rely on. Best-effort cleanup inside a seeder, such as pruning retired model rows, logs a warning instead.
At server startup, seeding runs in the background and a failure never stops the server: it is logged as an ERROR that names the failed seeders, and the app keeps serving. The exception is a fresh Lakebase installation (the attached database resource), which refuses to start when an essential seeder (model_configs, prompt_templates or tools) fails, because it has no earlier defaults to fall back on.
Don't use alembic upgrade head to create a database. The migration history has several roots and no baseline revision, so it cannot build a schema from nothing: on an empty SQLite file it stops at no such table: agents, and on a database that init_db() built it fails too, because it replays every migration from the start. CI runs that step as report-only for the same reason (see continuous integration).
What Alembic is for today is recording schema changes. To create a migration, bring a database to the current schema with init_db(), mark it as current, then autogenerate:
cd src/backend
uv run --frozen alembic stamp head # once per database that init_db() built
uv run --frozen alembic revision --autogenerate -m "description"Review the generated file before you commit it. On SQLite, autogenerate also reports type changes (for example NUMERIC to UUID) that are only differences between the SQLite and PostgreSQL column types; delete those operations.
To use PostgreSQL, start the server with ./run.sh postgres and set DATABASE_TYPE=postgres (plus the POSTGRES_* variables) on every other command you run. To use another SQLite file, set SQLITE_DB_PATH the same way.
Alembic does not run at startup. The app builds its schema with init_db() (create_all plus the self-heal steps in src/backend/src/db/self_heal/), so a column added to an existing table needs a self-heal step as well as a migration. Seeders run in the background at startup while AUTO_SEED_DATABASE is on.
This section gives developers a high-level view of the front end and back end. It explains core components and shows how to understand and trace them.
The backend uses FastAPI with a clean layered structure. It separates HTTP routing, business services, repositories, and SQLAlchemy models.
- API routers:
src/backend/src/api/map HTTP endpoints to service calls. - Services:
src/backend/src/services/<domain>/implement business logic and hold the transaction. Every service lives in a domain package. - Repositories:
src/backend/src/repositories/own every query and every row write. - Models and schemas:
src/backend/src/models/andsrc/backend/src/schemas/define persistence and I/O contracts. - Execution:
src/backend/src/services/execution/holds the agent runtime (runtime/), the harnesses (harnesses/) and the shared kernel, behind the Chat (services/chat/), Agent Builder (services/agent_builder/) and Flow Builder (services/flow_builder/) paths. LLM calls go throughsrc/backend/src/core/llm/. - Database and sessions:
src/backend/src/db/configures sessions, the database router and the schema self-heal; migrations live insrc/backend/migrations/. - Config and security:
src/backend/src/config/andsrc/backend/src/dependencies/provide settings, identity and group context.
A request passes through router, service, repository, and database. LLM calls route through the LLM manager when needed.
sequenceDiagram
participant C as Client
participant R as FastAPI Router
participant S as Service
participant U as Session (routed)
participant Repo as Repository
participant DB as Database
participant L as LLM Manager
C->>R: HTTP Request
R->>S: Validate and delegate
S->>U: Use the injected session (never opens its own)
S->>Repo: Query/Command
Repo->>DB: SQLAlchemy operation
DB-->>Repo: Rows/Status
alt Needs LLM
S->>L: Generate/Score
L-->>S: LLM Result
end
S->>U: Request dependency commits (or rolls back) at end of request
S-->>R: DTO/Schema
R-->>C: HTTP Response
- Start at the router file for the endpoint path.
- Open the service it calls and read business logic.
- Inspect repository methods and referenced models.
- Check schema types for request and response contracts.
- Review unit tests under
src/backend/testsfor examples.
This shows the router, service and repository wiring the backend uses (modelled
on api/agents_router.py and services/CLAUDE.md). The session is created once
per request by the SessionDep dependency and flows down: router, then
service, then repository. Nothing below the router acquires a session of its own.
# api/items_router.py
async def get_item_service(session: SessionDep) -> ItemService:
return ItemService(session=session)
ItemServiceDep = Annotated[ItemService, Depends(get_item_service)]
@router.get("/{item_id}", response_model=ItemOut)
async def get_item(item_id: UUID, service: ItemServiceDep, group_context: GroupContextDep):
item = await service.get_item(item_id, group_context)
if item is None:
raise HTTPException(status_code=404, detail="Item not found")
return item
# services/items/item_service.py (every service lives in a domain package)
class ItemService:
def __init__(self, session: AsyncSession,
repository_class: Type[ItemRepository] = ItemRepository):
self.session = session
self.repository = repository_class(session) # built on the GIVEN session
async def get_item(self, item_id: UUID, group_context: GroupContext) -> ItemOut | None:
item = await self.repository.get_for_group(item_id, group_context.group_ids)
return ItemOut.model_validate(item) if item else None
# repositories/item_repository.py
class ItemRepository(BaseRepository[Item]):
def __init__(self, session: AsyncSession):
super().__init__(Item, session) # receives a session, never opens one
async def get_for_group(self, item_id: UUID, group_ids: list[str]) -> Item | None:
result = await self.session.execute(
select(Item).where(Item.id == item_id, Item.group_id.in_(group_ids))
)
return result.scalar_one_or_none()The rules CI enforces (details in src/backend/src/services/CLAUDE.md):
- Layers only import downward.
api→services→repositories→models, withcore,utilsandschemasbelow everything.import-linterchecks this, including transitive chains; the contracts are[tool.importlinter]insrc/backend/pyproject.toml. - Every service is a package. A new service goes in
services/<domain>/<module>.py, never a looseservices/<name>_service.py. - A service never builds queries or persists rows.
select(...),session.executeandsession.add/delete/mergebelong in a repository; a service holds the session only for transaction control. - A service never opens a session or builds an engine. Use the injected
session inside a request, or
routed_scoped_session()outside one. - Another domain's data goes through that domain's service, not its repository, so the owning domain's rules (group scoping, encryption, status transitions) still apply.
- There is no Unit of Work.
src.core.unit_of_workhas been deleted; the session already is the unit of work. Treat any example usingself.uoworUnitOfWorkas stale.
The AST checks for the last four rules live in src/backend/tests/unit/architecture/.
The frontend is a React + TypeScript application. It organizes UI components, API clients, state stores, hooks, and utilities.
- App shell:
src/frontend/src/app/holds routes, startup and cross-feature composition. - Features:
src/frontend/src/features/<domain>/hold a domain's views, feature-local state, hooks and API calls (for examplefeatures/chat/,features/workflow/,features/executions/). - Shared:
src/frontend/src/shared/holds the HTTP client (shared/api/client.ts), presentation helpers and the A2UI renderer. - API clients:
src/frontend/src/api/wrap domain endpoints with typed static methods overapiClient. - Stores:
src/frontend/src/store/holds the shared Zustand stores. Zustand is the only state library. - Types and config:
src/frontend/src/types/andsrc/frontend/src/config/provide contracts, defaults and i18n.
Components call hooks, which use stores and API clients. Responses update state and re-render the UI.
flowchart LR
A[UI Component] --> B[Hook]
B --> C[State Store]
B --> D[API Client]
D -->|HTTP| E[Backend API]
E -->|JSON| D
D --> C
C --> A
- Locate the component rendering the feature.
- Check its hook usage and props.
- Open the API client method it calls.
- Review the store slice it reads or writes.
- Inspect related types in src/frontend/src/types.
A component loads items through an API service built on apiClient, which sets the base URL (/api/v1 in production) and reports failures. Don't call fetch with hand-built auth headers: in Databricks Apps the platform proxy supplies the identity.
// api/items/ItemService.ts
import { apiClient } from '../../shared/api/client';
export class ItemService {
static async getItem(id: string): Promise<Item> {
const response = await apiClient.get<Item>(`/items/${id}`);
return response.data;
}
}
// features/items/components/ItemView.tsx
const ItemView: React.FC<{ id: string }> = ({ id }) => {
const [item, setItem] = useState<Item | null>(null);
useEffect(() => { ItemService.getItem(id).then(setItem); }, [id]);
return item ? <div>{item.name}</div> : <span>Loading...</span>;
};This ties front end and back end with shared contracts. It helps new developers trace a feature quickly.
- The request path runs: Frontend Component, Hook, Store/API Client, Backend Router, Service, Repository, DB.
- Shared types and response shapes live in frontend types and backend schemas.
- Tests show usage patterns:
src/backend/tests/unit/mirrorssrc/backend/src/, and frontend tests sit next to the code as*.test.ts(x).
Run the backend checks from src/backend:
uv run python run_tests.py # tests, then black, isort, ruff, the mypy baseline and import-linter
uv run python run_tests.py --skip-lint # tests only
uv run python run_tests.py --lint-only # static checks only
uv run python run_tests.py --type unit --coveragerun_tests.py runs pytest in parallel by default (--parallel auto); pass --parallel 0 to step through a test. The mypy step fails only on errors that are not in mypy-baseline.json.
Run the frontend checks from src/frontend. The frontend uses Vitest with React Testing Library; there is no Jest and no Cypress or Playwright suite:
npm run test:run # Vitest, single run (what CI runs); npm test for watch mode
npm run lint
npm run build # tsc -b, then vite buildCI runs the same commands. For what gates a pull request and what is report-only, see continuous integration.
Back to the documentation hub.