Persona K58: every model call of a wake carries its own request id; the Interpreter's retry is no ReplayConflict
Soul's R16: decide_in re-asked under the segment's one id with a changed prompt, and the connector's replay store refuses an id it saw with other bytes, so every retry failed live. One root per render (wake_id, wall clock, minted once); each call's id is call_id(root, stage, segment), and the Interpreter appends its attempt. A replay-enforcing fake model (Replaying) drives a read wake whose Interpreter retries on both segments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7dc537eb19
commit
14732b4168
4 changed files with 118 additions and 16 deletions
|
|
@ -367,10 +367,17 @@ fn unshown_omitted(report: &mut Value, packet: &Packet) {
|
|||
}
|
||||
}
|
||||
|
||||
/// A request id no earlier call carried: the kit's replay store answers a repeated id with its first answer, and a
|
||||
/// turn's clock may be a fixture's, the same on every run, so the id takes the wall clock.
|
||||
fn request_id(instance: &str, stage: &str, tag: &str, seed: u64) -> String {
|
||||
format!("{AGENT}:{instance}:project:{stage}:{tag}:{seed}:{}", Utc::now().timestamp_nanos_opt().unwrap_or_default())
|
||||
/// The root of one render's call ids, minted once per render. The kit's replay store binds an id to the one invocation
|
||||
/// it first carried: the same bytes again get its first answer, any other bytes `ReplayConflict`. So no two calls may
|
||||
/// share an id; and a turn's clock may be a fixture's, the same on every run, so the root takes the wall clock.
|
||||
fn wake_id(instance: &str, tag: &str, seed: u64) -> String {
|
||||
format!("{AGENT}:{instance}:project:{tag}:{seed}:{}", Utc::now().timestamp_nanos_opt().unwrap_or_default())
|
||||
}
|
||||
|
||||
/// One call's id under its render's root: its stage and the segment of her wake it serves (0 before any read, n after
|
||||
/// the nth). The Interpreter appends its attempt (`interpret::decide_in`), so every call of a render has its own id.
|
||||
fn call_id(wake: &str, stage: &str, segment: usize) -> String {
|
||||
format!("{wake}:{stage}:{segment}")
|
||||
}
|
||||
|
||||
/// What a wake reads through (map 16.22.3): the port that performs her reads, and when her domain's window opened.
|
||||
|
|
@ -389,7 +396,8 @@ struct Reading<'a> {
|
|||
/// carries every segment's acts in order and every read (`reads`), which her next outcome lists without their content.
|
||||
fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, through: Through, reading: Option<Reading>) -> Value {
|
||||
let ids: Vec<String> = packet.selected.iter().map(|pick| pick.id.clone()).collect();
|
||||
let answer = match model.ask(&request_id(instance, "projector", tag, packet.seed), &tuning.projector.model, &projector_prompt(packet, tuning), None) {
|
||||
let wake_id = wake_id(instance, tag, packet.seed);
|
||||
let answer = match model.ask(&call_id(&wake_id, "projector", 0), &tuning.projector.model, &projector_prompt(packet, tuning), None) {
|
||||
Ok(answer) => answer,
|
||||
Err(failed) => return json!({ "seed": packet.seed, "lens": packet.lens, "selected": ids, "failed": failed.reason, "usage": failed.usage }),
|
||||
};
|
||||
|
|
@ -413,7 +421,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
|
|||
return render;
|
||||
}
|
||||
let mut turns = vec![Turn::User(persona_prompt(packet, &narration, tuning))];
|
||||
let first = match model.converse(&request_id(instance, "persona", tag, packet.seed), &tuning.persona.model, &turns) {
|
||||
let first = match model.converse(&call_id(&wake_id, "persona", 0), &tuning.persona.model, &turns) {
|
||||
Ok(spoke) => spoke,
|
||||
Err(failed) => {
|
||||
render["persona"] = json!({ "failed": failed.reason, "usage": failed.usage });
|
||||
|
|
@ -433,7 +441,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
|
|||
turns.push(Turn::Assistant { text: prose.clone(), states: first.states });
|
||||
let her = packet.forms;
|
||||
let (mut from, mut before, mut read_texts, mut read_bytes) = (0usize, String::new(), Vec::<String>::new(), 0usize);
|
||||
let mut decision = interpret::decide(model, &request_id(instance, "interpreter", tag, packet.seed), packet, &narration, &prose, tuning);
|
||||
let mut decision = interpret::decide(model, &call_id(&wake_id, "interpreter", 0), packet, &narration, &prose, tuning);
|
||||
let mut reads: Vec<Value> = Vec::new();
|
||||
let mut ended = None;
|
||||
while let (Some(reading), Some(read)) = (reading, decision.get("read").filter(|read| !read.is_null()).cloned()) {
|
||||
|
|
@ -472,7 +480,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
|
|||
reads.push(done);
|
||||
let bridge = read::bridge(her, &what, result.as_deref().map_err(|why| *why));
|
||||
turns.push(Turn::User(bridge.clone()));
|
||||
let continued = match model.converse(&request_id(instance, "persona", &format!("{tag}{}", reads.len()), packet.seed), &tuning.persona.model, &turns) {
|
||||
let continued = match model.converse(&call_id(&wake_id, "persona", reads.len()), &tuning.persona.model, &turns) {
|
||||
Ok(spoke) => spoke,
|
||||
Err(failed) => {
|
||||
decision["reads"] = json!(reads);
|
||||
|
|
@ -494,7 +502,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
|
|||
}
|
||||
decision["reads"] = json!(reads);
|
||||
let wake = interpret::Wake { earlier: Some(&decision), from, read: &read_texts, before: &before };
|
||||
let next = interpret::decide_in(model, &request_id(instance, "interpreter", &format!("{tag}{}", reads.len()), packet.seed), packet, &narration, &prose, tuning, wake);
|
||||
let next = interpret::decide_in(model, &call_id(&wake_id, "interpreter", reads.len()), packet, &narration, &prose, tuning, wake);
|
||||
// A failed call on a later segment fails that segment only: what was decided before it stands, and posts and
|
||||
// applies (Soul p5); the failure, with what it cost, is recorded beside it.
|
||||
if next.get("failed").is_some() {
|
||||
|
|
@ -615,6 +623,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
let other_inputs = Inputs { state: other, ..inputs };
|
||||
let others = successive(&other_inputs, tuning, renders.max(1));
|
||||
let other_narrations: Vec<Value> = others.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "b", Through::Projector, None)).collect();
|
||||
let judging = wake_id(&request.instance, "judge", request.seed);
|
||||
let mut verdicts = Vec::new();
|
||||
let mut right = 0usize;
|
||||
for (at, (narration, truth)) in narrations.iter().map(|one| (one, 'A')).chain(other_narrations.iter().map(|one| (one, 'B'))).enumerate() {
|
||||
|
|
@ -624,7 +633,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
let (first, second) = if swapped { (b, a) } else { (a, b) };
|
||||
// A narration that failed has nothing to judge; it is counted under the renders' failures.
|
||||
let Some(text) = narration["narration"].as_str() else { continue };
|
||||
let answer = match model.ask(&request_id(&request.instance, "judge", &at.to_string(), request.seed), &tuning.projector.model, &judge_prompt(first, second, text), None) {
|
||||
let answer = match model.ask(&call_id(&judging, "judge", at), &tuning.projector.model, &judge_prompt(first, second, text), None) {
|
||||
Ok(answer) => answer,
|
||||
Err(failed) => {
|
||||
verdicts.push(json!({ "from": truth.to_string(), "failed": failed.reason, "usage": failed.usage }));
|
||||
|
|
|
|||
|
|
@ -64,6 +64,33 @@ impl Model for Scripted {
|
|||
}
|
||||
}
|
||||
|
||||
/// The connector's replay store over `inner` (ModelConnector's contract): an id binds the first prompt it carried; that
|
||||
/// prompt again is answered with the first answer, any other is refused `ReplayConflict`, as the kit refuses it. Every
|
||||
/// id asked is kept in order.
|
||||
pub(crate) struct Replaying<'a> {
|
||||
pub inner: &'a dyn Model,
|
||||
pub ids: RefCell<Vec<String>>,
|
||||
bound: RefCell<std::collections::BTreeMap<String, (String, Answer)>>,
|
||||
}
|
||||
|
||||
impl<'a> Replaying<'a> {
|
||||
pub(crate) fn over(inner: &'a dyn Model) -> Self {
|
||||
Self { inner, ids: RefCell::default(), bound: RefCell::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Model for Replaying<'_> {
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str, schema: Option<&str>) -> std::result::Result<Answer, Failed> {
|
||||
self.ids.borrow_mut().push(request_id.to_owned());
|
||||
if let Some((sent, answer)) = self.bound.borrow().get(request_id) {
|
||||
return if sent == prompt { Ok(answer.clone()) } else { Err(Failed { reason: "the connector refused the call: ReplayConflict".into(), usage: None }) };
|
||||
}
|
||||
let answer = self.inner.ask(request_id, model, prompt, schema)?;
|
||||
self.bound.borrow_mut().insert(request_id.to_owned(), (prompt.to_owned(), answer.clone()));
|
||||
Ok(answer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fails the test if her stored state is asked for.
|
||||
pub(crate) struct NeverAsked;
|
||||
|
||||
|
|
|
|||
|
|
@ -707,8 +707,8 @@ fn echo(line: &str, bodies: &[String]) -> bool {
|
|||
/// One speech decision: the Interpreter's schema turn, then code's hold on each line. The report carries the lines said,
|
||||
/// the lines refused with why, the mentions dropped, the gaps, an unfinished draft, and the call's usage. A failed call
|
||||
/// or an answer that is not the contract is a failure, never a silence.
|
||||
pub(crate) fn decide(model: &dyn Model, request_id: &str, packet: &Packet, narration: &str, prose: &str, tuning: &Tuning) -> Value {
|
||||
decide_in(model, request_id, packet, narration, prose, tuning, Wake::default())
|
||||
pub(crate) fn decide(model: &dyn Model, segment_id: &str, packet: &Packet, narration: &str, prose: &str, tuning: &Tuning) -> Value {
|
||||
decide_in(model, segment_id, packet, narration, prose, tuning, Wake::default())
|
||||
}
|
||||
|
||||
/// One speech decision after the newest segment of her wake (`wake`), over the whole wake (Soul's R6): the Interpreter
|
||||
|
|
@ -717,8 +717,9 @@ pub(crate) fn decide(model: &dyn Model, request_id: &str, packet: &Packet, narra
|
|||
/// sent now. Earlier calls' acts and unsent drafts are not carried: every answer accounts for every draft of the wake
|
||||
/// (`unaccounted`), so the latest decision about a draft is its fate, and she is told it once (map 16.27). Their reads,
|
||||
/// refused reads and usage are carried. What she read joins what an echo is measured against, and `read` is the one read
|
||||
/// the newest segment reaches for, resolved, or none.
|
||||
pub(crate) fn decide_in(model: &dyn Model, request_id: &str, packet: &Packet, narration: &str, prose: &str, tuning: &Tuning, wake: Wake) -> Value {
|
||||
/// the newest segment reaches for, resolved, or none. Each attempt asks under its own id, `{segment_id}:{attempt}`: the
|
||||
/// retry is another prompt, and the connector refuses an id it saw with other bytes (`ReplayConflict`, Soul's R16).
|
||||
pub(crate) fn decide_in(model: &dyn Model, segment_id: &str, packet: &Packet, narration: &str, prose: &str, tuning: &Tuning, wake: Wake) -> Value {
|
||||
let found = drafts(prose);
|
||||
let segment = prose.get(wake.from..).unwrap_or_default();
|
||||
let prompt = speech_prompt_in(packet, narration, segment, tuning, wake.before, &found, &wake.earlier.and_then(|earlier| earlier["read_refused"].as_array()).cloned().unwrap_or_default());
|
||||
|
|
@ -737,8 +738,11 @@ pub(crate) fn decide_in(model: &dyn Model, request_id: &str, packet: &Packet, na
|
|||
// An answer that parsed, with its resolved gaps, its hold faults and its drafts accounted for nowhere.
|
||||
type Parsed = (Answer, Vec<Value>, Vec<String>, Vec<usize>);
|
||||
let mut parsed_first: Option<Parsed> = None;
|
||||
let mut attempt = 0usize;
|
||||
let (parsed, gaps, unopened, undecided) = loop {
|
||||
let answer = match model.ask(request_id, &tuning.interpreter.model, &asking, Some(SPEECH_SCHEMA)) {
|
||||
let request_id = format!("{segment_id}:{attempt}");
|
||||
attempt += 1;
|
||||
let answer = match model.ask(&request_id, &tuning.interpreter.model, &asking, Some(SPEECH_SCHEMA)) {
|
||||
Ok(answer) => answer,
|
||||
Err(failed) => {
|
||||
calls.push(json!({ "usage": failed.usage, "retry": false }));
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::path::Path;
|
|||
use serde_json::Value;
|
||||
|
||||
use super::*;
|
||||
use crate::projection::harness::tests::{Scripted, control};
|
||||
use crate::projection::harness::tests::{Replaying, Scripted, control};
|
||||
use serde_json::json;
|
||||
use crate::projection::narrate::PROJECTOR_OPENING;
|
||||
use crate::projection::{Answer, Failed, Model, Spent};
|
||||
|
|
@ -777,6 +777,68 @@ says = "good afternoon, everyone"
|
|||
assert!(model.calls.borrow().iter().any(|(_, prompt)| prompt.contains("Then he reads GameCult/Lodur#12 on the forge:")));
|
||||
}
|
||||
|
||||
/// Soul's R16: every call of a wake carries its own id, so the connector's replay store refuses none. Her wake reads
|
||||
/// once; the Interpreter's first answer on each segment is off the contract and asked again; the Persona goes on after
|
||||
/// the read. Under one id per segment the retry and the continuation are each another prompt, refused `ReplayConflict`.
|
||||
#[test]
|
||||
fn k58_every_call_of_a_wake_has_its_own_id_so_no_retry_or_continuation_is_a_replay_conflict() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
cast(dir.path());
|
||||
let path = dir.path().join("plan.toml");
|
||||
std::fs::write(
|
||||
&path,
|
||||
format!(
|
||||
r##"rounds = 1
|
||||
seed = 11
|
||||
wake = "ruled"
|
||||
beat_s = 0
|
||||
start = "2026-11-20T15:00:00Z"
|
||||
rooms = ["#minds:gamecult.org"]
|
||||
operators = ["@meta:gamecult.org"]
|
||||
[[cast]]
|
||||
id = "{TOBIAH}"
|
||||
name = "Tobiah"
|
||||
state = "tobiah.json"
|
||||
[[cast.read]]
|
||||
kind = "repo"
|
||||
target = "GameCult/Lodur#12"
|
||||
text = "SIM-READ: the knee bends first."
|
||||
[[cast]]
|
||||
id = "{LIBBY}"
|
||||
name = "Libby"
|
||||
state = "libby.json"
|
||||
[[message]]
|
||||
from = "@meta:gamecult.org"
|
||||
name = "Meta"
|
||||
room = "#minds:gamecult.org"
|
||||
says = "good afternoon, everyone"
|
||||
"##
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let scripted = Scripted::new(|prompt| if prompt.contains("SIM-READ") { "So the knee goes first.".into() } else { "I want to see what changed in GameCult/Lodur#12 today.".into() }).interpreting(|prompt| {
|
||||
if !prompt.contains("was not the speech contract") {
|
||||
return "not the contract".into();
|
||||
}
|
||||
let reads = prompt.contains("`read` names") && !prompt.contains("Earlier in his moment");
|
||||
let read = if reads { json!({ "kind": "repo", "target": "GameCult/Lodur#12", "quote": "I want to see what changed" }) } else { Value::Null };
|
||||
json!({ "say": [], "gaps": [], "read": read }).to_string()
|
||||
});
|
||||
let model = Replaying::over(&scripted);
|
||||
let (_, run) = run_plan(&path, &model);
|
||||
assert!(!run.to_string().contains("ReplayConflict"), "{run}");
|
||||
let turns = run["turns"].as_array().unwrap();
|
||||
let tobiah = turns.iter().find(|turn| turn["persona"] == "Tobiah").map(|turn| turn["report"]["renders"][0]["interpreter"].clone()).unwrap();
|
||||
assert!(tobiah.get("failed").is_none() && tobiah.get("later_failed").is_none() && tobiah.get("continuation_failed").is_none(), "{tobiah}");
|
||||
assert_eq!(tobiah["reads"][0]["ok"], json!(true), "{tobiah}");
|
||||
assert_eq!(tobiah["retried"]["failed"], json!("the answer is not the speech contract"), "the retry's answer stands: {tobiah}");
|
||||
let retries = tobiah["calls"].as_array().unwrap().iter().filter(|call| call["retry"] == true).count();
|
||||
assert_eq!(retries, 2, "one retry on each segment: {tobiah}");
|
||||
let ids = model.ids.borrow();
|
||||
let distinct: std::collections::BTreeSet<&String> = ids.iter().collect();
|
||||
assert_eq!(distinct.len(), ids.len(), "no id carried twice: {ids:?}");
|
||||
}
|
||||
|
||||
/// Soul's R5: the report shows the packet's flags, `promises_over` among them.
|
||||
#[test]
|
||||
fn r5_the_report_shows_the_packets_flags() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue