Skip to content

rationale attribute is never rendered by query / get_node — the node that answers is returned label-only #2821

Description

@alemacca1988

Version: 0.9.42
Affects: MCP query_graph / get_node, and graphify query / explain — any corpus with a semantic layer.

Summary

semantic_cleanup promotes sentence-like rationale nodes into a rationale attribute on the target node (_append_rationale_attr, semantic_cleanup.py:324), exactly as references/extraction-spec.md prescribes. Nothing ever reads it back.

On a 3-document markdown corpus this captured 50 of 75 nodes (67%) carrying ~8,900 characters of prose. None of it is reachable through any read surface.

This is not #2293. That issue is about scoring: a "why" question cannot reach the node, because _score_query never matches against rationale. This one is about rendering: the node is reached — it is the top seed — and its rationale still does not appear in the output. The two are complementary halves of the same attribute being write-only, and fixing #2293 alone would surface more nodes whose answers are still invisible.

Where it stops

  • _subgraph_to_text (serve.py:1028) emits only label, source_file, source_location, community (plus an optional learning= suffix). This is the single renderer behind both MCP query_graph and the CLI, which imports _query_graph_text from serve (cli.py:947).
  • _tool_get_node (serve.py:1698) emits label / ID / Source / Type / Community / Degree only.
  • No flag, env var or MCP parameter includes it.

Reproduction

graphify extract ./docs --backend <b>
graphify query "<question whose answer node has a rationale>" --budget 6000

The node is listed. Its rationale is not, at any budget.

Measurement

Four questions, budget 6000, on a corpus where the seed node was correct every time:

seed correct rationale shown exact fact recovered
all 4 questions yes no 0 / 4

Concretely — the graph contains, and never shows:

  • node Clustering per deposito → rationale: "cannot work on ORIGINALS_NORMALIZED as it stands, because timestamps and coordinates are missing"
  • node DIFETTO — la normalizzazione distrugge l'EXIF → rationale: "probable cause: Pillow img.save() does not preserve EXIF by default and OpenCV always loses it"

A caller asking why clustering is blocked gets the label of the node that answers, and nothing else.

Cost, and why opt-in

I simulated the rendering non-invasively (same query, rationale appended from graph.json) to size it before proposing anything:

output tokens
current 10,994
with rationale rendered 15,901
+45%

That is a real cost on a token-budgeted surface, so unconditional rendering seems wrong.

Suggested fix

An opt-in:

  • include_rationale: bool on the query_graph and get_node MCP tools;
  • --rationale on the CLI;
  • truncated per node (a rationale_chars cap, or charged against the existing token budget) so a few long rationales cannot crowd out the traversal.

Ordering it after the NODE line, as a WHY … continuation, keeps the existing line format stable for anything parsing it.

Happy to send a PR if this shape is agreeable — and to rebase it on the #2293 scorer change if that lands first, since the two are complementary rather than overlapping.

Activity

  1. yotamleo commented on Oct 17, 2026

    @yotamleo
    Contributor

    Still reproducible on 0.9.91, and we have two data points to add.

    Where it stops, 0.9.91 line numbers (the numbers in the issue body are from 0.9.42):

    • serve.py _subgraph_to_text (def at 1112). The NODE line (1193 to 1200) is built from label, source_file, source_location, community plus the optional learning= and attrs= suffixes. rationale is read in this file only by the scorer (_node_rationale_text, 402; the bonus, 701 to 704), never by the renderer.
    • cli.py explain (the Degree: / Connections block, about 2100 to 2178) prints node fields, Lesson: lines and connections. grep -n rationale cli.py has no hits.

    Repro (the node carries rationale in graph.json, so graphify query / graphify explain should be able to print it):

    graphify query "why does halt need no token"
    graphify explain "Halt/narrowing needs no token (fail-safe asymmetry)"
    

    On a graph holding this node:

    {"id": "retask_halt", "label": "Halt/narrowing needs no token (fail-safe asymmetry)",
     "source_file": "docs/internals/retask-channel.md", "_origin": "semantic",
     "rationale": "An attacker who can only make the child do less has a mild DoS, not a compromise; a stood-down worker must not reason a halt away as 'not genuine user input'."}

    Today query prints the node as label only:

    NODE Halt/narrowing needs no token (fail-safe asymmetry) [src=docs/internals/retask-channel.md loc=None community=...]
    

    and explain prints the label, source, type, community, degree and connections, with no rationale.

    Desired, one bounded line right under the NODE line and under explain's node header:

    NODE Halt/narrowing needs no token (fail-safe asymmetry) [src=docs/internals/retask-channel.md loc=None community=...]
      WHY An attacker who can only make the child do less has a mild DoS, not a compromise; a stood-down worker must not reason a halt away as 'not genuine user input'.
    

    Real-corpus size, since #2821 flagged cost: on our repo graph, 825 of 2170 nodes (38 %) carry a rationale. It is the only place the why of a design decision is stored, so a "why" question returns the right node and no answer.

    Sanitization is required, not optional. rationale is LLM-extracted free text from the corpus, so it is untrusted and flows into an agent's context. Render it through sanitize_label-style handling at minimum:

    1. strip control characters (C0, DEL, C1, and newlines, so one rationale stays one line and cannot forge a following NODE / EDGE line);
    2. cap the length per node. sanitize_label caps at 256 (security.py, _MAX_LABEL_LEN); we use 400 and it keeps the sentence intact in our corpus. Add a visible ... on truncation.

    Without this, a rationale that contains \nEDGE ... would inject fake graph lines into the output that agents parse.

    Workaround we carry (so you know the shape that works): a stdin filter that matches each NODE line to a graph node by label plus source file and appends the rationale, control characters stripped, 400-character cap. It exists only because the renderer drops the field. If #2821 lands we delete it.

    Happy to test a fix against our graph.

  2. yotamleo commented on Oct 20, 2026

    @yotamleo
    Contributor

    A rebased fix for this is up as #4576 (supersedes #2830, which has conflicted with v8 since 2026-08-20). It adds an opt-in --rationale flag to query and explain, and include_rationale to query_graph and get_node. The text goes through a sanitizer and is capped.

    On our own repo graph 825 of 2170 semantic nodes carry a rationale and none is printed today, so every "why" question returns the right node without the reason. We use it from the rebased branch; upstream CI is green. Review welcome.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions