Files
codex/codex-rs/guardian-context/src/retained_instructions_tests.rs
T
felixxia-oai 16ff14c266 Retain inherited Guardian instructions in standalone forks (#43478)
## Why

A standalone root forked from a worker needs its inherited user instructions for Guardian review, including after compaction. Parent input counters belong to a different thread, and omitted parent answers may have restricted an inherited authorization.

## What changed

- Adopt inherited user instructions into thread-owned retained context for standalone roots, recovering surviving checkpoint messages when needed.
- Keep inherited instructions before local evidence without sharing local acceptance counters, and preserve that distinction during reconciliation and rollback.
- Mark parent verified answers as incomplete and treat recovered checkpoint instructions as incomplete excerpts.

## Testing

Add coverage for standalone forks from copied history and truncated checkpoints, retention through compaction and resume, inherited/local ordering collisions, and rollback scope.

GitOrigin-RevId: bf716e224a1d29d6c528ef0dd7cdb118a413a08c
2026-09-07 13:58:57 +00:00

67 lines
2.2 KiB
Rust

use super::*;
use codex_history::RetainedContextEvent;
use codex_history::RetainedInputSource;
use codex_history::RetainedUserMessage;
use codex_history::VerifiedAnswer;
use codex_history::VerifiedQuestionAnswer;
use pretty_assertions::assert_eq;
#[test]
fn instructions_preserve_source_order_and_whole_records() {
let mut context = RetainedContext::default();
// An answer at the existing 900-token byte limit still fits with order framing.
let answer = "x".repeat(3_600 - "assistant: Publish?\nuser: \n".len());
context.record(&RetainedContextEvent::VerifiedAnswer {
answer: VerifiedAnswer {
turn_id: "grant".to_owned(),
call_id: "ask".to_owned(),
questions: vec![VerifiedQuestionAnswer {
question: "Publish?".to_owned(),
answer: answer.clone(),
}],
},
acceptance_order: None,
});
context.record_user_message(
RetainedUserMessage {
turn_id: "revocation".to_owned(),
message_id: Some("msg_revoke".to_owned()),
text: "Do not publish after all.".to_owned(),
complete: true,
},
RetainedInputSource::Local(None),
);
let rendered = render_retained_instructions(&context);
assert_eq!(
rendered,
vec!["Retained source order: 1\nuser: Do not publish after all.\n"]
);
let answers = crate::render_verified_answers(&context);
assert_eq!(
(answers.complete, answers.fragments),
(
true,
vec![format!(
"Retained source order: 0\nassistant: Publish?\nuser: {answer}\n"
)]
),
);
context.record_user_message(
RetainedUserMessage {
turn_id: "oversized".to_owned(),
message_id: Some("msg_large".to_owned()),
text: "Permission is conditional. ".repeat(200),
complete: true,
},
RetainedInputSource::Local(None),
);
let rendered = render_retained_instructions(&context);
assert_eq!(rendered.len(), 2);
assert!(rendered[0].starts_with("Host notice:"));
assert!(
!rendered
.iter()
.any(|fragment| fragment.contains("Permission is conditional"))
);
}