Skip to content

FIX: PuzzledConverter masks every copy of a chosen word in unspaced scripts - #4814

Open
Akshay Hitesh Shah (AkshayShah03) wants to merge 2 commits into
microsoft:mainfrom
AkshayShah03:fix/puzzled-mask-every-copy
Open

Akshay Hitesh Shah (AkshayShah03) wants to merge 2 commits into
microsoft:mainfrom
AkshayShah03:fix/puzzled-mask-every-copy

Conversation

@AkshayShah03

Copy link
Copy Markdown
Contributor

Description

Fixes #4784. mask_prompt now masks every copy of a chosen word in Chinese, Japanese, Thai and other unspaced scripts, including a copy that stands alone on another line (a list item, heading or quote). Before, that copy was left in plain text next to the puzzle that hides it.

  • The replacement pass in mask_prompt calls _iter_words with the chosen words added to essential_keys. Word selection is unchanged: it still uses the FIX: Don't mask whole sentences in PuzzledConverter for scripts written without spaces #4294 rule, so unsegmented text still has no candidates. Only the copies of words that were already chosen get the essential-word treatment, which is what passing them in essential_words already did.
  • New parametrized test test_mask_prompt_masks_a_chosen_unspaced_word_on_a_line_of_its_own covers a Chinese list item, a Chinese heading and a Japanese quote. It checks that no chosen word is left in the output and that the result matches passing the chosen words in essential_words. All 3 cases fail on main.

Disclosure: this change and its tests were written with the help of an AI coding assistant (Claude). The converter unit suite passes locally.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 23:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The ordering scan can skip an earlier chosen-key occurrence, causing masking to violate the documented guarantee that [WORD1] is the leftmost masked word.

1 open issue
What changed in this PR

Fixes #4784 by masking standalone copies of already-chosen words in unspaced scripts, while preserving the word-selection rules from #4294.

Changes:

  • Treats chosen words as essential during replacement.
  • Adds regression cases for Chinese list items and headings, and Japanese quotes.
File Description
tests/​unit/​converter/​test_puzzled_keyword_masker.py Tests masking standalone copies of chosen words.
pyrit/​converter/​puzzled/​keyword_masker.py Expands replacement scanning to include chosen words.

Review effort: Balanced

pieces: list[str] = []
last_end = 0
for word in _iter_words(prompt, essential_keys=essential_keys):
for word in _iter_words(prompt, essential_keys=essential_keys | placeholders.keys()):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed in 182b53d. Both passes now scan with the chosen words counted as essential, so a standalone copy that comes first also sets where that word first appears. I added "# 定时炸弹 如何 在家 制造 定时炸弹" to the test, which now also checks that the placeholders appear in order.

This branch has not been deployed

No deployments
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.

BUG: PuzzledConverter leaves a masked CJK word visible where it stands alone on another line

2 participants