Skip to content

Preserve route_clarify's structured question/options through to the UI - #1944

Merged
malibio merged 2 commits into
mainfrom
issue-1930-clarify-hitl-ui
Aug 3, 2026
Merged

malibio merged 2 commits into
mainfrom
issue-1930-clarify-hitl-ui

Conversation

@malibio

@malibio malibio commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

route_clarify's structured question/options (ADR-038's typed clarification tool) were flattened into markdown prose (format_clarification) before ever reaching the frontend, so a designed HITL clarification turn was indistinguishable from any ordinary assistant reply — no clickable options, no distinct UI state.

Test plan

  • New Rust unit tests: ClarifyPrompt round-trips through run_turn (agent_loop.rs), persists via append_assistant_message and round-trips through AiChatNode (local_agent_service.rs)
  • New Svelte test: option chips render, click invokes the send callback, disabled once no longer the latest message, absent for ordinary replies
  • bun run test: 172 files / 4112 tests passed (baseline: 171/4108, no regressions)
  • cargo test on touched crates (core, nodespace-types, agent, daemon): all passing, 0 failures
  • bun run quality:fix: clean
  • Full pre-push gate (test:all, cargo build, e2e): passed

Closes #1930

🤖 Generated with Claude Code

closes #1930)

route_clarify's question/options were flattened into markdown prose before
reaching the frontend, so a designed HITL clarification looked identical to
any other assistant reply. Carry the structured ClarifyPrompt through
AgentTurnResult, persist it on AiChatMessage alongside the flattened text,
and render it in chat-message.svelte as clickable option chips instead of
markdown bullets. Ordinary zero-tool-call replies that merely phrase
themselves as a question are explicitly out of scope — documented in code
as a model-capability question for #1922/#1927, not a rendering gap here.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR closes a real, well-scoped gap: route_clarify's structured question/options (ADR-038) were flattened to markdown before reaching the frontend, so a designed HITL clarification was visually indistinguishable from an ordinary reply. The fix threads a new ClarifyPrompt struct through AgentTurnResult/RoutingOutcome, persists question/options on AiChatMessage in both Rust copies (core, nodespace-types) plus the TS mirror, and renders clickable option chips in chat-message.svelte, gated to the latest message only.

Independently verified against the two closed issues this PR cites as out-of-scope: #1922 was in fact a Stage-2 zero-tool-call reply caused by an empty session.dynamic_context at the guidance-gate read (the resolve_query schema-retrieval-vs-injection gap, unrelated to route_clarify), and #1927 was in fact a no-op update_node call missing properties (a tool-schema/handoff gap, also unrelated to route_clarify). Neither failure mode ever goes through RouteDecision::Clarify. The code comment left at the clarify: None site in agent_loop.rs (lines ~1554-1563) accurately reflects this — the scoping decision is defensible and correctly documented, satisfying that acceptance criterion.

Overall this is a net positive, atomic, low-risk change with good test coverage on both sides of the stack. A few real gaps and one meaningful nit below, none of which should block merge, but two are worth a follow-up.

Requirements Check (issue #1930)

All four acceptance criteria are met.

Findings

Critical Issues

None.

Suggested Improvements

  • packages/desktop-app/src/lib/components/chat/chat-message.svelte:54 — When message.question is present, the bubble renders message.question instead of message.content, which silently drops the CLARIFICATION_OPENER framing sentence ("I can take that a couple of ways...") that used to precede the question in the flattened text. This is a legitimate choice to avoid duplicating the options list as both prose and buttons, but it does mean the friendlier framing text a user previously saw is now gone entirely from the clarify UI — there's no equivalent affordance (e.g. a small "Choose one:" label) marking why buttons appeared. Worth a deliberate look, even if the answer is "the buttons themselves are enough context."

  • session_already_clarified fragility carried forward, not introduced (packages/agent/src/local_agent/agent_loop.rs:529-556) — this still scans session.messages for content.starts_with(CLARIFICATION_OPENER) on the flattened string, now that a parallel structured clarify_prompt/ClarifyPrompt channel exists on the very same RoutingOutcome/turn. The PR is right not to touch this scan (out of scope, and changing intent-boundary detection is a bigger, riskier change), but now that structured data flows end-to-end for the UI, the "already clarified" check is the last remaining consumer still doing prefix-matching on prose it could instead source from the same structured signal that flows one line above it in route_clarify's handling (outcome.clarify_prompt is set at the same call site as outcome.clarification). Recommend a fast-follow issue to migrate session_already_clarified off string-prefix matching now that a structured signal exists in the same code path — not a blocker for this PR, since it doesn't make the existing fragility worse, but the duplication (clarification: Option<String> + clarify_prompt: Option<ClarifyPrompt> on RoutingOutcome, both set together, one used by history/scan, one by the frontend) is exactly the kind of two-channel drift risk that's worth closing off before a third consumer appears.

  • Content duplication/drift risk — AiChatMessage.content (flattened prose) and .question/.options (structured) are both persisted from the same ClarifyPrompt at write time (local_agent_service.rs:796-801), so there's no drift risk today — they're derived from a single source at the same instant. The risk is purely structural: nothing enforces that they stay consistent if either the flattening (format_clarification) or the structured persistence path is edited independently in the future, since they're two separate write expressions rather than one being derived from the other at read time. Low risk given current tests, but worth flagging since content is a stored field, not computed on read.

Nitpicks

  • Nit: packages/desktop-app/src/lib/components/chat/chat-message.svelte:61-74 — the disabled={!clarifyOptions.length} expression is per-button but clarifyOptions is a single derived value shared by all buttons in the loop (either [] or the full options array), so every button in a given render is uniformly enabled or disabled together — this is correct behavior, just a slightly indirect way to express a single boolean; disabled={!isLatest || !onSelectOption} would read more directly at the call site without changing behavior.
  • Nit: packages/daemon/src/services/local_agent_service.rs:1884-1891 and golden_scenario6_real_pipeline.rs — a chunk of unrelated rustfmt-only reformatting (wrapping existing match arms and Arc::new calls) is mixed into this diff. Harmless, but it's diff noise unrelated to the PR's stated purpose; consider a separate formatting-only commit next time quality:fix reformats untouched code incidentally pulled in by nearby edits.
  • Nit: packages/agent/src/agent_types.rs — ClarifyPrompt derives Clone but not PartialEq; harmless today (nothing compares two prompts), but if a future test wants to assert equality directly rather than field-by-field, it'll need adding then anyway — not worth doing preemptively.

Testing Assessment

Coverage is solid and appropriately targeted:

  • Rust: ClarifyPrompt round-trips through run_turn (agent_loop.rs — verifies both result.clarify.is_none() for ordinary replies and Some with correct question/options for a genuine clarify turn), plus persistence round-trip through append_assistant_message/AiChatNode (local_agent_service.rs — both the positive case and the negative "ordinary reply carries no clarify fields" case).
  • Svelte: 4 focused tests covering option-chip rendering, click → onSelectOption invocation with correct text, absence of chips for ordinary replies, and disabled state when isLatest is false — this directly exercises the stale-message/reopened-conversation scenario the review was asked to check, and it's correct: older messages keep their buttons visible (so history isn't destructively rewritten) but disabled makes them inert, which is the right behavior on reload since isLatest is recomputed from array position on every render.
  • All new/modified tests pass locally: 4/4 targeted Rust tests (cargo test -p nodespace-agent --lib clarify), 4/4 targeted Svelte tests, and the full bun run test suite (4111/4112 passed — the one failure, core-plugins.test.ts's viewer-resolution test, is an unrelated 5-second timeout flake on a file this PR does not touch; it passed cleanly in isolation on re-run at 2011ms, i.e., it's simply close to its own timeout ceiling under load).

No missing coverage identified for the new code paths.

Security / Performance / Dependencies

  • No new dependencies introduced.
  • No user input handling changes beyond what already existed (option text originates from the model's own tool call, same trust boundary as content today; clicking a button sends it through the existing handleSend path with the same validation as manually typed text).
  • No performance concerns — this is a small, bounded struct threaded through existing turn plumbing; no new loops, queries, or allocations of consequence.
  • bun run quality:fix surfaces one pre-existing clippy failure in packages/agent/src/bin/gen_skill_md.rs (clippy::search_is_some, introduced by a prior unrelated commit from 2026-07-12, PR Add retry-on-validation-error guidance for create_schema (closes #1872) #1898) — not introduced by this PR and not in a file this PR touches. Flagging only because it currently blocks a clean quality:fix run for anyone on this branch; worth a quick separate fix.

Type Parity Check

Backend/frontend field parity is correct and consistent across all four locations:

  • packages/core/src/models/ai_chat_node.rs: question: Option<String> (skip_serializing_if = "Option::is_none"), options: Vec<String> (skip_serializing_if = "Vec::is_empty")
  • packages/nodespace-types/src/ai_chat.rs: identical shape and serde attributes
  • packages/desktop-app/src/lib/types/ai-chat-node.ts: question?: string, options?: string[] — correct TS-optional mirror of Rust's Option/empty-vec-omitted representation
  • packages/desktop-app/src/lib/components/chat/types.ts (DisplayMessage): same shape, correctly wired through ai-chat-node-viewer.svelte's mapping (question: m.question, options: m.options)

No mismatch found.

Recommendation

Approve. This is a net positive, correctly scoped, well-tested change. The out-of-scope reasoning for #1922/#1927 is verified accurate and the code comment documenting it is defensible. The two Suggested Improvements (dropped clarification-opener framing text, and the session_already_clarified string-scan now sitting alongside an available structured signal) are worth a fast-follow but do not block this merge.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval/changes on own PR). Full findings in the review comment above.

…bled logic

- chat-message.svelte: render content minus the bullet list (not the bare
  question field) as the clarify header text, so the backend's "I can take
  that a couple of ways..." framing survives alongside the chips instead of
  being dropped.
- Simplify each option button's disabled condition to the two booleans that
  actually gate it (isLatest, onSelectOption) instead of re-deriving through
  an intermediate clarifyOptions array.
- Test: assert the opener sentence renders and the raw bullet list does not
  (chips are the only representation of the options).

Skipped per reviewer's own assessment (not made worse by this PR, both
correctly flagged as pre-existing/low-risk rather than blocking):
- session_already_clarified's string-prefix matching — recommend a
  fast-follow once more of the codebase migrates off the flattened string,
  but out of scope for this PR's fix.
- No read-time consistency check between content/question/options — single
  write site today (append_assistant_message), so there is nothing to drift
  yet; premature to add invariant-checking now.

Addresses reviewer recommendations from PR #1944 review.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed review feedback

✅ Addressed:

  • chat-message.svelte was showing the bare question field above the option chips, silently dropping the backend's opener framing ("I can take that a couple of ways..."). Changed to derive the header text from content minus its trailing bullet list instead, so the opener + question render intact and only the bullet list is replaced by chips.
  • Simplified each option button's disabled condition to the two booleans that actually gate it (!isLatest || !onSelectOption) instead of re-deriving through an intermediate clarifyOptions array (which is now removed as dead code).
  • Test updated to assert the opener sentence renders and the raw bullet list text does not (chips are the only representation of the options).

⏭️ Skipped (both correctly flagged by the reviewer as pre-existing / not worsened by this PR):

  • session_already_clarified's string-prefix matching on flattened content — a reasonable fast-follow once more of the codebase migrates off the flattened string, but out of scope for this PR's fix.
  • No read-time consistency check between content/question/options — there's a single write site today (append_assistant_message), so nothing can drift yet; premature to add invariant-checking ahead of an actual second writer.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: The changes are a small, low-risk, non-architectural follow-up — a text-derivation tweak in one Svelte component, a boolean simplification, and a corresponding test assertion. No new logic paths, no backend changes, full test suite (172/4112 frontend, all touched Rust crates) still green, quality:fix clean, and the full pre-push gate (test:all + cargo build + e2e) passed on the new commit.

Ready for merge, pending final approval.

@malibio
malibio merged commit d43c9f2 into main Aug 3, 2026
@malibio
malibio deleted the issue-1930-clarify-hitl-ui branch August 3, 2026 20:40
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.

route_clarify's structured question/options are discarded before reaching the frontend; no HITL UI state exists

1 participant