RAG 프롬프트의 각 [근거] 청크 머리에 출처/trust 라벨을 붙이고 (`[#n] source=jira trust=secondary doc=…`), system prompt 에 "저신뢰 출처를 권위 출처와 충돌 시 discount 하고 [#번호]로 귀속" 2규칙을 더한다. 출처 필터 (`--source`/`--trust-min`)가 못 잡는 생성 측 실패 — 저신뢰(jira) 청크가 권위(wiki) 청크를 답변에서 덮어쓰는 것 — 를 다룬다. - SearchHit 에 source_id/trust_level(additive optional). lexical/vector build_hit 가 documents 조인에서 채움(both 동일: trust_level lowercase TEXT → serde lowercase round-trip, doc_summary read-back 과 동형). hybrid fusion 전파. - pack_context 라벨 렌더(버전 무관 항상). SYSTEM_PROMPT_RAG_V4 = rag-v3 8규칙 verbatim + 2규칙. config 기본 rag-v3→rag-v4. multi-hop synth 도 2규칙 → rag-multi-hop-v1→v2(prompt 변경 = 버전 bump, design §9). - wire: search_hit.v1 에 두 필드 optional additive(required 아님, skip_serializing_if=None → 구 소비자 무영향, v2 bump 아님). - source_id 는 RAG 헤더에 렌더되므로 validate_sources 에 [A-Za-z0-9._-] char 검증. - opt-out: rag-v3 핀 = v3 system prompt 선택(discount 지시 빠짐, 라벨은 무해히 잔존). 검증: kebab-core/search/rag/config/eval 전 테스트 green(25 바이너리), clippy 0. 독립 코드 리뷰 APPROVE(7위험 PASS — trust round-trip 실 DB 확인; MEDIUM rag-v3 opt-out doc + multi-hop 버전 / LOW source_id 검증 반영). 도그푸딩: 라벨 메커니즘 end-to-end 검증(search --json 이 competing 쿼리에 wiki/primary + jira/secondary 둘 다 정확 라벨로 노출). LLM-judge(답변 비교)는 instruction LLM 부재로 보류(.2/.47 다운 + lemonade /api/generate it-model template 미적용) — 인프라, .2 복구 시 측정. 버전 bump 은 follow-up 들과 배치 릴리스에서 일괄. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Mc6W1fgsrbFKTsqA6P8La
280 lines
9.5 KiB
Rust
280 lines
9.5 KiB
Rust
//! p9-fb-40: integration tests for rag-v1 / rag-v2 / unknown-version dispatch.
|
|
//!
|
|
//! Wraps `MockLanguageModel` in a `CapturingLm` that snapshots
|
|
//! `GenerateRequest::system` on every `generate_stream` call so the
|
|
//! tests can assert which template constant the pipeline rendered.
|
|
|
|
mod common;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use common::{MockRetriever, RagEnv, id32, mk_hit};
|
|
use kebab_core::{
|
|
FinishReason, LanguageModel, Retriever, SearchMode, TokenChunk, TokenUsage, TrustLevel,
|
|
};
|
|
use kebab_llm::MockLanguageModel;
|
|
use kebab_rag::{AskOpts, RagPipeline};
|
|
|
|
const TEST_LM_ID: &str = "mock-lm";
|
|
|
|
/// LM wrapper that captures the system prompt of the most-recent
|
|
/// `generate_stream` call, so tests can assert which template was
|
|
/// rendered. Mirrors the `CountingLm` pattern from
|
|
/// `tests/streaming_events.rs` but stores `req.system` instead of a
|
|
/// call counter.
|
|
struct CapturingLm {
|
|
inner: MockLanguageModel,
|
|
captured_system: Arc<Mutex<Option<String>>>,
|
|
/// rag-provenance-label: also snapshot `req.user` (the packed [근거]
|
|
/// block) so tests can assert the per-chunk `source=`/`trust=` header.
|
|
captured_user: Arc<Mutex<Option<String>>>,
|
|
}
|
|
|
|
impl CapturingLm {
|
|
fn new(captured: Arc<Mutex<Option<String>>>) -> Self {
|
|
Self::with_user(captured, Arc::new(Mutex::new(None)))
|
|
}
|
|
|
|
fn with_user(
|
|
captured_system: Arc<Mutex<Option<String>>>,
|
|
captured_user: Arc<Mutex<Option<String>>>,
|
|
) -> Self {
|
|
Self {
|
|
inner: MockLanguageModel {
|
|
model_id: TEST_LM_ID.to_string(),
|
|
provider: "mock".to_string(),
|
|
context_tokens: 32_768,
|
|
canned_response: "근거가 충분합니다 [#1]".to_string(),
|
|
canned_finish: FinishReason::Stop,
|
|
canned_usage: TokenUsage {
|
|
prompt_tokens: 10,
|
|
completion_tokens: 5,
|
|
latency_ms: 7,
|
|
},
|
|
},
|
|
captured_system,
|
|
captured_user,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl LanguageModel for CapturingLm {
|
|
fn model_ref(&self) -> kebab_core::ModelRef {
|
|
self.inner.model_ref()
|
|
}
|
|
fn context_tokens(&self) -> usize {
|
|
self.inner.context_tokens()
|
|
}
|
|
fn generate_stream(
|
|
&self,
|
|
req: kebab_core::GenerateRequest,
|
|
) -> anyhow::Result<Box<dyn Iterator<Item = anyhow::Result<TokenChunk>> + Send>> {
|
|
*self.captured_system.lock().unwrap() = Some(req.system.clone());
|
|
*self.captured_user.lock().unwrap() = Some(req.user.clone());
|
|
self.inner.generate_stream(req)
|
|
}
|
|
}
|
|
|
|
/// Mirror of `streaming_events::opts_with_sink` minus the sink. p9-fb-41
|
|
/// added `impl Default for AskOpts` — these explicit fixtures stay
|
|
/// for now so a future field addition fails compilation here too,
|
|
/// surfacing intent. New callers should prefer `..Default::default()`.
|
|
fn lexical_opts() -> AskOpts {
|
|
AskOpts {
|
|
k: 3,
|
|
explain: false,
|
|
mode: SearchMode::Lexical,
|
|
temperature: Some(0.0),
|
|
seed: Some(0),
|
|
stream_sink: None,
|
|
history: Vec::new(),
|
|
conversation_id: None,
|
|
turn_index: None,
|
|
multi_hop: false,
|
|
}
|
|
}
|
|
|
|
/// Build a `RagPipeline` with the given `prompt_template_version`.
|
|
/// Returns the pipeline, the captured-system handle, and the env (kept
|
|
/// alive for the test body — drops the SqliteStore + tempdir together).
|
|
fn build_pipeline_with_template(
|
|
version: &str,
|
|
) -> (RagPipeline, Arc<Mutex<Option<String>>>, RagEnv) {
|
|
let mut env = RagEnv::new();
|
|
env.config.rag.prompt_template_version = version.to_string();
|
|
// Drop score gate so the seeded hit (fusion_score = 0.9) always
|
|
// makes it through — the dispatch we want to exercise lives past
|
|
// the gate.
|
|
env.config.rag.score_gate = 0.0;
|
|
let captured = Arc::new(Mutex::new(None));
|
|
let lm: Arc<dyn LanguageModel> = Arc::new(CapturingLm::new(captured.clone()));
|
|
// Seed one chunk so the [근거] block has content and the LM is
|
|
// actually invoked on the success path.
|
|
let chunk_id = id32("c");
|
|
let doc_id = id32("d");
|
|
env.seed_chunk(&chunk_id, &doc_id, "a.md", "hello world", &["H"]);
|
|
let hit = mk_hit(1, &chunk_id, &doc_id, "a.md", 0.9, &["H"]);
|
|
let retriever: Arc<dyn Retriever> = Arc::new(MockRetriever::new(vec![hit]));
|
|
let pipeline = RagPipeline::new(env.config.clone(), retriever, lm, env.sqlite.clone());
|
|
(pipeline, captured, env)
|
|
}
|
|
|
|
#[test]
|
|
fn ask_with_rag_v1_uses_v1_system_prompt() {
|
|
let (pipeline, captured, _env) = build_pipeline_with_template("rag-v1");
|
|
let _ = pipeline.ask("hello", lexical_opts());
|
|
let s = captured
|
|
.lock()
|
|
.unwrap()
|
|
.clone()
|
|
.expect("system prompt captured");
|
|
assert!(
|
|
s.contains("로컬 KB 위에서 동작"),
|
|
"shared V1/V2 prefix expected, got: {s}"
|
|
);
|
|
assert!(
|
|
!s.contains("학습 지식"),
|
|
"V1 must NOT contain V2-only 학습 지식 rule, got: {s}"
|
|
);
|
|
assert!(
|
|
!s.contains("확실하지 않다"),
|
|
"V1 must NOT contain V2-only 확실하지 않다 rule, got: {s}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ask_with_rag_v2_uses_v2_system_prompt() {
|
|
let (pipeline, captured, _env) = build_pipeline_with_template("rag-v2");
|
|
let _ = pipeline.ask("hello", lexical_opts());
|
|
let s = captured
|
|
.lock()
|
|
.unwrap()
|
|
.clone()
|
|
.expect("system prompt captured");
|
|
assert!(
|
|
s.contains("학습 지식"),
|
|
"V2 must contain 학습 지식 rule, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("확실하지 않다"),
|
|
"V2 must contain 확실하지 않다 rule, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("큰따옴표"),
|
|
"V2 must contain 큰따옴표 rule, got: {s}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ask_with_rag_v3_uses_v3_system_prompt() {
|
|
let (pipeline, captured, _env) = build_pipeline_with_template("rag-v3");
|
|
let _ = pipeline.ask("hello", lexical_opts());
|
|
let s = captured
|
|
.lock()
|
|
.unwrap()
|
|
.clone()
|
|
.expect("system prompt captured");
|
|
assert!(
|
|
s.contains("로컬 KB 위에서 동작"),
|
|
"shared prefix expected, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("학습 지식"),
|
|
"V3 must contain 학습 지식 rule, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("원본 질문"),
|
|
"V3 must contain language-matching rule (v3-only), got: {s}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ask_with_unknown_template_returns_early_error() {
|
|
let (pipeline, _captured, _env) = build_pipeline_with_template("rag-v99");
|
|
let result = pipeline.ask("hello", lexical_opts());
|
|
assert!(result.is_err(), "expected error on unknown version");
|
|
let msg = format!("{:#}", result.unwrap_err());
|
|
assert!(
|
|
msg.contains("rag-v99") && msg.contains("expected"),
|
|
"expected error to mention version + expected list, got: {msg}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ask_with_rag_v4_uses_v4_system_prompt() {
|
|
let (pipeline, captured, _env) = build_pipeline_with_template("rag-v4");
|
|
let _ = pipeline.ask("hello", lexical_opts());
|
|
let s = captured
|
|
.lock()
|
|
.unwrap()
|
|
.clone()
|
|
.expect("system prompt captured");
|
|
assert!(
|
|
s.contains("로컬 KB 위에서 동작"),
|
|
"shared prefix expected, got: {s}"
|
|
);
|
|
// rag-v4 = rag-v3 rules + the two provenance rules.
|
|
assert!(
|
|
s.contains("학습 지식") && s.contains("원본 질문"),
|
|
"V4 must retain V3 rules, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("신뢰도 우선") && s.contains("trust=primary"),
|
|
"V4 must contain trust-discount rule, got: {s}"
|
|
);
|
|
assert!(
|
|
s.contains("귀속"),
|
|
"V4 must contain attribution rule, got: {s}"
|
|
);
|
|
}
|
|
|
|
/// rag-provenance-label: build a pipeline whose retriever returns a single
|
|
/// hit with the given provenance, capturing the packed [근거] user prompt.
|
|
fn pack_user_prompt_for_hit(
|
|
source_id: Option<&str>,
|
|
trust_level: Option<TrustLevel>,
|
|
) -> String {
|
|
let mut env = RagEnv::new();
|
|
env.config.rag.prompt_template_version = "rag-v4".to_string();
|
|
env.config.rag.score_gate = 0.0;
|
|
let captured_system = Arc::new(Mutex::new(None));
|
|
let captured_user = Arc::new(Mutex::new(None));
|
|
let lm: Arc<dyn LanguageModel> =
|
|
Arc::new(CapturingLm::with_user(captured_system, captured_user.clone()));
|
|
let chunk_id = id32("c");
|
|
let doc_id = id32("d");
|
|
env.seed_chunk(&chunk_id, &doc_id, "a.md", "hello world", &["H"]);
|
|
let mut hit = mk_hit(1, &chunk_id, &doc_id, "a.md", 0.9, &["H"]);
|
|
hit.source_id = source_id.map(str::to_string);
|
|
hit.trust_level = trust_level;
|
|
let retriever: Arc<dyn Retriever> = Arc::new(MockRetriever::new(vec![hit]));
|
|
let pipeline = RagPipeline::new(env.config.clone(), retriever, lm, env.sqlite.clone());
|
|
let _ = pipeline.ask("hello", lexical_opts());
|
|
let out = captured_user
|
|
.lock()
|
|
.unwrap()
|
|
.clone()
|
|
.expect("user prompt captured");
|
|
// Keep env alive until after ask returns.
|
|
drop(env);
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn pack_context_header_renders_source_and_trust_labels() {
|
|
let user = pack_user_prompt_for_hit(Some("jira"), Some(TrustLevel::Secondary));
|
|
assert!(
|
|
user.contains("[#1] source=jira trust=secondary doc=a.md"),
|
|
"expected provenance label in chunk header, got: {user}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn pack_context_header_uses_default_source_and_unknown_trust_when_none() {
|
|
let user = pack_user_prompt_for_hit(None, None);
|
|
assert!(
|
|
user.contains("[#1] source=default trust=unknown doc=a.md"),
|
|
"expected default/unknown provenance label when fields absent, got: {user}"
|
|
);
|
|
}
|