Codebase Health Audit
A sweep of anti-patterns, inefficiencies, and security gaps across the Baseline Labs platform. 34 findings across server infrastructure, product APIs, database migrations, and static assets.
The platform is functional and shipping, but carries accumulated technical debt across four areas: silent error handling masks bugs in production, SQL construction patterns create injection surface area, duplicated utilities across products diverge in behaviour, and infrastructure configuration has race conditions and missing resource limits.
Most findings are fixable in isolation. The five quick wins at the bottom of this report can be done in under an hour and meaningfully reduce risk.
Findings that need immediate attention
os.environ.get("JWT_SECRET", "change-me-in-production") silently uses a guessable default if the environment variable is missing. Anyone who reads the source can forge admin tokens. Should raise an error on startup if unset.
_count_since() helper interpolates table names directly into SQL: f"SELECT COUNT(*) FROM {table}". While currently called with hardcoded values, the pattern invites future misuse. Should validate against a whitelist.
get_requests() and get_errors() reference a product variable that is never defined as a parameter. These code paths will raise NameError at runtime.
012 prefix: 012_geo_tables.sql and 012_schema_separation.sql. Migration runners may skip one, leaving the schema in an inconsistent state.
Reliability, performance, and data integrity
except: pass across 6+ locationscore/api/admin.py, markupschema/api/inference.py, and core/api/auth.py. Bugs in production become invisible. Every bare except should at minimum call log_error().
user_profile() endpoint fires 7-8 separate database queries that could be consolidated into 1-2 using PostgreSQL CTEs.
flush_backlink_buffer(), if client.post() raises, client.aclose() is never called because it's not in a finally block. Connections leak in error scenarios.
__del____del__, which is unreliable in Python. Under load this can exhaust the connection pool. Should enforce context manager usage.
POST /submit-batch has no Depends(require_auth) or Depends(require_api_key). Anyone can spam the scrape queue.
deploy.resources.limits on any service. A memory leak or runaway process can crash the entire host, taking all services down.
geo_queries(id) instead of geo.geo_queries(id). After schema separation this FK constraint will fail.
api_keys and usage_logs reference users(id) without cascade. Deleting a user leaves orphaned records and will fail if the FK is enforced.
[ -f "$LOCK" ] then writes. Between check and write, another agent can grab the same lock. Needs atomic file creation.
Efficiency and maintainability
extract_domain() implementationsscrape.py, schema_generator.py, inference.py, and mention_scraper.py each implement domain extraction differently. Edge cases around www. stripping, protocol handling, and error recovery diverge. Should be one function in core/shared/.
schema_generator.py, scrape.py, geo/search.py, and server.py all create their own Redis connections. Some use singletons, some don't. Should be a shared utility in core/shared/cache.py.
GROUP BY and COUNT.
httpx.AsyncClient per request. Connection setup overhead is higher than reusing a persistent pool. The comment claims it "avoids pool exhaustion" but the opposite is true.
gpt_search() is defined twice. The first definition (a TODO stub) is silently overwritten by the second. Dead code.
is_admin = user_count < 3 is checked on signup. If a user is deleted, the count drops and the next signup gets admin. Should use a role field.
python3 -c "import urllib.request; ..." every 5 seconds per container. Each invocation takes 500-800ms. Should use curl or wget.
sys.path pollution during API discoverysys.path.insert(0, ...) adds paths during module loading and never cleans them up. In multi-worker environments this can cause import shadowing.
await. Pending database writes or locks may be abandoned mid-operation.
_link_buffer and _backlink_buffer are module-level lists with no max size. If flush fails repeatedly, memory grows without bound in the long-running worker.
SELECT * FROM shared.users ORDER BY id with no LIMIT. Will return increasingly large result sets as the user base grows.
shared.error_logs and shared.request_logs by timestamp, user_id, and service without indexes. Admin dashboard queries will degrade over time.
color: #fff in .btn--primary, .notif-count, .chat-bubble--user, and .chat-send. Violates the design rule to always use var(--c-*) tokens.
flush_link_buffer() and flush_backlink_buffer() are near-identical implementations. Should be a single parameterized function.
Cleanup and consistency
.sql.disabled files sit alongside their replacements (018, 019). Confusing for anyone reading the migration history.
{"success": true} vs bare data). No standard error response format.
.section rule.section defined at line ~680 and again at ~2470 with conflicting properties (the second adds animations that override the first).
!important usage indicates specificity issues!important in responsive breakpoints. Usually a sign of specificity conflicts that should be resolved structurally.
2826, 2840) and language ("en") are hardcoded in function bodies instead of config.
python3 compile.py runs on every container start via both the Dockerfile CMD and the Compose command override. Two sources of truth for the entrypoint.
High impact, low effort
| Fix | Impact | Effort |
|---|---|---|
Add auth to /submit-batch |
Closes unauthenticated endpoint | 1 line |
Remove dead gpt_search duplicate |
Eliminates confusion | Delete 3 lines |
Extract shared extract_domain() |
Fixes 4 inconsistent implementations | ~30 min |
Replace bare except: pass with logging |
Makes production debugging possible | Find and replace |
| Rename migration 012 collision | Prevents migration runner ambiguity | Rename 1 file |
except: pass with log_error(). Until this is done, production issues are invisible.extract_domain() and Redis singleton into core/shared/. Reduces duplication across 8+ files and prevents behavioural divergence.