fix(audit): code audit, health check, and performance improvements#66
Open
fix(audit): code audit, health check, and performance improvements#66
Conversation
pgvector-based persistent memory for cross-session recall of user preferences, decisions, corrections, and device patterns. Explicit tool-call storage (no background extraction), semantic retrieval injected into agent context, user management via UI and API. Made-with: Cursor
Replace flat vector-only memory with a hybrid approach: MemoryNode table with pgvector embeddings for semantic search, plus MemoryEdge table for typed relationships to other memories and domain entities (devices, areas, automations, insights). Graph traversal via recursive CTEs, bounded to 2 hops. No external graph DB — all within PostgreSQL. Made-with: Cursor
Comprehensive code audit across 270+ Python files (28,500 LOC): - 7 oversized files needing modularization (P2-P3) - 33 bare except-Exception instances swallowing errors (P0) - N+1 queries, sequential I/O, expensive orchestrator model (P1) - Positive: zero f-string logging, no TODOs, good security posture Prioritized plan with T1-T14 tasks across 4 phases. Made-with: Cursor
T1: Replace `body: dict = {}` (B006) with `Body(default={})` in the
YAML update endpoint — mutable default is a correctness bug.
T3: Replace per-status loop (N separate DB queries) with a single
`ProposalRepository.list_recent()` query for unfiltered listing.
Made-with: Cursor
T2+T4: Replace 3× duplicated 15-line model_context boilerplate with a single `_inherited_model_context()` context manager. Narrow every `except Exception` in agent tools from a generic catch-all to specific `httpx.HTTPError | TimeoutError | ConnectionError` for network errors (with logging), with a final `except Exception` that logs at ERROR and returns a generic safe message instead of leaking internal details. Made-with: Cursor
… mgmt T5: Force a fast-tier model (e.g. gpt-4o-mini) for intent classification regardless of the user's selected model. Classification is simple JSON output — using frontier models adds latency and cost for no benefit. T7: Replace manual session factory + try/finally with `async with get_session()` context manager. Also use the existing `list_routable()` DAL method instead of filtering in Python. Made-with: Cursor
T6: Replace sequential automation and script config HTTP fetches with asyncio.gather() bounded by a semaphore (max 10 concurrent). For a typical HA instance with 50+ automations, this reduces sync time from O(n × latency) to O(latency + n/10 × latency). Also: remove redundant `import logging` in sandbox runner and fix duplicate MPLCONFIGDIR env var. Made-with: Cursor
090bdea to
b350753
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
Bug Fixes
body: dict = {}in proposals YAML update endpoint (security/correctness)list_proposals— replaced per-status loop with singlelist_recent()queryasync with get_session()instead of manual factoryPerformance
asyncio.gather()with semaphore (10 concurrent)Refactoring
_inherited_model_context()helper — eliminated 45 lines of 3× duplicationexcept Exceptionin all 6 agent tools to specific exception types with proper loggingDocumentation
Test plan
make ci-localpasses (format, lint, mypy, bandit, tests)Made with Cursor