# Track Specification: Context Preview & Slice Editor Fixes

## Overview

Fix critical failures in the context composition feature: Preview button generates no content, and Inspect/Slices buttons fail to open their respective editor panels.

## Current State Audit (as of 7f40630)

### Already Implemented (DO NOT re-implement)

1. **Context Preview Window** (`gui_2.py:5308-5320`): `render_context_preview_window` function exists and renders markdown via `markdown_helper.render(app.context_preview_text, context_id="ctx_preview")`. The window is opened via `app._render_window_if_open("Context Preview", lambda: render_context_preview_window(app))` at line 1238.

2. **Context Batch Actions with Preview** (`gui_2.py:2805-2843`): `render_context_batch_actions` contains the Preview button at line 2839:
   ```python
   if imgui.button("Preview##ctx"):
       app.context_preview_text = app.controller._do_generate()[0]
       app.show_windows["Context Preview"] = True
   ```

3. **AST Inspector Modal** (`gui_2.py:2900-3036`): `render_ast_inspector_modal` exists with dual-pane layout (tree + content). Triggered via `app._show_ast_inspector = True` and `app.ui_inspecting_ast_file = f_item` set in `render_context_files_table` at lines 3145-3146.

4. **Text Viewer Window** (`gui_2.py:4077-4145`): `render_text_viewer_window` function exists with syntax highlighting via `ImGuiColorTextEdit`. Triggered via `app.show_text_viewer = True` and content set via `app.text_viewer_content`, `app.text_viewer_title`, `app.text_viewer_type`.

5. **Context Files Table** (`gui_2.py:3090-3198`): `render_context_files_table` with Inspect and Slices buttons at lines 3143-3158.

6. **AppController._do_generate** (`app_controller.py:3433-3468`): Returns `(full_md, path, file_items, stable_md, discussion_text)`. Used by Preview button at `gui_2.py:2840`.

7. **render_context_modals** (`gui_2.py:5275-5306`): Calls `render_add_context_files_modal`, `render_ast_inspector_modal`.

### Gaps to Fill (This Track's Scope)

1. **Gap 1**: The Preview button at `gui_2.py:2840` calls `app.controller._do_generate()[0]` but `AppController` at line 991 shows `self.context_files: List[models.FileItem] = []`. The controller's `context_files` is NEVER synchronized with `App.context_files` from `gui_2.py`. The Preview therefore shows empty content.

2. **Gap 2**: `render_context_modals` at line 5275 calls `render_ast_inspector_modal` but **never actually calls** `render_text_viewer_window`. The Slices button sets `app.show_text_viewer = True` and populates `app.text_viewer_content`, but `render_text_viewer_window` is NOT invoked in the main render loop.

3. **Gap 3**: `render_ast_inspector_modal` uses `app._show_ast_inspector` flag and `imgui.open_popup` pattern, but this is not integrated into `render_context_modals` properly (modal rendering may conflict with window rendering timing).

## Goals

- Fix Preview button to show actual aggregated context content from context_files
- Fix Inspect button to open AST Inspector modal properly
- Fix Slices button to open Text Viewer window with file content

## Functional Requirements

1. **Synchronize context_files between App and AppController**: The Preview button calls `app.controller._do_generate()` which reads `self.context_files` (controller's empty list). Must sync `App.context_files` → `AppController.context_files` before calling `_do_generate()`.

2. **Add Text Viewer Window to render loop**: `render_text_viewer_window(app)` must be called in the main render loop (after line 1238 or in `render_context_modals`).

3. **Fix AST Inspector modal integration**: Ensure `render_ast_inspector_modal` is properly called in `render_context_modals` and uses correct popup pattern.

4. **Add defensive empty-state handling**: If `context_files` is empty, Preview should show "No files in context composition" message.

## Architecture Reference

- docs/guide_architecture.md#render-loop - Main render loop structure
- docs/guide_architecture.md#modal-patterns - Modal and window rendering patterns
- docs/guide_tools.md#mcp-bridge - Tool execution flow

## Out of Scope

- Changes to aggregation logic (only fixing invocation, not the aggregation itself)
- Changes to the FileItem model structure
- Changes to view_mode handling (only fixing the buttons that trigger editors)