Fix terraphim/terraphim-clients#77: include JSON instructions in RLM prompt#5
Merged
Merged
Conversation
…rate markdown-wrapped LLM output
…oid 10s API timeout
- P2-1: fall back to role_from_env when direct OpenRouter client build fails; document precedence between env-var client and role configuration. - P2-2: add maintainability and safety notes to openrouter_client.rs explaining intentional duplication and summarize() hard-error contract. - P2-3: warn when the free-tier default model is used, noting data-handling considerations for repository content excerpts. Refs terraphim/terraphim-clients#77
- P3-1: map transient OpenRouter errors (network, rate limit, API, decode) to ServiceError::Common(CommonError::Network) instead of Config, so upstream handling can distinguish transient conditions from user misconfiguration. - P3-2: make reqwest an optional dependency tied to the llm feature, avoiding unconditional build cost when llm is disabled. - P3-3: add tests for prompt assembly (AnswerSignature instructions present in answer path, absent in list path), OpenRouterClient construction (input validation, base URL resolution, trait object), and extract_json edge cases (generic fence, array payload, unterminated fence, mixed language fences). Refs terraphim/terraphim-clients#77
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes terraphim/terraphim-clients#77 by including
AnswerSignature::instructions()in the RLM prompt and adding a tolerant JSON extractor that handles markdown-wrapped or prose-embedded LLM output.Root cause
AnswerSignatureexpects a JSON object but the prompt never told the model to return JSON. Models that emit plain text (e.g.deepseek/deepseek-v4-flash,meta-llama/llama-3.2-*) had their answers silently dropped.Changes
crates/terraphim_grep/src/lib.rs: appendAnswerSignature::instructions()to the user message wheninclude_answeris true.crates/terraphim_grep/src/signatures.rs: addextract_json()to strip markdown fences and locate top-level JSON objects/arrays inside prose.crates/terraphim_grep/src/openrouter_client.rs: new long-timeout (120s) OpenRouter client so slow providers do not hit the shared 10s API timeout.crates/terraphim_grep/src/main.rs: prefer the long-timeout OpenRouter client whenOPENROUTER_API_KEYis set.Verification
deepseek/deepseek-v4-flashanswer: nullanswer: PRESENT, confidence 1.0amazon/nova-micro-v1answer: PRESENTanswer: PRESENT, confidence 0.95meta-llama/llama-3.2-3b-instructanswer: nullanswer: null(model still ignores JSON instructions; acceptable)All
terraphim_greptests pass (cargo test -p terraphim_grep --features "code-search openrouter").Trade-off note
Benchmark analysis on
terraphim-aibranchtask/3098-doc-grep-openrouter-rlmshowsgpt-oss-20bis too large and slow for this use case; recommended models remainamazon/nova-micro-v1,mistralai/mistral-nemo, anddeepseek/deepseek-v4-flash.Refs terraphim/terraphim-clients#77