Refresh Guardian v3 evidence before retrying an approval review (#63363)

## Why

A user's authorization can change while Guardian is reviewing a tool call.
A retry could reuse the original evidence cutoff and miss a revocation
that arrived during the first attempt.

## What changed

Capture the root thread's history position when the action is bound and
defer evidence routing until Guardian prepares the review. Capture a fresh
position on retries or if authorization changed before the first attempt.
Use bounded history reads to catch up through the selected position and
wait for routing to reach it before submitting the assessment.

## Testing

Extend the app-server test to revoke authorization through a paused user
goal while an MCP tool call's approval is pending. Verify that Guardian
discards the stale allow, sees the revocation on retry, and denies the
tool call. Cover bounded history reads and shared routing progress in
unit tests.

GitOrigin-RevId: d7cf6f03412c4aa5399700427f40710da3123e52
This commit is contained in:
felixxia-oai
2026-11-23 15:57:15 +00:00
committed by copyberry
parent 8b02797a2e
commit 223b61a9a8
9 changed files with 411 additions and 92 deletions
@@ -62,27 +62,43 @@ pub(super) async fn read(
"original history requires a paginated root",
));
}
let end_byte_offset = complete_end(&mut file)?;
let end_ordinal_exclusive = {
let mut scanner = ReverseJsonlScanner::new_at(&mut file, end_byte_offset)?;
let Some(ScanOutcome::Parsed(last)) = scanner.scan_next_rollout_line()? else {
return Err(io::Error::other("original history has an invalid tail"));
};
last.ordinal
.and_then(|ordinal| ordinal.checked_add(1))
.ok_or_else(|| io::Error::other("original history tail has no next ordinal"))?
};
let end = HistoryPosition {
thread_id,
end_ordinal_exclusive,
end_byte_offset,
let end = match &request {
OriginalHistoryRequest::Range { end, .. } => {
if end.thread_id != thread_id
|| end.end_byte_offset > file.seek(SeekFrom::End(0))?
{
return Err(io::Error::other(
"original history cutoff no longer applies",
));
}
*end
}
OriginalHistoryRequest::Tail | OriginalHistoryRequest::After(_) => {
let end_byte_offset = complete_end(&mut file)?;
let end_ordinal_exclusive = {
let mut scanner = ReverseJsonlScanner::new_at(&mut file, end_byte_offset)?;
let Some(ScanOutcome::Parsed(last)) = scanner.scan_next_rollout_line()? else {
return Err(io::Error::other("original history has an invalid tail"));
};
last.ordinal
.and_then(|ordinal| ordinal.checked_add(1))
.ok_or_else(|| {
io::Error::other("original history tail has no next ordinal")
})?
};
HistoryPosition {
thread_id,
end_ordinal_exclusive,
end_byte_offset,
}
}
};
match request {
OriginalHistoryRequest::Tail => Ok(OriginalHistoryRead {
items: Vec::new(),
end,
}),
OriginalHistoryRequest::After(start) => {
OriginalHistoryRequest::After(start) | OriginalHistoryRequest::Range { start, .. } => {
if start.thread_id != thread_id
|| start.end_byte_offset > end.end_byte_offset
|| start.end_ordinal_exclusive > end.end_ordinal_exclusive
@@ -64,6 +64,15 @@ async fn reads_complete_fixed_suffix_and_rejects_truncated_source()
later["ordinal"] = json!(2);
later["payload"]["content"][0]["text"] = json!("After the cutoff");
writeln!(file, "{later}")?;
let bounded = store
.read_original_history(owner, OriginalHistoryRequest::Range { start, end })
.await?;
assert_eq!(bounded.end, end);
assert_eq!(bounded.items.len(), 1);
assert_eq!(
serde_json::to_value(&bounded.items[0].item)?,
serde_json::to_value(&read.items[0].item)?
);
assert_eq!(read.end, end);
assert_eq!(read.items.len(), 1);
assert_eq!(read.items[0].end, end);
@@ -12,6 +12,11 @@ pub enum OriginalHistoryRequest {
/// Freeze the current complete tail and read originals after this position.
/// Appends beyond that cutoff are excluded from the returned records.
After(HistoryPosition),
/// Read only the records between two previously captured positions in this rollout.
Range {
start: HistoryPosition,
end: HistoryPosition,
},
}
/// One original record and the exclusive position immediately after it.