Skip to content

feat(native): composer context chips and terminal Add to chat - #80

Merged
rynfar merged 3 commits into
nativefrom
nw/77-context-chips
Nov 2, 2026
Merged

rynfar merged 3 commits into
nativefrom
nw/77-context-chips

Conversation

@rynfar

@rynfar rynfar commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Problem

The native composer had no context chips: a thread picked from the @ menu sat in the prompt as a raw t3-context:// link, and there was no way to hand terminal output to the chat. The web lets you select terminal text, choose Add to chat, and get an inline chip with a details popover whose excerpt rides along as a TerminalContextRecord.

Approach

Rules (crates/core/src/composer/context.rs, tests mirror the TS):

  • Ports lib/terminalContext.ts (label, formatTerminalContextReference, normalizeTerminalContextText, expiry, legacy \uFFFC placeholder migration), terminalContextRecord, normalizeTerminalContextForThread and the same-lines dedup from composerDraftStore.ts, the link edits of lib/composerContextReferences.ts (insert at the caret with the web's spacing, remove with one space), terminalSelectionLineRange, and deriveComposerSendState / snapshotComposerSend / buildExpiredTerminalContextToastCopy.
  • buildMessageContext now carries linked terminal records first (terminal, thread, attachments, as the web orders them). Drafts and stash entries persist linked excerpts with their text (PersistedTerminalContextDraft shape); an entry without text restores expired.
  • Emulator::selection_rows: selection rows counted from the top of the scrollback, the screen coordinates the web's getSelectionPosition uses.

Terminal entry points (crates/app/src/terminal/pane.rs, drawer.rs):

  • Right-click menu gains Add to chat first, disabled without a non-blank selection (Copy follows the same rule, as on the web).
  • The selection action menu (observeSelectionActions + showSelectionAction): after a drag selection it opens at the pointer with Add to chat / Copy; after a double or triple click it waits 500ms. Dismissed by a click outside, a key, a scroll, or blur.
  • Add to chat clears the selection, names the excerpt after the terminal (Terminal 1 lines 5-6), links it at the composer's caret (one undoable edit), and moves focus to the prompt. Adding the same lines of the same terminal twice is a no-op.
  • The pane wrapper now takes the row's height explicitly (h_flex centers children); offscreen renders gave the pane a one-row grid without it.

Chips (crates/app/src/composer/context.rs):

  • ContextChipShell look: 14px, 500 weight, 14px icon in the kind's ink; label underlines on hover when the chip opens something. The prompt is a plain textarea, so each linked context shows as a chip in a row above it, in prompt order, with a 14px X (ContextChipAction) that removes the link. Deleting the link text removes the chip; undo brings both back (records are kept until send).
  • Terminal: click opens the TerminalContextInlineChip popover (384 wide, compact): terminal icon, terminal label, Lines 5–6, and the captured text in mono on muted, scrolling at 320px. Expired: invalid ink, not clickable, the web's tooltip.
  • Thread: the thread's current title, "Open thread" tooltip and click to open; "Thread no longer available"; invalid ink with the web's copy when it belongs to another environment.
  • Links with no record: UnresolvedContextChip (dashed circle, current color, the web's tooltip).
  • Send: expired excerpts are dropped with their links; "Expired terminal context(s) omitted from message" or, when nothing is left, "won't be sent" (the web's toasts). A side chat or approval feedback refuses added context as before.

Theme (scripts/gen-theme.ts): the web solves chip inks at runtime (contextChipVariables). The generator now runs the same OKLCH lightness solve per palette (culori for parsing, the web's gamut map and contrast math), adding context_chip_terminal, _thread, _environment and _invalid. All 48 values (4 tokens × 12 palettes) match contextChipVariables run in vitest exactly.

Ledger

CO-CONTEXT: todo → partial, Native crates/core/src/composer/context.rs, crates/app/src/composer/context.rs, crates/app/src/terminal/pane.rs, Issue #77. Web source now also lists contextChipParts.tsx, composerContextPresentation.tsx, chat/TerminalContextInlineChip.tsx, lib/terminalContext.ts, lib/contextChipAccents.ts.

Gaps:

  • Chips are not inline: the native prompt is a plain textarea (gpui-component has no inline atoms), so the raw link text stays in the prompt and chips sit in a row above it. Removal is the X, or deleting the text, rather than Backspace on an atom.
  • Environment chips: the ink token is there, but native has no machine mentions yet, so nothing adds one.
  • Thread chips use the generic thread icon, not the project favicon. The unreadable check compares against the current environment only (no linked environments).
  • Review-comment chips (DF-COMMENTS), preview annotation, element and PR chips; inline image and file chips (attachments keep their strip); copy and paste of chips (composerContextClipboard).
  • Add to chat is ignored while the prompt answers a question (as on the web) or edits a queued message (native queued edits are text-only).

Evidence

Offscreen renders (snapshot renderer with #72's gestures) against the smoke server, thread "Native client showcase", and one real window by PID:

  • Terminal context menu, before / after (Add to chat first):
    /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/before-context-menu-dark.png / /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-context-menu-dark.png
  • Selection action menu after a drag: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-selection-menu-dark.png
  • Composer with a thread link, before (raw link) / after (thread and terminal chips):
    /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/before-composer-dark.png / /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-chips-dark.png
  • After Add to chat (selection cleared, chip linked): /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-chip-dark.png
  • Terminal chip popover, dark / light: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-popover-dark.png / /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-popover-light.png
  • Real window (own PID, restored draft): live terminal chip, an expired excerpt in invalid ink, and a thread chip: /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/after-real-window.png

The test terminal was closed afterwards (terminal_probe --close, new flag).

Wire (composer_probe --send --provider devReplay; --send now links a terminal excerpt and stops and archives its thread):

launched 77e8561b-5ee8-4d7d-bb5d-dd871b63ef28
recorded: UserMessage { text: "Please read [DatabaseMaintenance.test.ts](src/persistence/DatabaseMaintenance.test.ts) and compare with [Ask a Clarifying Question](t3-context://v1/thread/thread_08863efe-…) [Terminal 1 lines 3-4](t3-context://v1/terminal/terminal_22666682-…)",
  context: {"records": [{"contextId": "terminal_22666682-…", "kind": "terminal", "label": "Terminal 1 lines 3-4", "lineEnd": 4, "lineStart": 3, "terminalId": "term-1", "terminalLabel": "Terminal 1", "text": "$ cargo test -p t3-core\ntest result: ok", "version": 1},
                        {"contextId": "thread_08863efe-…", "kind": "thread", …}], "version": 1} }
stopped 77e8561b-…: true, archived: true

The excerpt's text went in as \r\n$ cargo test -p t3-core\r\ntest result: ok\n and was normalized.

Tests: crates/core/src/composer/context/tests.rs (13, including the web's legacy-id and placeholder cases), plus cases added to the draft round-trip (excerpts persist with text; a textless entry restores expired; placeholder migration), message_context (terminal records lead, unlinked ones stay home), and selection_and_scrollback (scrollback-relative rows).

Gates: cargo clippy --workspace --all-targets -- -D warnings ✓, cargo test --workspace ✓, node scripts/gen-theme.ts --check ✓, node --test parity/sync.test.ts ✓, node ../../apps/native/conformance/generate.ts --check ✓. Rebased on native at #72.

Closes #77

@rynfar rynfar mentioned this pull request Nov 2, 2026
@rynfar

rynfar commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Review findings addressed in ca721ea (rebased on native at #79). All four were real.

  1. Send ack deleted context added in flight: fixed. A send now takes the threads and excerpts its text links when it starts (begin_send) and puts them back if it fails. Landing no longer clears anything, so records added while the send is in flight stay with their links. The lifecycle is now take_for_send / restore_unsent in t3_core::composer::context (generic over excerpts and threads). a_send_in_flight_keeps_records_added_meanwhile adds an excerpt after the send took its own and asserts it is still held (the old ack cleared everything), then checks the failure path restores once. The bug lived in the GPUI composer's ordering, which has no unit harness; the test pins the core lifecycle the composer now calls.
  2. Retained excerpts outlived their undo history: fixed. Leaving a target runs reset_history with the draft prompt being saved, so threads and excerpts it no longer links go, as useRetainedContextPayloads clears on a target change (set_value clears the textarea's undo, so nothing could bring them back). It uses the draft prompt, not the visible one, so an open question's answer or a queued edit doesn't drop the draft's links. Test: a_new_prompt_history_drops_unlinked_records.
  3. Per-keystroke copies of captured output: fixed. TerminalContext::text is now Arc<str>, so draft capture, same_content and the chip row copy a pointer, and Arc's Eq fast path compares unchanged excerpts by pointer. is_expired no longer allocates (covered in the normalization test).
  4. Chips not keyboard reachable: fixed. The details trigger and an available thread chip are tab stops with a 2px inset ring in the chip's ink. The X is a tab stop with a 1px ring. Enter and Space click them through GPUI's keyboard click. Escape closes an open popover from the chip or from the prompt. Offscreen evidence after Escape (popover closed, chip keeps its keyboard ring): /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/review-escape-closes.png. The harness sends no key-up, so it can't show Enter reopening it.

Gates: clippy ✓, cargo test --workspace ✓ (832 core), gen-theme ✓, parity sync ✓, conformance ✓.

@rynfar
rynfar force-pushed the nw/77-context-chips branch from 131d733 to ca721ea Compare November 2, 2026 22:15
@rynfar

rynfar commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Follow-up fixed in 2b60e34. Escape handling moved from the chip trigger onto the chip row (their common ancestor), so Escape closes the details from the trigger or the X. Escape from the prompt still goes through the composer's Escape action. Checked offscreen: click the chip, Tab to the X (popover open, /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/review2-x-open-crop.png), Escape (popover closed, X keeps focus, /private/tmp/claude-501/-Volumes-DevSSD-t3code-native/15deb025-b650-4cf9-a1aa-1cc16b006f6d/scratchpad/shots77/review2-x-escape-crop.png). Clippy and cargo test --workspace pass; already on native at #79.

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