Skip to content

Conversation

@srikrishnavansi
Copy link
Contributor

@srikrishnavansi srikrishnavansi commented Jan 12, 2026


Summary

Add comprehensive AI-powered tutor for package education with:

  • Interactive tutoring using TutorAgent orchestration
  • LLM-powered lessons, code examples, and Q&A (Claude API via cortex.llm_router)
  • SQLite-based progress tracking and student profiling
  • CLI integration via cortex tutor <package>

Related Issue

Closes cortexlinux/cortex-distro#30

Type of Change

  • New feature

Demo

Demo.mov

AI Disclosure

  • AI/IDE/Agents used (please describe below)

Used Claude (Anthropic) via Claude Code CLI for:

  • Code implementation and architecture design
  • PR review fixes (addressing Gemini/CodeRabbit comments)
  • Documentation generation

Testing

  • Run: pytest tests/tutor/ -v
  • Manual testing: cortex tutor docker --fresh

Checklist

Summary by CodeRabbit

  • New Features

    • AI-powered interactive tutor in the CLI: interactive lessons, step-by-step tutorials, Q&A, package list/progress/reset, and fresh/question options.
  • UX / Branding

    • Rich terminal UI: banners, menus, progress bars, panels, tables, formatted code and markdown.
  • Persistence & Tools

    • Local SQLite-backed progress/lesson cache, fallback lessons, deterministic tutor tools, and LLM-backed lesson/question flows.
  • Configuration & Validation

    • Env-driven configuration, data-dir handling, and comprehensive input validators.
  • Documentation

    • New comprehensive AI_TUTOR guide.
  • Tests

    • Extensive unit, integration, and end-to-end tests.
  • Chores

    • Packaging updated to include the tutor package.

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

Add comprehensive AI tutor for package education with:
- Interactive tutoring with Plan→Act→Reflect LangGraph workflow
- LLM-powered lessons, code examples, and Q&A (Claude API via LangChain)
- SQLite-based progress tracking with topic completion stats
- Best practices and step-by-step tutorials
- Rich terminal UI with branded output

Features:
- cortex tutor <package> - Start interactive session
- cortex tutor --list - Show studied packages
- cortex tutor --progress - View learning progress
- cortex tutor --reset - Reset progress

Technical:
- 266 tests with 85.61% coverage
- Lazy imports for non-LLM operations (no API key needed for --list/--progress)
- Pydantic models for type safety
- 7-layer prompt architecture

Closes cortexlinux#131
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 12, 2026

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Adds a new AI-powered "tutor" subsystem under cortex.tutor (agent, LLM adapter, deterministic tools, validators, contracts, SQLite persistence, Rich branding), wires a cortex tutor CLI subcommand, adds extensive tests and docs, and updates packaging to include the new package.

Changes

Cohort / File(s) Summary
Top-level CLI wiring
\cortex/cli.py`, `cortex/tutor/cli.py``
Adds tutor subcommand and parser flags (--list, --progress, --reset, --fresh, -q/--question, positional package); implements handlers for teach/question/list/progress/reset flows and main dispatch.
Package exports
\cortex/tutor/init.py``
New package init exporting Config, agent, console, tutor_print.
Branding / UI helpers
\cortex/tutor/branding.py``
New Rich-based console utilities: banners, menus, panels, progress bars, input helpers, printing helpers.
Configuration
\cortex/tutor/config.py``
New Pydantic Config, env/.env loading, API key validation, data-dir/DB path helpers, module-level caching.
Agent & interactive session
\cortex/tutor/agent.py``
TutorAgent (teach/ask/progress/reset/profile/etc.) and InteractiveTutor (menu-driven interactive session and handlers).
LLM integration
\cortex/tutor/llm.py``
Anthropic client singleton, structured tool-output handling, cost accounting, generate_lesson and answer_question flows.
Contracts / schemas
\cortex/tutor/contracts.py``
Pydantic models for lessons, progress, quizzes, and LLM responses with (de)serialization and helpers.
Persistence (SQLite)
\cortex/tutor/sqlite_store.py``
Thread-safe SQLiteStore for progress, quizzes, student profiles, lesson cache (TTL), and CRUD/aggregate APIs.
Deterministic tools
\cortex/tutor/tools.py``
LessonLoaderTool, ProgressTrackerTool, fallback lessons, cache-first loading, and helper utilities.
Validators
\cortex/tutor/validators.py``
Rule-based validators/sanitizers for package names, questions, topics, scores, extraction helpers and ValidationResult.
Tests
\tests/tutor/*`, `tests/tutor/test_llm.py``
Extensive unit and integration tests covering CLI, agent, interactive tutor, tools, validators, LLM parsing, SQLite store, and deterministic tools.
Docs & packaging
\docs/AI_TUTOR.md`, `pyproject.toml``
Adds AI_TUTOR documentation and includes cortex.tutor in packaged modules; updates pytest filterwarnings.
Formatting-only SQL changes
multiple \cortex/*.py`(e.g.,`cortex/context_memory.py`, `cortex/graceful_degradation.py``, ...)
Consolidated SQL string formatting and some index additions; no functional schema changes for most edits.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CLI as cortex/cli.py
    participant TutorCLI as cortex/tutor/cli.py
    participant Agent as TutorAgent
    participant Tools as LessonLoaderTool/Validators
    participant LLM as cortex/tutor/llm.py
    participant Storage as SQLiteStore

    User->>CLI: cortex tutor docker
    CLI->>TutorCLI: cmd_teach(package)
    TutorCLI->>Tools: validate_package_name()
    TutorCLI->>Agent: teach(package, force_fresh?)
    Agent->>Tools: load_lesson_with_fallback(package)
    alt cached or fallback
        Tools->>Storage: get_cached_lesson()
        Storage-->>Tools: lesson
        Tools-->>Agent: lesson (source=cache/fallback)
    else generate
        Agent->>LLM: generate_lesson(package)
        LLM-->>Agent: lesson + cost
        Agent->>Storage: cache_lesson()
    end
    Agent-->>TutorCLI: lesson payload
    TutorCLI->>Agent: start InteractiveTutor (if interactive)
    Agent->>Storage: update_progress()
    Storage-->>Agent: ok
    Agent-->>User: interactive menu rendered
Loading
sequenceDiagram
    actor User
    participant CLI as cortex/cli.py
    participant TutorCLI as cortex/tutor/cli.py
    participant Agent as TutorAgent
    participant LLM as cortex/tutor/llm.py
    participant Storage as SQLiteStore

    User->>CLI: cortex tutor docker -q "How to use volumes?"
    CLI->>TutorCLI: cmd_question(package, question)
    TutorCLI->>Agent: ask(package, question)
    Agent->>Storage: get_student_profile()
    Agent->>LLM: answer_question(package, question, context)
    LLM-->>Agent: answer + cost
    Agent->>Storage: (optionally) update_progress()
    Agent-->>TutorCLI: answer payload
    TutorCLI-->>User: formatted answer
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

  • #30: AI-Powered Installation Tutor — Implements an interactive tutor, Q&A, examples, progress tracking, and docs matching the issue objectives.
  • cortexlinux/cortex#131 — The PR implements the tutor subsystem aligning with that issue's requested feature set.

Possibly related PRs

  • cortexlinux/cortex#346 — Overlapping database initialization/index changes in context_memory.py; may require cross-review for index/SQL conventions.

Suggested labels

MVP

Suggested reviewers

  • Suyashd999
  • Anshgrover23

Poem

🐰 I hopped through code to teach and guide,
Lessons cached and LLMs by my side,
Menus that sparkle, progress stored so neat,
Ask a question — answers quick and sweet,
Hooray — the tutor makes learning a treat!

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive Some formatting-only changes to unrelated database/SQL modules and the licensing module appear out of scope; however, the core tutor implementation is in-scope with linked-issue alignment. Clarify whether formatting changes to cortex/context_memory.py, cortex/licensing.py, and similar database modules are intentional or should be removed in a follow-up cleanup.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title '[tutor] Add AI-Powered Installation Tutor' clearly and specifically describes the main change—adding a comprehensive AI-powered tutor feature.
Description check ✅ Passed The PR description includes a clear summary, related issue reference, type of change, AI disclosure, testing instructions, and a completed checklist, mostly following the required template.
Linked Issues check ✅ Passed The PR implements all major functional requirements from issue #30: LLM-powered lessons/Q&A, best-practice recommendations, code examples, step-by-step tutorials, progress tracking, CLI integration, unit tests, and documentation.
Docstring Coverage ✅ Passed Docstring coverage is 99.29% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

@github-actions
Copy link

github-actions bot commented Jan 12, 2026

CLA Verification Passed

All contributors have signed the CLA.

Contributor Signed As
@srikrishnavansi @srikrishnavansi
@Anshgrover23 @Anshgrover23
@srikrishnavansi @srikrishnavansi

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @srikrishnavansi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates an advanced AI-powered tutor into Cortex Linux, providing users with an interactive and personalized way to learn about various software packages. The system intelligently generates educational content, tracks learning progress, and offers quick answers to questions, enhancing the user's ability to understand and utilize system tools effectively.

Highlights

  • New AI-Powered Installation Tutor: Introduced a comprehensive AI tutor feature (cortex tutor) for interactive learning about software packages, leveraging Claude AI via LangChain.
  • Interactive Learning Workflow: Implemented a Plan→Act→Reflect LangGraph workflow for dynamic and adaptive tutoring sessions, including LLM-powered lessons, code examples, and Q&A.
  • Progress Tracking and Caching: Added SQLite-based progress tracking for topics and packages, along with smart caching of generated lessons to reduce API costs and improve performance.
  • Rich Terminal UI: Developed a rich terminal user interface using the Rich library, featuring branded output, syntax-highlighted code, progress bars, and interactive menus.
  • CLI Commands: New CLI commands are available: cortex tutor <package> for interactive lessons, cortex tutor -q 'question' for quick Q&A, cortex tutor --list to view studied packages, cortex tutor --progress to check learning progress, and cortex tutor --reset to clear progress.
  • Technical Enhancements: The implementation includes lazy imports for non-LLM operations (allowing --list/--progress without an API key), Pydantic models for type safety, and a 7-layer prompt architecture for robust AI agent behavior.
  • Comprehensive Documentation and Testing: A detailed AI_TUTOR.md document outlines the feature's architecture, technical design, API reference, and usage. The new codebase is covered by 266 tests with 85.61% coverage.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This is an excellent and comprehensive pull request that adds a powerful AI Tutor feature. The architecture is well-designed, leveraging modern tools like LangGraph and Pydantic for a robust and maintainable implementation. The code is clean, well-documented, and includes a thorough test suite, which is fantastic to see. The 7-layer prompt architecture is particularly impressive and shows a deep understanding of prompt engineering best practices.

My review focuses on a few minor areas for improvement to enhance maintainability and robustness, such as centralizing CLI argument definitions and removing hardcoded values. Overall, this is a very high-quality contribution.

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: 9

🤖 Fix all issues with AI agents
In @cortex/tutor/cli.py:
- Around line 110-150: The cmd_teach function accepts a fresh flag but never
passes it to the InteractiveTutor; update cmd_teach to forward the fresh flag
when constructing InteractiveTutor (e.g., InteractiveTutor(package,
force_fresh=fresh) or matching param name), and update InteractiveTutor.__init__
in cortex.tutor.agents.tutor_agent to accept and store the corresponding
parameter (force_fresh or fresh) so the interactive session honors the flag.

In @cortex/tutor/config.py:
- Line 44: The db_path field is declared as Path but defaults to None causing a
type mismatch; update the annotation to Optional[Path] (and add from typing
import Optional if missing) for the db_path Field declaration so it accurately
allows None prior to model_post_init, keeping the Field(..., description="Path
to SQLite database") usage and leaving model_post_init behavior unchanged.

In @cortex/tutor/memory/sqlite_store.py:
- Around line 340-377: The singleton profile logic can race when two threads
insert simultaneously; add a UNIQUE constraint to the student_profile table
(e.g., enforce a single row via id PRIMARY KEY or a UNIQUE constant key) and
change _create_default_profile() to perform an atomic upsert (SQLite: INSERT OR
IGNORE or INSERT ... ON CONFLICT DO NOTHING) instead of blindly inserting, then
re-query the table and return the actual stored row; update
get_student_profile() to, after attempting creation, SELECT the profile again
(don’t recurse) so whichever thread succeeded returns the canonical row and
duplicates/constraint errors are avoided.

In @cortex/tutor/tests/test_cli.py:
- Around line 103-115: The test mocks the wrong import location for
InteractiveTutor so cmd_teach still uses the real class; update the patch target
to where cmd_teach looks up the name by replacing
@patch("cortex.tutor.agents.tutor_agent.InteractiveTutor") with
@patch("cortex.tutor.cli.InteractiveTutor") in the test (test_successful_teach)
so the mocked InteractiveTutor instance is injected when cmd_teach("docker")
constructs and calls InteractiveTutor.start.

In @cortex/tutor/tests/test_progress_tracker.py:
- Around line 290-304: The three test stubs in TestConvenienceFunctions
(test_get_learning_progress, test_mark_topic_completed, test_get_package_stats)
should be implemented or removed; to fix, implement them using the temp_db
fixture to create minimal sample data, import and call the actual convenience
functions (get_learning_progress, mark_topic_completed, get_package_stats), then
assert expected behavior: for get_learning_progress verify returned structure
and percentages for a package/topic based on created progress rows, for
mark_topic_completed verify it creates/updates a progress record marking the
topic completed and sets completed_at or equivalent flag, and for
get_package_stats verify aggregate stats (total topics, completed count,
completion rate) match the test data; use the function/class names above to
locate targets and clean up any leftover pass placeholders if you prefer
removing unused tests.

In @cortex/tutor/tools/deterministic/lesson_loader.py:
- Around line 131-149: The clear_cache function's behavior doesn't match its
docstring: clear_cache(package_name) currently overwrites the cache entry via
self.store.cache_lesson(package_name, {}, ttl_hours=0) (marking it
expired/unretrievable to get_cached_lesson) rather than deleting it, and
clear_cache(None) calls self.store.clear_expired_cache() which only removes
already-expired entries. Update the clear_cache docstring to say that passing a
package_name marks that package's cache as expired (using cache_lesson with
ttl_hours=0) and that the function returns 1 on successful marking or 0 on
error; also clarify that calling clear_cache(None) only removes entries whose
expires_at <= now and that the return value is whatever
self.store.clear_expired_cache() returns (number of entries removed). Ensure
references to the symbols clear_cache, self.store.cache_lesson,
self.store.clear_expired_cache, and get_cached_lesson are used in the docstring
to make behavior explicit.

In @docs/AI_TUTOR.md:
- Line 753: The documentation line "Current coverage: **87%** (266 tests)" is
stale; update that exact string to reflect the current test coverage percentage
and test count (replace both the percentage and the test count with the latest
values), and scan for other occurrences of the same string in the docs to keep
coverage numbers consistent; commit the updated line so the README/doc shows the
accurate "Current coverage: **X%** (Y tests)" value.
- Line 6: The coverage badge in the README currently shows 87% ("[![Test
Coverage](https://img.shields.io/badge/coverage-87%25-brightgreen.svg)](https://github.com/cortexlinux/cortex)"),
which conflicts with the PR's reported 85.61%; update the badge value to match
the actual coverage (replace "87%25" with "85.61%25") or switch to a dynamic
coverage badge that reflects the real-time metric so the percentage stays
accurate.

In @requirements.txt:
- Around line 27-31: The dependency version ranges in requirements (langchain,
langchain-anthropic, langgraph) are too loose and risk pulling breaking major
releases; update the entries for langchain, langchain-anthropic, and langgraph
to use compatible release specifiers (e.g., change langchain>=0.3.0 to an
approximate pin like langchain~=1.2.0, langchain-anthropic>=0.3.0 to
langchain-anthropic~=1.3.0, and langgraph>=0.2.0 to langgraph~=1.0.0) so you
allow patch/minor updates but prevent unintended major version jumps while
leaving pydantic as-is unless you want to similarly tighten it.
🧹 Nitpick comments (42)
cortex/tutor/tools/agentic/examples_provider.py (2)

7-7: Unused import.

Path from pathlib is imported but not used in this module.

🧹 Suggested fix
-from pathlib import Path
 from typing import Any, Dict, List, Optional

59-157: Consider reducing duplication between _run and _arun.

Both methods share identical prompt construction and response structuring logic. Extracting the common setup into a helper method would improve maintainability.

♻️ Optional refactor to reduce duplication
def _build_chain(self):
    """Build the prompt-LLM-parser chain."""
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", self._get_system_prompt()),
            ("human", self._get_generation_prompt()),
        ]
    )
    return prompt | self.llm | JsonOutputParser()

def _build_inputs(self, package_name, topic, difficulty, learning_style, existing_knowledge):
    """Build input dict for chain invocation."""
    return {
        "package_name": package_name,
        "topic": topic,
        "difficulty": difficulty,
        "learning_style": learning_style,
        "existing_knowledge": ", ".join(existing_knowledge or []) or "basics",
    }

def _run(self, package_name: str, topic: str, ...) -> Dict[str, Any]:
    try:
        chain = self._build_chain()
        inputs = self._build_inputs(package_name, topic, difficulty, learning_style, existing_knowledge)
        result = chain.invoke(inputs)
        examples = self._structure_response(result, package_name, topic)
        return {"success": True, "examples": examples, "cost_gbp": 0.01}
    except Exception as e:
        return {"success": False, "error": str(e), "examples": None}
requirements.txt (1)

25-26: Pre-existing duplicate PyYAML entries noted.

There are three PyYAML specifications in this file (lines 9, 21, and 25) with conflicting constraints (>=6.0.0 vs ==6.0.3). While not introduced by this PR, this could cause dependency resolution issues. Consider consolidating in a follow-up.

cortex/tutor/tests/test_branding.py (3)

8-9: Remove unused imports.

MagicMock and StringIO are imported but never used in this test module.

Proposed fix
-from unittest.mock import Mock, patch, MagicMock
-from io import StringIO
+from unittest.mock import Mock, patch

46-70: Consider adding output assertions for stronger tests.

These tests accept capsys but don't use it to verify the output. While they serve as smoke tests ensuring no exceptions are raised, they don't validate the actual output content.

For improved test quality, consider capturing and asserting on key output patterns:

def test_tutor_print_success(self, capsys):
    tutor_print("Test message", "success")
    captured = capsys.readouterr()
    assert "Test message" in captured.out

243-248: Clarify test intent and strengthen assertion.

The test name test_get_user_input_strips suggests testing strip behavior, but the comment says "get_user_input should return raw input" (no stripping). The assertion assert "spaced" in result passes regardless of whether input is stripped or not.

Clarify the intended behavior and use a precise assertion:

Proposed fix for raw input (no stripping)
     @patch("builtins.input", return_value="  spaced  ")
-    def test_get_user_input_strips(self, mock_input):
-        """Test input stripping is not done (raw input)."""
+    def test_get_user_input_returns_raw(self, mock_input):
+        """Test get_user_input returns raw input without stripping."""
         result = get_user_input("Enter value")
-        # Note: get_user_input should return raw input
-        assert "spaced" in result
+        assert result == "  spaced  "
cortex/tutor/tests/test_tutor_agent.py (3)

9-10: Remove unused imports.

tempfile and Path are imported but never used in this test module.

Proposed fix
-import tempfile
-from pathlib import Path

78-84: Floating-point comparison may be fragile.

Direct equality comparison state["cost_gbp"] == 0.03 on floats can fail due to precision issues. While 0.02 + 0.01 is exact in this case, consider using pytest.approx for robustness:

assert state["cost_gbp"] == pytest.approx(0.03)

303-317: Consider splitting multiple exception assertions into separate tests.

This test validates two different input validation scenarios (empty package name and empty question) in a single test. If the first assertion fails, the second is never executed, potentially hiding additional issues.

Consider splitting into test_ask_validates_package_name and test_ask_validates_question for better test isolation.

cortex/tutor/tests/test_interactive_tutor.py (2)

8-8: Remove unused import.

call is imported from unittest.mock but never used in this test module.

Proposed fix
-from unittest.mock import Mock, patch, MagicMock, call
+from unittest.mock import Mock, patch, MagicMock

246-262: Consider adding behavior assertions for None lesson handling.

The test verifies that methods don't raise exceptions when lesson is None, which is valuable. However, adding assertions about the expected behavior (e.g., verifying that appropriate "No content" messages are printed or that methods return early) would strengthen the test.

cortex/tutor/prompts/tools/lesson_generator.md (2)

66-74: Add language identifier to fenced code block.

Per static analysis (markdownlint MD040), this code block should specify a language. Since it's JSON, add json after the opening fence.

-```
+```json
 {
   "package_name": "name of the package to teach",

104-132: Consider adding a language identifier for the workflow code block.

This fenced code block (starting at Line 104) lacks a language specification. Since it's pseudo-code/plaintext describing the workflow, you could use text or plaintext as the identifier to satisfy linting rules.

cortex/tutor/tests/test_integration.py (1)

207-216: Consider adding minimal assertions or removing unused capsys.

The capsys fixture is captured but not used in the assertions. If verifying Rich console output is impractical, consider removing the capture or adding a comment explaining the intentional no-op check.

♻️ Suggested refactor
     def test_tutor_print_success(self, capsys):
         """Test tutor_print with success status."""
         tutor_print("Test message", "success")
-        captured = capsys.readouterr()
-        # Rich console output is complex, just ensure no errors
+        # Rich console output is complex; this test verifies no exceptions are raised

     def test_tutor_print_error(self, capsys):
         """Test tutor_print with error status."""
         tutor_print("Error message", "error")
-        captured = capsys.readouterr()
+        # Verifies no exceptions are raised with error status
cortex/tutor/tests/test_validators.py (2)

216-219: Type hint inconsistency: sanitize_input accepts None but typed as str.

The test correctly verifies that sanitize_input(None) returns "", but the function signature in validators.py is typed as sanitize_input(input_text: str). Consider updating the type hint to Optional[str] for consistency, or document this behavior.


240-243: Same type consideration for extract_package_name(None).

Similar to sanitize_input, this tests None input while the function signature expects str.

cortex/tutor/tests/test_deterministic_tools.py (1)

99-108: Consider using tmp_path fixture instead of custom temp_db.

Pytest provides a built-in tmp_path fixture that handles cleanup automatically. The current approach works, but using tmp_path would be slightly more idiomatic.

♻️ Optional refactor
-    @pytest.fixture
-    def temp_db(self):
-        """Create a temporary database."""
-        with tempfile.TemporaryDirectory() as tmpdir:
-            yield Path(tmpdir) / "test.db"
+    @pytest.fixture
+    def temp_db(self, tmp_path):
+        """Create a temporary database."""
+        return tmp_path / "test.db"
cortex/tutor/tools/__init__.py (1)

7-11: Consider adding LessonLoaderTool to parent package exports for API consistency.

The cortex/tutor/tools/deterministic/__init__.py exports LessonLoaderTool, but this parent package's __all__ does not include it. Since other deterministic tools (ProgressTrackerTool, validate_package_name, validate_input) are re-exported here, LessonLoaderTool should also be added for a complete and consistent public API. This allows users to import it as from cortex.tutor.tools import LessonLoaderTool alongside other deterministic tools.

cortex/tutor/tests/test_progress_tracker.py (1)

27-44: Consider adding return type hints to fixtures.

Per coding guidelines, type hints are required. The fixtures should specify return types.

♻️ Suggested improvement
 @pytest.fixture
-def temp_db():
+def temp_db() -> Path:
     """Create a temporary database for testing."""
     with tempfile.TemporaryDirectory() as tmpdir:
         db_path = Path(tmpdir) / "test_progress.db"
         yield db_path


 @pytest.fixture
-def store(temp_db):
+def store(temp_db: Path) -> SQLiteStore:
     """Create a SQLite store with temp database."""
     return SQLiteStore(temp_db)


 @pytest.fixture
-def tracker(temp_db):
+def tracker(temp_db: Path) -> ProgressTrackerTool:
     """Create a progress tracker with temp database."""
     return ProgressTrackerTool(temp_db)
cortex/tutor/tests/test_agent_methods.py (1)

7-10: Remove unused imports.

tempfile and Path are imported but never used in this test file.

♻️ Proposed fix
 import pytest
 from unittest.mock import Mock, patch, MagicMock
-import tempfile
-from pathlib import Path
cortex/tutor/tests/test_cli.py (1)

10-10: Remove unused import.

StringIO is imported but never used in this file.

♻️ Proposed fix
-from io import StringIO
cortex/tutor/tests/test_tools.py (2)

8-8: Remove unused import.

MagicMock is imported but never used; only Mock is used.

♻️ Proposed fix
-from unittest.mock import Mock, patch, MagicMock
+from unittest.mock import Mock, patch

276-307: Fragile test pattern using __new__ to bypass __init__.

Using ConversationHandler.__new__() and manually setting attributes bypasses the constructor, which can lead to tests that don't catch initialization bugs. Consider properly mocking the config dependency and instantiating normally.

♻️ Suggested improvement
     def test_build_context_empty(self):
         """Test context building with empty history."""
-        with patch("cortex.tutor.tools.agentic.qa_handler.get_config"):
-            handler = ConversationHandler.__new__(ConversationHandler)
-            handler.history = []
+        with patch("cortex.tutor.tools.agentic.qa_handler.get_config") as mock_config:
+            mock_config.return_value = Mock(
+                anthropic_api_key="test_key",
+                model="claude-sonnet-4-20250514",
+            )
+            with patch("cortex.tutor.tools.agentic.qa_handler.ChatAnthropic"):
+                handler = ConversationHandler()
+                handler.history = []  # Reset for test
 
             context = handler._build_context()
             assert "Starting fresh" in context
cortex/tutor/agents/tutor_agent/tutor_agent.py (2)

248-258: Unused current_step attribute and missing force_fresh support.

The current_step attribute (line 258) is initialized but never used in the class. Additionally, the CLI's --fresh flag (passed to cmd_teach) is not propagated to InteractiveTutor, so users cannot force fresh content generation in interactive mode.

Consider either:

  1. Removing current_step if not needed
  2. Adding force_fresh: bool = False parameter to __init__ and passing it to self.agent.teach()
♻️ Suggested fix to support force_fresh
-    def __init__(self, package_name: str) -> None:
+    def __init__(self, package_name: str, force_fresh: bool = False) -> None:
         """
         Initialize interactive tutor for a package.

         Args:
             package_name: Package to learn.
+            force_fresh: Skip cache and generate fresh content.
         """
         self.package_name = package_name
         self.agent = TutorAgent(verbose=False)
         self.lesson: Optional[Dict[str, Any]] = None
-        self.current_step = 0
+        self.force_fresh = force_fresh

439-446: Consider extracting the magic number 5 as a constant.

The default topic count of 5 (line 443) is used as a fallback when stats.get("total", 0) is 0. While the inline comment explains it, extracting this as a named constant would improve clarity.

cortex/tutor/tools/deterministic/lesson_loader.py (1)

100-106: Consider async SQLite operations in the future.

The async method currently delegates to the synchronous implementation. This is acceptable for fast SQLite operations but could be improved with aiosqlite if async performance becomes important.

cortex/tutor/tools/agentic/lesson_generator.py (3)

1-19: Unused imports from lesson_context.

LessonContext, CodeExample, and TutorialStep (line 18) are imported but not used in this module. The _structure_response method returns a plain Dict rather than using these Pydantic models for validation.

Consider either:

  1. Removing unused imports
  2. Using LessonContext.model_validate() in _structure_response to leverage Pydantic validation

21-36: _load_prompt_template is defined but never called.

The function loads a prompt template from a file but is never invoked anywhere in this module. The actual prompts are generated inline by _get_system_prompt() and _get_generation_prompt().

♻️ Consider removing unused function
-# Load prompt template
-def _load_prompt_template() -> str:
-    """Load the lesson generator prompt from file."""
-    prompt_path = Path(__file__).parent.parent.parent / "prompts" / "tools" / "lesson_generator.md"
-    if prompt_path.exists():
-        return prompt_path.read_text()
-    # Fallback inline prompt
-    return """You are a lesson content generator. Generate comprehensive educational content
-    for the package: {package_name}
-
-    Student level: {student_level}
-    Learning style: {learning_style}
-    Focus areas: {focus_areas}
-
-    Return a JSON object with: summary, explanation, use_cases, best_practices,
-    code_examples, tutorial_steps, installation_command, confidence."""
-
-

142-184: Consider extracting shared prompt construction logic.

The _arun method duplicates the prompt construction from _run (lines 105-110 vs 152-157). Extracting this to a helper method would reduce duplication.

cortex/tutor/contracts/progress_context.py (1)

98-100: datetime.utcnow() is deprecated; use timezone-aware datetime.

datetime.utcnow() is deprecated since Python 3.12. Use datetime.now(timezone.utc) for timezone-aware UTC timestamps.

♻️ Suggested fix
+from datetime import datetime, timezone
-from datetime import datetime
 ...
     last_updated: datetime = Field(
-        default_factory=datetime.utcnow, description="Last update timestamp"
+        default_factory=lambda: datetime.now(timezone.utc), description="Last update timestamp (UTC)"
     )
cortex/tutor/agents/tutor_agent/graph.py (1)

1-25: Unused import: ExamplesProviderTool.

ExamplesProviderTool (line 24) is imported but never used in this module.

♻️ Remove unused import
 from cortex.tutor.tools.agentic.qa_handler import QAHandlerTool
-from cortex.tutor.tools.agentic.examples_provider import ExamplesProviderTool
cortex/tutor/contracts/lesson_context.py (1)

99-101: datetime.utcnow() is deprecated.

Same issue as in progress_context.py. Use datetime.now(timezone.utc) for timezone-aware UTC timestamps.

cortex/tutor/tools/deterministic/validators.py (1)

40-49: Unused constant: KNOWN_PACKAGE_CATEGORIES.

This constant is defined but never referenced anywhere in the module. Consider removing it or documenting its intended future use.

🔧 Suggested fix

Either remove the unused constant:

-# Common package categories for validation hints
-KNOWN_PACKAGE_CATEGORIES = [
-    "system",  # apt, systemctl, journalctl
-    "development",  # git, docker, npm, pip
-    "database",  # postgresql, mysql, redis, mongodb
-    "web",  # nginx, apache, curl, wget
-    "security",  # ufw, fail2ban, openssl
-    "networking",  # ssh, netstat, iptables
-    "utilities",  # vim, tmux, htop, grep
-]

Or add a TODO comment explaining its intended use:

+# TODO: Use for package categorization hints in future validation enhancements
 KNOWN_PACKAGE_CATEGORIES = [
cortex/tutor/tools/deterministic/progress_tracker.py (4)

7-10: Unused import: Path.

The Path type is imported but never used in this module.

🔧 Suggested fix
 from datetime import datetime
-from pathlib import Path
 from typing import Any, Dict, List, Optional

115-117: Async method delegates to sync - no actual async benefit.

The _arun method simply calls the synchronous _run, which blocks the event loop. This is acceptable if the underlying SQLite operations are fast, but consider noting this limitation in the docstring.

📝 Suggested docstring improvement
     async def _arun(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
-        """Async version - delegates to sync implementation."""
+        """Async version - delegates to sync implementation.
+
+        Note: SQLite operations are synchronous. For true async, consider
+        aiosqlite in future iterations.
+        """
         return self._run(*args, **kwargs)

258-280: Potential parameter shadowing issue.

The concept parameter is fetched from kwargs first, then falls back to the function parameter. This is redundant since concept would already be in kwargs if passed as a keyword argument.

🔧 Suggested simplification
     def _add_mastered_concept(
         self,
         concept: Optional[str] = None,
         **kwargs: Any,
     ) -> Dict[str, Any]:
         """Add a mastered concept to student profile."""
-        concept = kwargs.get("concept") or concept
         if not concept:
             return {"success": False, "error": "concept required"}
         self.store.add_mastered_concept(concept)
         return {"success": True, "message": f"Added mastered concept: {concept}"}

     def _add_weak_concept(
         self,
         concept: Optional[str] = None,
         **kwargs: Any,
     ) -> Dict[str, Any]:
         """Add a weak concept to student profile."""
-        concept = kwargs.get("concept") or concept
         if not concept:
             return {"success": False, "error": "concept required"}
         self.store.add_weak_concept(concept)
         return {"success": True, "message": f"Added weak concept: {concept}"}

301-346: Convenience functions create new tool instances on each call.

Each call instantiates a new ProgressTrackerTool, which opens a new database connection. For repeated operations, this is inefficient. Consider documenting this trade-off or providing a module-level singleton option.

📝 Suggested improvement

Add a note about usage patterns:

 # Convenience functions for direct usage
+# Note: Each function creates a new tool instance. For batch operations,
+# instantiate ProgressTrackerTool once and reuse it.


 def get_learning_progress(package_name: str, topic: str) -> Optional[Dict[str, Any]]:

Or provide a singleton pattern:

_default_tool: Optional[ProgressTrackerTool] = None

def _get_tool() -> ProgressTrackerTool:
    global _default_tool
    if _default_tool is None:
        _default_tool = ProgressTrackerTool()
    return _default_tool
cortex/tutor/tools/agentic/qa_handler.py (4)

7-8: Unused import: Path.

The Path type is imported but never used in this module.

🔧 Suggested fix
-from pathlib import Path
 from typing import Any, Dict, List, Optional

59-162: Significant code duplication between _run and _arun.

The sync and async methods share nearly identical logic. Extract the common setup into a helper method.

♻️ Suggested refactor
+    def _build_chain(self):
+        """Build the prompt chain for Q&A."""
+        prompt = ChatPromptTemplate.from_messages(
+            [
+                ("system", self._get_system_prompt()),
+                ("human", self._get_qa_prompt()),
+            ]
+        )
+        return prompt | self.llm | JsonOutputParser()
+
+    def _build_invoke_params(
+        self,
+        package_name: str,
+        question: str,
+        learning_style: str,
+        mastered_concepts: Optional[List[str]],
+        weak_concepts: Optional[List[str]],
+        lesson_context: Optional[str],
+    ) -> Dict[str, Any]:
+        """Build parameters for chain invocation."""
+        return {
+            "package_name": package_name,
+            "question": question,
+            "learning_style": learning_style,
+            "mastered_concepts": ", ".join(mastered_concepts or []) or "none specified",
+            "weak_concepts": ", ".join(weak_concepts or []) or "none specified",
+            "lesson_context": lesson_context or "starting fresh",
+        }
+
     def _run(self, ...) -> Dict[str, Any]:
         try:
-            prompt = ChatPromptTemplate.from_messages(...)
-            chain = prompt | self.llm | JsonOutputParser()
-            result = chain.invoke({...})
+            chain = self._build_chain()
+            params = self._build_invoke_params(...)
+            result = chain.invoke(params)
             # ... rest unchanged

105-109: Hardcoded cost estimate may become inaccurate.

The cost_gbp: 0.02 is static and may not reflect actual API costs as pricing changes.

📝 Suggested improvement

Consider computing based on token usage or making it configurable:

+            # Note: Estimated cost; actual varies by token usage
             return {
                 "success": True,
                 "answer": answer,
-                "cost_gbp": 0.02,
+                "cost_estimate_gbp": 0.02,  # Approximate based on ~2K tokens
             }

268-344: Unbounded conversation history growth.

The history list grows indefinitely during long sessions. While _build_context only uses the last 3 exchanges, the full history is retained in memory.

🔧 Suggested fix to cap history size
     def ask(
         self,
         question: str,
         ...
     ) -> Dict[str, Any]:
         ...
         # Update history
         if result.get("success"):
             self.history.append(
                 {
                     "question": question,
                     "answer": result["answer"].get("answer", ""),
                 }
             )
+            # Keep history bounded to prevent memory growth
+            max_history = 20  # or make configurable
+            if len(self.history) > max_history:
+                self.history = self.history[-max_history:]

         return result
cortex/tutor/memory/sqlite_store.py (1)

41-48: Replace mutable default values with Field(default_factory=list).

Pydantic recommends using Field(default_factory=list) for list defaults to ensure each instance gets a fresh list and to maintain consistency across Pydantic versions. While Pydantic does deepcopy mutable defaults, explicitly using default_factory is the clearer, future-proof approach.

🔧 Suggested fix
+from pydantic import BaseModel, Field
-from pydantic import BaseModel


 class StudentProfile(BaseModel):
     """Model for student profile."""

     id: Optional[int] = None
-    mastered_concepts: List[str] = []
-    weak_concepts: List[str] = []
+    mastered_concepts: List[str] = Field(default_factory=list)
+    weak_concepts: List[str] = Field(default_factory=list)
     learning_style: str = "reading"  # visual, reading, hands-on
     last_session: Optional[str] = None
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 337522a and 1fbed6a.

📒 Files selected for processing (42)
  • cortex/cli.py
  • cortex/tutor/__init__.py
  • cortex/tutor/agents/__init__.py
  • cortex/tutor/agents/tutor_agent/__init__.py
  • cortex/tutor/agents/tutor_agent/graph.py
  • cortex/tutor/agents/tutor_agent/state.py
  • cortex/tutor/agents/tutor_agent/tutor_agent.py
  • cortex/tutor/branding.py
  • cortex/tutor/cli.py
  • cortex/tutor/config.py
  • cortex/tutor/contracts/__init__.py
  • cortex/tutor/contracts/lesson_context.py
  • cortex/tutor/contracts/progress_context.py
  • cortex/tutor/memory/__init__.py
  • cortex/tutor/memory/sqlite_store.py
  • cortex/tutor/prompts/agents/tutor/system.md
  • cortex/tutor/prompts/tools/examples_provider.md
  • cortex/tutor/prompts/tools/lesson_generator.md
  • cortex/tutor/prompts/tools/qa_handler.md
  • cortex/tutor/tests/__init__.py
  • cortex/tutor/tests/test_agent_methods.py
  • cortex/tutor/tests/test_agentic_tools.py
  • cortex/tutor/tests/test_branding.py
  • cortex/tutor/tests/test_cli.py
  • cortex/tutor/tests/test_deterministic_tools.py
  • cortex/tutor/tests/test_integration.py
  • cortex/tutor/tests/test_interactive_tutor.py
  • cortex/tutor/tests/test_progress_tracker.py
  • cortex/tutor/tests/test_tools.py
  • cortex/tutor/tests/test_tutor_agent.py
  • cortex/tutor/tests/test_validators.py
  • cortex/tutor/tools/__init__.py
  • cortex/tutor/tools/agentic/__init__.py
  • cortex/tutor/tools/agentic/examples_provider.py
  • cortex/tutor/tools/agentic/lesson_generator.py
  • cortex/tutor/tools/agentic/qa_handler.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/tutor/tools/deterministic/lesson_loader.py
  • cortex/tutor/tools/deterministic/progress_tracker.py
  • cortex/tutor/tools/deterministic/validators.py
  • docs/AI_TUTOR.md
  • requirements.txt
🧰 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/tutor/agents/__init__.py
  • cortex/tutor/agents/tutor_agent/state.py
  • cortex/tutor/agents/tutor_agent/__init__.py
  • cortex/tutor/tests/__init__.py
  • cortex/tutor/memory/__init__.py
  • cortex/tutor/tools/agentic/__init__.py
  • cortex/tutor/tests/test_branding.py
  • cortex/tutor/tests/test_validators.py
  • cortex/tutor/tests/test_agent_methods.py
  • cortex/tutor/tests/test_cli.py
  • cortex/tutor/config.py
  • cortex/tutor/tools/__init__.py
  • cortex/tutor/tests/test_tools.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/cli.py
  • cortex/tutor/tests/test_tutor_agent.py
  • cortex/tutor/cli.py
  • cortex/tutor/tests/test_interactive_tutor.py
  • cortex/tutor/tests/test_progress_tracker.py
  • cortex/tutor/contracts/progress_context.py
  • cortex/tutor/contracts/lesson_context.py
  • cortex/tutor/tests/test_agentic_tools.py
  • cortex/tutor/tests/test_integration.py
  • cortex/tutor/branding.py
  • cortex/tutor/agents/tutor_agent/graph.py
  • cortex/tutor/tools/agentic/examples_provider.py
  • cortex/tutor/tools/agentic/qa_handler.py
  • cortex/tutor/tools/deterministic/lesson_loader.py
  • cortex/tutor/agents/tutor_agent/tutor_agent.py
  • cortex/tutor/tools/deterministic/progress_tracker.py
  • cortex/tutor/tests/test_deterministic_tools.py
  • cortex/tutor/contracts/__init__.py
  • cortex/tutor/__init__.py
  • cortex/tutor/tools/agentic/lesson_generator.py
  • cortex/tutor/tools/deterministic/validators.py
  • cortex/tutor/memory/sqlite_store.py
{setup.py,setup.cfg,pyproject.toml,**/__init__.py}

📄 CodeRabbit inference engine (AGENTS.md)

Use Python 3.10 or higher as the minimum supported version

Files:

  • cortex/tutor/agents/__init__.py
  • cortex/tutor/agents/tutor_agent/__init__.py
  • cortex/tutor/tests/__init__.py
  • cortex/tutor/memory/__init__.py
  • cortex/tutor/tools/agentic/__init__.py
  • cortex/tutor/tools/__init__.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/tutor/contracts/__init__.py
  • cortex/tutor/__init__.py
🧠 Learnings (2)
📚 Learning: 2025-12-11T12:03:24.071Z
Learnt from: CR
Repo: cortexlinux/cortex PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-11T12:03:24.071Z
Learning: Applies to **/*.py : Type hints required in Python code

Applied to files:

  • requirements.txt
📚 Learning: 2025-12-11T12:03:24.071Z
Learnt from: CR
Repo: cortexlinux/cortex PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-11T12:03:24.071Z
Learning: Applies to tests/**/*.py : Maintain >80% test coverage for pull requests

Applied to files:

  • cortex/tutor/tests/__init__.py
🧬 Code graph analysis (20)
cortex/tutor/agents/__init__.py (2)
cortex/cli.py (1)
  • tutor (1016-1074)
cortex/tutor/agents/tutor_agent/tutor_agent.py (1)
  • TutorAgent (20-238)
cortex/tutor/agents/tutor_agent/__init__.py (2)
cortex/tutor/agents/tutor_agent/state.py (1)
  • TutorAgentState (50-104)
cortex/tutor/agents/tutor_agent/tutor_agent.py (2)
  • TutorAgent (20-238)
  • InteractiveTutor (241-447)
cortex/tutor/memory/__init__.py (1)
cortex/tutor/memory/sqlite_store.py (1)
  • SQLiteStore (51-531)
cortex/tutor/tools/agentic/__init__.py (3)
cortex/tutor/tools/agentic/lesson_generator.py (1)
  • LessonGeneratorTool (39-303)
cortex/tutor/tools/agentic/examples_provider.py (1)
  • ExamplesProviderTool (19-236)
cortex/tutor/tools/agentic/qa_handler.py (1)
  • QAHandlerTool (19-241)
cortex/tutor/tests/test_validators.py (1)
cortex/tutor/tools/deterministic/validators.py (11)
  • validate_package_name (52-94)
  • validate_input (97-136)
  • validate_question (139-149)
  • validate_topic (152-174)
  • validate_score (177-193)
  • validate_learning_style (196-211)
  • sanitize_input (214-237)
  • extract_package_name (240-278)
  • get_validation_errors (281-321)
  • validate_all (349-368)
  • ValidationResult (324-346)
cortex/tutor/tests/test_agent_methods.py (2)
cortex/tutor/agents/tutor_agent/graph.py (10)
  • plan_node (30-87)
  • load_cache_node (90-109)
  • generate_lesson_node (112-154)
  • qa_node (157-199)
  • reflect_node (202-251)
  • fail_node (254-275)
  • route_after_plan (281-297)
  • route_after_act (300-311)
  • create_tutor_graph (317-373)
  • get_tutor_graph (380-390)
cortex/tutor/agents/tutor_agent/tutor_agent.py (8)
  • TutorAgent (20-238)
  • teach (47-94)
  • ask (96-139)
  • get_profile (155-162)
  • update_learning_style (164-175)
  • mark_completed (177-195)
  • reset_progress (197-208)
  • get_packages_studied (210-218)
cortex/tutor/tests/test_cli.py (1)
cortex/tutor/cli.py (7)
  • create_parser (33-107)
  • cmd_teach (110-150)
  • cmd_question (153-208)
  • cmd_list_packages (211-238)
  • cmd_progress (241-311)
  • cmd_reset (314-345)
  • main (348-386)
cortex/tutor/config.py (6)
cortex/tutor/tools/agentic/examples_provider.py (1)
  • Config (39-40)
cortex/tutor/tools/agentic/lesson_generator.py (1)
  • Config (62-63)
cortex/tutor/tools/agentic/qa_handler.py (1)
  • Config (39-40)
cortex/tutor/tools/deterministic/lesson_loader.py (1)
  • Config (35-36)
cortex/tutor/tools/deterministic/progress_tracker.py (1)
  • Config (45-46)
cortex/logging_system.py (1)
  • debug (196-198)
cortex/tutor/tools/__init__.py (5)
cortex/tutor/tools/deterministic/progress_tracker.py (1)
  • ProgressTrackerTool (23-295)
cortex/tutor/tools/deterministic/validators.py (2)
  • validate_package_name (52-94)
  • validate_input (97-136)
cortex/tutor/tools/agentic/lesson_generator.py (1)
  • LessonGeneratorTool (39-303)
cortex/tutor/tools/agentic/examples_provider.py (1)
  • ExamplesProviderTool (19-236)
cortex/tutor/tools/agentic/qa_handler.py (1)
  • QAHandlerTool (19-241)
cortex/tutor/tests/test_tools.py (1)
cortex/tutor/tools/deterministic/lesson_loader.py (4)
  • LessonLoaderTool (18-149)
  • get_fallback_lesson (227-237)
  • load_lesson_with_fallback (240-276)
  • _run (51-98)
cortex/tutor/tools/deterministic/__init__.py (3)
cortex/tutor/tools/deterministic/progress_tracker.py (1)
  • ProgressTrackerTool (23-295)
cortex/tutor/tools/deterministic/validators.py (2)
  • validate_package_name (52-94)
  • validate_input (97-136)
cortex/tutor/tools/deterministic/lesson_loader.py (1)
  • LessonLoaderTool (18-149)
cortex/tutor/tests/test_tutor_agent.py (3)
cortex/tutor/tools/agentic/qa_handler.py (1)
  • _run (59-116)
cortex/tutor/tools/deterministic/lesson_loader.py (1)
  • _run (51-98)
cortex/tutor/config.py (1)
  • reset_config (144-147)
cortex/tutor/cli.py (5)
cortex/tutor/branding.py (8)
  • print_banner (53-69)
  • tutor_print (35-50)
  • print_table (162-180)
  • print_progress_summary (143-159)
  • print_error_panel (238-251)
  • print_success_panel (254-267)
  • get_user_input (194-216)
  • print_code_example (102-115)
cortex/tutor/tools/deterministic/validators.py (1)
  • validate_package_name (52-94)
cortex/tutor/config.py (3)
  • Config (19-121)
  • from_env (60-89)
  • get_db_path (101-109)
cortex/tutor/memory/sqlite_store.py (5)
  • SQLiteStore (51-531)
  • get_packages_studied (520-531)
  • get_completion_stats (257-286)
  • get_all_progress (171-203)
  • reset_progress (500-518)
cortex/tutor/agents/tutor_agent/tutor_agent.py (6)
  • InteractiveTutor (241-447)
  • start (260-326)
  • TutorAgent (20-238)
  • ask (96-139)
  • get_packages_studied (210-218)
  • reset_progress (197-208)
cortex/tutor/tests/test_interactive_tutor.py (2)
cortex/tutor/tools/agentic/qa_handler.py (1)
  • ask (286-327)
cortex/tutor/memory/sqlite_store.py (1)
  • get_progress (142-169)
cortex/tutor/tests/test_agentic_tools.py (3)
cortex/tutor/tools/agentic/lesson_generator.py (2)
  • LessonGeneratorTool (39-303)
  • _structure_response (251-303)
cortex/tutor/tools/agentic/examples_provider.py (1)
  • _structure_response (208-236)
cortex/tutor/tools/agentic/qa_handler.py (5)
  • _structure_response (215-241)
  • QAHandlerTool (19-241)
  • ConversationHandler (268-344)
  • _build_context (329-340)
  • clear_history (342-344)
cortex/tutor/tests/test_integration.py (5)
cortex/tutor/config.py (5)
  • Config (19-121)
  • get_config (128-141)
  • reset_config (144-147)
  • from_env (60-89)
  • ensure_data_dir (91-99)
cortex/tutor/branding.py (2)
  • tutor_print (35-50)
  • print_banner (53-69)
cortex/tutor/contracts/lesson_context.py (6)
  • LessonContext (36-132)
  • CodeExample (13-21)
  • TutorialStep (24-33)
  • to_json (103-105)
  • from_json (108-110)
  • to_display_dict (120-132)
cortex/tutor/contracts/progress_context.py (8)
  • ProgressContext (66-143)
  • PackageProgress (23-63)
  • TopicProgress (13-20)
  • completion_percentage (33-38)
  • average_score (42-46)
  • is_complete (61-63)
  • get_next_topic (54-59)
  • get_recommendations (115-131)
cortex/tutor/cli.py (1)
  • create_parser (33-107)
cortex/tutor/contracts/__init__.py (2)
cortex/tutor/contracts/lesson_context.py (1)
  • LessonContext (36-132)
cortex/tutor/contracts/progress_context.py (1)
  • ProgressContext (66-143)
cortex/tutor/__init__.py (2)
cortex/tutor/config.py (1)
  • Config (19-121)
cortex/tutor/branding.py (1)
  • tutor_print (35-50)
cortex/tutor/tools/agentic/lesson_generator.py (2)
cortex/tutor/config.py (2)
  • get_config (128-141)
  • Config (19-121)
cortex/tutor/contracts/lesson_context.py (3)
  • LessonContext (36-132)
  • CodeExample (13-21)
  • TutorialStep (24-33)
cortex/tutor/memory/sqlite_store.py (5)
cortex/tutor/contracts/progress_context.py (1)
  • total_time_seconds (50-52)
cortex/tutor/agents/tutor_agent/tutor_agent.py (3)
  • get_progress (141-153)
  • reset_progress (197-208)
  • get_packages_studied (210-218)
cortex/tutor/tools/deterministic/progress_tracker.py (1)
  • mark_topic_completed (317-331)
cortex/hwprofiler.py (1)
  • profile (421-448)
cortex/tutor/tools/deterministic/lesson_loader.py (1)
  • cache_lesson (108-129)
🪛 markdownlint-cli2 (0.18.1)
docs/AI_TUTOR.md

92-92: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


250-250: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


310-310: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


362-362: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


401-401: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


443-443: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

cortex/tutor/prompts/tools/lesson_generator.md

66-66: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


97-97: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

@srikrishnavansi
Copy link
Contributor Author

recheck

Copy link
Collaborator

@Anshgrover23 Anshgrover23 left a comment

Choose a reason for hiding this comment

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

@srikrishnavansi Follow the contributing.md guidelines( i.e. add demonstrating video in pr description, add Documentation for the feature you're creating, add tests and AI Usage as well, thanks.
Also, kindly address CODERABBITAI comments.

@Anshgrover23 Anshgrover23 marked this pull request as draft January 12, 2026 05:47
@srikrishnavansi srikrishnavansi marked this pull request as ready for review January 12, 2026 05:56
@srikrishnavansi
Copy link
Contributor Author

I will update the PR and changes suggested by CodeRabbit.

@Anshgrover23 Anshgrover23 marked this pull request as draft January 12, 2026 06:47
@srikrishnavansi srikrishnavansi changed the title [tutor] Add AI-Powered Installation Tutor (Issue #131) [tutor] Add AI-Powered Installation Tutor (Issue #131) Jan 12, 2026
srikrishnavansi and others added 7 commits January 12, 2026 12:21
Fixes identified in PR cortexlinux#566 code review:

- Fix coverage badge discrepancy (87% → 85.6%) in docs/AI_TUTOR.md
- Fix db_path type annotation to Optional[Path] in config.py
- Remove unused ExamplesProviderTool import from graph.py
- Add DEFAULT_TUTOR_TOPICS constant to avoid magic numbers
- Fix --fresh flag propagation to InteractiveTutor
- Fix race condition in profile creation with INSERT OR IGNORE
- Tighten version constraints in requirements.txt (<2.0.0 bounds)
- Update clear_cache docstring to match actual behavior
- Implement empty test methods in TestConvenienceFunctions

All 266 tests pass with 86% coverage.
- Fix import sorting (I001) with ruff format
- Modernize type annotations for Python 3.10+:
  - Replace Dict/List/Tuple with dict/list/tuple
  - Replace Optional[X] with X | None
- Remove deprecated typing imports
- Fix f-string without placeholders

All 266 tests pass.
The tutor tests were in cortex/tutor/tests/ but CI runs
pytest tests/ - causing 0% coverage on tutor code.

Copied tests to tests/tutor/ so CI discovers them and
includes tutor code in coverage metrics.

All 266 tutor tests pass from new location.
@srikrishnavansi srikrishnavansi marked this pull request as ready for review January 12, 2026 07:20
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: 5

🤖 Fix all issues with AI agents
In @cortex/tutor/contracts/lesson_context.py:
- Around line 97-99: The Field default_factory for generated_at currently uses
datetime.utcnow (in the generated_at: datetime = Field(...
default_factory=datetime.utcnow ...)), which is deprecated; replace it with a
timezone-aware factory using datetime.now(timezone.utc) and ensure timezone is
imported from datetime (or use datetime.timezone) so generated_at remains
UTC-aware; update the default_factory reference in the generated_at Field and
add the necessary import for timezone at the top of the module.

In @cortex/tutor/contracts/progress_context.py:
- Line 157: The QuizContext.timestamp Field currently uses datetime.utcnow() (in
the default_factory) which is deprecated; change the default_factory for
QuizContext.timestamp to produce a timezone-aware UTC datetime (e.g., use
datetime.now(timezone.utc) or an equivalent lambda), and update imports if
needed to include timezone so the Field default is timezone-aware while keeping
the type as datetime.
- Around line 98-100: The Field default_factory for last_updated in
progress_context.py uses deprecated datetime.utcnow(); update the
default_factory to use datetime.now(timezone.utc) instead and ensure timezone is
imported from datetime (mirror the fix applied in lesson_context.py); modify the
default_factory on the last_updated Field (and any similar uses in this module)
to call datetime.now(timezone.utc) to produce an aware UTC timestamp.

In @cortex/tutor/memory/sqlite_store.py:
- Around line 206-239: The upsert_progress method currently returns
cursor.lastrowid which is 0 when the INSERT triggers the ON CONFLICT ... DO
UPDATE branch; change upsert_progress (learning_progress upsert) to detect that
case and return the actual row id by querying the table for the record after the
upsert (e.g., SELECT id or rowid WHERE package_name = ? AND topic = ?) when
cursor.lastrowid is 0, ensuring the function always returns the real row id for
the given progress.package_name and progress.topic.
- Around line 419-444: The functions add_mastered_concept and add_weak_concept
perform a read-modify-write via get_student_profile() and
update_student_profile(), which opens a TOCTOU race; replace the Python-side
mutation with an atomic database operation (or wrap the operation in a single
transaction) so concurrent updates cannot be lost: implement a single SQL UPDATE
that uses JSON functions to append/remove the concept from the mastered_concepts
or weak_concepts JSON array (and remove it from the opposite array when
promoting/demoting), or execute the read and write inside a DB transaction with
appropriate locking/isolation in the same code paths where get_student_profile()
and update_student_profile() are called so add_mastered_concept and
add_weak_concept no longer rely on separate read and write calls that can
interleave.
🧹 Nitpick comments (46)
cortex/tutor/tests/test_validators.py (2)

7-7: Unused pytest import.

The pytest module is imported but never used in this test file. No pytest.fixture, pytest.raises, or other pytest-specific features are utilized.

🔧 Suggested fix
-import pytest
-
 from cortex.tutor.tools.deterministic.validators import (

165-180: Consider adding boundary value tests for score validation.

The tests cover valid scores and out-of-range values, but consider adding explicit boundary tests for exactly 0.0 and 1.0 to ensure inclusive boundary handling is verified.

🧪 Additional boundary test
def test_boundary_scores(self):
    """Test exact boundary values are valid."""
    # Ensure 0.0 and 1.0 are explicitly accepted
    is_valid, error = validate_score(0.0)
    assert is_valid, "0.0 should be valid"
    is_valid, error = validate_score(1.0)
    assert is_valid, "1.0 should be valid"
cortex/tutor/memory/sqlite_store.py (2)

52-57: Docstring claims "connection pooling" but implementation creates new connections.

The class docstring mentions "connection pooling," but _get_connection creates a new connection on each call and closes it after use. This is connection-per-request, not pooling.

📝 Suggested docstring fix
 class SQLiteStore:
     """
     SQLite-based storage for learning progress and student data.
 
-    Thread-safe implementation with connection pooling.
+    Thread-safe implementation with per-request connections.
 
     Attributes:
         db_path: Path to the SQLite database file.
     """

42-50: Use Field(default_factory=list) for explicit mutable defaults.

While Pydantic v2 safely deep-copies empty list defaults for each instance, the idiomatic approach is Field(default_factory=list) to explicitly signal that each instance gets its own list. This also guards against issues if the codebase ever migrates to older Pydantic versions where mutable defaults were not safely handled. Apply to mastered_concepts and weak_concepts fields.

cortex/tutor/tests/test_deterministic_tools.py (2)

9-9: Unused imports from unittest.mock.

Mock and patch are imported but never used in this test file.

🔧 Suggested fix
 import tempfile
 from pathlib import Path
-from unittest.mock import Mock, patch
 
 import pytest

100-109: Duplicate temp_db fixture definition.

The temp_db fixture is defined identically in both TestLessonLoaderTool (lines 26-30) and TestProgressTrackerTool (lines 100-104). Consider moving it to a conftest.py file for reuse.

♻️ Suggested refactor

Create cortex/tutor/tests/conftest.py:

import tempfile
from pathlib import Path

import pytest

@pytest.fixture
def temp_db():
    """Create a temporary database."""
    with tempfile.TemporaryDirectory() as tmpdir:
        yield Path(tmpdir) / "test.db"

Then remove the duplicate fixtures from both test classes.

tests/tutor/test_agentic_tools.py (1)

7-7: Remove unused import MagicMock.

MagicMock is imported but never used in this file; only Mock is utilized.

Suggested fix
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch
cortex/tutor/tests/test_agent_methods.py (1)

7-11: Remove unused imports.

tempfile and MagicMock are imported but not used in this file.

Suggested fix
-import tempfile
 from pathlib import Path
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch
cortex/tutor/tests/test_branding.py (2)

7-10: Remove unused imports.

StringIO and MagicMock are imported but never used.

Suggested fix
-from io import StringIO
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch

244-249: Clarify test assertion for stripping behavior.

The test name suggests verifying stripping behavior, but assert "spaced" in result passes whether result is " spaced " or "spaced". Consider asserting the exact expected value.

Suggested fix for explicit verification
     @patch("builtins.input", return_value="  spaced  ")
     def test_get_user_input_strips(self, mock_input):
         """Test input stripping is not done (raw input)."""
         result = get_user_input("Enter value")
-        # Note: get_user_input should return raw input
-        assert "spaced" in result
+        # Verify raw input is returned without stripping
+        assert result == "  spaced  "
tests/tutor/test_tutor_agent.py (1)

7-11: Remove unused imports.

tempfile and MagicMock are imported but not used.

Suggested fix
-import tempfile
 from pathlib import Path
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch
cortex/tutor/tests/test_integration.py (2)

10-10: Remove unused import.

MagicMock is imported but not used.

Suggested fix
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import Mock, patch

205-222: Consider removing duplicate branding tests.

These tests duplicate coverage from tests/tutor/test_branding.py. Consider removing them to reduce maintenance overhead, or keep them intentionally as integration-level smoke tests.

tests/tutor/test_interactive_tutor.py (2)

1-11: Unused import detected.

The call import from unittest.mock is not used anywhere in this file.

🧹 Remove unused import
-from unittest.mock import MagicMock, Mock, call, patch
+from unittest.mock import MagicMock, Mock, patch

247-264: Test coverage for None lesson is incomplete.

This test only exercises 4 of the 6 helper methods (_show_concepts, _show_examples, _run_tutorial, _show_best_practices). Consider adding _ask_question and _show_progress for completeness.

Additionally, the test only verifies no exceptions are raised but doesn't assert expected behavior (e.g., that appropriate "no data" messages are printed).

🧪 Extended test with assertions
     def test_methods_with_no_lesson(self, mock_agent_class):
         """Test methods handle None lesson gracefully."""
         from cortex.tutor.agents.tutor_agent import InteractiveTutor

         tutor = InteractiveTutor("docker")
         tutor.lesson = None
         tutor.agent = Mock()
+        tutor.agent.get_progress.return_value = {"success": False}

         # These should not raise errors
-        tutor._show_concepts()
-        tutor._show_examples()
-        tutor._run_tutorial()
-        tutor._show_best_practices()
+        with patch("cortex.tutor.agents.tutor_agent.tutor_agent.tutor_print") as mock_print:
+            tutor._show_concepts()
+            tutor._show_examples()
+            tutor._run_tutorial()
+            tutor._show_best_practices()
+            tutor._show_progress()
tests/tutor/test_agent_methods.py (1)

411-441: State helpers well-tested, but has_critical_error is missing a direct test.

The has_critical_error function is imported (line 33) but not directly tested in TestStateHelpers. It's indirectly tested via TestRouting.test_route_after_plan_fail_on_error, but a direct unit test would improve coverage.

🧪 Add direct test for has_critical_error
def test_has_critical_error_true(self):
    """Test has_critical_error returns True for non-recoverable errors."""
    state = create_initial_state("docker")
    add_error(state, "test", "Critical error", recoverable=False)
    assert has_critical_error(state) is True

def test_has_critical_error_false(self):
    """Test has_critical_error returns False for recoverable errors."""
    state = create_initial_state("docker")
    add_error(state, "test", "Minor error", recoverable=True)
    assert has_critical_error(state) is False
tests/tutor/test_integration.py (2)

205-222: Branding tests verify no errors but lack output assertions.

The capsys.readouterr() result is captured but not verified. While Rich console output is complex, consider at minimum asserting the output is non-empty for success cases, or use Rich's Console with a StringIO for testable output.

🧪 Add minimal assertions
     def test_tutor_print_success(self, capsys):
         """Test tutor_print with success status."""
         tutor_print("Test message", "success")
         captured = capsys.readouterr()
-        # Rich console output is complex, just ensure no errors
+        # Rich console output is complex, verify something was printed
+        assert len(captured.out) > 0 or len(captured.err) > 0

298-365: Good end-to-end test for cache-hit workflow.

The test properly exercises the Plan→Load→Reflect flow with mocked tools and verifies state transitions. The note about removing real API tests is appropriate for CI.

Consider adding a similar test for the cache-miss path (Plan→Generate→Reflect) to cover both main workflows.

cortex/tutor/tests/test_progress_tracker.py (1)

290-351: Redundant imports inside test methods.

Mock and patch are imported inside each test method. Since these are already available in the module scope (line 9), the local imports are unnecessary.

🧹 Remove redundant imports
     def test_get_learning_progress(self, temp_db):
         """Test get_learning_progress function."""
-        from unittest.mock import Mock, patch
-
         # Mock the global config to use temp_db
         mock_config = Mock()

Apply similar changes to test_mark_topic_completed and test_get_package_stats.

cortex/tutor/tests/test_tools.py (1)

277-310: ConversationHandler tests use __new__ to bypass initialization.

While using __new__ to create uninitialized instances works for testing, it's fragile and couples tests to implementation details. Consider adding a factory method or making the handler more testable.

# Alternative: Initialize properly with mocked config
with patch("cortex.tutor.tools.agentic.qa_handler.get_config") as mock_config:
    mock_config.return_value = Mock(anthropic_api_key="test", model="test")
    with patch("cortex.tutor.tools.agentic.qa_handler.ChatAnthropic"):
        handler = ConversationHandler()
        handler.history = []  # Reset history for test
cortex/tutor/config.py (2)

45-49: Direct mutation in model_post_init works but consider model_validator.

Pydantic v2 allows mutation in model_post_init, but using a model_validator with mode='after' is more idiomatic for computed defaults.

♻️ Alternative using model_validator
from pydantic import model_validator

@model_validator(mode='after')
def set_db_path_default(self) -> 'Config':
    """Set default db_path if not provided."""
    if self.db_path is None:
        object.__setattr__(self, 'db_path', self.data_dir / "tutor_progress.db")
    return self

100-108: Return type inconsistency with field type.

get_db_path() returns Path but db_path field is typed as Path | None. While model_post_init ensures it's always set, the type checker may flag this. Consider adding an assertion or adjusting the type.

🔧 Add runtime assertion for type safety
     def get_db_path(self) -> Path:
         """
         Get the full path to the SQLite database.

         Returns:
             Path: Full path to tutor_progress.db
         """
         self.ensure_data_dir()
+        assert self.db_path is not None, "db_path should be set by model_post_init"
         return self.db_path
cortex/tutor/branding.py (2)

7-7: Remove unused Optional import.

The code uses modern union syntax (str | None) throughout, so Optional is not needed.

Proposed fix
-from typing import Literal, Optional
+from typing import Literal

218-234: Unused description parameter.

The description parameter is accepted but not used. If the intent is to set a default task description, it should be returned with a pre-added task or documented that callers should use progress.add_task(description).

Option 1: Remove unused parameter
-def create_progress_bar(description: str = "Processing") -> Progress:
+def create_progress_bar() -> Progress:
     """
     Create a Rich progress bar for long-running operations.

-    Args:
-        description: Description text for the progress bar.
-
     Returns:
         Progress: Configured Rich Progress instance.
     """
Option 2: Use the parameter
+from typing import Tuple
+
-def create_progress_bar(description: str = "Processing") -> Progress:
+def create_progress_bar(description: str = "Processing") -> Tuple[Progress, int]:
     """
     Create a Rich progress bar for long-running operations.

     Args:
         description: Description text for the progress bar.

     Returns:
-        Progress: Configured Rich Progress instance.
+        Tuple of (Progress instance, task_id).
     """
-    return Progress(
+    progress = Progress(
         SpinnerColumn(),
         TextColumn("[progress.description]{task.description}"),
         BarColumn(),
         TaskProgressColumn(),
         console=console,
     )
+    task_id = progress.add_task(description, total=100)
+    return progress, task_id
tests/tutor/test_progress_tracker.py (1)

7-7: Remove unused os import.

The os module is imported but not used in this file.

Proposed fix
-import os
 import tempfile
cortex/tutor/agents/tutor_agent/tutor_agent.py (1)

86-92: Calling private _run method directly on LangChain tools.

The _run method is a private API of LangChain's BaseTool. For tool invocation, the public API is run() or invoke(). Using private methods may break if the LangChain library changes its internal implementation.

♻️ Suggested refactor
-            self.progress_tool._run(
-                "update_progress",
-                package_name=package_name,
-                topic="overview",
-            )
+            self.progress_tool.run(
+                "update_progress",
+                package_name=package_name,
+                topic="overview",
+            )

Apply the same pattern to all other ._run() calls in this file (Lines 155, 156, 165, 177, 192-197, 210, 220).

cortex/tutor/tools/deterministic/lesson_loader.py (2)

100-106: Async method delegates to synchronous implementation.

The _arun method simply calls the sync _run method, which could block the event loop if the SQLite operations are slow. For a deterministic cache lookup, this is likely acceptable, but consider documenting this limitation.


125-129: Silent exception swallowing in cache_lesson.

Catching all exceptions and returning False hides the root cause of failures. Consider logging the exception for debugging purposes.

♻️ Suggested improvement
+import logging
+
+logger = logging.getLogger(__name__)
+
     def cache_lesson(
         self,
         package_name: str,
         lesson: dict[str, Any],
         ttl_hours: int = 24,
     ) -> bool:
         ...
         try:
             self.store.cache_lesson(package_name, lesson, ttl_hours)
             return True
-        except Exception:
+        except Exception as e:
+            logger.warning("Failed to cache lesson for %s: %s", package_name, e)
             return False
cortex/tutor/tools/agentic/lesson_generator.py (2)

21-36: Unused function _load_prompt_template.

This function is defined but never called. The prompts are instead hardcoded in _get_system_prompt and _get_generation_prompt methods. Either use this function or remove it.

♻️ Option 1: Remove unused code
-# Load prompt template
-def _load_prompt_template() -> str:
-    """Load the lesson generator prompt from file."""
-    prompt_path = Path(__file__).parent.parent.parent / "prompts" / "tools" / "lesson_generator.md"
-    if prompt_path.exists():
-        return prompt_path.read_text()
-    # Fallback inline prompt
-    return """You are a lesson content generator. Generate comprehensive educational content
-    for the package: {package_name}
-
-    Student level: {student_level}
-    Learning style: {learning_style}
-    Focus areas: {focus_areas}
-
-    Return a JSON object with: summary, explanation, use_cases, best_practices,
-    code_examples, tutorial_steps, installation_command, confidence."""
-

82-140: DRY violation: _run and _arun have duplicated logic.

The async _arun method duplicates most of the logic from _run, including prompt construction, chain setup, and error handling. Consider extracting shared logic.

♻️ Suggested refactor
def _build_chain(self):
    """Build the LLM chain for lesson generation."""
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", self._get_system_prompt()),
            ("human", self._get_generation_prompt()),
        ]
    )
    return prompt | self.llm | JsonOutputParser()

def _build_invoke_params(self, package_name, student_level, learning_style, focus_areas, skip_areas):
    """Build parameters for chain invocation."""
    return {
        "package_name": package_name,
        "student_level": student_level,
        "learning_style": learning_style,
        "focus_areas": ", ".join(focus_areas or []) or "all topics",
        "skip_areas": ", ".join(skip_areas or []) or "none",
    }

def _run(self, package_name, student_level="beginner", ...):
    try:
        chain = self._build_chain()
        params = self._build_invoke_params(...)
        result = chain.invoke(params)
        # ...

Also applies to: 142-184

tests/tutor/test_tools.py (1)

280-309: Fragile test setup using __new__ to bypass initialization.

Using __new__ to create objects without calling __init__ is fragile and can break if the class implementation changes. Consider using proper mocking or fixtures.

♻️ Suggested improvement
 class TestConversationHandler:
     """Tests for ConversationHandler."""

     def test_build_context_empty(self):
         """Test context building with empty history."""
-        with patch("cortex.tutor.tools.agentic.qa_handler.get_config"):
-            handler = ConversationHandler.__new__(ConversationHandler)
-            handler.history = []
-
+        with patch("cortex.tutor.tools.agentic.qa_handler.get_config") as mock_config:
+            mock_config.return_value = Mock(
+                anthropic_api_key="test_key",
+                model="claude-sonnet-4-20250514",
+            )
+            with patch("cortex.tutor.tools.agentic.qa_handler.ChatAnthropic"):
+                handler = ConversationHandler(package_name="test")
+                handler.history = []
+
             context = handler._build_context()
             assert "Starting fresh" in context
cortex/tutor/tools/agentic/examples_provider.py (2)

7-8: Unused import: Path.

The Path import from pathlib is not used anywhere in this file.

♻️ Suggested fix
-from pathlib import Path
 from typing import Any

59-113: DRY violation: _run and _arun duplicate logic.

Same issue as in lesson_generator.py. The async method duplicates the sync implementation. Consider extracting shared chain-building logic.

Also applies to: 115-157

cortex/tutor/agents/tutor_agent/graph.py (2)

374-388: Singleton graph creation is not thread-safe.

The get_tutor_graph() function uses a global variable without synchronization. In a multi-threaded environment, multiple threads could create the graph simultaneously.

♻️ Suggested thread-safe implementation
+import threading
+
 # Create singleton graph instance
 _graph = None
+_graph_lock = threading.Lock()


 def get_tutor_graph() -> StateGraph:
     """
     Get the singleton Tutor Agent graph.

     Returns:
         Compiled StateGraph.
     """
     global _graph
-    if _graph is None:
-        _graph = create_tutor_graph()
+    if _graph is None:
+        with _graph_lock:
+            if _graph is None:  # Double-check locking
+                _graph = create_tutor_graph()
     return _graph

44-45: Using private _run method on LangChain tools throughout graph nodes.

Multiple nodes call tool._run() which is a private API. Consider using the public run() or invoke() methods for better compatibility with future LangChain versions.

Also applies to: 62-63, 122-123, 173-174

cortex/tutor/cli.py (3)

155-210: Consider handling missing validation_passed key more defensively.

If agent.ask() returns a malformed response without the validation_passed key, .get("validation_passed") returns None, which is falsy and falls through to the error case. This is reasonable behavior, but you may want to log or handle unexpected response formats explicitly for debugging purposes.


213-240: Docstring is missing the verbose parameter.

The cmd_list_packages function accepts a verbose parameter but the docstring doesn't document it.

📝 Suggested fix
 def cmd_list_packages(verbose: bool = False) -> int:
     """
     List packages that have been studied.
 
+    Args:
+        verbose: Enable verbose output.
+
     Returns:
         Exit code.
     """

243-313: Docstring is missing the verbose parameter documentation.

Similar to cmd_list_packages, the verbose parameter is not documented in the docstring.

📝 Suggested fix
 def cmd_progress(package: str | None = None, verbose: bool = False) -> int:
     """
     Show learning progress.
 
     Args:
         package: Optional package filter.
+        verbose: Enable verbose output.
 
     Returns:
         Exit code.
     """
cortex/tutor/tools/deterministic/progress_tracker.py (2)

115-117: Async implementation blocks the event loop.

The _arun method calls the synchronous _run method directly, which will block the event loop if _run performs I/O (SQLite operations). For a truly async implementation, consider using asyncio.to_thread() or an async-compatible SQLite library.

♻️ Suggested fix to avoid blocking the event loop
     async def _arun(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
         """Async version - delegates to sync implementation."""
-        return self._run(*args, **kwargs)
+        import asyncio
+        return await asyncio.to_thread(self._run, *args, **kwargs)

301-346: Convenience functions create new tool instances per call.

Each convenience function (get_learning_progress, mark_topic_completed, get_package_stats) instantiates a new ProgressTrackerTool and thus a new SQLiteStore. This is inefficient for repeated calls and may have connection overhead. Consider caching or accepting an optional tool/store parameter.

cortex/tutor/tools/agentic/qa_handler.py (3)

1-8: Unused import: Path is imported but never used.

The Path import from pathlib is not used in this file.

🧹 Remove unused import
-from pathlib import Path
 from typing import Any

118-162: Code duplication between _run and _arun.

The sync and async methods have nearly identical logic. Consider extracting the common prompt/chain setup into a helper method.

♻️ Suggested refactor to reduce duplication
def _build_chain(self):
    """Build the prompt chain for Q&A."""
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", self._get_system_prompt()),
            ("human", self._get_qa_prompt()),
        ]
    )
    return prompt | self.llm | JsonOutputParser()

def _build_invoke_params(self, package_name, question, learning_style, 
                          mastered_concepts, weak_concepts, lesson_context):
    """Build parameters for chain invocation."""
    return {
        "package_name": package_name,
        "question": question,
        "learning_style": learning_style,
        "mastered_concepts": ", ".join(mastered_concepts or []) or "none specified",
        "weak_concepts": ", ".join(weak_concepts or []) or "none specified",
        "lesson_context": lesson_context or "starting fresh",
    }

268-344: Unbounded history growth in ConversationHandler.

The history list grows unboundedly. While _build_context only uses the last 3 exchanges, the full history is retained. Consider limiting history size to prevent memory growth in long sessions.

♻️ Suggested fix to limit history size
+    MAX_HISTORY_SIZE = 10  # Keep last 10 exchanges
+
     def ask(
         self,
         question: str,
         # ... params
     ) -> dict[str, Any]:
         # ... existing code ...
         
         # Update history
         if result.get("success"):
             self.history.append(
                 {
                     "question": question,
                     "answer": result["answer"].get("answer", ""),
                 }
             )
+            # Trim history to prevent unbounded growth
+            if len(self.history) > self.MAX_HISTORY_SIZE:
+                self.history = self.history[-self.MAX_HISTORY_SIZE:]
 
         return result
tests/tutor/test_branding.py (3)

7-8: Unused imports: StringIO, MagicMock, and Mock are imported but never used.

These imports are not utilized in the test file.

🧹 Remove unused imports
-from io import StringIO
-from unittest.mock import MagicMock, Mock, patch
+from unittest.mock import patch

44-70: Tests lack assertions to verify output content.

The TestTutorPrint tests call the functions but don't assert anything about the output. While smoke testing is valuable, consider adding at least basic assertions using capsys.readouterr() to verify expected output patterns.

💡 Example of adding assertions
def test_tutor_print_success(self, capsys):
    """Test success status print."""
    tutor_print("Test message", "success")
    captured = capsys.readouterr()
    assert "Test message" in captured.out or len(captured.out) > 0

73-91: Tests lack meaningful assertions.

Similar to TestTutorPrint, these tests only verify that functions don't raise exceptions. Consider adding assertions to verify output contains expected content.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1fbed6a and a799d27.

📒 Files selected for processing (46)
  • cortex/cli.py
  • cortex/tutor/__init__.py
  • cortex/tutor/agents/tutor_agent/__init__.py
  • cortex/tutor/agents/tutor_agent/graph.py
  • cortex/tutor/agents/tutor_agent/state.py
  • cortex/tutor/agents/tutor_agent/tutor_agent.py
  • cortex/tutor/branding.py
  • cortex/tutor/cli.py
  • cortex/tutor/config.py
  • cortex/tutor/contracts/lesson_context.py
  • cortex/tutor/contracts/progress_context.py
  • cortex/tutor/memory/sqlite_store.py
  • cortex/tutor/tests/test_agent_methods.py
  • cortex/tutor/tests/test_agentic_tools.py
  • cortex/tutor/tests/test_branding.py
  • cortex/tutor/tests/test_cli.py
  • cortex/tutor/tests/test_deterministic_tools.py
  • cortex/tutor/tests/test_integration.py
  • cortex/tutor/tests/test_interactive_tutor.py
  • cortex/tutor/tests/test_progress_tracker.py
  • cortex/tutor/tests/test_tools.py
  • cortex/tutor/tests/test_tutor_agent.py
  • cortex/tutor/tests/test_validators.py
  • cortex/tutor/tools/__init__.py
  • cortex/tutor/tools/agentic/__init__.py
  • cortex/tutor/tools/agentic/examples_provider.py
  • cortex/tutor/tools/agentic/lesson_generator.py
  • cortex/tutor/tools/agentic/qa_handler.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/tutor/tools/deterministic/lesson_loader.py
  • cortex/tutor/tools/deterministic/progress_tracker.py
  • cortex/tutor/tools/deterministic/validators.py
  • docs/AI_TUTOR.md
  • requirements.txt
  • tests/tutor/__init__.py
  • tests/tutor/test_agent_methods.py
  • tests/tutor/test_agentic_tools.py
  • tests/tutor/test_branding.py
  • tests/tutor/test_cli.py
  • tests/tutor/test_deterministic_tools.py
  • tests/tutor/test_integration.py
  • tests/tutor/test_interactive_tutor.py
  • tests/tutor/test_progress_tracker.py
  • tests/tutor/test_tools.py
  • tests/tutor/test_tutor_agent.py
  • tests/tutor/test_validators.py
✅ Files skipped from review due to trivial changes (1)
  • tests/tutor/init.py
🚧 Files skipped from review as they are similar to previous changes (9)
  • docs/AI_TUTOR.md
  • cortex/tutor/tests/test_cli.py
  • cortex/tutor/tests/test_agentic_tools.py
  • cortex/tutor/tests/test_tutor_agent.py
  • requirements.txt
  • cortex/tutor/init.py
  • cortex/tutor/agents/tutor_agent/init.py
  • cortex/tutor/tests/test_interactive_tutor.py
  • cortex/cli.py
🧰 Additional context used
📓 Path-based instructions (3)
**/*.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:

  • tests/tutor/test_agentic_tools.py
  • cortex/tutor/tests/test_integration.py
  • tests/tutor/test_tutor_agent.py
  • cortex/tutor/tools/agentic/__init__.py
  • tests/tutor/test_cli.py
  • cortex/tutor/tests/test_agent_methods.py
  • tests/tutor/test_interactive_tutor.py
  • cortex/tutor/tests/test_progress_tracker.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/tutor/config.py
  • tests/tutor/test_deterministic_tools.py
  • cortex/tutor/tools/__init__.py
  • tests/tutor/test_tools.py
  • cortex/tutor/contracts/progress_context.py
  • cortex/tutor/tests/test_tools.py
  • cortex/tutor/branding.py
  • tests/tutor/test_integration.py
  • tests/tutor/test_progress_tracker.py
  • tests/tutor/test_agent_methods.py
  • cortex/tutor/tests/test_validators.py
  • cortex/tutor/agents/tutor_agent/tutor_agent.py
  • cortex/tutor/memory/sqlite_store.py
  • cortex/tutor/agents/tutor_agent/state.py
  • cortex/tutor/tools/deterministic/progress_tracker.py
  • cortex/tutor/agents/tutor_agent/graph.py
  • cortex/tutor/contracts/lesson_context.py
  • cortex/tutor/tools/deterministic/validators.py
  • cortex/tutor/tools/agentic/lesson_generator.py
  • cortex/tutor/tools/deterministic/lesson_loader.py
  • cortex/tutor/tools/agentic/examples_provider.py
  • cortex/tutor/tests/test_deterministic_tools.py
  • cortex/tutor/cli.py
  • tests/tutor/test_validators.py
  • cortex/tutor/tools/agentic/qa_handler.py
  • tests/tutor/test_branding.py
  • cortex/tutor/tests/test_branding.py
tests/**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Maintain >80% test coverage for pull requests

Files:

  • tests/tutor/test_agentic_tools.py
  • tests/tutor/test_tutor_agent.py
  • tests/tutor/test_cli.py
  • tests/tutor/test_interactive_tutor.py
  • tests/tutor/test_deterministic_tools.py
  • tests/tutor/test_tools.py
  • tests/tutor/test_integration.py
  • tests/tutor/test_progress_tracker.py
  • tests/tutor/test_agent_methods.py
  • tests/tutor/test_validators.py
  • tests/tutor/test_branding.py
{setup.py,setup.cfg,pyproject.toml,**/__init__.py}

📄 CodeRabbit inference engine (AGENTS.md)

Use Python 3.10 or higher as the minimum supported version

Files:

  • cortex/tutor/tools/agentic/__init__.py
  • cortex/tutor/tools/deterministic/__init__.py
  • cortex/tutor/tools/__init__.py
🧠 Learnings (1)
📚 Learning: 2025-12-11T12:03:24.071Z
Learnt from: CR
Repo: cortexlinux/cortex PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-11T12:03:24.071Z
Learning: Applies to tests/**/*.py : Maintain >80% test coverage for pull requests

Applied to files:

  • cortex/tutor/tests/test_progress_tracker.py
🔇 Additional comments (77)
cortex/tutor/tests/test_validators.py (2)

26-90: Thorough package name validation tests.

Good coverage of edge cases including valid names, empty input, whitespace, length limits, blocked patterns, and invalid characters. The blocked pattern tests appropriately cover dangerous shell commands.


266-302: Well-structured integration tests for validation workflow.

The TestGetValidationErrors and TestValidateAll classes properly test the aggregation and combined validation flows. Good use of multiple validation types in combination.

cortex/tutor/memory/sqlite_store.py (2)

362-390: Race condition handling in _create_default_profile is well implemented.

Good use of INSERT OR IGNORE followed by a re-fetch to handle concurrent profile creation attempts. The re-entrant lock (RLock) also helps with nested calls.


474-496: Lesson cache expiration logic is correct.

The TTL-based cache properly uses UTC timestamps and the expiration check in get_cached_lesson correctly filters expired entries.

cortex/tutor/tools/agentic/__init__.py (1)

1-17: Clean package initialization with proper exports.

Well-structured __init__.py with a clear docstring explaining the purpose of agentic tools. The __all__ properly defines the public API surface.

cortex/tutor/tests/test_deterministic_tools.py (3)

23-58: Good cache behavior tests for LessonLoaderTool.

Tests properly verify cache miss, cache storage/retrieval, and force_fresh bypass. The use of a temporary database fixture ensures test isolation.


174-179: Good coverage of error handling for invalid actions.

The test properly verifies that invalid actions return a failure response with an error message.


89-95: Ensure fallback validation is exhaustive.

The test iterates over FALLBACK_LESSONS but only checks for package_name and summary fields. Consider verifying all required fields that downstream code depends on.

tests/tutor/test_validators.py (1)

1-303: Verify whether duplicate test files exist.

This review raises a concern about potential duplicate test files. Before acting on this claim:

  1. Confirm that both cortex/tutor/tests/test_validators.py and tests/tutor/test_validators.py exist in the current state
  2. If both exist, compare them to verify they are identical
  3. If confirmed as duplicates, determine which should be removed based on your refactoring intent
tests/tutor/test_agentic_tools.py (1)

12-197: Test structure and coverage look solid.

The tests properly mock external dependencies (get_config, ChatAnthropic) and validate _structure_response behavior across full and minimal response scenarios for each agentic tool. The ConversationHandler context-building tests cover both empty and populated history states.

cortex/tutor/tools/deterministic/__init__.py (1)

1-17: Clean package initialization with appropriate exports.

The module docstring clearly explains that these are deterministic (non-LLM) tools, and __all__ properly exposes the public API.

cortex/tutor/tests/test_agent_methods.py (1)

37-441: Comprehensive test coverage for TutorAgent orchestration.

Tests thoroughly cover agent methods, graph node behaviors (success/failure/exception paths), routing decisions, and state helpers. The mocking strategy properly isolates units from external dependencies.

cortex/tutor/tests/test_branding.py (1)

30-227: UI smoke tests provide reasonable coverage.

The tests verify that branding functions execute without errors across various input scenarios. For Rich console output, smoke testing is a practical approach since exact output assertions would be brittle.

tests/tutor/test_tutor_agent.py (1)

33-321: Well-structured tests covering state management, graph nodes, and routing.

Tests comprehensively validate state creation, error/checkpoint/cost accumulation, critical error detection, and routing logic. The integration tests properly verify input validation.

tests/tutor/test_deterministic_tools.py (1)

1-179: Excellent test coverage for deterministic tools.

Tests properly isolate database operations using temporary directories, cover all ProgressTrackerTool actions including error handling for invalid actions, and verify fallback lesson content including case-insensitivity and required fields. The fixture pattern ensures clean test isolation.

cortex/tutor/tests/test_integration.py (1)

298-364: Solid end-to-end workflow test.

The test validates the complete cache-hit lesson workflow through plan_node → load_cache_node → reflect_node, verifying state transitions and final output validation. Good use of mocking to isolate from actual LLM calls.

tests/tutor/test_interactive_tutor.py (1)

84-123: Well-structured test fixture.

The mock_tutor fixture provides comprehensive mock data covering all lesson components (examples, tutorial steps, best practices) with properly configured agent mocks. This enables clean, focused test methods.

tests/tutor/test_agent_methods.py (2)

37-198: Good coverage of TutorAgent public API.

The tests comprehensively cover all public methods (teach, ask, get_profile, update_learning_style, mark_completed, reset_progress, get_packages_studied) with proper mocking of dependencies.


200-259: Comprehensive node testing with error scenarios.

The generate_lesson_node tests cover success, API failure, and exception handling paths. The caching behavior on success is properly verified.

tests/tutor/test_integration.py (1)

32-81: Config tests are comprehensive.

Good coverage of environment loading, API key validation, path expansion, and directory creation with proper use of monkeypatch and tempfile for isolation.

tests/tutor/test_cli.py (3)

24-87: Parser tests are thorough.

All CLI flags and arguments are tested including edge cases like --reset with and without package argument.


89-149: Good security test for blocked patterns.

The test_blocked_package_name test verifies that potentially dangerous input like "rm -rf /" is rejected, which is important for security.


401-453: Main entry point routing is well-tested.

Tests verify that CLI arguments are correctly routed to their respective command handlers.

cortex/tutor/tests/test_progress_tracker.py (2)

47-212: Excellent SQLiteStore test coverage.

Comprehensive testing of all store operations including CRUD, statistics, profile management, lesson caching with TTL, and reset functionality.


214-288: Good action-based testing of ProgressTrackerTool.

All supported actions are tested, including error cases for unknown actions and missing required parameters.

cortex/tutor/tests/test_tools.py (1)

31-122: Good caching and fallback behavior testing.

Tests cover cache hit/miss scenarios, force-refresh, fallback templates, and the priority order (cache → fallback → none).

cortex/tutor/config.py (2)

127-141: Docstring Raises is accurate but indirect.

The docstring correctly notes ValueError can be raised, which happens via Config.from_env(). This is accurate.


1-16: Well-structured configuration module.

Good use of Pydantic for validation, proper docstrings, type hints, and secure defaults (0o700 permissions for data directory). The environment-based configuration with optional API key requirement enables the lazy import pattern mentioned in the PR objectives.

cortex/tutor/tools/__init__.py (1)

1-20: LGTM!

Clean package initialization with proper docstring and centralized exports. The __all__ list correctly exposes the public API for both deterministic and agentic tools.

cortex/tutor/contracts/lesson_context.py (2)

101-131: Well-designed serialization and utility methods.

The to_json(), from_json(), and to_display_dict() methods provide a clean API for caching and presentation. Good use of Pydantic's built-in serialization.


56-75: Pydantic v2 max_length on list fields correctly constrains list size.

In Pydantic v2, Field(max_length=N) on a list field constrains the number of items in the list, not the length of individual string elements. The code is using this correctly and requires no changes.

Likely an incorrect or invalid review comment.

cortex/tutor/branding.py (2)

34-50: Well-designed status printing utility.

Clean implementation with proper fallback to default config. The status-emoji mapping provides a consistent UI experience.


193-216: Good defensive error handling in get_user_input.

Properly handles EOFError and KeyboardInterrupt to gracefully cancel operations without crashing.

tests/tutor/test_progress_tracker.py (3)

27-44: Well-structured test fixtures.

Good use of tempfile.TemporaryDirectory context manager with yield for automatic cleanup. The fixture chain (temp_dbstore/tracker) provides proper test isolation.


278-288: Good error case coverage.

Tests for unknown actions and missing required parameters ensure the tool gracefully handles invalid inputs.


293-350: Proper mocking pattern for convenience functions.

Good use of patch to substitute config, ensuring tests are isolated from the actual database path. The tests verify end-to-end behavior through the public API.

cortex/tutor/contracts/progress_context.py (2)

31-63: Well-designed computed fields with proper edge case handling.

The @computed_field decorators correctly implement derived properties. The is_complete() method properly returns False for empty topic lists, preventing false positives.


159-176: Clean factory method pattern.

from_results properly guards against division by zero and provides sensible defaults. The 70% pass threshold is a reasonable default for quiz assessment.

cortex/tutor/agents/tutor_agent/tutor_agent.py (3)

265-275: Lazy imports inside start() method are appropriate.

The PR objectives mention "lazy imports to allow listing/progress commands without an API key." This approach is intentional and correctly defers heavy imports until interactive mode is needed.


309-313: Consider validating menu option range before conversion.

The input handling catches ValueError for non-numeric input, but the range check happens after successful conversion. This is acceptable but could provide clearer feedback.


23-48: TutorAgent class is well-structured with proper docstrings and type hints.

The class follows PEP 8 style, includes comprehensive docstrings with examples, and uses type hints throughout. The initialization pattern is clean.

cortex/tutor/tools/deterministic/lesson_loader.py (2)

163-232: FALLBACK_LESSONS provides good offline resilience.

The predefined fallback lessons for common packages (docker, git, nginx) ensure the tutor can function without API access. The lower confidence score (0.7) appropriately signals these are fallback content.


18-49: LessonLoaderTool class is well-documented with proper type hints.

The class follows PEP 8, has comprehensive docstrings, and uses Pydantic fields appropriately. The Config class enables arbitrary types for the SQLiteStore field.

cortex/tutor/tools/agentic/lesson_generator.py (2)

262-276: Good defensive coding in _structure_response.

The method properly handles missing fields with defaults, clamps confidence to valid range (0.0-1.0), and limits list sizes to prevent unbounded responses. This is well-implemented.


186-202: System prompt has appropriate safety rules.

The critical rules against fabricating features, URLs, or versions are important for educational content integrity. Good security posture.

tests/tutor/test_tools.py (2)

24-28: Good use of pytest fixture for test isolation.

The temp_db fixture properly creates an isolated temporary database for each test, ensuring tests don't interfere with each other.


127-181: Test mocking is thorough and at the correct level.

The tests mock ChatAnthropic and get_config at the appropriate module level, and verify the tool's response structure without requiring actual LLM calls.

cortex/tutor/tools/agentic/examples_provider.py (2)

159-174: System prompt enforces good safety practices.

The critical rules against inventing flags, using real credentials, and flagging dangerous commands are appropriate for educational content generation.


208-236: Good defensive coding in _structure_response.

The method properly validates example structure, limits list sizes, and provides sensible defaults. The confidence clamping is correctly implemented.

cortex/tutor/agents/tutor_agent/graph.py (2)

200-249: Reflect node has well-designed validation logic.

The confidence calculation accounting for errors and cache staleness, combined with comprehensive output preparation, demonstrates good design. The validation_passed flag provides clear success/failure indication.


315-371: Graph structure is well-organized with clear entry point and edges.

The workflow correctly implements the Plan→Act→Reflect pattern with proper conditional routing and terminal states. The conditional edges provide flexibility for different session types.

cortex/tutor/agents/tutor_agent/state.py (4)

50-105: Well-designed TypedDict state schema.

The TutorAgentState provides a clear contract for the workflow state with comprehensive documentation. Using total=False allows optional fields while maintaining type safety.


107-150: Factory function provides clean state initialization.

create_initial_state sets sensible defaults for all fields, ensuring the state is always in a valid initial configuration. The explicit initialization prevents undefined state issues.


153-171: Helper functions mutate state in-place.

The add_error, add_checkpoint, and add_cost functions modify the state dictionary directly. This is appropriate for LangGraph workflows where state is passed through nodes and accumulated.


42-47: ErrorState uses total=True (default) enforcing required fields.

Unlike other state TypedDicts, ErrorState requires all fields (node, error, recoverable), which is appropriate since an error entry should always have complete information.

cortex/tutor/cli.py (5)

1-33: LGTM! Well-structured module docstring and imports.

The module docstring clearly documents CLI usage patterns. Lazy imports strategy for API-key-dependent modules is a good design choice. The DEFAULT_TUTOR_TOPICS constant is appropriately defined at module level.


35-109: LGTM! Argument parser is well-configured.

The parser correctly handles all required CLI options including version, verbose mode, package name, list, progress, reset, fresh, and question flags. The --reset with nargs="?" and const="__all__" is a nice pattern for optional argument handling.


112-152: LGTM! Solid error handling in teach command.

The function properly validates input before expensive imports, handles ValueError, KeyboardInterrupt, and general exceptions with appropriate exit codes. The lazy import pattern ensures API key is only required when actually starting a session.


316-347: LGTM! Reset command with proper confirmation flow.

The confirmation prompt before destructive operations is good UX practice. The scope calculation handles both None and "__all__" correctly.


350-392: LGTM! Clean command dispatch logic.

The main function correctly handles argument parsing, conditional banner display, and command routing. The fallback to parser.print_help() when no command is specified is appropriate.

cortex/tutor/tools/deterministic/validators.py (7)

15-34: LGTM! Comprehensive security patterns for input validation.

The blocked patterns list covers common injection vectors (shell commands, fork bombs, Python code injection). This is a solid defense-in-depth approach for a CLI tool that might pass user input to an LLM.


36-48: LGTM! Well-defined validation patterns and categories.

The regex pattern for valid package names is appropriately restrictive while allowing common naming conventions (dots, hyphens, underscores). The package categories list provides good context for documentation.


51-93: LGTM! Thorough package name validation with clear error messages.

The function correctly handles empty input, length limits, blocked patterns, and format validation. The docstring includes helpful examples demonstrating expected behavior.


96-135: LGTM! Generic input validation with configurable parameters.

The function properly handles the allow_empty flag and applies length limits before checking blocked patterns.


138-210: LGTM! Specialized validators with appropriate constraints.

The validate_question, validate_topic, validate_score, and validate_learning_style functions properly delegate to base validators or implement specific rules. The score validation correctly handles both int and float types.


213-277: LGTM! Input sanitization and package name extraction utilities.

The sanitize_input function removes null bytes and enforces length limits. The extract_package_name function uses a reasonable set of patterns to extract package names from natural language input.


280-367: LGTM! Clean aggregation utilities with ValidationResult class.

The get_validation_errors and validate_all functions provide convenient batch validation. The ValidationResult class implements __bool__ and __str__ for ergonomic usage.

cortex/tutor/tools/deterministic/progress_tracker.py (5)

23-59: LGTM! Well-documented tool class with proper initialization.

The class docstring clearly explains the tool's purpose and constraints (no LLM calls). The initialization correctly handles optional db_path with fallback to config default.


61-113: LGTM! Clean action dispatcher with error handling.

The dispatcher pattern is well-implemented with a dictionary mapping actions to handlers. Unknown actions return helpful error messages with valid action lists.


119-164: LGTM! Progress retrieval handlers with proper validation.

Both _get_progress and _get_all_progress correctly handle missing required parameters and return structured responses.


166-213: LGTM! Progress update handlers with proper data preservation.

The _mark_completed and _update_progress handlers correctly preserve existing values when updating progress. The time accumulation logic in _update_progress is correctly implemented.


215-295: LGTM! Stats and profile management handlers.

The handlers correctly implement statistics retrieval, profile management, and progress reset functionality. Error handling is consistent across all methods.

cortex/tutor/tools/agentic/qa_handler.py (4)

59-116: LGTM! Robust error handling and response structuring.

The _run method properly wraps LLM calls in try-except, structures responses consistently, and includes cost estimation. The chain composition with prompt | self.llm | JsonOutputParser() is idiomatic LangChain.


164-213: LGTM! Well-designed system and Q&A prompts.

The system prompt includes critical safety rules (no fabrication, no fake URLs, confidence expression). The Q&A prompt template defines a clear JSON schema for structured responses.


215-241: LGTM! Response structuring with validation.

The _structure_response method properly clamps confidence to [0.0, 1.0], limits array lengths for related topics and suggestions, and safely handles missing or malformed code examples.


36-57: Model name is valid and pricing estimate is reasonable.

The model identifier claude-sonnet-4-20250514 is confirmed as a valid current Anthropic model. Current pricing is $3.00 per 1M input tokens and $15.00 per 1M output tokens. With max_tokens set to 2048, the estimated cost of ~$0.02 per question is reasonable for typical usage patterns.

tests/tutor/test_branding.py (1)

229-249: LGTM! User input tests with proper mocking and assertions.

These tests correctly mock builtins.input and verify the expected behavior. The test at line 245-249 correctly tests that raw input is returned (checking "spaced" in result rather than exact match).

@srikrishnavansi
Copy link
Contributor Author

@Anshgrover23 All checks are now passing! ✅

Summary of updates:

  • ✅ CLA signed (PR chore(cla): add Doddi Sri Krishna Vamsi to signers #570 merged)
  • ✅ Demo video added to PR description
  • ✅ AI Usage disclosure added
  • ✅ All CodeRabbit/Gemini review comments addressed
  • ✅ Ruff lint errors fixed (modern Python 3.10+ type annotations)
  • ✅ Tests moved to tests/tutor/ for CI coverage
  • ✅ All 1280+ tests passing (266 tutor + 1014 existing)
  • ✅ Coverage threshold met

Ready for review. Please take a look when you get a chance. Thanks!

Copy link
Collaborator

@Anshgrover23 Anshgrover23 left a comment

Choose a reason for hiding this comment

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

@srikrishnavansi Your SonarQubeCloud Quality Gate is failing kindly address that issues. Also address coderabbit comments that are still open.

@srikrishnavansi srikrishnavansi marked this pull request as ready for review January 17, 2026 11:09
@srikrishnavansi
Copy link
Contributor Author

Summary of Changes

  1. Structured Outputs - Refactored llm.py to use Anthropic's tool_use feature with Pydantic models for structured LLM outputs instead of manual JSON parsing
  2. New Response Models - Added LessonResponse, QAResponse, and QACodeExample models in contracts.py for type-safe structured outputs
  3. Dead Code Removal - Removed unused classes (LessonPlanOutput, LessonReflectionOutput) and constants (KNOWN_PACKAGE_CATEGORIES)
  4. Input Validation - Added validate_question() call in cli.py before processing user questions
  5. SonarQube Fixes - Replaced conlist with plain list[T] types to resolve unexpected argument warnings
  6. Test Updates - Rewrote test_llm.py to match new structured output implementation with proper mocking
  7. Documentation - Updated AI_TUTOR.md to reflect the new structured outputs architecture

@Anshgrover23 Please let me know if any further changes are needed!

mikejmorgan-ai
mikejmorgan-ai previously approved these changes Jan 17, 2026
Copy link
Member

@mikejmorgan-ai mikejmorgan-ai left a comment

Choose a reason for hiding this comment

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

Review: APPROVED ✅

AI-Powered Installation Tutor (PR #566)

Code Quality

  • ✅ Clean architecture: agent.py, cli.py, config.py, validators.py, sqlite_store.py, tools.py
  • ✅ Strong input validation with blocked dangerous patterns (rm -rf, fork bombs, eval injection)
  • ✅ Secure file permissions (0o700 for data directory)
  • ✅ Proper error handling with user-friendly messages
  • ✅ Lazy imports for LLM components (only loaded when API key available)

Security

  • ✅ BLOCKED_PATTERNS prevents command injection attempts
  • ✅ Package name validation: alphanumeric + dots/hyphens/underscores only
  • ✅ Input length limits prevent abuse
  • ✅ Sanitization removes null bytes and truncates long inputs
  • ✅ API key validated before use

Test Coverage

  • ✅ 167 tests all passing
  • ✅ Tests cover: CLI, tools, validators, progress tracking, LLM integration
  • ✅ Edge cases: blocked patterns, empty inputs, invalid scores, etc.

Architecture

  • TutorAgent orchestrates lesson generation and Q&A
  • InteractiveTutor provides menu-driven CLI experience
  • SQLiteStore handles progress persistence
  • LessonLoaderTool manages caching with fallback templates

Integration

  • ✅ Clean integration with main CLI via cortex tutor <package>
  • ✅ Supports: --list, --progress, --reset, --fresh, -q flags
  • ✅ Documentation in docs/AI_TUTOR.md

Notes

  • Requires Python 3.10+ (union type hints)
  • Requires ANTHROPIC_API_KEY for LLM features
  • Offline fallback lessons available for common packages (docker, git, nginx)

Ready for merge once Ansh approves.

Reviewed-by: Claude Code

Copy link
Collaborator

@Anshgrover23 Anshgrover23 left a comment

Choose a reason for hiding this comment

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

@srikrishnavansi Added some comments.

from cortex.tutor.contracts import LessonResponse, QAResponse

logger = logging.getLogger(__name__)
_client: anthropic.Anthropic | None = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no proper thread-safe initialization (no locks), for e.g SQLiteStore uses RLock.

)

# Extract tool use result
for block in response.content:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if response.content is empty or None ?

Comment on lines 72 to 78
def cache_lesson(
self,
package_name: str,
lesson: dict[str, Any],
ttl_hours: int = 24,
) -> bool:
"""Cache a lesson for future retrieval."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

TTL uses default but isn't configurable through Config class.

- Make model name configurable via TUTOR_MODEL_NAME env variable
- Add thread-safe lock to client initialization (double-checked locking)
- Handle empty/None response.content in llm.py
- Remove duplicate validation from cli.py (agent.py handles it)
- Remove unused _description parameter from create_progress_bar
- Add _error_response helper for consistent error structure
- Rename DEFAULT_TUTOR_TOPICS to DEFAULT_TUTOR_TOPICS_COUNT
- Make cache TTL configurable via TUTOR_CACHE_TTL_HOURS env variable
- Update tests to work with new config-based approach
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: 1

🤖 Fix all issues with AI agents
In `@cortex/tutor/tools.py`:
- Around line 88-97: The clear_cache method's docstring and name imply a full
purge but the current path for package_name=None only calls
self.store.clear_expired_cache; either update the docstring to state it only
clears expired entries or implement a true "clear all" branch: add logic in
clear_cache to call a store-level full clear (e.g., self.store.clear_all_cache()
or iterate over cached keys and remove them) when package_name is None, and keep
the existing package-specific path that uses
self.store.cache_lesson(package_name, {}, ttl_hours=0); ensure any new store
method name (clear_all_cache or similar) is referenced consistently in the
method and tests.
♻️ Duplicate comments (1)
cortex/tutor/agent.py (1)

320-335: Don’t mark the tutorial complete if the user quits early.

The loop breaks on 'q', but completion is still recorded with a full score. Track completed steps and compute a proportional score (or skip marking if quit immediately).

🔧 Proposed fix
-        for step in steps:
+        completed_steps = 0
+        for step in steps:
             print_tutorial_step(
                 step.get("content", ""),
                 step.get("step_number", 1),
                 len(steps),
             )
@@
             response = get_user_input("Press Enter to continue (or 'q' to quit)")
             if response.lower() == "q":
                 break
+            completed_steps += 1
 
-        self.agent.mark_completed(self.package_name, "tutorial", 0.9)
+        if completed_steps > 0:
+            score = 0.9 * (completed_steps / len(steps))
+            self.agent.mark_completed(self.package_name, "tutorial", score)
🧹 Nitpick comments (4)
cortex/tutor/config.py (2)

53-56: Add a type hint for the model_post_init context parameter.

__context is untyped; adding a type hint keeps this aligned with the project’s “type hints required” rule.

🔧 Proposed fix
+from typing import Any
 from pathlib import Path
@@
-    def model_post_init(self, __context) -> None:
+    def model_post_init(self, __context: Any) -> None:

As per coding guidelines, type hints are required in Python code.


112-113: Avoid a mkdir race by using exist_ok=True.

A concurrent creator can trigger FileExistsError between the exists() check and mkdir().

🔧 Proposed fix
-            self.data_dir.mkdir(parents=True, mode=0o700)
+            self.data_dir.mkdir(parents=True, mode=0o700, exist_ok=True)
cortex/tutor/tools.py (1)

46-70: Log cache read/write failures to avoid silent errors.

Exceptions are swallowed and only returned to callers; adding logger.exception preserves tracebacks for debugging.

🔧 Proposed fix
         except Exception as e:
+            logger.exception("Failed to load cached lesson for %s", package_name)
             return {
                 "success": False,
                 "cache_hit": False,
                 "lesson": None,
                 "error": str(e),
             }
@@
-        except Exception:
+        except Exception:
+            logger.exception("Failed to cache lesson for %s", package_name)
             return False

Also applies to: 79-86

cortex/tutor/llm.py (1)

39-48: Model-specific pricing should replace hardcoded Sonnet rates.

config.model_name is configurable via environment variable, but cost calculation uses hardcoded Sonnet rates ($3.00 input, $15.00 output per 1M tokens). This means costs will be inaccurate for other Claude models (Opus, Haiku) or Sonnet 4's long-context tier (>200K input tokens, which costs $6.00/$22.50).

A per-model rate map with sensible defaults would ensure cost accuracy across model variants:

Suggested implementation
-# Cost per 1M tokens for Claude Sonnet
-COST_INPUT_PER_1M = 3.0
-COST_OUTPUT_PER_1M = 15.0
+# Cost per 1M tokens (input_rate, output_rate) by model
+_COST_PER_1M_BY_MODEL: dict[str, tuple[float, float]] = {
+    "claude-sonnet-4-20250514": (3.0, 15.0),
+}
@@
-def _calculate_cost(input_tokens: int, output_tokens: int) -> float:
+def _calculate_cost(input_tokens: int, output_tokens: int, model_name: str) -> float:
     """Calculate cost in USD."""
-    input_cost = (input_tokens / 1_000_000) * COST_INPUT_PER_1M
-    output_cost = (output_tokens / 1_000_000) * COST_OUTPUT_PER_1M
+    input_rate, output_rate = _COST_PER_1M_BY_MODEL.get(model_name, (0.0, 0.0))
+    input_cost = (input_tokens / 1_000_000) * input_rate
+    output_cost = (output_tokens / 1_000_000) * output_rate
     return input_cost + output_cost
@@
-            cost = _calculate_cost(response.usage.input_tokens, response.usage.output_tokens)
+            cost = _calculate_cost(
+                response.usage.input_tokens,
+                response.usage.output_tokens,
+                config.model_name,
+            )

@srikrishnavansi
Copy link
Contributor Author

@Anshgrover23

Code Changes:

  1. Hardcoded model name - Now configurable via TUTOR_MODEL_NAME env variable (default: claude-sonnet-4-20250514)
  2. No thread-safe client initialization - Added threading.Lock with double-checked locking pattern in llm.py
  3. Empty/None response.content not handled - Added null check: if not response.content: raise ValueError(...)
  4. Duplicate package validation in cli.py - Removed. agent.py handles validation, CLI catches ValueError
  5. Unused _description parameter - Removed from create_progress_bar()
  6. Error response structure inconsistent - Added _error_response() helper for consistent error structure
  7. Magic number DEFAULT_TUTOR_TOPICS = 5 - Renamed to DEFAULT_TUTOR_TOPICS_COUNT with clear purpose
  8. Cache TTL not configurable - Now configurable via TUTOR_CACHE_TTL_HOURS env variable (default: 24)
  9. clear_cache docstring misleading - Updated docstring to clarify behavior
  10. --fresh flag not propagated - Already addressed. InteractiveTutor accepts force_fresh parameter

Documentation Updates:

Updated docs/AI_TUTOR.md with new environment variables (TUTOR_MODEL_NAME, TUTOR_CACHE_TTL_HOURS)

Ready for re-review!

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: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cortex/cli.py (1)

2352-2367: Add -v/--verbose flag to tutor subcommand parser for consistency.

The tutor subcommand parser is missing the --verbose/-v argument that is defined in cortex/tutor/cli.py and present in other subcommand parsers (benchmark, systemd, gpu, printer). This creates an inconsistency where users must use cortex -v tutor docker instead of the expected cortex tutor -v docker. The delegation to cortex/tutor/cli.py functions works, but the parser definition should match to provide consistent UX across all subcommands.

🤖 Fix all issues with AI agents
In `@cortex/tutor/tools.py`:
- Around line 64-70: The LessonLoaderTool._run currently catches all exceptions
and returns an error dict without logging; update LessonLoaderTool._run to log
the exception before returning (mirroring ProgressTrackerTool._run) by calling
the appropriate logger (e.g., self.logger.exception(...) or module/process
logger) with the exception and stack trace, then return the same error dict;
ensure you reference the LessonLoaderTool._run function and use logger.exception
or logger.error(..., exc_info=True) so the stack trace is recorded.
- Around line 85-86: The cache_lesson function currently swallows all exceptions
and returns False with no diagnostics; update its except block to catch
Exception as e and log the error and stacktrace (e.g., use logger.exception or
logging.exception) including contextual info like the lesson id or parameters
used, then continue to return False (or re-raise if desired) so failures are
visible in logs; locate the except block in cache_lesson and replace the bare
except with a logged exception that includes the exception object and relevant
context.
♻️ Duplicate comments (1)
cortex/tutor/tools.py (1)

449-461: Missing audit logging for _reset_progress operation.

Based on learnings, audit logging to ~/.cortex/history.db is required for installation operations. The _reset_progress method performs a destructive operation (deleting progress records) but doesn't implement audit logging. This was flagged in a past review as a major issue.

🧹 Nitpick comments (6)
cortex/licensing.py (1)

204-218: Box alignment may break with variable-length content.

The box uses fixed-width borders, but lines 209, 213, and 215 contain variable-length content (feature_display, tier_display, price, PRICING_URL) that won't align with the closing characters. Consider either:

  1. Using dynamic padding to align the closing borders
  2. Removing the right-side borders for lines with dynamic content
♻️ Suggested fix (remove right borders for dynamic lines)
 print(f"""
 ┌─────────────────────────────────────────────────────────┐
 │  ⚡ UPGRADE REQUIRED                                    │
 ├─────────────────────────────────────────────────────────┤
 │                                                         │
-│  '{feature_display}' requires Cortex {tier_display}
+│  '{feature_display}' requires Cortex {tier_display}     │
 │                                                         │
 │  ✅ Upgrade now:  cortex upgrade                        │
 │                                                         │
-│  Plans start at {price}/month with 14-day free trial.
+│  Plans start at {price}/month with 14-day free trial.   │
 │                                                         │
-│  🌐 {PRICING_URL}
+│  🌐 {PRICING_URL:<42} │
 │                                                         │
 └─────────────────────────────────────────────────────────┘
 """)
docs/AI_TUTOR.md (2)

91-117: Add language specifier to fenced code block.

This ASCII art output example is missing a language specifier. Use text or plaintext for non-code output blocks to satisfy linting and improve accessibility.

🔧 Suggested fix
-```
+```text
   ___       _       _ _ _                  _     _____      _

249-303: Add language specifiers to architecture diagram code blocks.

The ASCII architecture diagrams at lines 249, 309, 348, 390, and 432 are missing language specifiers. Use text for these blocks.

🔧 Suggested fix (apply to each diagram block)
-```
+```text
 ┌─────────────────────────────────────────────────────────────────┐
 │                         User Request                             │

Also applies to lines: 309-344, 348-386, 390-424, 432-444

cortex/tutor/config.py (2)

51-54: Consider using __context: Any type hint for Pydantic compatibility.

The model_post_init method is correctly used for computed field initialization. However, the __context parameter should be typed as Any for proper Pydantic compatibility and to satisfy type checkers.

♻️ Suggested improvement
+from typing import Any
+
-    def model_post_init(self, __context) -> None:
+    def model_post_init(self, __context: Any) -> None:
         """Initialize computed fields after model creation."""
         if self.db_path is None:
             self.db_path = self.data_dir / "tutor_progress.db"

123-133: API key validation is minimal — consider documenting limitations.

The validate_api_key() method only checks the sk-ant- prefix. While this is a reasonable quick check, Anthropic API keys have additional format requirements. Consider documenting that this is a format check only, not a validity check against the API.

📝 Suggested docstring enhancement
     def validate_api_key(self) -> bool:
         """
-        Validate that the API key is properly configured.
+        Validate that the API key has the expected format.
+
+        This performs a basic format check (prefix validation) only.
+        It does not verify the key against the Anthropic API.

         Returns:
-            bool: True if API key is valid format, False otherwise.
+            bool: True if API key has valid format, False otherwise.
         """
cortex/tutor/tools.py (1)

472-490: Convenience functions instantiate new tools on every call.

Each convenience function (get_learning_progress, mark_topic_completed, get_package_stats) creates a new ProgressTrackerTool instance, which in turn creates a new SQLiteStore connection. For high-frequency usage, consider caching or reusing instances.

♻️ Optional: Add a module-level cached instance
# Module-level cached instance for convenience functions
_cached_tracker: ProgressTrackerTool | None = None

def _get_tracker() -> ProgressTrackerTool:
    """Get or create a cached ProgressTrackerTool instance."""
    global _cached_tracker
    if _cached_tracker is None:
        _cached_tracker = ProgressTrackerTool()
    return _cached_tracker

def get_learning_progress(package_name: str, topic: str) -> dict[str, Any] | None:
    """Get learning progress for a specific topic."""
    result = _get_tracker()._run("get_progress", package_name=package_name, topic=topic)
    return result.get("progress")

@srikrishnavansi srikrishnavansi marked this pull request as draft January 18, 2026 06:21
@srikrishnavansi srikrishnavansi marked this pull request as ready for review January 18, 2026 06:31
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: 1

🤖 Fix all issues with AI agents
In `@cortex/tutor/tools.py`:
- Around line 415-425: The current _update_profile method uses a truthiness
check for learning_style which prevents intentionally setting it to an empty
string; change the condition so it checks explicitly for None (i.e., use
"learning_style is not None") before assigning profile.learning_style, leaving
the calls to self.store.get_student_profile() and
self.store.update_student_profile(profile) unchanged so an empty string can
clear the field.
♻️ Duplicate comments (1)
cortex/tutor/tools.py (1)

223-234: Missing audit logging for write operations.

Per coding guidelines, operations should be logged to ~/.cortex/history.db for audit purposes. This tool performs mutations (mark_completed, update_progress, reset, update_profile, add_mastered, add_weak) without any audit trail.

Consider adding audit logging, especially for destructive operations like reset_progress. Based on learnings, this is a compliance requirement.

🧹 Nitpick comments (4)
cortex/tutor/tools.py (4)

100-105: Silent exception catch in clear_cache lacks logging.

The except Exception block silently returns 0 without logging, inconsistent with the logging added to _run (line 65) and cache_lesson (line 87). This makes debugging cache clearing failures difficult.

Suggested fix
         if package_name:
             try:
                 self.store.cache_lesson(package_name, {}, ttl_hours=0)
                 return 1
-            except Exception:
+            except Exception:
+                logger.exception("Failed to clear cache for package '%s'", package_name)
                 return 0

188-216: Consider exposing a public interface instead of calling private method _run.

load_lesson_with_fallback calls loader._run() (line 194), but the underscore prefix conventionally indicates a private method. This creates a fragile dependency on internal implementation details.

Suggested approach

Either rename _run to run (making it explicitly public), or add a public method to LessonLoaderTool:

def load(self, package_name: str, force_fresh: bool = False) -> dict[str, Any]:
    """Load cached lesson content (public interface)."""
    return self._run(package_name, force_fresh)

427-449: Confusing dual parameter extraction pattern.

The pattern concept = kwargs.get("concept") or concept (lines 433, 445) allows concept to be passed both as a direct parameter and inside kwargs. This is confusing and has subtle bugs: if concept="" is explicitly passed but kwargs["concept"] has a value, kwargs wins due to the or operator's truthiness behavior.

Suggested fix

Pick one source of truth. Since _run passes kwargs through, prefer extracting from kwargs only:

     def _add_mastered_concept(
         self,
-        concept: str | None = None,
-        **kwargs: Any,
+        **kwargs: Any,
     ) -> dict[str, Any]:
         """Add a mastered concept to student profile."""
-        concept = kwargs.get("concept") or concept
+        concept = kwargs.get("concept")
         if not concept:
             return {"success": False, "error": "concept required"}

Or, document that concept in kwargs takes precedence and use explicit None check:

-        concept = kwargs.get("concept") or concept
+        concept = kwargs.get("concept") if "concept" in kwargs else concept

474-492: Convenience functions create new instances and connections on each call.

Each convenience function instantiates a new ProgressTrackerTool, which creates a new SQLiteStore and database connection. This is acceptable for one-off CLI usage but inefficient if called repeatedly (e.g., in a loop or interactive session).

Additionally, these call tool._run(), which follows the private method anti-pattern noted earlier.

Consider either:

  1. Using a module-level cached instance (lazy singleton), or
  2. Documenting that these are for one-off usage and providing the class directly for batch operations.

@srikrishnavansi srikrishnavansi marked this pull request as draft January 18, 2026 06:34
- Change 'if learning_style:' to 'if learning_style is not None:'
  to allow explicitly clearing learning_style with empty string
- Add logger.exception() to clear_cache exception handler
@srikrishnavansi srikrishnavansi marked this pull request as ready for review January 18, 2026 06:44
@srikrishnavansi
Copy link
Contributor Author

@Anshgrover23
Addressed CodeRabbit review comments:

  1. Fixed truthiness check in _update_profile - Changed if learning_style: to if learning_style is not None: to allow explicitly clearing the learning style with an empty string.
  2. Added logging to silent exception in clear_cache - Added logger.exception() call to log failures when clearing cache for a specific package.

Ready for re-review.

@sonarqubecloud
Copy link

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.

AI-Powered Installation Tutor

3 participants