Repository navigation
[REFACTOR]: Add shared linear trace runner #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Spencer Schoenberg (spencrr)
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
spencrr:dev/spencrr/trace-runner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+647
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """Shared linear trace execution and final-trace evaluation helpers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field, replace | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from rampart.common.text import safe_str_list | ||
| from rampart.core.types import ( | ||
| EvalContext, | ||
| EvalResult, | ||
| EvaluationPurpose, | ||
| ObservabilityLevel, | ||
| TraceEndReason, | ||
| Turn, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from rampart.core.adapter import Session | ||
| from rampart.core.evaluator import Evaluator | ||
| from rampart.core.manifest import AppManifest | ||
| from rampart.core.prompt_driver import PromptDriver | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True, eq=False) | ||
| class EvaluationRecord: | ||
| """One online evaluation and the exact context it judged. | ||
|
|
||
| Args: | ||
| evaluator: Evaluator object that produced the result. Identity is the | ||
| reuse boundary. | ||
| context: Exact raw-trace context passed to the evaluator. | ||
| result: Evaluation returned for that context. | ||
| """ | ||
|
|
||
| evaluator: Evaluator | ||
| context: EvalContext | ||
| result: EvalResult | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class TraceRun: | ||
| """A completed linear trace and its latest online evaluation. | ||
|
|
||
| ``turns`` is the driver/report view and may carry online evidence. | ||
| ``raw_turns`` is the evaluator view and never carries framework-produced | ||
| evaluation annotations. | ||
|
|
||
| Args: | ||
| trace_end_reason: Why the trace stopped producing turns. | ||
| observability_level: What the adapter can observe. | ||
| manifest: Agent capabilities used to create evaluator contexts. | ||
| turns: Annotated history passed to prompt drivers and results. | ||
| raw_turns: Annotation-free history passed to evaluators. | ||
| latest_online_evaluation: Most recent stop-condition evaluation. | ||
| """ | ||
|
|
||
| trace_end_reason: TraceEndReason | ||
| observability_level: ObservabilityLevel | ||
| manifest: AppManifest | None = None | ||
| turns: list[Turn] = field(default_factory=list[Turn]) | ||
| raw_turns: list[Turn] = field(default_factory=list[Turn]) | ||
| latest_online_evaluation: EvaluationRecord | None = None | ||
|
|
||
|
|
||
| def _evaluation_context( | ||
| *, | ||
| raw_turns: list[Turn], | ||
| observability_level: ObservabilityLevel, | ||
| manifest: AppManifest | None, | ||
| ) -> EvalContext: | ||
| """Build an evaluator context from a snapshot of the raw trace. | ||
|
|
||
| Returns: | ||
| EvalContext: Context holding a shallow snapshot of raw turns. | ||
| """ | ||
| return EvalContext( | ||
| turns=list(raw_turns), | ||
| observability_level=observability_level, | ||
| manifest=manifest, | ||
| ) | ||
|
|
||
|
|
||
| def _matches_final_trace_context(*, context: EvalContext, run: TraceRun) -> bool: | ||
| """Check that the raw trace and adapter context are unchanged. | ||
|
|
||
| Returns: | ||
| bool: Whether this context can supply the final-trace judgment. | ||
| """ | ||
| return ( | ||
| context.observability_level is run.observability_level | ||
| and context.manifest is run.manifest | ||
| and len(context.turns) == len(run.raw_turns) | ||
| and all( | ||
| evaluated is final | ||
| for evaluated, final in zip(context.turns, run.raw_turns, strict=True) | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| async def run_trace_async( | ||
| *, | ||
| session: Session, | ||
| driver: PromptDriver, | ||
| max_turns: int, | ||
| observability_level: ObservabilityLevel, | ||
| stop_when: Evaluator | None = None, | ||
| manifest: AppManifest | None = None, | ||
| ) -> TraceRun: | ||
| """Drive a linear conversation with optional online stopping. | ||
|
|
||
| The runner does not own session lifetime or exception conversion. Callers | ||
| keep the session context active around this function, and exceptions from | ||
| the driver, session, or evaluator propagate unchanged. | ||
|
|
||
| Args: | ||
| session: Active agent session. | ||
| driver: Prompt source for the conversation. | ||
| max_turns: Maximum number of requests sent to the agent. | ||
| observability_level: What the adapter can observe. | ||
| stop_when: Optional evaluator checked after every response. A detected | ||
| outcome terminates the trace. | ||
| manifest: Agent capabilities exposed to evaluators. | ||
|
|
||
| Returns: | ||
| TraceRun: Completed turns, termination reason, and online evidence. | ||
|
|
||
| Raises: | ||
| ValueError: If ``max_turns`` is negative. | ||
| """ | ||
| if max_turns < 0: | ||
| msg = "max_turns must be non-negative." | ||
| raise ValueError(msg) | ||
|
|
||
| run = TraceRun( | ||
| trace_end_reason=TraceEndReason.MAX_TURNS_REACHED, | ||
| observability_level=observability_level, | ||
| manifest=manifest, | ||
| ) | ||
|
|
||
| for turn_index in range(max_turns): | ||
| decision = await driver.next_prompt_async(history=list(run.turns)) | ||
| if decision is None: | ||
| run.trace_end_reason = TraceEndReason.DRIVER_EXHAUSTED | ||
| return run | ||
|
|
||
| response = await session.send_async(decision.request) | ||
| raw_turn = Turn( | ||
| request=decision.request, | ||
| response=response, | ||
| turn_number=turn_index, | ||
| driver_reasoning=decision.reasoning, | ||
| ) | ||
| run.raw_turns.append(raw_turn) | ||
|
|
||
| if stop_when is None: | ||
| run.turns.append(raw_turn) | ||
| continue | ||
|
|
||
| context = _evaluation_context( | ||
| raw_turns=run.raw_turns, | ||
| observability_level=observability_level, | ||
| manifest=manifest, | ||
| ) | ||
| evaluation = await stop_when.evaluate_async(context=context) | ||
| run.latest_online_evaluation = EvaluationRecord( | ||
| evaluator=stop_when, | ||
| context=context, | ||
| result=evaluation, | ||
| ) | ||
| run.turns.append( | ||
| replace( | ||
| raw_turn, | ||
| eval_result=evaluation, | ||
| eval_purpose=EvaluationPurpose.STOP_CHECK, | ||
| ), | ||
| ) | ||
| if evaluation.detected: | ||
| run.trace_end_reason = TraceEndReason.STOP_CONDITION_MET | ||
| return run | ||
|
|
||
| return run | ||
|
|
||
|
|
||
| async def evaluate_final_trace_async( | ||
| *, | ||
| evaluator: Evaluator, | ||
| run: TraceRun, | ||
| ) -> EvalResult | None: | ||
| """Evaluate the final raw trace, reusing an identical online judgment. | ||
|
|
||
| Args: | ||
| evaluator: Evaluator responsible for the final verdict. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "Evaluator applied to the final trace" is more accurate than current arg description because it makes clear that evaluator produces evidence versus determining safety status |
||
| run: Completed trace from :func:`run_trace_async`. | ||
|
|
||
| Returns: | ||
| EvalResult | None: Final evaluation, or None when no turns exist. | ||
|
|
||
| Call this before leaving any active session or injection context required | ||
| by the evaluator. Reuse requires matching evaluator, raw-turn, and manifest | ||
| identities and the same observability level. Requests, responses, manifests, | ||
| and their nested values are treated as immutable once evaluated. | ||
| """ | ||
| if not run.raw_turns: | ||
| return None | ||
|
|
||
| record = run.latest_online_evaluation | ||
| if ( | ||
| record is not None | ||
| and record.evaluator is evaluator | ||
| and _matches_final_trace_context(context=record.context, run=run) | ||
| ): | ||
| return replace( | ||
| record.result, | ||
| evidence=safe_str_list(value=record.result.evidence), | ||
| undetermined_operands=safe_str_list( | ||
| value=record.result.undetermined_operands, | ||
| ), | ||
| ) | ||
|
|
||
| context = _evaluation_context( | ||
| raw_turns=run.raw_turns, | ||
| observability_level=run.observability_level, | ||
| manifest=run.manifest, | ||
| ) | ||
| return await evaluator.evaluate_async(context=context) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Consider renaming to
latest_stop_checksince this is relating specifically to stop conditions