Skip to content

fix: render node rationale in query, get_node and explain output (#2821) - #4576

Open
yotamleo wants to merge 4 commits into
Graphify-Labs:v8from
yotamleo-org:fix/2821-render-rationale
Open

yotamleo wants to merge 4 commits into
Graphify-Labs:v8from
yotamleo-org:fix/2821-render-rationale

Conversation

@yotamleo

Copy link
Copy Markdown
Contributor

Fixes #2821. Supersedes #2830, which has been conflicting with v8 since 2026-08-20; this is that change rebased onto current v8, with the original commit and author kept.

What

graph.json stores a rationale on semantic nodes, but query, get_node and explain never printed it. This adds it, opt-in:

  • CLI: graphify query ... --rationale and graphify explain ... --rationale
  • MCP: include_rationale on query_graph and get_node (strict boolean)
  • query appends WHY <text> after the NODE line; explain and get_node print Rationale: <text>
  • Default output is unchanged without the flag.

The text is untrusted corpus/LLM output, so it goes through sanitize_rationale() in graphify/security.py:

  • control characters, C1 (\x80-\x9f), bidi overrides and zero-width characters stripped
  • single-line mode for query (cap 512), multi-line for explain/get_node (cap 2048), explicit ... marker when capped
  • in detail mode, continuation lines are indented so a rationale cannot pose as a sibling field (Degree:, Node:)
  • non-string values are ignored

Changes on top of #2830

The first commit is #2830 rebased onto v8 (conflicts in cli.py, serve.py, tests/test_explain_cli.py, tests/test_serve.py; the new --seed, _resolve_single_node and attrs suffix are preserved). The second commit is review hardening:

  • forged-field and line-separator handling described above
  • the shared _CONTROL_CHAR_RE is left as on v8; the C1 range applies to rationale only, so sanitize_label behaviour does not change
  • --rationale listed in the main help text

Before / after

Fixture: a small graph with rationale on two semantic nodes.

Before (v8), graphify query "halt narrowing token" --graph g.json:

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

graphify explain "completes-ticket line and Ticket coverage" --graph g.json --rationale prints no Rationale line (the flag is ignored).

After, with --rationale:

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'.
  Rationale: The merge script closes the ticket only on --jira-transition because a default closes multi-PR tickets early (HIMMEL-3059).

Without the flag the output is identical to v8. A node whose rationale contains ESC, BEL and \x9b bytes, a newline and a ~1200-char tail prints with the control bytes stripped; the query WHY is collapsed to one line.

Tests

  • New tests in tests/test_security.py, tests/test_query_cli.py, tests/test_explain_cli.py, tests/test_serve.py; the flag/kwarg does not exist on v8, so they fail there.
  • pytest tests/: 8418 passed, 14 skipped, 1 deselected.
  • The deselected test, tests/test_extract.py::test_collect_files_from_dir, is an environment artifact, not related to this diff: it fails only when the checkout sits under a hidden directory (here .worktrees/), because collect_files skips hidden path components. It passes on a clean v8 copy outside a hidden path, and this PR does not touch extract.py.
  • ruff check . clean. pyright graphify reports 498 errors on both v8 and this branch (no new ones).

hopstreax and others added 2 commits October 20, 2026 02:51
…haracters (Graphify-Labs#2821)

Review follow-ups on the rebased Graphify-Labs#2830:
- indent continuation lines in explain/get_node so a rationale cannot pose as a sibling field
- strip bidi/zero-width characters; treat U+2028, U+2029 and NEL as line breaks
- keep the shared control-char regex unchanged; the C1 range applies to rationale only
- ignore non-string rationale values; clamp a negative max_chars
- list --rationale in the main help text

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@yotamleo
yotamleo requested a review from safishamsi as a code owner October 20, 2026 01:10
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 20, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @yotamleo. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 6 changed function(s) (3 sampled, not proven, 3 not verified).

Formal verification. PR-changed functions: 3/6 verified (0 proven, 3 may-equivalent, 0 distinguished) · 3 not verified (3 vacuous).

Not verified on this run: dispatch\_command (vacuous: never exercised), \_build\_server (vacuous: never exercised), \_render\_query (vacuous: never exercised).


Graphify review — findings

Adds an opt-in --rationale flag to graphify query and graphify explain that shows a node's stored rationale. Query appends it as a single-line WHY suffix capped at 512 chars, and explain prints an indented Rationale: block that keeps paragraphs up to 2048 chars; without the flag, output is unchanged. Introduces sanitize_rationale and indent_continuation, which strip control, bidi and zero-width characters, mark truncation with ..., and indent continuation lines so multi-line text can't pose as a sibling field, while sanitize_label keeps its existing behaviour. Threads include_rationale through _subgraph_to_text and _query_graph_text into the rendered output and documents the flag in the help text.

Review partial — this diff was larger than one review pass covers, so later files were not reviewed; some findings may be missing.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2377 functions depend on the 689 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: to_json() — 87 callers, 11 callees
  • worse: dispatch_command() — 5 callers, 136 callees
  • worse: main() — 111 callers, 3 callees
  • worse: _query_graph_text() — 49 callers, 5 callees
  • worse: _subgraph_to_text() — 31 callers, 4 callees
  • worse: _build_server() — 6 callers, 19 callees

Verification — 2377 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1719 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

96 of 432 test file(s) selected (22%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_benchmark_real_query.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_callflow_html.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cloud_cta.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_community_labels_skill.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_confidence.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_dedup.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_devin.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_explain_cli.py — impact, changed-test
  • tests/test_explain_same_file_tie.py — impact
  • tests/test_export.py — impact
  • tests/test_export_control_characters.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_god_nodes_cli.py — impact
  • tests/test_god_nodes_exclude_hubs.py — impact
  • tests/test_hollow_chunks_arm_shrink_guard.py — impact
  • tests/test_hook_guard_token_match.py — impact
  • tests/test_hook_out_of_project_paths.py — impact
  • tests/test_hook_strict.py — impact
  • tests/test_html_script_breakout.py — impact
  • tests/test_html_theme.py — impact
  • tests/test_hyperedge_roundtrip.py — impact
  • tests/test_hypergraph.py — impact
  • tests/test_incomplete_build_guard.py — impact
  • tests/test_ingest.py — impact
  • tests/test_ingest_url_type.py — impact
  • … and 46 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

Formal verification

No difference found (not proven): No behavior difference found in \_run\_cli (not a proof).

The verifier ran both versions of \_run\_cli on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify dispatch\_command.

The verifier did not have enough to check dispatch\_command, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 40 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly SystemExit — names the real obstacle, not a sampling gap)

Could not verify: Could not verify \_build\_server.

The verifier did not have enough to check \_build\_server, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 40 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly ValueError — names the real obstacle, not a sampling gap)

No difference found (not proven): No behavior difference found in \_query\_graph\_text (not a proof).

The verifier ran both versions of \_query\_graph\_text on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify \_render\_query.

The verifier did not have enough to check \_render\_query, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 223 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

No difference found (not proven): No behavior difference found in \_subgraph\_to\_text (not a proof).

The verifier ran both versions of \_subgraph\_to\_text on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

· 1 grounded finding(s) anchored inline below; 5 more finding(s) on lines outside this diff (see the check run).

Comment thread graphify/serve.py Resolved
@yotamleo

Copy link
Copy Markdown
Contributor Author

Replying to the review's findings that sit outside the diff (the coupling list in the check run):

  • to_json() and main(): neither is modified here. to_json is only called from the new tests, and main() reaches the new code through dispatch_command, so their caller counts are inherited from existing callers.
  • dispatch_command(): gains one --rationale branch each in query and explain and two usage strings. No new callees beyond the sanitizers.
  • _query_graph_text() and _render_query(): one new keyword argument (include_rationale, default False), forwarded unchanged. Callers that do not pass it behave as before.
  • _subgraph_to_text(): same optional keyword, default False; the new line is only built when it is set, so default output is byte-identical (covered by the "without --rationale" assertions in tests/test_query_cli.py and tests/test_explain_cli.py).
  • _build_server(): two optional include_rationale properties in the tool schemas and the matching reads in _tool_query_graph / _tool_get_node, accepted only when it is exactly true.

No signature changes for existing callers, so no change pushed. The "docs that may be stale" entry points at an older plan file that does not mention these flags.

Resolves the serve.py and test_security.py conflicts: keeps v8's _query_name_refusal block and redact_secrets tests alongside the include_rationale kwarg and sanitize_rationale tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
yotamleo added a commit to yotamleo-org/Himmel that referenced this pull request Oct 20, 2026
…and drop append-rationale.py (#3323)

## Summary

`graphify query` and `graphify explain` now print a node's rationale
natively with `--rationale` (upstream Graphify-Labs/graphify#4576,
merged-clean). Until a release ships it, himmel carries it on the
`yotamleo-org/graphify` fork, installed by commit sha the way qmd is.

- Fork `himmel-main` is `02e7e4a9b2eeb4570fa2e926a5f44bdebde4690d`: fork
PR 14 lands the #4576 change, fork PR 15 stamps the build
`0.9.93+himmel.1`. Both were judged and merged through the fork-adapter
flow.
- `scripts/lib/graphify-bin.sh`: `_graphify_fork_ref` (sha pin,
`GRAPHIFY_FORK_REF` overrides), `_graphify_version` returns the stamp,
`_graphify_spec` builds `graphifyy[extras] @
git+https://github.com/yotamleo-org/graphify@<sha>`. `GRAPHIFY_VERSION`
set still selects the PyPI `==` form. `_graphify_version_lt` sorts
`+himmel.N` newer than the same base, and the installed-version regexes
keep the `+himmel.N` part.
- `scripts/upstreams.json`: graphify row gains a `fork` block and drops
`version_pin` (qmd precedent), so the drift bumper reports it SKIP
(manual).
- `scripts/graphify/leg-graph.sh` passes `--rationale` right after the
question for `query` and `explain`; `rank-why-how.py` reads the ` WHY `
suffix on the NODE line. `append-rationale.py` and its rows are removed.
- Pin readers follow the stamp: `scripts/cloud/setup-env.sh` (installs
the fork ref), `scripts/cloud/parity-probe.sh`,
`scripts/vm/upgrade-path-guest.sh`.

## Proof

- RED first: `test-leg-graph-rationale.sh` showed 9 failures before the
wrapper change; the new `test-graphify-bin.sh` rows failed 7 against the
old resolver.
- Real install from the git ref: `_graphify_stage_install` on the real
network installed `graphify` and `graphify-mcp`; `graphify --version`
reads `0.9.93+himmel.1`, and the mcp and openai imports work. The fork's
own `ci.yml` never runs on PRs into `himmel-main`, so this and the
resolver tests are the proof the fork build installs.
- On `scripts/graphify/fixtures/rationale-graph.json` with the real fork
build: `test-leg-graph-rationale.sh` ALL PASS (T5: 2 of 5 answers carry
the reason text) and `test-anchor-seeds.sh` ALL PASS.
- Impacted suites run green: `test-graphify-bin.sh`,
`test-leg-graph.sh`, `test-leg-graph-rationale.sh`,
`test-anchor-seeds.sh`, `test-semantic-seeds.sh`,
`test-retrieval-eval.sh`, `cloud/test-setup-env.sh`,
`cloud/test-parity-probe.sh`, `test-check-plugin-drift.sh`,
`upstreams/test-apply-drift-bump.sh`, `upstreams/test-resync-fork.sh`,
`upstreams/test-drift-fix-cadence.sh`,
`upstreams/test-upstream-watch-cadence.sh`,
`upstreams/test-apply-tool-upgrade.sh`, `ci/test-fork-drift-issue.sh`,
`vm/test-upgrade-path-stage.sh`, `test-himmel-update.sh`, `-versions`,
`-drift`, `-tools`.

## Residual risk

A graphify older than the pin (the station has 0.9.92) does not know
`--rationale`. `leg-graph.sh` probes `graphify --help` and omits the
flag then, so answers lose the rationale text (the old behaviour)
instead of failing, until the station updates to the fork build
(`himmel-update --only graphify`, per the station-follows-main rule).
`test-leg-graph.sh` T1b covers the degrade.

Touches `scripts/lib/` (a `ci-trust-paths.txt` pattern): trust-reviewed
GO.

## Ticket coverage

- Comment on upstream issue Graphify-Labs/graphify#2821: done
- Sync the yotamleo-org/graphify fork with upstream and land #4576 on
`himmel-main` through the fork-adapter flow: done
- Bump himmel's graphify pin to the fork commit: done
- Make the wrapper pass `--rationale` and remove `append-rationale.py`
with its test rows: done
- Prove it on `scripts/graphify/fixtures/rationale-graph.json`: done

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Merge upstream v8 26818d0 into
fix/2821-render-rationale without rebasing or rewriting the PR branch.
Preserve exact-case node lookup, parallel-edge evidence, standalone CLI
help and both sides' regression tests while retaining opt-in rationale.

Adapt the rationale tests to call real registered MCP 1.x and 2.x
handlers and inspect the tools' wire-schema aliases. Production MCP
registration remains unchanged. New standalone-help assertions failed
before restoring --rationale in the shared usage strings.

Verification: full pytest 9173 passed, 18 skipped, 1 xfailed; MCP 1.30
protocol checks 3 passed; ruff check . and git diff --check passed.
Pyright was not re-run: classifier-denied under pytest-only operator
authorization. Its earlier direct run reported 417 errors across 96
files, not proven against a baseline. Verification scope was explicitly
narrowed to pytest plus ruff; no clean type-check claim is made.

Platforms tested: linux
Security reviewed: manual — bounded rationale output, terminal controls,
strict MCP booleans, continuation indentation and unchanged default output
Co-Authored-By: Claude Code <[email protected]>
@yotamleo

yotamleo commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Note

🤖 gpt-6.1-sol responding on behalf of yotamleo

Merged current v8 into this branch without rewriting history (ec6b6858416eb637b854510144a4b5ecb25f6db3), preserving opt-in rationale and newer upstream behavior. The PR is mergeable; all CI checks and review gates now pass, including Python 3.10–3.14 and install smoke tests.

Local verification: 9,173 passed, 18 skipped, 1 xfailed; MCP 1.30 protocol checks: 3 passed; Ruff clean. Rationale tests now support the MCP 1.x and 2.x handler/schema APIs.

Pyright reports 417 errors across 96 files, unchanged between runs but not compared against an upstream baseline. Bandit findings remain tolerated by upstream CI; pip-audit reports no known vulnerabilities in CI's installed default environment.

@CBEPX

CBEPX commented Nov 15, 2026

Copy link
Copy Markdown

Agent work: stage finished

Client: codex · Session: graphify-query-rationale-20261115 · Claim: dbcc9ebb-2e6f-4eb3-98e2-799a0d7aef74

Mode: write · Started: 2026-11-15T13:44:11+03:00 · Updated: 2026-11-15T13:45:02+03:00

Stage: Recovery: Claim projection returned HTTP404; no dependent writes or background requests started. Stop and release own reservation.

Scope in this repository: .superpowers/sdd/query-rationale-20261115

This branch has not been deployed

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants