Skip to content

fix(chat): check for a masked prompt before typing an immediate send's OpenCode skill - #3174

Merged
devswha merged 10 commits into
mainfrom
fix/immediate-send-masked-guard
Nov 6, 2026
Merged

devswha merged 10 commits into
mainfrom
fix/immediate-send-masked-guard

Conversation

@devswha

@devswha devswha commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Summary

An immediate (not queued) chat message that names an OpenCode skill is typed key by key (typeWithSkills in server/core/composer.ts). That path rechecked only the pane, its session and blocked state before each write, so the skill's accept key, the rest of the message and its Enter could go into a masked password prompt that came up on the terminal. #3172 added a masked-prompt check to the queued path's skill typing (beforeKey + a live-screen read); this routes the immediate path through the same guard:

  • revalidate (run before every write and before the Enter) now also reads the live screen and refuses with agent_blocked when a masked prompt is showing.
  • The skill-accept key gets beforeKey: the screen it was decided on (escapes stripped), then the live screen, so a scrolled viewport that still shows the autocomplete row cannot hide a prompt below.
  • The check does not trust one pane width: an immediate send need not be attached, and an attachment's width can change during the read. maskedPromptAtAnyWidth tries every width at which the row above the last reaches the right edge (the only rows secretPrompt joins), measured in terminal columns with displayWidth (fix(shared): recognize a masked prompt wrapped by display width or with a leading space #3177), so a wrapped prompt is refused at any width, wide characters included.
  • A refusal at the first check now lets go of the OpenCode watch it had started.

Fail closed: nothing typed -> the send is refused (agent_blocked); part typed -> the existing submit_partial answer (chat text kept, check the terminal). Everything else on the immediate path is unchanged; agent-only messages still go through agent.prompt.

Stacked on #3172 (fix/pending-birth-guard); GitHub retargets it to main once #3172 merges.

Validation

…fore a write

A queued message's last checks before the paste compared only the PID in
front, so a process that took the agent's PID (or whose known start became
unreadable) passed, and a masked prompt that came up during that last
process read got the paste or the Enter.

- stillInFront also compares the bound process start after the PID is seen
- samePendingIdentity refuses a known start of the same PID that can no
  longer be read
- the screen is read once more right before the paste and the Enter; the
  window left is that read and herdr's write, as herdr has no
  compare-and-write
… checks

Review round on #3172 (omo, Codex):
- stillInFront checks the bound PID, so a bound process gone from the
  front is refused, and compares its start last, after Claude's session
  read, through samePendingIdentity (platform leeway and its mocks apply)
- the start is compared once more after the last screen read
- Pi's last check after its receipt anchor passes the bound identity
- OpenCode skill keys and Codex steering's Escape re-read the screen for a
  masked prompt right before they are written
… as unknown

The stricter identity check failed a Pi receipt for good when the agent's
start was unreadable for a moment at the same PID. Reconciliation now reads
it again later; only a write is refused on an unknown start.
…rocess start

OpenCode's skill-accept Tab is decided on a screen read after revalidate.
typeWithSkills now hands that screen to a beforeKey hook right before the
key: the pending path refuses a masked prompt on it and compares the bound
process start (no further herdr read).
…a launch's unknown start

- the skill-accept Tab's own screen is the viewport, which may be scrolled:
  the live screen is read once more and the start compared before the key
- a launch-bound first message whose agent's start reads as unknown for the
  same PID waits for the next read instead of being held as replaced
…ng on any status since its read

From CodeRabbit's review:
- a message whose deadline passed during the final reads before the paste
  is not typed (submit_timeout)
- Codex steering's Escape compares the status from before its screen read
- the birth-guard tests use starts outside macOS's one-second leeway
- the changelog says a refused message stays queued when nothing was typed
@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Review round on 88b51bc: omo review CLEAR; Codex (adversarial) found two HIGHs and one LOW, all reproduced with its own tests and fixed in the next commit:

  • HIGH: a masked prompt wrapped across rows was missed when the send had no attachment (no width, so secretPrompt could not join the rows). Fixed: the check tries every width at which the row above the last reaches the right edge, so it needs no width.
  • HIGH: an attachment resized during the screen read left the check at the old width. Fixed by the same change: no width is taken from the attachment any more.
  • LOW: a refusal at the first check left its OpenCode watch registered (repeated refusals could crowd out real sends' watches). Fixed: the watch is cancelled on that refusal.

Note for #3172: noMaskedPrompt in server/core/pending.ts takes its width before the read too, so the resize case may apply there as well (the queued path always has an attachment, so the no-attachment case does not).

@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Re-review at 80a23b6: omo review CLEAR. A targeted Codex re-check confirmed the two HIGHs and the LOW above are closed (no writes, agent_blocked, watch cancelled) and found no false positive on the 16 recorded opencode-* screens.

It found one more HIGH, in the shared recognizer rather than this PR: secretPrompt drops the space at the start of a wrapped row while joining rows, so a prompt wrapped over several rows can be missed. Reproduced on main; it affects every caller (queued input, typed input, the browser's masked-input detection) equally. Filed as #3175; not fixed here.

CodeRabbit skipped this PR (base is not the default branch); the omo and Codex reviews above stand in for it.

devswha added a commit that referenced this pull request Nov 6, 2026
…th a leading space (#3177)

Fixes #3175.

## What
`secretPrompt` (shared/secret-prompt.ts) is what keeps queued messages,
typed input and the
immediate send's OpenCode skill text out of a password prompt. It missed
a masked prompt wrapped
over several rows in three ways:

- **Leading space dropped**: joining a row onto the one above called
`trimStart()` on the joined
text, so a wrapped row starting with a space (`" '/tmp/a-ver"`) lost it
and the prompt no longer
matched. Any prompt wrapped into 3+ rows where a later row starts with a
space was missed (e.g.
  `Enter passphrase for key '/tmp/key':` at 10 columns).
- **Rows measured by string length**: a row is only joined when it
reaches the right edge, but a
row of Korean/Chinese/Japanese text fills 80 columns with far fewer than
80 characters, so a
  CJK key path at 80 or 20 columns was never joined.

## How
- Join rows without trimming (the candidate is still trimmed before
matching).
- Measure rows in terminal columns with a small `displayWidth`, per
grapheme cluster (East Asian
Wide/Fullwidth or emoji presentation = 2, combining/zero-width only = 0,
else 1; code points
where `Intl.Segmenter` is missing), and pad to the edge by columns. A
wide character that did not
fit leaves the last cell empty, which is padded as a space exactly as
xterm's
`translateToString` reads the wrapped row in the browser, so the browser
and the server keep
  agreeing on the prompt text (`server/core/input.ts` compares them).
- `displayWidth` is exported so callers that guess the width from the
row above (#3174) can
  measure it the same way.

## Validation
- Failing-first: the new cases in `shared/secret-prompt.test.ts` fail on
main (leading space,
4 rows at 10 columns, CJK at 80 and 20 columns, emoji and a ZWJ emoji
sequence) and pass with the fix.
- Masked-prompt / pending-input tests: `bun test
shared/secret-prompt.test.ts
server/pending-input.contract.test.ts server/pane-routes.test.ts
server/submit.contract.test.ts
  server/terminal-insert.contract.test.ts server/prompt.test.ts
server/opencode-first-message.contract.test.ts
server/core/status.test.ts` (isolated test session).
- `bun run check fast`.
- Skipped: browser regressions (no UI change;
`src/lib/useTerminalMount.ts` only calls the shared
  function).

## Known limits (pre-existing, not changed here)
- A row whose text ends in two or more spaces before the wrap is still
not joined (the server's
  row text is trimmed); same on main.
- Two separate CJK output lines that happen to fill a row exactly can be
read as one wrapped line
(`Password for 管理者` + `is stored here:` at 19 columns): input is
refused, never typed.
@devswha
devswha force-pushed the fix/immediate-send-masked-guard branch from 80a23b6 to 55d3d91 Compare November 6, 2026 11:50
Base automatically changed from fix/pending-birth-guard to main November 6, 2026 12:19
@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Merge note (merge pass) at head 55d3d91:

  • Merged on GitHub's required checks, all green on this head (Fast checks, Integration and browser, Native Windows install).
  • omo review receipt: CLEAR at this exact head. Unresolved review threads: 0 (GraphQL). CodeRabbit: skipped; no CodeRabbit review ran on this head.
  • Queued/immediate input (real review): the immediate OpenCode skill send now gets the same masked-prompt guard as the queued path (fail closed), with a display-width check at any width. The merge pass's own strict Codex pass reproduced a HIGH in the shared masked-prompt parser (CJK path wrapped at 80 columns, 4+ wrapped rows), fixed separately in fix(shared): recognize a masked prompt wrapped by display width or with a leading space #3177. Delta-checked at this head: every repro screen is detected at any width and ordinary output is not. Not reproduced, recorded: the immediate path pins no foreground PID/start the way the queued path does (predates this PR).

@devswha
devswha merged commit db0d6f8 into main Nov 6, 2026
21 of 23 checks passed
@devswha
devswha deleted the fix/immediate-send-masked-guard branch November 6, 2026 12:20
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