Repository navigation
FEAT: nested argument matching, so a dotted path can check recipient.email - #4360
Open
WatchTree-19 wants to merge 9 commits into
Open
WatchTree-19 wants to merge 9 commits into
WatchTree-19 wants to merge 9 commits into
Conversation
… 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.
Contributor
There was a problem hiding this comment.
🟡 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
| if kept is not None and not is_argument_kept(constraint.name, kept=kept): | ||
| verdict = None | ||
| continue | ||
| found, value = read_argument(arguments, constraint.name) |
| 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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Description
This answers open question 3 in the scorer contract design: "Are
ToolCallEvent.argumentsflat?" 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.Paths.
ToolArgumentRequirement.nameand each entry ofkept_argumentsmay 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.ContainsorRegex, 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.
recipient.emailstores{"recipient": {"email": ...}}and redacts the recipient's other fields. The payload validator rejects an event that holds anything beyond its kept paths.recipient.emaildecidesrecipient.email; keepingrecipientdecidesrecipient.emailandrecipient.name. Keepingrecipient.emailleavesrecipientundetermined, because only part of its value was kept, and leavesrecipient.nameundetermined, because it was redacted.MessageToolCallScorerreads 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 andnullon 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.pyandtests/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.pyand.ipynb: nested arguments and nested kept paths, kept in sync.How it was tested
tests/unit/score,tests/unit/models,tests/unit/executorandtests/unit/scenario: 14309 passed, 91 skipped.ruff checkandruff format --checkclean.tyreports no new diagnostics on the changed files compared with main..ipynbcells were regenerated from the.pycells and checked to match them, with outputs untouched.ToolArgumentRequirement