Skip to content

Iris: Automatically switch the context of an active conversation - #701

Open
Senan04 wants to merge 14 commits into
mainfrom
feature/iris/automatic-context-switching
Open

Iris: Automatically switch the context of an active conversation#701
Senan04 wants to merge 14 commits into
mainfrom
feature/iris/automatic-context-switching

Conversation

@Senan04

@Senan04 Senan04 commented Jul 21, 2026

Copy link
Copy Markdown
Member

Note

This should be tested with PR #13292 on Artemis

Summary

Iris can now switch the context of a running chat on its own. When a student asks about a different exercise or lecture than the currently active context, the agent looks up the target's ID, calls the new switch_chat_context tool, and then answers about the new context. The requested target travels to Artemis on the final result status update, which applies the switch together with the answer.

This is the Pyris side of the feature. The Artemis counterpart is ls1intum/Artemis#13292 and both PRs have to be deployed together.

Motivation

Students have to change the context of a chat manually through the context selection menu. Whenever they ask about another exercise or lecture without doing so, Iris answers with the wrong context in its prompt and its retrieval tools operate on the previous entity. Letting the agent request the switch removes that manual step.

Changes

  • switch_chat_context tool (iris/src/iris/tools/switch_chat_context.py): takes a target mode (PROGRAMMING_EXERCISE_CHAT, TEXT_EXERCISE_CHAT, LECTURE_CHAT, COURSE_CHAT) and an entity id. It validates the target against the execution DTO: unknown modes are rejected, exercise and lecture ids that do not exist in the course are rejected with a message that tells the agent which tool to use for the correct id, exercise types other than programming and text are rejected, and a mixed-up programming/text mode is silently corrected from the exercise type on the DTO. A switch to the course always uses the course id from the DTO, and a switch to the already active context clears the pending switch instead of registering a no-op. The tool changes nothing inside the running pipeline, it only records the target.
  • lecture_list tool (iris/src/iris/tools/lecture_list.py, course_dto.py): lecture content retrieval reports lecture names but never lecture ids, and it only ever sees the active lecture, so it cannot supply the id a lecture switch needs. The new tool returns the id, the name and the lecture unit names of every lecture of the course, which gives the agent a non-guessing path from a lecture the student names to the id the switch takes. It reads the new lectures field on CourseDTO, which Artemis fills in the accompanying PR. An Artemis instance that does not send the field yet leaves the list empty: the provider logs a warning so the version skew stays visible, and the tool degrades to an empty list rather than failing. Lecture id validation in switch_chat_context is skipped in that case, leaving the check to Artemis.
  • Lecture retrieval scope (lecture_content_retrieval.py, chat_tool_providers.py): retrieval used to bind its lecture scope when the tool was created, so a switch during a run left it pointed at the old lecture and the agent could not answer about the new one. The tool now resolves the scope at call time through a supplier that reads the pending switch. A retrieval after a switch to another lecture covers that lecture as a whole, without the lecture unit filter of the previous context, and a switch away from a lecture context widens retrieval to the whole course.
  • Pipeline state (abstract_agent_pipeline.py, chat_tool_providers.py): AgentPipelineExecutionState carries a pending_context_switch, reset at the start of every run. The new provide_switch_chat_context provider wires the tool's callback to that field, so the last call wins.
  • Status update (suggested_context_dto.py, chat_status_update_dto.py, chat_pipeline.py, status_update.py): the new SuggestedContextDTO (mode plus entityId) is attached to the final result status update and serialized under the suggestedContext alias that Artemis expects. A run without a switch sends suggestedContext: null, the same shape the other optional fields of the status DTO use when they are unset.
  • System prompt (chat_system_prompt.j2): a context switching block states the currently active context and instructs the agent to look up the target id first, call the tool, and then answer in the new context. It names the exercise list tool for exercises and the lecture list tool for lectures, and it states that lecture content retrieval is scoped to the active lecture and does not help in finding another one. Worked examples cover both the exercise case and the lecture case, where the agent calls lecture content retrieval again after the switch to reach the content of the new lecture. The block rules out switching on a passing mention or a comparison, requires a switch back to the course context for general course-level questions, and asks the agent to keep the announcement short because Artemis renders a context switch divider. The block is skipped for the MCQ variant.

Tests

tests/test_switch_chat_context_tool.py covers target validation and correction for exercises, lectures and the course, the no-op switch to the active context, and the wire shape of suggestedContext in both directions. The lecture A to B case runs through the real tool providers: it performs the switch and then invokes the lecture retrieval tool the pipeline hands the agent, asserting that the retriever is queried for the new lecture and without the lecture unit of the old one. tests/test_lecture_list_tool.py covers the lecture list output, including a lecture whose units are all unreleased and a course whose lectures Artemis did not send.

Summary by CodeRabbit

  • New Features

    • Added agent-driven context switching for course, lecture, and exercise discussions.
    • Chat status updates can include an optional suggestedContext with mode and entity ID.
    • Added lecture listing with lecture, unit, and identifier details.
  • Improvements

    • Context switches are validated against available content and applied with the final response.
    • Lecture content retrieval now follows the active context.
    • Updated chat guidance for switching between contexts.
  • Tests

    • Added coverage for lecture listing, context switching, retrieval behavior, and status payloads.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f2ed5630-6c08-41ee-9400-97656da65d34

📥 Commits

Reviewing files that changed from the base of the PR and between b528f7c and a44805a.

📒 Files selected for processing (5)
  • iris/src/iris/pipeline/chat/chat_pipeline.py
  • iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2
  • iris/src/iris/tools/__init__.py
  • iris/src/iris/tools/chat_tool_providers.py
  • iris/tests/test_switch_chat_context_tool.py
💤 Files with no reviewable changes (2)
  • iris/src/iris/pipeline/chat/chat_pipeline.py
  • iris/src/iris/tools/init.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2
  • iris/src/iris/tools/chat_tool_providers.py

📝 Walkthrough

Walkthrough

Adds a validated switch_chat_context tool, stores its target in pipeline state, supports lecture discovery and dynamic retrieval scope, updates prompt instructions, and includes suggestedContext in final chat status updates.

Changes

Chat context switching

Layer / File(s) Summary
Context switch contract
iris/src/iris/domain/status/suggested_context_dto.py, iris/src/iris/domain/status/chat_status_update_dto.py, iris/src/iris/domain/data/course_dto.py
Defines the context-switch DTO, adds optional status output, and includes lectures in course data.
Context switch tool validation
iris/src/iris/tools/switch_chat_context.py
Validates modes and targets, normalizes exercise modes, handles no-op requests, and records valid switches.
Tool registration and dynamic retrieval
iris/src/iris/tools/__init__.py, iris/src/iris/tools/chat_tool_providers.py, iris/src/iris/tools/lecture_list.py, iris/src/iris/tools/lecture_content_retrieval.py, iris/src/iris/pipeline/abstract_agent_pipeline.py
Registers tools, stores pending switches, and resolves lecture retrieval scope from execution state.
Prompt instructions and status delivery
iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2, iris/src/iris/pipeline/chat/chat_pipeline.py, iris/src/iris/web/status/status_update.py
Adds context-switching instructions and forwards pending switches in final status payloads.
Context switching validation
iris/tests/test_lecture_list_tool.py, iris/tests/test_switch_chat_context_tool.py
Tests lecture listing, validation, retrieval scope, status serialization, and DTO parsing.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Student
  participant ChatAgent
  participant switch_chat_context
  participant ChatPipeline
  participant ArtemisAPI
  Student->>ChatAgent: ask about another exercise or lecture
  ChatAgent->>switch_chat_context: request target mode and entity ID
  switch_chat_context->>ChatPipeline: record suggested context
  ChatPipeline->>ArtemisAPI: send final result with suggested_context
  ArtemisAPI-->>Student: process context switch with final answer
Loading

Possibly related PRs

Suggested labels: ready for review

Suggested reviewers: bassner, claudia-anthropica

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 31.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the primary change: automatic context switching for active Iris conversations.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/iris/automatic-context-switching

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.

@Senan04
Senan04 marked this pull request as ready for review July 21, 2026 19:21
@Senan04
Senan04 requested a review from a team as a code owner July 21, 2026 19:21

@Claudia-Anthropica Claudia-Anthropica left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Senan04 The state and status plumbing looks clean, but the lecture-switching path cannot currently discover a target lecture ID or retrieve outside the active lecture. That blocks a core advertised flow; see the inline comment.

Comment thread iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2 Outdated
bassner
bassner previously approved these changes Jul 25, 2026

@bassner bassner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Locally verified end-to-end with Artemis #13292 in isolated OrbStack containers. The switch_chat_context tool validated and emitted the text-exercise target, and Artemis applied and rendered the context switch with the final response.

@Claudia-Anthropica Claudia-Anthropica left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Senan04 The course-wide lecture lookup and dynamic retargeting fix the functional blocker. The requested A→B regression still needs to exercise retrieval after the switch and verify it queries lecture B; I left the exact assertion in the existing thread.

The A to B test stopped after recording the switch, so the retrieval side
of the flow stayed unverified. Drive the real providers and assert the
retriever queries the new lecture without the old lecture unit.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@iris/tests/test_switch_chat_context_tool.py`:
- Around line 309-318: Update the serializer used by ChatRunCallback.send_result
to omit fields whose values are None, specifically ensuring suggested_context
does not produce a suggestedContext key when no switch is provided. Change
test_send_result_without_switch_omits_suggested_context to assert that
"suggestedContext" is absent from the payload rather than equal to None.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 81c70041-f995-458c-b5fd-b1b532db20d3

📥 Commits

Reviewing files that changed from the base of the PR and between 1f8f62b and b528f7c.

📒 Files selected for processing (8)
  • iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2
  • iris/src/iris/tools/__init__.py
  • iris/src/iris/tools/chat_tool_providers.py
  • iris/src/iris/tools/lecture_content_retrieval.py
  • iris/src/iris/tools/lecture_list.py
  • iris/src/iris/tools/switch_chat_context.py
  • iris/tests/test_lecture_list_tool.py
  • iris/tests/test_switch_chat_context_tool.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • iris/src/iris/tools/init.py
  • iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2
  • iris/src/iris/tools/switch_chat_context.py

Comment thread iris/tests/test_switch_chat_context_tool.py

@Claudia-Anthropica Claudia-Anthropica left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Senan04 The requested A-to-B regression now exercises the provided retrieval tool and verifies the new lecture scope, and the implementation looks good. Approving this update.

@Predixx Predixx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One gap: in a course with lectures but no ingested content the lecture path never switches. provide_lecture_list is gated on allow_lecture_tool, so the agent gets no lecture IDs, while the prompt tells it to look one up and never guess. Gating on dto.course.lectures would be enough.

Predixx
Predixx previously approved these changes Jul 27, 2026

@Predixx Predixx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested locally against Artemis #13292 with a real LLM. Course → exercise, exercise → exercise and back to course all switch correctly, including the marker and the session mode.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

There hasn't been any activity on this pull request recently. Therefore, this pull request has been automatically marked as stale and will be closed if no further activity occurs within seven days. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Aug 4, 2026

@Claudia-Anthropica Claudia-Anthropica left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Senan04 The merge preserves the context-switching and dynamic-retrieval paths, and the Iris test check passes. However, the previously noted [medium] lecture-discovery gap remains: provide_lecture_list is gated by allow_lecture_tool, which is false when a course has lectures but no indexed lecture units, leaving the agent unable to obtain a target lecture ID and perform the advertised switch. Make lecture-list availability depend on state.dto.course.lectures rather than retrieval-index availability, and cover that provider case with a regression test.

@github-actions github-actions Bot removed the stale label Aug 10, 2026
@bassner

bassner commented Aug 11, 2026

Copy link
Copy Markdown
Member

@Claudia-Anthropica review

@Claudia-Anthropica

Copy link
Copy Markdown
Collaborator

@bassner acknowledged, queuing this up for immediate review!

@Claudia-Anthropica Claudia-Anthropica left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Senan04 The new commits correctly decouple lecture-list availability from indexed lecture content, and the captured Iris test and lint checks pass. However, the same-run exercise-switch path can still use tools bound to the previous exercise and misattribute its submission data to the target. Please prevent original-context exercise tools from serving data after a switch, as detailed inline.

Comment thread iris/src/iris/pipeline/prompts/templates/chat_system_prompt.j2

@thynguyentumde thynguyentumde left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested locally along with the Artemis PR. Confirmed Iris can detect and switch context automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants