Skip to content

feat(tui): carry link destinations through rendering as OSC 8 hyperlinks - #703

Merged
mohitranka merged 1 commit into
mainfrom
feat/tui-hyperlinks
Sep 15, 2026
Merged

mohitranka merged 1 commit into
mainfrom
feat/tui-hyperlinks

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Links in the transcript were parsed and thrown away: Tag::Link kept the
underline and dropped dest_url, so link text looked clickable and did
nothing. Destinations now travel beside the rendered rows as column ranges
and are attached to the ratatui buffer's cells after layout, so the terminal
owns the click.

Why the destination cannot live in the text

An OSC 8 sequence is zero columns wide on screen, but its bytes are ordinary
characters to Span::width. A destination written into a Span would be
measured as columns, and every wrapped row after the link would land in the
wrong place - a direct hit on the 80x18 layout invariant (§4.10). Keeping the
destination out of the text also keeps it out of the copy path: Line rows are
what selection and visible_conversation_copy_rows read.

So the carrier is a column range per row (links::HyperlinkLine), remapped at
every step that moves text: markdown wrapping (a wrapped link becomes a range on
each row it occupies), the transcript assembly, the plan dock swap, and the
assistant message indent.

The destination is model output going into an escape sequence

FORGE-DESIGN.md §9 already states the rule for OSC 9 notifications - "Text
is sanitized, not trusted... an embedded ESC or BEL would terminate the
sequence early and leave the remainder to be read as terminal commands" - and
OSC 8 inherits it. Three layers:

  • Policy (links::destination_for): http/https only, no control bytes,
    no whitespace, a real host, 2 KiB cap. Rejecting rather than repairing: a
    control byte in a URL is corruption or an injection attempt, and "this is not
    a link" is the honest reading of both.
  • The writer re-checks it (mark_buffer_hyperlinks). TerminalHyperlink is
    constructible by any caller, and this is the function that writes bytes to the
    terminal, so a struct cannot be the only gate.
  • The underline is a promise: a refused scheme renders as plain text with no
    underline, because an affordance that cannot be acted on is worse than none.

The sequence is emitted only for terminals known to render it, resolved once per
process from TERM_PROGRAM, and any multiplexer disqualifies (tmux before 3.4
cannot forward hyperlinks, and TERM_PROGRAM names the outer terminal inside one
either way). Unknown terminals degrade to exactly today's rendering. No hint row
advertises a click, because the binding's availability is not Forge's to promise.

Deliberately not in this change

  • No browser launch. Nothing hands a URL to a browser.
  • No bare-URL scanning. A link exists because the author wrote markdown;
    a bare URL in tool output stays prose. No detection, no false positives.
  • Streaming tails and table cells carry no link columns yet. A link in a
    still-streaming answer joins when the block settles; a table cell link keeps
    its underline, because render_table re-lays cells out against its own
    geometry. Both are follow-ups, not regressions.
  • A keyboard link picker was built and cut during review: it duplicated the
    click for terminals where the click works, for the cost of a command surface.

Verification

cargo fmt --all -- --check and cargo clippy --workspace --all-targets --locked -- -D warnings clean; the forge-tui lib suite is green apart from five
failures that reproduce on the pre-change tree (git stash on the base commit),
three of them the sandbox-unavailable background-task tests that CI exercises
with bubblewrap.

New tests pin the claims rather than the implementation:

  • the end-to-end one: a link in an answer, through the real model -> markdown ->
    assembly -> paint path, lands as an OSC 8 sequence over exactly the cells the
    label occupies;
  • link columns follow a wrap, and two destinations on one row stay separate;
  • a control-byte or BEL-carrying destination produces no escape bytes even when
    the struct is built directly;
  • marking_stops_at_the_pane_edge, so a clipped link cannot push an escape into
    the next row;
  • with the capability off, no escape byte is written anywhere.

FORGE-DESIGN.md §9.4 gains the three rules this ships.

Known unknown

The load-bearing assumption was not verified outside this container: that
Cmd-click on an OSC 8 link still reaches the terminal while Forge holds
EnableMouseCapture (app/shell.rs). Worth one manual check in Ghostty or
iTerm2 before merge.

Links in the transcript were parsed and thrown away: `Tag::Link` kept the
underline and dropped `dest_url`, so the text looked clickable and was
inert. Destinations now travel beside the rendered rows as column ranges and
are attached to the ratatui buffer's cells after layout.

An OSC 8 sequence is zero-width on screen but its bytes are characters to
`Span::width`, so a destination inside span content would be measured as
columns and shift every wrapped row after it. Keeping the destination out of
the text also keeps it out of the copy path.

The underline is now a promise: only destinations that may be emitted get
it (`http`/`https`, no control bytes, a real host, under the length cap).
The writer re-checks that policy, so no caller can put a `BEL`-carrying
destination into an escape sequence by building the struct directly, and the
sequence is emitted only for terminals known to render it, resolved once per
process, with any multiplexer disqualifying.

Streaming answers and table cells do not carry link columns yet; the
keyboard picker and the remaining panes follow.
@mohitranka
mohitranka merged commit a974c09 into main Sep 15, 2026
3 checks passed
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.

1 participant