Skip to content

fix(chat): keep model output from rewriting the terminal or a pasted command line - #983

Draft
rominf wants to merge 1 commit into
mainfrom
fix/chat-paste-safe-model-values
Draft

rominf wants to merge 1 commit into
mainfrom
fix/chat-paste-safe-model-values

Conversation

@rominf

@rominf rominf commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rocm chat prints text a model chose: its reply, tool output, error messages that quote either, and command lines built from the values in its tool calls (the advanced manual command: line, the approval card, the request plan's tool_call: lines). Two gaps let that text decide what the user sees or runs:

  • Prose was printed verbatim. A reply could hide part of a line (SGR conceal), retitle the window, write the clipboard or move the cursor. Tool arguments were already refused for control and format characters, but other invisible characters (tag characters, Hangul fillers, variation selectors) got through.
  • Command lines are POSIX-quoted, which Windows shells read differently. That quoting is right for sh, bash and WSL. PowerShell ends a single-quoted string at an embedded quote, and cmd.exe gives single quotes no meaning at all, so & | < > ^ %, " and (under delayed expansion) ! keep their meaning inside them. A value the model chose could therefore add a command to a line the user pastes on Windows.

Fix

Tool-call values. Every string a tool call carries, from the model or built from what the user typed, is refused if it holds something a terminal would not show as it is: a control or escape sequence, a format character such as a bidi override, a line separator, or another default-ignorable character such as a tag character. The exception is the characters an emoji is spelled with: a single zero-width joiner or non-joiner, or emoji or text presentation selector, directly after a drawn character, and a joiner directly after such a selector (❤️, 👩‍💻, 🏳️‍🌈).

Values the model chose for a command line the CLI shows (the approval card, the advanced manual command: line, the install preview, and the plan's tool_call: lines) are held to more:

  • they may not carry a character a Windows shell reads as syntax even inside single quotes (', typographic single quotes, & | < > ^ % " !);
  • a model-requested rocm serve may only use long options rocm serve has, must name exactly one model, and may only use . _ - / : @ + \, letters and digits in its values (at most 256, not starting with -);
  • they are refused for any invisible character, a single joiner or presentation selector included.

Calls the CLI builds from the user's own request are not held to these: a folder in a request the CLI recognises itself (install therock into <folder>), or in a dashboard slash command, is rendered as typed. A folder the user names in other words reaches the CLI through the model's call and is held to them like any value the model chose.

Each call is checked before it runs, and a plan before it is shown; a read-only check earlier in the same reply may already have run. The refusal names the value and the command to run with it (only words that are real rocm subcommands, so the command named exists), and says nothing was changed and no command was offered to paste for that call. In rocm chat --prompt nothing from the turn reaches stdout.

Plans. The plan natural_language_plan returns for a request the model wrote is checked the same way before it is shown, because the planner turns words of that request into the values on its tool_call: lines. The tool reports those argvs beside the text, and a result whose argvs cannot be read is refused. The planner's own <model> placeholder passes only as the model of a rocm serve.

Terminal output. Prose that cannot be refused (the reply, the model name the server reports, tool results) is printed by rocm chat --prompt with escape sequences (CSI, OSC, DCS, SOS, PM, APC, and their 8-bit forms) and invisible characters removed and line breaks kept, so concealed text is shown. A sequence left unterminated ends at its own line, so the lines printed after it, including the CLI's own, survive. The error report every rocm command prints goes through the same cleaning. GGUF architecture names and listed file names are held to the same character set.

The dashboard agent's tool calls go through a new required RocmToolExecutor::execute_model_call, so they get the model-chosen rules; dashboard slash commands keep execute.

The assistant's built-in guidance (skills/rocm-cli-assistant/SKILL.md, compiled into the chat system prompt) states these rules, so the model hands such a value back to the user instead of rewriting it. The ASCII-output contract's exemption note for terminal.rs is reworded for the wider set of characters that module names.

Tests

  • E2E, default mock lane (no GPU):
    • chat-20 (@id:chat-cli-reply-escapes-stripped): escapes are removed, concealed text is shown, and the line after an unterminated sequence is kept.
    • chat-21 (@id:chat-assistant-value-unsafe-to-paste-refused): an install_sdk folder with & is refused, nothing reaches stdout, and the user's own rocm install sdk --dry-run into the same folder is accepted and names it as one argument, which shows the remediation the refusal names works.
    • chat-22 (@id:chat-assistant-plan-unsafe-to-paste-refused): a plan for the assistant's request into that folder is refused.
    • runtime-lifecycle-32 (@id:runtime-lifecycle-error-report-carries-no-escape): rocm runtimes activate with a runtime name carrying escape sequences fails, its error report names the runtime with the sequences removed and holds no control character, and no runtime is active.
  • Unit: the sanitizer, escape forms, the emoji shapers and invisible characters in rocm-core's terminal.rs, with exact-output and property tests; the paste-unsafe set, the serve rules, the placeholder slot, the plan check (including unreadable argvs and an unsafe plan that is not the last) and planned_args pinned to the plan's tool_call: lines in tool_policy.rs and freeform_planner.rs; model-versus-user origin and check-before-run in providers.rs (the chat turn against a scripted local model, which also checks no folder, server or runtime was added) and dash_seam.rs (refused and safe model calls through the real executor); the error report, which also passes with RUST_BACKTRACE=1.
  • The serve option and character rules and the invisible-character refusal for model-chosen values have unit tests only. They take the same refusal path chat-21 and chat-22 exercise end to end, with a different rule firing.
  • Each new test was shown to fail with its target broken: for example, removing the error-report cleaning fails runtime-lifecycle-32, treating every dashboard model call's result as a plan fails the safe-call seam test, and dropping the selector-then-joiner rule fails the emoji rows.

Verified locally (Linux)

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo xtask check-architecture-doc, cargo xtask check-crate-edges, cargo xtask check-module-size.
  • cargo test --workspace --all-targets --no-fail-fast: all pass except tests in code this change does not touch that fail on this host for its own reasons: service_control::tests::stop_of_a_container_engine_is_unconfirmed_when_the_engine_stop_fails (docker here is a podman shim) and two build_script tests that create a scratch git commit under this host's signing configuration.
  • cargo xtask e2e for chat-20..chat-22, therock-next-15 and the runtime-lifecycle scenarios: all pass except runtime-lifecycle-12 (@requires-gpu @requires-engine:vllm), which this host ran against a Lemonade service; that scenario's own comment notes it cannot pass there, and this change touches no runtime activation or service restart code.

Not verified

  • Windows: the new scenarios and the paste-safety rules have not run on Windows; CI's Windows job runs the unit tests.
  • Pasting the refused and accepted lines by hand into a real PowerShell or cmd.exe.
  • cargo xtask smoke.

Behaviour change

  • rocm chat --prompt output and every rocm error report no longer contain terminal escape sequences or invisible characters; concealed text is shown, and a lone carriage return or line/paragraph separator becomes a line break.
  • A chat or dashboard assistant, or the provider planner, can no longer propose a command whose model-chosen value contains ', a typographic single quote, & | < > ^ % " !, or an invisible character. The request is refused with a message telling the user to run the command with that value themselves.
  • A model-requested serve is limited to the options and character set above.
  • A plan the assistant requests is refused when it would show such a value.
  • Tool arguments containing a default-ignorable character are refused, except the emoji shapers above in a value the CLI built from the user's own request.
  • These rules are documented in the README chat section, rocm chat --help, docs/llm-tool-use.md, docs/testing.md and docs/manual-testing.md.

Follow-ups

  • strip_terminal_control_sequences, used for the provider planner's failure note, still uses a narrower predicate than the sanitizer; the note is sanitized again when chat prints it.
  • The serve allow-list accepts every option rocm serve has, including --api-key and --engine-env; the approval card shows them.
  • The dashboard transcript still shows invisible characters other than controls (ratatui drops only those); fixing it needs a shared leaf crate.
  • One-line fields such as a served model name are cleaned with the prose rules, which keep line breaks.
  • A read-only rocm_command is held to the paste rule though no command line is shown for it.
  • Outside serve, a model-chosen value may hold a space: cmd.exe then splits it into separate arguments of the same rocm command when pasted, though nothing in it can run as a command of its own (stated in docs/llm-tool-use.md).
  • The provider planner's "unsupported rocm action" note quotes the model's argv before the paste check runs; it is a refusal note, not a line offered to paste.
  • Open decisions: render per OS instead of refusing; decide model-chosen by an origin field rather than call position; whether to refuse look-alike spaces in model-chosen values; listed names that are dropped without notice; the serve rules apply only when args[0] is serve, not to remote serve.

  • If this PR fixes a bug, searched tests/e2e-cucumber/expectations.toml for the fixed ticket ID and removed/narrowed any now-stale xfail rows.
  • If this PR adds a new subcommand or subsystem, its domain implementation lives in its own file per docs/architecture.md (the clap declaration in cli.rs and the dispatch wiring in main.rs/lib.rs are expected, not a violation).
  • If this PR adds, renames or removes a production source file (or a .rs file under xtask/src or tests/e2e-cucumber), docs/architecture.md has the matching entry (cargo xtask check-architecture-doc fails otherwise), and the prose of any entry whose file changed role still describes it, which the check cannot verify.
  • Every new or changed user-facing message was read against the code path that runs after it, and its test asserts the resulting state — not only the wording — per AGENTS.md §3. Covers claims of an outcome, remediation advice naming a command, and promises that something will not happen.
  • Comments and doc lines this PR adds or changes that state how the code behaves were checked against that code, and each that is the reason for a decision names the file and the function, constant or test that makes it true (AGENTS.md §3).
  • Any existing scenario this PR moves off the blocking mock e2e job (for example by a tag) is justified as a deletion (AGENTS.md §8).
  • Commits answering review feedback were diffed whole against the reviewed head and widen no documented user-facing option's meaning or authority (AGENTS.md §11).
  • Every commit message on the branch describes what its own commit does in words still true at the branch head (AGENTS.md §5).

.iter()
.map(|args| format_structured_tool_call("rocm", args))
.collect::<Vec<_>>();
assert_eq!(lines.len(), 3, "{text}");

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.

This line is an assertion message inside the unit test the_planned_args_are_the_tool_call_lines_of_the_text (#[cfg(test)], freeform_planner.rs): {text} is the plan the test itself builds from the fixed request serve qwen3.5 with vllm, and it is printed only when the assertion fails under cargo test. Nothing reaches a log from it and no outside input flows into it, so there is nothing to clean here; the code still holds as of 0786864.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
Security scan found no critical or high findings on lines changed by this pull request

1 medium, low, or unrated finding(s) are not listed; the scan run has them.

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

@rominf
rominf force-pushed the fix/chat-paste-safe-model-values branch from 0a5d61d to 0786864 Compare November 17, 2026 15:52
…line

`rocm chat` prints text a model chose: its reply, tool output, error
messages that quote either, and command lines built from the values in
its tool calls. Two gaps let that text decide what the user sees or runs.

Prose was printed verbatim, so a reply could hide part of a line, retitle
the window, write the clipboard or move the cursor.
`rocm_core::terminal::sanitize_terminal_text` now removes escape
sequences whole in their 7-bit and 8-bit forms, drops invisible
characters, keeps line breaks and shows concealed text instead of
dropping it. A string sequence left unterminated ends at its own line,
so the CLI's own lines after it always survive. The shared terminal grammar
also accepts the 8-bit string terminator, and the check that decides
whether a log line may be quoted into a printed command refuses the same
invisible characters. The reply, the model
name, each tool result and the follow-up answer are sanitized before
they are composed with the CLI's lines, the whole rendered turn goes
through it once more, and so does the error report every `rocm` command
prints.

The values refused in tool arguments widen from controls and format
characters to Unicode's whole default-ignorable set. The joiners and
presentation selectors an emoji is spelled with stay allowed: one directly
after a drawn character, and a joiner directly after such a selector, so
an emoji folder name still works. Values the model chose refuse any
invisible character. GGUF architecture names and listed file names are
held to the same character set.

Command lines shown for the user to approve or paste are POSIX-quoted,
which Windows shells read differently: PowerShell and cmd.exe give
quotes and `& | < > ^ % " !` meaning inside single quotes. A value the
model chose is now refused if it holds one of those or a typographic
single quote, with a message naming the character and the `rocm`
command the user can run with it themselves, and saying that nothing was
changed and nothing was offered to paste for that call. This applies to
calls in the provider's reply, the plan `natural_language_plan` returns
for a model-written request (a result whose planned argvs cannot be read
is refused), the dashboard agent's tool calls (through a new
`execute_model_call` on the tool executor) and the provider planner.
Each call is checked before it runs, and a plan before it is shown; a
read-only check earlier in the same reply may already have run. Calls the
CLI builds from the user's own request, and the dashboard's slash
commands, are not affected.

A `serve` the model requests may only use long options `rocm serve` has,
must name exactly one model, and names its model and option values only
with letters, digits and `. _ - / : @ + \`, not starting with `-`.

New e2e scenarios chat-20 to chat-22 cover the printed reply, the
refused value and the refused plan, and runtime-lifecycle-32 covers the
cleaned error report, all on the mock lane.

Signed-off-by: Roman Inflianskas <Roman.Inflianskas@amd.com>
@rominf
rominf force-pushed the fix/chat-paste-safe-model-values branch from 0786864 to 5652aaf Compare November 17, 2026 16:03
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.

2 participants