Skip to content

feat(cli): make interactive shell documentation-aware (#1166)#1172

Merged
rrajan94 merged 4 commits intoTracer-Cloud:mainfrom
Davidson3556:feat/docs-aware-interactive-cli
May 2, 2026
Merged

feat(cli): make interactive shell documentation-aware (#1166)#1172
rrajan94 merged 4 commits intoTracer-Cloud:mainfrom
Davidson3556:feat/docs-aware-interactive-cli

Conversation

@Davidson3556
Copy link
Copy Markdown
Contributor

Fixes #1166

Describe the changes you have made in this PR -

Make the OpenSRE interactive shell documentation-aware so procedural how-to
questions ("how do I configure Datadog?", "how do I deploy this?", "how do I
run an RCA?") are answered from the current project docs/ directory instead of
relying on model memory.

What changed:

  • New module app/cli/interactive_shell/docs_reference.py walks docs/,
    parses MDX frontmatter and headings, ranks pages by query-token overlap
    (slug/title weighted, exact-slug match boosted, nested-path penalized,
    asset/image/font dirs skipped), excerpts the top-N pages, and always
    appends a compact index of all available pages.
  • app/cli/interactive_shell/cli_help.py now runs its own LLM call grounded
    in both the docs reference and the CLI --help reference. The system
    prompt explicitly tells the model to cite doc page names, avoid inventing
    setup steps that are not in the docs, and fall back to
    https://www.opensre.com/docs when the local docs/ directory is missing
    (e.g. non-editable installs).
  • app/cli/interactive_shell/router.py adds patterns so docs-style
    questions route to the docs-grounded handler:
    configure / deploy / integrate / connect / set up phrasings,
    "what is/are the integrations/features/...",
    "does opensre support ...", "can opensre integrate with ...",
    and explicit references like "check/according to the docs".
    The bare docs/documentation token is intentionally NOT a help signal,
    so an incident description that mentions a service named "docs" still
    routes to the LangGraph investigation pipeline.

Maintainer note: docs are read at runtime from docs/*.mdx. There is no
build step and no cache file to keep in sync. Adding a new .mdx file
under docs/ makes it discoverable on the next shell turn.

Demo/Screenshot for feature changes and bug fixes -

Retrieval smoke test against the real docs/ directory (no LLM call,
runs in any environment):

$ .venv/bin/python -c "
from app.cli.interactive_shell.docs_reference import discover_docs, find_relevant_docs
pages = discover_docs()
for q in ['how do I configure Datadog?', 'how do I deploy this?', 'how do I run an RCA?', 'how do I set up grafana', 'what does masking do']:
    ranked = find_relevant_docs(q, pages, top_n=3)
    print(q)
    for p in ranked:
        print(f'  -> {p.relpath}')
"

how do I configure Datadog?
  -> datadog.mdx
  -> comparisons/Tracer_vs_Datadog.mdx
  -> integrations-overview.mdx
how do I deploy this?
  -> deployment.mdx
  -> remote-runtime-investigation.mdx
how do I run an RCA?
  -> openclaw.mdx
  -> install-enterprise.mdx
  -> install-local.mdx
how do I set up grafana
  -> grafana.mdx
  -> comparisons/Tracer_vs_Grafana.mdx
  -> multi-instance-integrations.mdx
what does masking do
  -> masking.mdx
  -> index.mdx
  -> comparisons/Tracer_vs_AWS_CloudWatch.mdx

End-to-end inside opensre shell: typing "how do I configure Datadog?"
streams an answer that quotes the API key and Application key steps from
docs/datadog.mdx and cites the page.


Screenshot 2026-05-01 at 02 15 26 Screenshot 2026-05-01 at 02 17 46

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

Problem: the interactive CLI helps users operate the agent but had no way to
ground answers in the current OpenSRE docs. Procedural questions ("how do I
configure Datadog?", "how do I deploy?") fell back to model memory and could
drift from the docs.

Approach considered:

  1. Pull docs from the hosted https://www.opensre.com/docs site at runtime —
    rejected because it adds a network dependency and a hosted-docs lag.
  2. Build a vector index over docs/ with embeddings — rejected as overkill
    for ~140 small Mintlify MDX pages and would add an embedding model
    dependency.
  3. Read docs/*.mdx from disk and rank by token overlap (chosen).

The chosen approach is local-first, dependency-free, and self-refreshing
since the file system is the source of truth.

Key components I added:

  • DocPage / discover_docs / _strip_frontmatter / _extract_title in
    app/cli/interactive_shell/docs_reference.py: walk the docs root, parse
    YAML-style frontmatter, derive a display title (frontmatter > first H1 >
    slug). Asset/image/font/style/snippet directories are skipped because
    they are not user-facing prose.
  • _tokenize / _query_tokens / _score / find_relevant_docs: simple lexical
    ranking. Stopwords are removed from the query (including "opensre" itself
    because every page mentions it). Slug and title hits weigh more than body
    hits because docs are organized by topic and the slug usually IS the
    integration name. An exact slug match (slug "datadog" vs query token
    "datadog") gets a +12 boost so the canonical setup page outranks
    comparison pages. Subdirectory pages get a small depth penalty so
    root-level integration pages outrank tutorial deep dives.
  • build_docs_reference_text: assembles the top-N excerpts and always
    appends a compact index of all available pages so the LLM can suggest
    related pages even when nothing matched the query directly. There is a
    total-character cap so the prompt never exceeds the model context.
  • _build_grounded_prompt and answer_cli_help in
    app/cli/interactive_shell/cli_help.py: build the system prompt with both
    docs and CLI references, instruct the model to cite doc page names and
    avoid inventing setup steps when the docs do not cover the question, fall
    back to a CLI-only prompt + canonical docs URL when the local docs/
    directory is unavailable.
  • Router patterns: extend _CLI_HELP_PATTERNS to catch configure / deploy /
    integrate / connect / set-up phrasings, feature-inventory questions, and
    explicit "check the docs" / "according to the docs" references. The bare
    docs/documentation token was intentionally NOT used as a help signal
    because it caused incident text mentioning a "docs" service to be
    misrouted away from the LangGraph investigation pipeline (regression test
    added: test_incident_text_mentioning_docs_still_routes_to_new_alert).

Edge cases handled:

  • docs/ directory missing (non-editable install): empty reference, prompt
    switches to CLI-only mode + canonical docs URL hint, tested.
  • query is only stopwords ("how do I"): returns empty match list, tested.
  • LLM client raises: red error printed, no session corruption, tested.
  • Reference text exceeds char cap: truncated with marker, tested.
  • Frontmatter title is quoted: surrounding quotes stripped, tested.
  • Page has no frontmatter (just # Heading): falls back to first H1, tested.

Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

)

Procedural how-to questions in the interactive shell now ground answers
in the project docs/ directory instead of relying on model memory.

- New docs_reference module discovers MDX pages, ranks by query overlap
  (slug/title weighted, exact slug match boosted, asset dirs skipped),
  excerpts the top-N pages, and always appends a compact index.
- cli_help runs its own docs+CLI-grounded LLM call, instructs the model
  to cite doc pages and avoid inventing setup steps, and falls back to
  the canonical https://www.opensre.com/docs URL when docs are missing.
- router catches more documentation-style questions ("how do I configure
  Datadog?", "how do I deploy?", "what are the integrations?", "does
  opensre support X?", queries mentioning docs/documentation).
- Tests cover discovery, ranking, excerpting, missing-docs fallback,
  prompt grounding, Markdown rendering, and router classification.

Maintainers: docs are read at runtime from docs/*.mdx — no rebuild step.
Drop a new .mdx file in docs/ and it is discovered automatically.
…cer-Cloud#1166)

The bare \b(docs|documentation)\b pattern caught incident descriptions
that incidentally mention a 'docs' service ("the database docs service
returned 502 errors..."), routing them to the docs-grounded help handler
instead of the LangGraph investigation pipeline.

Replace the bare match with phrasing-anchored patterns: docs/documentation
must appear with a question word (what/where/which do/does/are/is),
preposition (in/according to/per), or read-style verb (check/read/see/
find/search/show/reference/consult/look at). Add a regression test for
incident text that mentions docs.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 1, 2026

Greptile Summary

This PR makes the OpenSRE interactive shell documentation-aware by adding a new docs_reference module that ranks and excerpts local docs/*.mdx pages via lexical token overlap, then plumbs those excerpts into a dedicated LLM call inside cli_help.py. Router patterns are expanded so deploy/configure/integrate phrasings and explicit docs references are dispatched to the new grounded handler, with a regression guard ensuring bare incident text mentioning "docs" still routes to LangGraph investigation. Test coverage is thorough — grounding, Markdown rendering, cache lifecycle, fallback when docs are absent, and LLM failure handling are all exercised.

Confidence Score: 5/5

Safe to merge; all remaining findings are P2 style/documentation issues with no correctness impact.

The implementation is solid — caching strategy is appropriate, prompt construction is well-guarded, and the routing regression test prevents the key misrouting scenario. The three findings are all P2: a misleading module docstring about cache freshness, a private function incorrectly listed in __all__, and a depth-penalty edge case that could surprise contributors working with deeply-nested docs but does not affect the primary supported path.

app/cli/interactive_shell/docs_reference.py — module docstring and depth-penalty behaviour worth fixing before the docs/ directory grows many nested pages.

Important Files Changed

Filename Overview
app/cli/interactive_shell/docs_reference.py New module: lexical doc-ranking, excerpt assembly, and LRU-cached page discovery. Two style-level findings: misleading module-level freshness claim, and depth penalty that can silently drop partially-matching nested pages.
app/cli/interactive_shell/cli_help.py Substantial rewrite: now builds a grounded prompt from docs+CLI references and calls the reasoning LLM directly. Private helper _build_grounded_prompt incorrectly listed in __all__; otherwise clean.
app/cli/interactive_shell/router.py Adds configure/deploy/integrate verb expansions and docs-style question patterns. Regression test for the bare "docs" misrouting case is present and passes.
tests/cli/interactive_shell/test_docs_reference.py New test file with comprehensive coverage of discovery, title extraction, scoring, truncation, and edge cases. Cache-clear fixture is correctly autouse.
tests/cli/interactive_shell/test_cli_help.py Rewrites thin smoke test into a full integration-style suite covering LLM grounding, Markdown rendering, missing-docs fallback, failure handling, and loader context management.
tests/cli/interactive_shell/test_router.py Adds routing tests for docs-style questions and the critical regression test confirming incident text containing "docs" still routes to new_alert.

Sequence Diagram

sequenceDiagram
    participant U as User (shell input)
    participant R as router.py
    participant CH as cli_help.py
    participant DR as docs_reference.py
    participant FS as docs/ (filesystem)
    participant LLM as LLM (reasoning)

    U->>R: classify_input(text)
    R-->>CH: "cli_help" → answer_cli_help(question)
    CH->>DR: build_docs_reference_text(question)
    DR->>FS: _discover_docs_cached(root) [lru_cache]
    FS-->>DR: tuple[DocPage, ...]
    DR->>DR: find_relevant_docs(query, pages, top_n=4)
    DR->>DR: _score() per page (slug/title/heading/body weights + depth penalty)
    DR-->>CH: reference text (excerpts + index, ≤22 000 chars)
    CH->>CH: _build_grounded_prompt(question, cli_ref, docs_ref)
    CH->>LLM: client.invoke(prompt)
    LLM-->>CH: response
    CH->>U: console.print(Markdown(response))
Loading

Reviews (1): Last reviewed commit: "fix(cli): tighten docs router pattern to..." | Re-trigger Greptile

Comment thread app/cli/interactive_shell/docs_reference.py Outdated
Comment thread app/cli/interactive_shell/cli_help.py
Comment thread app/cli/interactive_shell/docs_reference.py Outdated
@Tracer-Cloud Tracer-Cloud deleted a comment from Ghraven May 1, 2026
…acer-Cloud#1166)

Three fixes from automated review on PR Tracer-Cloud#1172:

1. Module docstring overpromised freshness. _discover_docs_cached is
   decorated with @lru_cache(maxsize=1), so parsed pages are frozen for
   the life of the process. Edits to docs/*.mdx during a running shell
   are NOT reflected until restart. Updated the "How docs stay fresh"
   section to describe the actual behavior.

2. _build_grounded_prompt has a leading underscore (private) but was
   listed in __all__, signalling stable public API. Tests already import
   the symbol directly, so they don't need it in __all__. Removed.

3. The depth penalty in _score was applied unconditionally before the
   score>0 filter, so a legitimately-matching page nested 2+ levels deep
   with raw_score <= depth would score 0 (or negative) and be excluded
   entirely. Now the penalty only applies when there is a positive raw
   match score and the result is clamped to a floor of 1, so weak
   tutorials/ or use-cases/ matches still surface as lower-ranked
   results. Added a regression test that fails against the old behavior.
@Davidson3556
Copy link
Copy Markdown
Contributor Author

@VaibhavUpreti @davincios kindly review

@rrajan94
Copy link
Copy Markdown
Collaborator

rrajan94 commented May 1, 2026

hey @Davidson3556

One more routing case to fix before merge.
classify_input checks _is_cli_help_intent before _reads_like_investigation_request, so any docs-regex hit short-circuits the investigation pipeline. The (in|according to|per)\s+(the\s+)?(docs|documentation) pattern fires on incident phrasing like "the API errors are happening in docs" — classifies as cli_help instead of new_alert.

Can be fix by: either reorder classify_input so investigation detection runs first when the text has alert vocab / status codes / timestamps, or tighten the docs patterns to require question-shape.

…racer-Cloud#1166)

Review feedback on PR Tracer-Cloud#1172: classify_input checks _is_cli_help_intent
before _reads_like_investigation_request, so any docs regex hit
short-circuits the investigation pipeline. The bare \b(in)\s+(the)?\s+
(docs|documentation)\b pattern fired on incident phrasing like "the API
errors are happening in docs" and misrouted it to cli_help.

Split the third docs pattern in two:

- "according to (the) docs" / "per (the) docs" — citation phrasings,
  almost exclusively docs questions, kept without question-shape
  requirement.
- "in (the) docs/documentation" — too broad on its own, now requires a
  `?` reachable in the same clause ([^.!\n]*\?), so incident text
  routes to the investigation pipeline while legitimate phrasings like
  "in the docs, where is the OAuth flow?" still match.

Tests:
- regression: "the API errors are happening in docs" → new_alert
- preservation: "in the docs, where is the OAuth flow?" → cli_help
@Davidson3556
Copy link
Copy Markdown
Contributor Author

Davidson3556 commented May 1, 2026

thank you, Good catch. fixed in 21a4347. Took the second path (tighten patterns to require question-shape) over reordering.

Considered the reorder, but cli_help > new_alert priority is load-bearing for cases like "How do I run an investigation?" , a blanket reorder, or a hybrid keyed on mentions_alert_signal, would also misroute legit config questions that happen to mention an alert keyword (e.g. "how do I configure Datadog when my errors are spiking").

Pattern fix is more targeted: only the bare (in)\s+(the)?\s+(docs|documentation) was misbehaving. Split it:

  • (according to|per) (the) docs — kept as-is; citation phrasings, almost exclusively docs questions.
  • in (the) docs/documentation — now requires a ? reachable in the same clause, so "errors happening in docs" routes to new_alert while "in the docs, where is the OAuth flow?" still routes to cli_help.

Regression test added (test_short_incident_with_in_docs_phrase_routes_to_new_alert) using your exact example, plus a preservation test for the legitimate question-shaped phrasing.

@rrajan94

@Davidson3556
Copy link
Copy Markdown
Contributor Author

kindly review
@rrajan94 @VaibhavUpreti @davincios

@rrajan94
Copy link
Copy Markdown
Collaborator

rrajan94 commented May 2, 2026

LGTM 👍
the in docs fix looks good, verified locally:

  • the API errors are happening in docs → new_alert ✅
  • in the docs, where is the OAuth flow? → cli_help ✅
  • the api errors in the docs api have been spiking → new_alert ✅

thanks for the quick turnaround on the in docs fix @Davidson3556 🙌

@rrajan94 rrajan94 merged commit f1542c6 into Tracer-Cloud:main May 2, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

🧑‍💻 @Davidson3556 has entered the contributor hall of fame. Merged. Done. Shipped. Go touch grass (then come back with another PR). 🌱


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make interactive CLI documentation-aware

2 participants