Problem
The api/search.py module contains a monolithic search handler of approximately 450 lines that mixes three data sources (SQLite workspace queries, JSONL CLI agent sessions, and in-memory caches) in a single function with deeply nested control flow. The baseline eval identifies this as part of the "Monolith-Duplication Cycle" compound dynamic — monolithic functions prevent extraction, which preserves duplication across api/workspaces.py, api/export_api.py, and scripts/export.py. The eval also notes that api/search.py uses manual sqlite3.connect()/close() patterns that bypass context managers, creating resource leak risk on exception paths.
Acceptance Criteria
Implementation Notes
Start by identifying the three logical sections within the search handler: (1) workspace SQLite search (queries against ItemTable/cursorDiskKV tables), (2) CLI agent session search (JSONL file scanning from ~/.cursor/chats/), and (3) result aggregation/ranking/pagination. Extract each into a named function in api/search.py or a new utils/search_helpers.py module. Replace all manual conn = sqlite3.connect(); ... conn.close() with with statements. The workspace-mapping logic duplicated between api/search.py and api/workspaces.py should be imported from a shared location rather than re-implemented. Reference api/workspaces.py for the existing workspace collection pattern. Do NOT touch scripts/export.py duplication in this PR — that's a separate future item.
References
- Eval finding: Test 4, cluster "cognitive-load"
- Related files:
api/search.py, api/workspaces.py, utils/cli_chat_reader.py, utils/workspace_utils.py (or equivalent shared module), tests/
Problem
The
api/search.pymodule contains a monolithic search handler of approximately 450 lines that mixes three data sources (SQLite workspace queries, JSONL CLI agent sessions, and in-memory caches) in a single function with deeply nested control flow. The baseline eval identifies this as part of the "Monolith-Duplication Cycle" compound dynamic — monolithic functions prevent extraction, which preserves duplication acrossapi/workspaces.py,api/export_api.py, andscripts/export.py. The eval also notes thatapi/search.pyuses manualsqlite3.connect()/close()patterns that bypass context managers, creating resource leak risk on exception paths.Acceptance Criteria
with sqlite3.connect(...) as conn:context managers (no manual close)Implementation Notes
Start by identifying the three logical sections within the search handler: (1) workspace SQLite search (queries against
ItemTable/cursorDiskKVtables), (2) CLI agent session search (JSONL file scanning from~/.cursor/chats/), and (3) result aggregation/ranking/pagination. Extract each into a named function inapi/search.pyor a newutils/search_helpers.pymodule. Replace all manualconn = sqlite3.connect(); ... conn.close()withwithstatements. The workspace-mapping logic duplicated betweenapi/search.pyandapi/workspaces.pyshould be imported from a shared location rather than re-implemented. Referenceapi/workspaces.pyfor the existing workspace collection pattern. Do NOT touchscripts/export.pyduplication in this PR — that's a separate future item.References
api/search.py,api/workspaces.py,utils/cli_chat_reader.py,utils/workspace_utils.py(or equivalent shared module),tests/