@@ -61,6 +62,9 @@ Recent decisions affecting current work:
- [Roadmap]: Coarse 3-phase structure — Foundation → Agent Features → Operator Experience
- [Roadmap]: Phase 3 portal gated on Phase 2 completing (DB schema stability after memory + tool data models)
- [Roadmap]: WhatsApp Business Verification must be initiated during Phase 1 (1-6 week approval, WhatsApp goes live in Phase 2)
- [Phase 01-foundation]: PostgreSQL RLS with FORCE ROW LEVEL SECURITY chosen for tenant isolation; app connects as konstruct_app role (not superuser)
- [Phase 01-foundation]: SET LOCAL app.current_tenant uses UUID-sanitized f-string (not parameterized) — asyncpg does not support prepared statement placeholders for SET LOCAL
- [Phase 01-foundation]: channel_type stored as TEXT with CHECK constraint — native sa.Enum caused duplicate CREATE TYPE DDL in Alembic migrations
- ContextVar-based RLS injection via before_cursor_execute event hook
- UUID-sanitized SET LOCAL to prevent SQL injection in RLS path
- FORCE ROW LEVEL SECURITY on all tenant-scoped tables
- All app code connects as konstruct_app role (not postgres superuser)
- Redis key format: {tenant_id}:{type}:{discriminator} — no bare keys possible
- Alembic migrations run as admin user; app runs as limited konstruct_app role
- pytest-asyncio auto mode; session-scoped DB created via subprocess Alembic run
key-files:
created:
- pyproject.toml
- docker-compose.yml
- .env.example
- alembic.ini
- scripts/init-db.sh
- packages/shared/pyproject.toml
- packages/shared/shared/config.py
- packages/shared/shared/db.py
- packages/shared/shared/rls.py
- packages/shared/shared/redis_keys.py
- packages/shared/shared/models/message.py
- packages/shared/shared/models/tenant.py
- packages/shared/shared/models/auth.py
- migrations/env.py
- migrations/script.py.mako
- migrations/versions/001_initial_schema.py
- tests/conftest.py
- tests/unit/test_normalize.py
- tests/unit/test_tenant_resolution.py
- tests/unit/test_redis_namespacing.py
- tests/integration/test_tenant_isolation.py
modified: []
key-decisions:
- "PostgreSQL RLS with FORCE ROW LEVEL SECURITY chosen for Starter-tier tenant isolation — prevents accidental superuser bypass"
- "SET LOCAL app.current_tenant sanitized via UUID round-trip (not parameterized) — asyncpg does not support prepared statement placeholders for SET LOCAL"
- "channel_type stored as TEXT with CHECK constraint in migration rather than native ENUM — avoids SQLAlchemy auto-emitting duplicate CREATE TYPE DDL"
- "Alembic migrations run as postgres admin; application connects as konstruct_app — role separation is what makes RLS real"
- "Session-scoped test DB with subprocess Alembic migration + function-scoped AsyncEngine — avoids pytest-asyncio cross-loop-scope issues with SQLAlchemy"
- "uv workspace with --all-packages sync required for workspace member packages to be installed in editable mode"
# Phase 01 Plan 01: Monorepo Scaffolding, Data Models, and RLS Tenant Isolation Summary
**uv workspace monorepo with PostgreSQL RLS proving Tenant A cannot access Tenant B's data through SQLAlchemy + asyncpg with FORCE ROW LEVEL SECURITY enforced at schema level**
## Performance
- **Duration:** 12 min
- **Started:** 2026-03-23T15:45:02Z
- **Completed:** 2026-03-23T15:57:51Z
- **Tasks:** 2
- **Files modified:** 32
## Accomplishments
- uv workspace monorepo with 5 member packages — all importable with `uv run python -c "from shared.models.message import KonstructMessage"`
- PostgreSQL RLS tenant isolation proven green: 7 integration tests confirm cross-tenant data leakage is impossible via `FORCE ROW LEVEL SECURITY` on agents and channel_connections tables
- Alembic async migration creates all tables, grants to konstruct_app role, and configures RLS policies via `current_setting('app.current_tenant', TRUE)::uuid`
-`packages/shared/shared/config.py` — Pydantic Settings loading all env vars
-`migrations/versions/001_initial_schema.py` — full schema with RLS policies and FORCE ROW LEVEL SECURITY
-`tests/integration/test_tenant_isolation.py` — 7 tests proving RLS isolation across both tenant-scoped tables
## Decisions Made
- **SET LOCAL parameterization:** asyncpg does not support `%s` placeholders for `SET LOCAL` statements. Switched to f-string with UUID round-trip sanitization — `str(UUID(str(tenant_id)))` ensures only valid hex+hyphen characters reach the DB.
- **channel_type as TEXT:** Native `sa.Enum` type caused SQLAlchemy to auto-emit a second `CREATE TYPE channel_type_enum` statement in `create_table`, conflicting with our explicit `CREATE TYPE`. Stored as `TEXT` with a `CHECK (channel_type IN (...))` constraint instead.
- **Function-scoped AsyncEngine in tests:** pytest-asyncio 1.3.0 raises cross-loop-scope errors with session-scoped async fixtures. Moved engine creation to function scope; used a session-scoped synchronous fixture to create the test DB and run migrations once via subprocess.
## Deviations from Plan
### Auto-fixed Issues
**1. [Rule 1 - Bug] asyncpg does not support %s parameterized SET LOCAL**
- **Found during:** Task 2 (integration test execution)
- **Issue:** `rls.py` used `cursor.execute("SET LOCAL app.current_tenant = %s", (tenant_id,))` — asyncpg raises `PostgresSyntaxError: syntax error at or near "%"` because asyncpg uses `$1` style and SET LOCAL doesn't accept prepared statement parameters at all
- **Fix:** Changed to `cursor.execute(f"SET LOCAL app.current_tenant = '{safe_id}'"`) where `safe_id = str(UUID(str(tenant_id)))` sanitizes the value
- **Verification:** All 7 integration tests pass including cross-tenant isolation test
- **Committed in:** `47e7862` (Task 2 commit)
**2. [Rule 1 - Bug] SQLAlchemy double-emits CREATE TYPE for sa.Enum in create_table**
- **Found during:** Task 2 (alembic upgrade head execution)
- **Issue:** `sa.Enum(..., create_type=False)` in `op.create_table` still triggered a second `CREATE TYPE channel_type_enum` DDL, causing `DuplicateObjectError` even when our explicit `CREATE TYPE` ran first
- **Fix:** Changed `channel_type` column to `sa.Text` with a `CHECK` constraint enforcing valid values
- **Issue:** Including `[build-system]` in root `pyproject.toml` caused hatchling to fail looking for a `konstruct` package directory; workspace roots should not be buildable packages
- **Fix:** Removed `[build-system]` from root `pyproject.toml`; used `uv sync --all-packages` to install workspace members
- **Files modified:** `pyproject.toml`
- **Verification:** `uv sync --all-packages` succeeds; all workspace packages listed in `uv pip list`
**Impact on plan:** All fixes necessary for correctness. No scope creep.
## Issues Encountered
- pytest-asyncio 1.3.0 cross-loop-scope issue with session-scoped async `db_engine` fixture — resolved by making engine function-scoped and using a synchronous session-scoped fixture for one-time DB creation
-`::jsonb` cast syntax in SQLAlchemy `text()` query rejected by asyncpg parameter parser — removed cast, PostgreSQL coerces JSON text implicitly
## User Setup Required
None — the Docker Compose environment provides all required services. Copy `.env.example` to `.env` to start.
## Next Phase Readiness
- All packages/shared imports available for Plans 02–04
-`DATABASE_URL` must use `konstruct_app` role (not superuser) — documented in `.env.example`
-`DATABASE_ADMIN_URL` must be set for `alembic upgrade head` in CI/CD
- Docker Compose boots cleanly with `docker compose up -d postgres redis`
- Ollama GPU reservation is optional — service starts without GPU
## Self-Check: PASSED
All created files verified present. All task commits verified in git history.
---
*Phase: 01-foundation*
*Completed: 2026-03-23*
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.