feat(tui): fb-41 PR-6 — TUI Ask multi-hop toggle + hop trace summary

fb-41 multi-hop RAG 의 **마지막 component PR** (PR-5 머지 직후). TUI Ask
패널의 user-facing surface — F2 toggle, multi-hop badge, status panel
의 hop count summary, cheatsheet 안내. v0.18.0 cut 준비.

설계: docs/superpowers/specs/2026-05-25-p9-fb-41-multi-hop-rag-design.md
계획: docs/superpowers/plans/2026-05-25-p9-fb-41-multi-hop-rag.md (PR-6 단락)

## TUI surface

- `crates/kebab-tui/src/app.rs`:
  - `AskState.multi_hop: bool` field + Default false. 사용자 토글
    상태를 인-패널 보존, 대화 history 와 직교 — F2 flipping mid-
    conversation 도 turns 보존 (다음 turn 만 다른 pipeline 으로 route).
- `crates/kebab-tui/src/ask.rs`:
  - `handle_key_ask` 에 `(KeyCode::F(2), _) → s.multi_hop = !s.multi_hop`.
    Mode-agnostic (physical function key — Normal/Insert 양쪽 작동, typing
    ambiguity 없음). Briefing 의 candidate (F2 vs Ctrl-T) 중 F2 채택 —
    Ctrl-M 은 Enter 와 collision 이미 명시, F2 가 cleanest.
  - `spawn_ask_worker` 의 `AskOpts.multi_hop` 가 spawn 시점에 토글값
    snapshot. 이후 F2 flip 은 다음 Enter 부터 적용 (in-flight turn 무영향).
  - `render_input` 의 input pane title 에 `F2=multi-hop` binding 안내
    추가 + prompt row 에 `multi-hop` badge (Success 녹색, toggled-on 일
    때만). 사용자가 어떤 pipeline 으로 다음 query 를 보낼지 항상 가시.
  - `render_status` 의 status panel 에 `multi-hop: N hops` line 추가
    (last_answer.hops 가 Some 일 때만). forced_stop 발생 시
    `forced_stop=K` suffix — depth/pool cap tuning 단서.
- `crates/kebab-tui/src/cheatsheet.rs`:
  - Ask section 에 `F2 toggle multi-hop pipeline` entry 추가.

## 변경 없음 (의도된 deferral)

- `InspectTarget::Hop(turn_index)` variant — plan 의 PR-6 stretch goal.
  per-iter hop trace detail 을 Inspect 패널에 노출하는 기능은 별 PR
  (PR-6b 또는 v0.18 dogfood follow-up). PR-6 의 핵심 가치
  (사용자가 multi-hop pipeline 을 토글하고 결과의 hop count 를 본다)
  는 status panel 의 한 줄 summary 로 100% cover. Inspect 진입은
  multi-hop 사용자가 *드물게* 필요한 surface — v0.18 cut 부담 회피.
- prompt_template_version (`rag-multi-hop-v1`) — 그대로.
- MCP / CLI surface — PR-4 / PR-5 의 책임.

## Tests (`tests/ask.rs` 신규 6 multi-hop pins)

- `f2_toggles_multi_hop_flag_from_insert_mode`: Insert 에서 F2 toggle
  (fresh_app default mode).
- `f2_toggles_multi_hop_flag_from_normal_mode`: Normal 에서도 동일
  — mode-agnostic 회귀 핀.
- `input_pane_shows_multi_hop_badge_when_toggled_on`: 토글 on 시
  prompt row 에 `multi-hop` 등장 + title 의 `F2=multi-hop` binding
  hint 등장.
- `input_pane_omits_multi_hop_badge_when_toggled_off`: 토글 off 시
  prompt row 의 badge 부재 (title hint 는 유지 — 사용자 discoverability).
- `status_panel_summarizes_hops_when_answer_has_trace`: 3-hop trace
  (Decompose + Decide + Synthesize) → `multi-hop: 3 hops` line.
- `status_panel_omits_hops_summary_for_single_pass`: hops=None → 본문
  에 summary line 부재 (title binding hint 만).
- `spawn_snapshot_multi_hop_into_askopts`: AskState.multi_hop 의
  field shape 회귀 핀 (default false / settable / round-trip).

## 검증

- `cargo test -p kebab-tui -j 1` — 신규 6 multi-hop + 기존 ask /
  search / library / mode / cheatsheet / inspect / status_bar 모두
  통과 (42 ask test + 10 mode + 기타). 회귀 없음.
- `cargo clippy -p kebab-tui --all-targets -j 1 -- -D warnings` clean.
- 단일 crate 직렬 build (16 GB RAM 제약).

## v0.18.0 cut (다음 단계)

- Workspace `Cargo.toml` version 0.17.2 → 0.18.0 (minor — surface
  확장 + new prompt_template_version `rag-multi-hop-v1`).
- HANDOFF.md / HOTFIXES.md / INDEX.md 갱신 (fb-41 entry 정리).
- `gitea-release v0.18.0 --auto-notes`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 09:26:29 +00:00
parent bf28a1e4d9
commit 016f380428
4 changed files with 253 additions and 4 deletions

View File

@@ -988,3 +988,203 @@ fn follow_tail_renders_tail_when_transcript_overflows() {
"tail of transcript must be visible when follow_tail is on; got:\n{rendered}"
);
}
// ── p9-fb-41: multi-hop toggle ───────────────────────────────────────────
/// `F2` flips `AskState.multi_hop` from any mode (Normal or Insert)
/// — it's a physical function key, not a Char, so the mode gating
/// in handle_key_ask doesn't apply.
#[test]
fn f2_toggles_multi_hop_flag_from_insert_mode() {
let mut app = fresh_app();
// fresh_app sets Insert mode on the Ask pane (auto-flip).
assert_eq!(app.mode, kebab_tui::Mode::Insert);
assert!(!app.ask.as_ref().unwrap().multi_hop, "default off");
handle_key_ask(&mut app, KeyEvent::new(KeyCode::F(2), KeyModifiers::NONE));
assert!(
app.ask.as_ref().unwrap().multi_hop,
"first F2 turns multi-hop on"
);
handle_key_ask(&mut app, KeyEvent::new(KeyCode::F(2), KeyModifiers::NONE));
assert!(
!app.ask.as_ref().unwrap().multi_hop,
"second F2 turns it back off"
);
}
#[test]
fn f2_toggles_multi_hop_flag_from_normal_mode() {
let mut app = fresh_app();
app.mode = kebab_tui::Mode::Normal;
assert!(!app.ask.as_ref().unwrap().multi_hop);
handle_key_ask(&mut app, KeyEvent::new(KeyCode::F(2), KeyModifiers::NONE));
assert!(
app.ask.as_ref().unwrap().multi_hop,
"F2 in Normal mode must also toggle multi-hop"
);
}
#[test]
fn input_pane_shows_multi_hop_badge_when_toggled_on() {
let mut app = fresh_app();
app.ask.as_mut().unwrap().multi_hop = true;
let backend = TestBackend::new(80, 20);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render_ask(f, Rect::new(0, 0, 80, 20), &app))
.unwrap();
let rendered = render_to_string(terminal.backend().buffer());
assert!(
rendered.contains("multi-hop"),
"input pane must surface a multi-hop badge when toggled on; got:\n{rendered}"
);
assert!(
rendered.contains("F2=multi-hop"),
"ask input title must advertise the F2 binding; got:\n{rendered}"
);
}
#[test]
fn input_pane_omits_multi_hop_badge_when_toggled_off() {
let app = fresh_app();
assert!(!app.ask.as_ref().unwrap().multi_hop);
let backend = TestBackend::new(80, 20);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render_ask(f, Rect::new(0, 0, 80, 20), &app))
.unwrap();
let rendered = render_to_string(terminal.backend().buffer());
// The title still advertises the binding (so users discover the
// feature) but the *badge* text "multi-hop" must NOT appear next
// to the prompt — the line is the toggle-state signal.
//
// We can't simply assert `!rendered.contains("multi-hop")` because
// the title itself contains the word. Instead split on the input
// prompt and confirm the badge segment of the input line is absent.
// Match the layout: the input pane is the first row, title on the
// border, prompt + badge on the inner row.
assert!(
rendered.contains("F2=multi-hop"),
"title binding hint must always be visible; got:\n{rendered}"
);
let prompt_row = rendered.lines().find(|l| l.contains("?")).unwrap_or("");
assert!(
!prompt_row.contains("multi-hop"),
"the badge belongs on the prompt row only when toggled on; got row:\n{prompt_row}"
);
}
#[test]
fn status_panel_summarizes_hops_when_answer_has_trace() {
use kebab_core::{HopKind, HopRecord};
let mut app = fresh_app();
let mut answer = make_answer(true, None, "compound answer [#1]");
answer.hops = Some(vec![
HopRecord {
iter: 0,
kind: HopKind::Decompose,
sub_queries: vec!["q1".into(), "q2".into()],
context_chunks_added: 0,
forced_stop: false,
llm_call_ms: 7,
},
HopRecord {
iter: 1,
kind: HopKind::Decide,
sub_queries: vec![],
context_chunks_added: 3,
forced_stop: false,
llm_call_ms: 5,
},
HopRecord {
iter: 2,
kind: HopKind::Synthesize,
sub_queries: vec![],
context_chunks_added: 0,
forced_stop: false,
llm_call_ms: 11,
},
]);
app.ask.as_mut().unwrap().last_answer = Some(answer);
let backend = TestBackend::new(80, 20);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render_ask(f, Rect::new(0, 0, 80, 20), &app))
.unwrap();
let rendered = render_to_string(terminal.backend().buffer());
assert!(
rendered.contains("multi-hop: 3 hops"),
"status panel must surface the hop count; got:\n{rendered}"
);
}
#[test]
fn status_panel_omits_hops_summary_for_single_pass() {
let mut app = fresh_app();
let mut answer = make_answer(true, None, "single-pass answer [#1]");
answer.hops = None;
app.ask.as_mut().unwrap().last_answer = Some(answer);
let backend = TestBackend::new(80, 20);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render_ask(f, Rect::new(0, 0, 80, 20), &app))
.unwrap();
let rendered = render_to_string(terminal.backend().buffer());
// The status panel renders 3 lines (grounded / prompt / k/used)
// for single-pass — no "multi-hop:" line. Only the *title*
// binding hint ("F2=multi-hop") may contain the substring.
// Filter that row out, then assert the remaining buffer has no
// hops summary.
let body: String = rendered
.lines()
.filter(|l| !l.contains("F2=multi-hop"))
.collect::<Vec<_>>()
.join("\n");
assert!(
!body.contains("multi-hop:"),
"single-pass answer must NOT render the multi-hop summary line; got:\n{body}"
);
}
#[test]
fn spawn_snapshot_multi_hop_into_askopts() {
// We can't actually spawn the worker (no Ollama / KB), but we
// can pin the snapshot semantics: the toggle's value at the
// moment of Enter is the value the worker sees. Flipping F2
// after Enter doesn't retro-affect the in-flight turn.
//
// Strategy: read the toggle directly, then verify the contract
// by inspecting `AskState.multi_hop` after a flip — the
// toggle's "next-turn" semantics are documented but we keep
// the regression light: the field exists, is bool, defaults
// to false, and round-trips through the public surface.
let mut app = fresh_app();
let s = app.ask.as_mut().unwrap();
assert!(!s.multi_hop, "default false");
s.multi_hop = true;
assert!(s.multi_hop, "settable to true");
s.multi_hop = false;
assert!(!s.multi_hop, "settable back to false");
}
/// Small render helper shared with the rest of the test module's
/// buffer-snapshot pattern. We define it locally here to avoid
/// reaching into private internals.
fn render_to_string(buffer: &ratatui::buffer::Buffer) -> String {
(0..buffer.area.height)
.map(|y| {
(0..buffer.area.width)
.map(|x| buffer[(x, y)].symbol())
.collect::<String>()
})
.collect::<Vec<_>>()
.join("\n")
}