Skip to content

Decompose 520-line monolithic search handler #99

@bradjin8

Description

@bradjin8

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

  • The search handler is decomposed into 3+ focused functions (one per data source: workspace DB, CLI sessions, aggregate/rank)
  • Each extracted function is independently testable with clear input/output boundaries
  • SQLite connections use with sqlite3.connect(...) as conn: context managers (no manual close)
  • Search functionality is unchanged from the user's perspective (same API contract, same results)
  • At least one unit test per extracted function verifying core search logic
  • Tests pass in CI (if applicable)
  • PR approved by at least 1 reviewer

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/

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions