Private
Public Access
0
0

fix(project): Reload context_files from new project on project switch

When switching projects, the previous project's context_files remained
visible in the Context Composition panel because the controller's
self.context_files list was not reloaded from the new project's TOML
files.paths entry.

Fix in _refresh_from_project:
- After loading self.files from the project TOML, populate
  self.context_files with deep copies of those FileItem objects
- Reset self._app.ui_selected_context_files to match the new project's
  auto_aggregate set
- Guard the _app access with hasattr so the controller is usable
  standalone (in tests, headless mode, etc.) without an attached App

Test: 1 new test in tests/test_project_switch_persona_preset.py
- test_switch_project_resets_context_files: switches from project_a
  (forth + gte_hello files) to project_b (gencpp timing files) and
  asserts context_files contains ONLY project_b's files
This commit is contained in:
2026-06-04 21:03:16 -04:00
parent 7df65dff14
commit 36f3292249
2 changed files with 58 additions and 0 deletions
+10
View File
@@ -2723,6 +2723,16 @@ class AppController:
self.files.append(models.FileItem.from_dict(p))
else:
self.files.append(models.FileItem(path=str(p)))
import copy
self.context_files = []
for f in self.files:
if isinstance(f, models.FileItem):
fi = copy.deepcopy(f)
else:
fi = models.FileItem(path=str(f))
self.context_files.append(fi)
if hasattr(self, "_app") and self._app is not None:
self._app.ui_selected_context_files = {f.path for f in self.context_files if f.auto_aggregate}
self.screenshots = list(self.project.get("screenshots", {}).get("paths", []))
disc_sec = self.project.get("discussion", {})
self.disc_roles = list(disc_sec.get("roles", ["User", "AI", "Vendor API", "System"]))
@@ -170,3 +170,51 @@ def test_load_context_preset_missing_raises_keyerror(tmp_path, monkeypatch):
with pytest.raises(KeyError, match="Context preset 'NonexistentPreset' not found"):
ctrl.load_context_preset("NonexistentPreset")
def test_switch_project_resets_context_files(tmp_path, monkeypatch):
project_a_path, project_b_path = _setup_two_projects(tmp_path)
proj_a_data = '''[project]
name = "project_a"
[files]
base_dir = "."
paths = [
{ path = "C:/projects/forth/bootslop/main.c", view_mode = "full" },
{ path = "C:/projects/Pikuma/ps1/code/gte_hello/hello_gte.c", view_mode = "full" },
]
'''
project_a_path.write_text(proj_a_data)
proj_b_data = '''[project]
name = "project_b"
[files]
base_dir = "."
paths = [
{ path = "C:/projects/gencpp/base/dependencies/timing.cpp", view_mode = "full" },
{ path = "C:/projects/gencpp/base/dependencies/timing.hpp", view_mode = "full" },
]
'''
project_b_path.write_text(proj_b_data)
ctrl = AppController()
monkeypatch.setattr(ctrl, "_rebuild_rag_index", lambda: None)
monkeypatch.setattr(ctrl, "_flush_to_project", lambda: None)
ctrl.active_project_path = str(project_a_path)
ctrl.project = project_manager.load_project(str(project_a_path))
ctrl.preset_manager = presets.PresetManager(Path(project_a_path).parent)
ctrl.tool_preset_manager = tool_presets.ToolPresetManager(Path(project_a_path).parent)
ctrl.persona_manager = PersonaManager(Path(project_a_path).parent)
ctrl._refresh_from_project()
assert len(ctrl.context_files) == 2
assert any("forth" in f.path for f in ctrl.context_files)
assert any("gte_hello" in f.path for f in ctrl.context_files)
ctrl._switch_project(str(project_b_path))
assert len(ctrl.context_files) == 2
assert all("gencpp" in f.path for f in ctrl.context_files)
assert not any("forth" in f.path for f in ctrl.context_files)
assert not any("gte_hello" in f.path for f in ctrl.context_files)