Skip to content

Conversation

@ShreeJejurikar
Copy link
Collaborator

@ShreeJejurikar ShreeJejurikar commented Dec 29, 2025

Closes #393

Summary

Enhances the existing cortex ask command to include tutor-like capabilities without adding a separate command or flag. The LLM automatically detects whether a question is educational or diagnostic and responds appropriately.

Changes:

  • Enhanced system prompt to detect educational vs diagnostic queries automatically
  • Added LearningTracker class to track educational topics explored by the user
  • Learning history stored in ~/.cortex/learning_history.json
  • Increased max_tokens from 500 to 2000 for longer educational responses
  • Added terminal-friendly formatting rules (no markdown headings that render poorly)
  • Rich Markdown rendering for proper terminal display
  • Added 25 new unit tests (50 total) for ask module
  • Updated COMMANDS.md with cortex ask documentation

Example usage:

# Educational queries - get structured tutorials
cortex ask "explain how Docker containers work"
cortex ask "best practices for nginx configuration"
cortex ask "teach me about systemd"

# Diagnostic queries - get system-specific answers
cortex ask "what version of Python do I have"
cortex ask "why is my disk full"

Checklist
 Tests pass (pytest tests/)
 MVP label added if closing MVP issue
 Update "Cortex -h" (if needed)



<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

## Release Notes

* **New Features**
  * Automatic learning history tracking saves educational topics and maintains persistent query history
  * Enhanced system prompts differentiate between educational and diagnostic questions for more targeted responses
  * Support for longer, more detailed responses

* **Documentation**
  * Added comprehensive cortex ask command documentation with usage examples and learning guides
  * New walkthroughs illustrating diagnostic and educational query patterns with automatic history tracking

<sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Shree and others added 3 commits December 29, 2025 15:26
…ortexlinux#393)

- Enhanced system prompt to detect educational vs diagnostic queries
- Added LearningTracker class for tracking educational topics
- Learning history stored in ~/.cortex/learning_history.json
- Increased max_tokens from 500 to 2000 for longer responses
- Added terminal-friendly formatting rules
- Rich Markdown rendering for proper terminal display
- Added 25 new unit tests (50 total) for ask module
- Updated COMMANDS.md with cortex ask documentation
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 29, 2025

📝 Walkthrough

Walkthrough

A new LearningTracker class detects educational queries, extracts and records learning topics to ~/.cortex/learning_history.json, and exposes history retrieval APIs through AskHandler. The system prompt is expanded to differentiate educational and diagnostic questions, max tokens increase from 500 to 2000, and comprehensive documentation and tests are added.

Changes

Cohort / File(s) Summary
Core Learning Feature
cortex/ask.py
Introduces LearningTracker class with educational query detection via regex, topic extraction, persistent history storage (with error resilience), and public methods for retrieving history and recent topics. Extends AskHandler to instantiate tracker, record topics, and expose get_learning_history() and get_recent_topics() APIs. Expands system prompt with Educational vs. Diagnostic question differentiation and Output Formatting Rules. Increases max_tokens from 500 to 2000 in OpenAI and Claude calls. Adds imports: re, datetime, pathlib.Path.
CLI Rendering
cortex/cli.py
Wraps Ask command output with Markdown(...) for formatted rendering instead of plain text.
Documentation
docs/COMMANDS.md
Adds comprehensive cortex ask command documentation including usage, diagnostic/educational question types, features, learning history tracking, and end-to-end examples. Covers Quick Reference, Expanded Commands, and Learning/Guides sections.
Unit Tests
tests/test_ask.py
Adds TestLearningTracker and TestAskHandlerLearning classes with extensive test cases covering educational query detection, topic extraction, history persistence, recent topics retrieval, and system prompt validation. Uses temporary file paths for test isolation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AskHandler
    participant LearningTracker
    participant FileSystem as File System
    participant LLM as OpenAI/Claude

    User->>AskHandler: ask(question)
    AskHandler->>LearningTracker: is_educational_query(question)
    activate LearningTracker
    LearningTracker-->>AskHandler: true/false
    deactivate LearningTracker
    
    alt Question is Educational
        AskHandler->>LearningTracker: extract_topic(question)
        LearningTracker-->>AskHandler: topic
        AskHandler->>LearningTracker: record_topic(question)
        activate LearningTracker
        LearningTracker->>FileSystem: load_history()
        FileSystem-->>LearningTracker: history dict
        LearningTracker->>LearningTracker: update topic in history
        LearningTracker->>FileSystem: save_history(updated)
        deactivate LearningTracker
    end
    
    rect rgb(200, 220, 255)
    Note over AskHandler: Enhanced system prompt<br/>with Educational context
    end
    
    AskHandler->>LLM: call with expanded prompt + tokens=2000
    LLM-->>AskHandler: response
    AskHandler-->>User: formatted response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • mikejmorgan-ai
  • Suyashd999

Poem

🐰 A question hops in, we learn if it's wise,
Topics extracted, history's prize!
Educational paths tracked in files so neat,
Enhanced prompts and tokens make learning complete! 🌟

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately captures the main enhancement: adding interactive tutor capabilities to cortex ask command.
Description check ✅ Passed Description covers all template sections with issue reference, comprehensive summary, and mostly complete checklist items.
Linked Issues check ✅ Passed Code changes fully implement all objectives from #393: educational query detection, LearningTracker for progress tracking, structured responses, and automatic intent detection without new flags.
Out of Scope Changes check ✅ Passed All changes align with issue #393 objectives: prompt enhancement, LearningTracker class, token limit increase, markdown rendering, tests, and documentation updates are all scope-appropriate.
Docstring Coverage ✅ Passed Docstring coverage is 91.67% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
cortex/ask.py (2)

172-200: Consider edge case in topic truncation logic.

Line 199 truncates topics to 50 characters at word boundaries using topic[:50].rsplit(" ", 1)[0]. If the first 50 characters contain no spaces, rsplit returns a single-element list, and [0] will return the full 50-character string, which is correct.

However, this could still result in awkwardly truncated topics for long compound words or URLs. Consider whether 50 characters is sufficient for your use cases.

Example edge case:

# Topic with no spaces in first 50 chars
topic = "verylongcompoundwordwithoutanyspacesthatexceeds50characters"
# Result: "verylongcompoundwordwithoutanyspacesthatexceeds50"

The current behavior is safe but might not be ideal for all inputs.


255-262: Silent failure on save may complicate debugging.

Line 262 silently suppresses OSError when writing the learning history file. While this prevents the CLI from crashing when the history file can't be written (e.g., permission issues, disk full), it makes debugging difficult if users expect their learning history to be tracked but it's failing silently.

Consider whether this trade-off aligns with your UX goals. For a "Chill" review mode, this is acceptable, but you might want to log these failures in verbose mode in the future.

Based on learnings from similar patterns in the codebase.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e95c874 and 8d13ebe.

📒 Files selected for processing (4)
  • cortex/ask.py
  • cortex/cli.py
  • docs/COMMANDS.md
  • tests/test_ask.py
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Follow PEP 8 style guide
Type hints required in Python code
Docstrings required for all public APIs

Files:

  • cortex/cli.py
  • tests/test_ask.py
  • cortex/ask.py
tests/**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Maintain >80% test coverage for pull requests

Files:

  • tests/test_ask.py
🧬 Code graph analysis (1)
tests/test_ask.py (1)
cortex/ask.py (11)
  • ask (458-526)
  • LearningTracker (139-262)
  • SystemInfoGatherer (20-136)
  • is_educational_query (165-170)
  • extract_topic (172-200)
  • record_topic (202-225)
  • get_history (227-229)
  • get_recent_topics (231-242)
  • get_recent_topics (536-545)
  • gather_context (129-136)
  • _get_system_prompt (334-391)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Test (Python 3.10)
  • GitHub Check: test (3.12)
  • GitHub Check: test (3.11)
  • GitHub Check: test (3.10)
🔇 Additional comments (13)
cortex/cli.py (2)

9-10: LGTM! Appropriate import for Markdown rendering.

The import extends the existing Rich library usage to support formatted terminal output for the enhanced educational responses.


302-303: LGTM! Clean integration of Markdown rendering.

The change properly leverages Rich's Markdown class to render formatted educational responses in the terminal, aligning with the enhanced output formatting rules in the system prompt.

tests/test_ask.py (3)

273-396: Excellent test coverage for LearningTracker.

The test class comprehensively covers:

  • Educational query pattern detection across multiple patterns
  • Topic extraction with various prefixes
  • Truncation handling for long topics
  • History persistence and incremental updates
  • Non-educational query filtering

The use of temporary files and proper patching ensures test isolation.


398-460: LGTM! Strong integration tests for learning features.

The tests validate:

  • Educational queries are properly recorded through AskHandler
  • Diagnostic queries are correctly excluded from learning history
  • Recent topics retrieval works through the handler API
  • System prompt contains the necessary educational guidance

Good use of the fake provider to avoid external API dependencies.


462-493: Good validation of system prompt enhancements.

The tests ensure the enhanced system prompt includes:

  • Query type detection guidance
  • Educational response instructions with examples and best practices
  • Diagnostic response instructions with system context

This validates the PR's core objective of distinguishing between query types.

docs/COMMANDS.md (2)

75-132: Excellent documentation for the enhanced ask command.

The documentation clearly explains:

  • The dual nature of the command (diagnostic vs educational)
  • Automatic intent detection
  • System-aware responses and learning progress tracking
  • Practical examples for both query types

The feature list and notes provide users with a complete understanding of the command's capabilities.


429-445: Well-structured learning workflow section.

The workflow provides a clear progression:

  1. Diagnostic queries for system information
  2. Educational queries for learning
  3. Step-by-step tutorials
  4. Automatic tracking of learning history

This practical guide helps users understand and leverage the new capabilities.

cortex/ask.py (6)

1-17: LGTM! Clean import additions and updated docstring.

The new imports (re, datetime, Path) are from the standard library and support the learning tracking functionality. The docstring accurately reflects the expanded scope to include educational content and progress tracking.


139-170: Well-designed LearningTracker class with comprehensive patterns.

The class structure is solid:

  • Pre-compiled regex patterns for efficiency
  • Comprehensive educational query detection patterns
  • Case-insensitive matching appropriately handles user input variations

The pattern list covers the main educational query types mentioned in the PR objectives.


288-288: LGTM! Proper integration of LearningTracker.

The learning tracker is correctly instantiated in __init__, ensuring it's available for all ask operations.


334-391: Excellent system prompt design for dual-mode operation.

The enhanced system prompt effectively:

  • Distinguishes between educational and diagnostic query types with clear trigger patterns
  • Provides specific guidelines for each response type
  • Includes explicit output formatting rules to prevent terminal rendering issues
  • Provides a concrete example to guide the LLM

The level of detail is appropriate for ensuring consistent, high-quality responses across both query modes. The terminal-specific formatting rules (avoiding markdown headings, limiting line length) are particularly valuable.


401-401: Appropriate token increase for educational responses.

Increasing max_tokens from 500 to 2000 for both OpenAI and Claude is necessary to support the longer, structured educational responses with code examples and best practices. The 4x increase aligns with the PR's objective to provide comprehensive tutorials.

Note: This will increase API costs, but is appropriate for the enhanced functionality.

Also applies to: 413-413


523-545: Clean integration and well-designed public API.

The learning tracking integration:

  • Records topics at the appropriate point (after successful answer generation)
  • Exposes a clean public API through get_learning_history() and get_recent_topics()
  • Follows Python conventions with proper type hints and docstrings
  • Delegates appropriately to the LearningTracker instance

The API design allows external code to access learning history without tight coupling to the tracker implementation.

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.

[FEATURE] Enhance cortex ask with interactive tutor capabilities

1 participant