Skip to content

FEAT: nested argument matching, so a dotted path can check recipient.email - #4360

Open
WatchTree-19 wants to merge 9 commits into
microsoft:mainfrom
WatchTree-19:feat/nested-argument-matching
Open

WatchTree-19 wants to merge 9 commits into
microsoft:mainfrom
WatchTree-19:feat/nested-argument-matching

Conversation

@WatchTree-19

Copy link
Copy Markdown
Contributor

Description

This answers open question 3 in the scorer contract design: "Are ToolCallEvent.arguments flat?" It stacks on the redacted argument PR (feat/redacted-argument-matching); only the second commit is new here.

Real tool arguments are nested JSON. Flattening them to dict[str, str] loses structure, and a general path expression is, as the design says, "a small query language nobody wants to own". This takes the smallest step between the two: a dotted path of exact object keys, used in the two places that already name arguments, and nothing else.

ToolArgumentRequirement(name="recipient.email", matcher=Equals(value="a@example.com"))
OtelTraceSource(trace_client=client, kept_arguments=("recipient.email",))

Paths.

  • ToolArgumentRequirement.name and each entry of kept_arguments may be a top-level key, as before, or dot-separated object keys into a nested argument. Each segment is an exact, case-sensitive key; an empty segment (a..b, .a, a.) is rejected.
  • A path steps through objects only. A missing key, or a value on the way that is not an object, means the field is missing.
  • There are no wildcards, filters, array indexes or escapes. A path cannot name a key that itself contains a dot. To check inside an array, match the array's compact JSON text with Contains or Regex, which already works.

Kept paths and redaction. The rule from the previous PR carries over: a field that was not kept is undetermined, never absent.

  • A kept path keeps only that nested field, nested as it was. Keeping recipient.email stores {"recipient": {"email": ...}} and redacts the recipient's other fields. The payload validator rejects an event that holds anything beyond its kept paths.
  • A requirement is decided only by a kept path or one that contains it. Keeping recipient.email decides recipient.email; keeping recipient decides recipient.email and recipient.name. Keeping recipient.email leaves recipient undetermined, because only part of its value was kept, and leaves recipient.name undetermined, because it was redacted.
  • Kept paths must not repeat or lie inside one another, so the stored view of each call is unambiguous.

MessageToolCallScorer reads nested fields from the full stored arguments, and still never reports false. A top-level name means what it meant before, and stored conditions and observations keep their form; only the shared path helpers are new (pyrit/models/score/_argument_paths.py, private).

Tests and Documentation

  • tests/unit/models/test_argument_paths.py (new): copying only the named fields, no aliasing of the source objects, rejecting stored arguments beyond the kept paths (including a literal dotted key and a letter-prefix of a key), kept path validation.
  • tests/unit/score/test_tool_argument_matching.py: a dotted name reads a nested field (missing keys, scalars, arrays and null on the way, and a literal "recipient.email" key are all missing), a nested object compares as compact JSON, only a kept path or one containing it decides a nested field.
  • tests/unit/score/test_otel_tool_call_scorer.py: the source keeps only the nested field a path names, complete evidence decides a nested field only through a kept path (through scoring and replay), overlapping or malformed kept paths are rejected.
  • tests/unit/score/test_message_tool_call_scorer.py: a dotted name reads the nested stored argument.
  • tests/unit/models/test_condition.py and tests/unit/models/test_tool_observation.py: dotted names round trip, empty segments are rejected, nested kept arguments round trip, nested arguments beyond the kept path and overlapping kept paths are rejected.
  • doc/code/scoring/5_tool_call_scorer.py and .ipynb: nested arguments and nested kept paths, kept in sync.

How it was tested

python -m pytest -q -n 8 tests/unit/score tests/unit/models tests/unit/executor tests/unit/scenario
python -m ruff check <changed files>
python -m ruff format --check <changed files>
ty check <changed files>
  • tests/unit/score, tests/unit/models, tests/unit/executor and tests/unit/scenario: 14309 passed, 91 skipped.
  • ruff check and ruff format --check clean. ty reports no new diagnostics on the changed files compared with main.
  • The doc change is Markdown only: the .ipynb cells were regenerated from the .py cells and checked to match them, with outputs untouched.
  • Each guard was checked by breaking it and confirming tests fail, then restoring it:
Mutation Tests that fail
Look a dotted name up as one top-level key 12
A kept container no longer covers its fields 3
A kept field decides its whole parent 2
Coverage by string prefix instead of path segments 1
Source keeps the whole top-level object of a nested path 11
Payload accepts any field under a kept prefix 3
Allow kept paths inside one another 4
Allow empty path segments 9
Payload validator accepts any stored arguments 4
Remove path validation from ToolArgumentRequirement 4
Let a path index into arrays 2
Rationale checks redaction by exact name instead of by path 1

… score

OtelTraceSource gains kept_arguments, an opt-in list of top-level argument
names. It reads each span's gen_ai.tool.call.arguments attribute, keeps the
named arguments on ToolExecution.arguments, and drops the rest, and the raw
attribute, before the observation is stored. The payload records the list, and
its validator rejects an event that holds any other argument.

An argument the source did not keep was redacted at write time. A requirement
on it can neither confirm nor refute a call, so a verdict that would rest on it
is undetermined even when the trace evidence is complete, and the rationale
names the arguments that were not kept, or, when the source keeps none, says
that trace evidence keeps no arguments. A kept argument that the call lacks, or
whose value does not match, still refutes the call, and so does complete
evidence with no call of the tool. Arguments that are missing, do not parse, or
are not a JSON object leave the call undetermined.

With no kept arguments, the source identity, the payload and each event keep
their stored form.
…email

Tool arguments are nested JSON. ToolArgumentRequirement.name, and each entry of
OtelTraceSource kept_arguments, may now be a dotted path of exact object keys,
such as recipient.email. A path steps through objects only: a key that is
missing, or a value on the way that is not an object, means the field is
missing. It does not index arrays and cannot name a key that contains a dot; an
array is still matched as its compact JSON text. There is no wildcard, filter or
escape syntax.

A kept path keeps only that nested field, nested as it was, and the payload
validator rejects an event that holds anything else. A requirement is decided
only by a kept path or one that contains it: keeping recipient.email decides
recipient.email but leaves recipient and recipient.name undetermined, because
the source kept only part of them. Kept paths must not lie inside one another.

A top-level name means what it meant before, and stored conditions and
observations keep their form.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 16:04

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

Dotted-path matching can reuse stale scores because the scorer version is unchanged, and stricter argument-name validation breaks loading historical scores without a compatibility or migration path.

2 open issues
What changed in this PR

Extends PyRIT’s tool-call scoring with exact dotted paths into nested JSON arguments, building on selective trace-argument retention.

Changes:

  • Adds shared helpers for path validation, nested lookup, and selective retention.
  • Updates trace scoring to distinguish kept, missing, and redacted nested fields.
  • Adds validation, scoring, and replay tests, plus synchronized documentation updates.
File Description
tests/​unit/​score/​test_tool_argument_matching.py Tests nested lookup and redaction-aware matching.
tests/​unit/​score/​test_otel_tool_call_scorer.py Tests retained paths, verdicts, replay, and validation.
tests/​unit/​score/​test_message_tool_call_scorer.py Tests nested arguments in stored messages.
tests/​unit/​models/​test_trace.py Tests retained-argument serialization and validation.
tests/​unit/​models/​test_tool_observation.py Tests retained-path boundaries and round trips.
tests/​unit/​models/​test_condition.py Tests dotted names and malformed-path rejection.
tests/​unit/​models/​test_argument_paths.py Adds focused path-helper tests.
pyrit/​score/​true_false/​otel_tool_call_scorer.py Matches retained arguments and explains redacted fields.
pyrit/​score/​tool_argument_matching.py Adds nested lookup and retention-aware verdicts.
pyrit/​score/​observation/​otel_trace_source.py Captures configured argument paths from trace spans.
pyrit/​models/​score/​trace.py Adds optional retained arguments to tool executions.
pyrit/​models/​score/​observation.py Declares and validates retained argument paths.
pyrit/​models/​score/​condition.py Validates argument requirements as dotted paths.
pyrit/​models/​score/​_argument_paths.py Implements shared path and retention helpers.
doc/​code/​scoring/​5_tool_call_scorer.py Updates explanatory Markdown for nested matching and retention.
doc/​code/​scoring/​5_tool_call_scorer.ipynb Mirrors the documentation changes in notebook Markdown.

Review effort: Balanced

Comment thread pyrit/score/tool_argument_matching.py Outdated
if kept is not None and not is_argument_kept(constraint.name, kept=kept):
verdict = None
continue
found, value = read_argument(arguments, constraint.name)
Comment thread pyrit/models/score/condition.py Outdated
model_config = ConfigDict(frozen=True, extra="forbid")

name: str = Field(min_length=1, strict=True)
name: ArgumentPath
A literal too large to represent, such as 1e400, parses to infinity, and an
already-parsed arguments object skips parsing altogether, so both reached the
matcher. The source now rejects any NaN or infinite number, nested ones
included, on both paths, and the call stays undetermined.
Any nonempty argument name was valid before names became dotted paths, so a
stored requirement can hold one that is not a path, such as a..b. Reading a
stored expectation now accepts it and matches it as one literal top-level
key, the meaning it was stored with. New requirements are still held to the
path rules. The validation context is passed on to conditions so they can
tell the two apart.

MessageToolCallScorer and OtelToolCallScorer bump matching_version, so a
score matched under the old meaning of a dotted name is not reused.
@hannahwestra25 hannahwestra25 self-assigned this Nov 24, 2026

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this. The dotted-path read (recipient.email) is a useful, standalone gain for MessageToolCallScorer. Please split it into its own PR on main. The nested kept-path code extends #4359's argument allowlist, which maintainers have not reviewed yet; add it later, sized to #4359's outcome.

@WatchTree-19

Copy link
Copy Markdown
Contributor Author

Thanks hannahwestra25, the dotted-path read is now on its own in #4609, based on main. I'll size the nested kept-argument part here once #4359 is decided.

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.

3 participants