Skip to content

feat: add knowledge sources database for HED discovery - #26

Merged
neuromechanist merged 4 commits into
developfrom
feature/issue-25-knowledge-sources
Jan 12, 2026
Merged

neuromechanist merged 4 commits into
developfrom
feature/issue-25-knowledge-sources

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Add SQLite + FTS5 database for syncing and searching HED-related GitHub discussions and academic papers. This is for discovery only, not as authoritative knowledge sources.

  • Knowledge database with full-text search (FTS5)
  • GitHub sync for hed-standard repos (issues/PRs via gh CLI)
  • Paper sync from OpenALEX, Semantic Scholar, and PubMed APIs
  • CLI commands: osa sync {init,github,papers,all,status,search}
  • Agent tools: search_hed_discussions, search_hed_papers
  • Optional API keys for higher rate limits

Test plan

  • All 379 tests pass (21 new tests for knowledge module)
  • CLI commands work: osa sync init, osa sync github, osa sync status, osa sync search
  • GitHub sync successfully pulls issues/PRs from hed-standard repos
  • FTS5 search returns relevant results
  • Agent tools correctly integrated into HEDAssistant

Closes #25

Add SQLite + FTS5 database for syncing and searching HED-related
GitHub discussions and academic papers. This is for discovery only,
not as authoritative knowledge sources.

New features:
- Knowledge database with full-text search (FTS5)
- GitHub sync for hed-standard repos (issues/PRs via gh CLI)
- Paper sync from OpenALEX, Semantic Scholar, and PubMed APIs
- CLI commands: osa sync {init,github,papers,all,status,search}
- Agent tools: search_hed_discussions, search_hed_papers
- Optional API keys for higher rate limits

Files added:
- src/knowledge/{__init__,db,github_sync,papers_sync,search}.py
- src/cli/sync.py
- src/tools/knowledge.py
- tests/test_knowledge/{__init__,test_db,test_search}.py

Files modified:
- src/agents/hed.py (add new tools to HEDAssistant)
- src/cli/main.py (register sync_app)
- src/cli/config.py (add API key fields)
- tests/test_agents/test_{hed,page_context}.py (update tool counts)

Refs: #25
Add background scheduler for automated knowledge sync in Docker:
- APScheduler for scheduled GitHub and papers sync
- Database persistence via volume mount
- Environment variables for API tokens (all optional)
- docker-compose.yml for easy deployment

Changes:
- src/api/scheduler.py: Background scheduler with configurable cron
- src/api/main.py: Start/stop scheduler on app lifecycle
- src/api/config.py: Add sync settings and API key fields
- src/cli/config.py: Support DATA_DIR env var for volume mount
- deploy/Dockerfile: Install gh CLI, document volumes and env vars
- deploy/docker-compose.yml: Production-ready compose file
- deploy/.env.example: Example environment configuration
- pyproject.toml: Add apscheduler dependency

Sync schedules (configurable via env vars):
- GitHub: Daily at 2am UTC (SYNC_GITHUB_CRON)
- Papers: Weekly Sunday at 3am UTC (SYNC_PAPERS_CRON)

Refs: #25
- Add knowledge sync settings to root .env.example
- Remove duplicate deploy/.env.example
- Update docker-compose.yml to use env_file from root

Refs: #25
- Add _sanitize_fts5_query() helper that escapes user input
- Wrap user queries in quotes to treat FTS5 operators as literals
- Add tests for sanitization function and special character handling
- Addresses code review finding on query injection vulnerability
@neuromechanist

Copy link
Copy Markdown
Member Author

Code Review Findings - Addressed

Following the code review, the critical finding has been addressed:

Critical: FTS5 Query Injection (Fixed in fd60760)

  • Added _sanitize_fts5_query() helper that escapes user input
  • Wraps queries in quotes to treat FTS5 operators (AND, OR, NOT, NEAR, *) as literals
  • Added 4 tests for sanitization and special character handling

Important: Incremental Sync Logic (By Design)

The comparison of createdAt vs last_sync_at is intentional. We want to catch any items created since the last sync, not just items modified. GitHub's --created filter is the appropriate choice for discovering new discussions.

Minor Findings (Acceptable)

  • Mock usage in tests: Only used for path patching to isolate test databases, not mocking actual functionality
  • Stale cache on settings: Standard pydantic-settings pattern with @lru_cache; settings don't change at runtime
  • Missing concurrent tests: Added to backlog for future hardening

neuromechanist added a commit to OpenScience-Collective/documentation that referenced this pull request Jan 12, 2026
- Add knowledge-sync.md with CLI commands, Docker config, API keys
- Update cli-reference.md with sync commands and env variables
- Update mkdocs.yml navigation to include Knowledge Sync page

Related to: OpenScience-Collective/osa#26
@neuromechanist

Copy link
Copy Markdown
Member Author

Documentation

Documentation PR created: OpenScience-Collective/documentation#2

Covers:

  • Knowledge sync CLI commands (osa sync)
  • Automated sync in Docker (cron schedules)
  • Environment variables and API keys
  • Database locations and troubleshooting

The docs PR should be merged when this PR is merged to develop.

@neuromechanist
neuromechanist merged commit b7d23b8 into develop Jan 12, 2026
@neuromechanist
neuromechanist deleted the feature/issue-25-knowledge-sources branch January 12, 2026 16:16
neuromechanist added a commit that referenced this pull request Jan 15, 2026
- 0.3.0: Knowledge sources database (PR #26)
- 0.3.1: Sync status API endpoints (PR #29)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant