ask-query-system

37 beliefs (34 IN, 3 OUT)

The ask query system is the primary interface for retrieving and synthesizing knowledge from the TMS belief network. It supports tiered query modes (ask-has-tiered-query-modes): full LLM synthesis with a bounded tool loop, a no-synth mode that bypasses the LLM entirely and returns raw FTS5 search results (ask-no-synth-bypasses-llm, no-synth-mode-skips-llm), a dual mode that combines TMS and source-document retrieval with up to three LLM calls (ask-dual-mode-makes-three-llm-calls, ask-dual-requires-sources-db), and a natural mode that strips belief metadata and citation instructions for plain-language output (ask-natural-mode-strips-metadata-and-cite). The system invokes the Claude CLI in pipe mode rather than the native tool-use API, so tool calls are parsed from JSON lines in the text output (ask-uses-text-based-tool-protocol).

The defining design principle is fault tolerance with bounded execution (ask-is-fault-tolerant-and-bounded). The function guarantees a string return on every code path and never raises exceptions to the caller (ask-always-returns-string, ask-never-raises-on-llm-failure). When LLM synthesis fails due to timeout, missing binary, or non-zero exit, the system falls back to raw FTS5 search results (ask-falls-back-to-raw-search). The tool-call loop is capped at three iterations without MCP servers (ask-tool-loop-capped-at-three) and five iterations with MCP servers (ask-mcp-iteration-limit-is-five), with a final-turn instruction appended on the last round to force an answer. Source document content persists across all tool-call round-trips so the LLM retains context throughout the multi-turn loop (ask-source-chunks-persist-across-tool-iterations). The LLM invocation also strips the CLAUDECODE environment variable to prevent recursive invocation (ask-strips-claudecode-env).

The FTS5 search layer has its own resilience mechanisms. Query errors are silently caught and fall back to substring matching (fts-errors-silently-caught-in-search). When a multi-term query returns no results, the engine progressively drops terms via combinations to find partial matches (fts-progressive-relaxation), but this relaxation is bounded at 50 queries to prevent combinatorial explosion (fts-relaxation-capped-at-fifty-queries, fts-relaxation-budget-caps-at-50). Stop words are filtered before querying, with a fallback to all words longer than one character if every term is a stop word (fts-stop-words-filtered-before-query, ask-stop-word-fallback-ensures-nonempty-query). The sources database layer similarly degrades gracefully, catching OperationalError and DatabaseError to return empty results rather than crashing (ask-sources-db-failure-silently-degrades).

MCP integration adds an optional layer for external tool access. The MCP bridge runs a dedicated asyncio event loop on a daemon thread (mcp-bridge-runs-dedicated-event-loop-thread) with timeout bounds at both connection (30s) and per-tool execution (60s) phases (mcp-bridge-is-timeout-bounded-at-all-phases, mcp-bridge-connect-blocks-up-to-30s, mcp-bridge-call-tool-timeout-60s). MCP errors during the ask loop are caught and fed back to the LLM as context rather than propagated (ask-mcp-errors-non-fatal), creating defense-in-depth bounding at both application and transport layers (ask-mcp-is-defense-in-depth-bounded). The mcp package itself is an optional dependency guarded by try/except at import time (mcp-is-optional-dependency). Two beliefs are currently retracted: one claiming the tool catalog always reflects current server capabilities (ask-mcp-tool-use-has-current-catalog) and a similar claim about accurate tool discovery (ask-mcp-achieves-accurate-bounded-tool-use). This is consistent with the active belief that the tool catalog is snapshot-at-connect and never refreshed (mcp-bridge-tools-snapshot-at-connect), meaning the catalog can go stale if an MCP server adds tools after the initial handshake. A third retracted belief (ask-degrades-across-all-external-dependencies) made a broader claim about graceful degradation across all three external dependencies; its retraction may reflect a refinement where the individual degradation behaviors are tracked by more specific beliefs rather than a single sweeping claim.