Skip to content

MAINT: make PromptShield parser dispatch exhaustive - #3797

Open
Slai.life (huong8373tt-beep) wants to merge 2 commits into
microsoft:mainfrom
huong8373tt-beep:fix/p311-03-promptshield-input-parser
Open

Slai.life (huong8373tt-beep) wants to merge 2 commits into
microsoft:mainfrom
huong8373tt-beep:fix/p311-03-promptshield-input-parser

Conversation

@huong8373tt-beep

@huong8373tt-beep Slai.life (huong8373tt-beep) commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • make the closed PromptShield entry-field dispatch explicit for None and preserve the historical fallback for unexpected runtime values
  • add a focused regression test for the fallback path

Part of #3166 (P311-03).

Validation:

  • focused PromptShield unit tests: 23 passed
  • Ruff check: passed
  • Ruff format: passed
  • ty check: passed
  • git diff --check: passed

Slai Agent Runtime performed the investigation, implementation, and test execution. The account owner authorized this contribution and is responsible for its content.

Signed-off-by: Slai.life <huong8373tt@gmail.com>
Comment on lines 315 to 331
# Preserve the historical runtime fallback for an unknown value.
user_prompt = ""
documents = []
split_input = input_str.split("<document>")

for element in split_input:
contents = element.split("</document>")

if len(contents) == 1:
user_prompt += contents[0]
else:
documents.append(contents[0])
user_prompt += contents[1]

return {"userPrompt": user_prompt, "documents": documents if documents else []}

def _add_auth_param_to_headers(self, headers: dict[str, str]) -> None:

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.

🟡 Should Fix: Can we keep one copy of the document parser? This block repeats the entire case None parser, although both paths are meant to behave identically. A future fix to tag handling or trailing text would have to be applied twice, and the new test only checks one example.

We can preserve both the type check and the runtime fallback without duplicating the parser:

case None:
    pass
case unknown_field:
    if TYPE_CHECKING:
        assert_never(unknown_field)

Then move the existing parsing block below the match. The two forced-field cases already return early, so both parsing paths would reach the same implementation.

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.

Implemented the suggested refactor in f3d5cbec: the None and unexpected-runtime-value paths now share one document parser below the match. The focused syntax check passed, and the full PR CI matrix has restarted.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

Thanks for the focused review. I agree the parser should have one implementation. I’m going to move the shared document parsing below the match so both the None case and the type-checking fallback reach the same block, then rerun the targeted tests and CI.

Signed-off-by: Slai.life <huong8373tt@gmail.com>
@huong8373tt-beep
Slai.life (huong8373tt-beep) force-pushed the fix/p311-03-promptshield-input-parser branch from ad807e6 to f3d5cbe Compare November 2, 2026 05:12

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.

2 participants