Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions core/continuum-mind/src/persona/card_hands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,32 @@ pub(crate) async fn end_card_turn(card: &CardHands, acts: usize) {
crate::persona::card_carry::request();
}

/// A held card's end-of-turn sync (commit and push, up to `SYNC_BOUND`), handed back to the
/// turn's owner instead of awaited inside the drive, so her reply is published first and the
/// sync still runs before anything else touches the checkout (LOCKS-AND-FIFOS-AUDIT.md row 11).
#[must_use = "run the card sync before her next act touches the checkout"]
pub(crate) struct CardSync {
hands: Option<CardHands>,
acts: usize,
}

impl CardSync {
/// No sync owed: the turn held no card, or its sync already ran.
pub(crate) fn done() -> Self {
Self { hands: None, acts: 0 }
}

/// Commit and push what the turn did in the card's checkout.
pub(crate) async fn run(self) {
if let Some(hands) = &self.hands {
end_card_turn(hands, self.acts).await;
}
}
}

/// One turn in the hands of the card she holds, start to end (card 959f3d5c): rooted at
/// the held card, its credit captured on her act, driven in the card claim's hands, and its
/// work synced to the card's branch. A room turn and her own-time turn are the same turn
/// card sync returned for the caller to run after it publishes what she said. A room turn and her own-time turn are the same turn
/// here; with no card held it drives in her default hands with no capture. The credit is
/// returned from the CARD, not her hands (see [`HeldCardTurn::credit`]).
pub(crate) async fn drive_in_held_card(
Expand All @@ -417,6 +440,7 @@ pub(crate) async fn drive_in_held_card(
) -> (
crate::cognition::act_observe::SettleOutcome,
Option<crate::persona::training_producer::CapturedCredit>,
CardSync,
) {
let held = root_at_held_card(cycle, persona_id, &*conversation).await;
// Her credit follows the card she holds through the turn (card 12ddd78f): a turn that
Expand All @@ -435,10 +459,9 @@ pub(crate) async fn drive_in_held_card(
Some(&mut capture as &mut dyn crate::cognition::act_observe::TurnCredit),
)
.await;
if let Some(hands) = &held.hands {
end_card_turn(hands, outcome.acts).await;
}
(outcome, capture.into_credit())
let credit = capture.into_credit();
let sync = CardSync { hands: held.hands, acts: outcome.acts };
(outcome, credit, sync)
}

impl HeldCardTurn {
Expand Down
3 changes: 2 additions & 1 deletion core/continuum-mind/src/persona/initiative_turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub(crate) async fn drive_initiative_turn(
// forever. Her held card's turn, exactly as a room turn drives it; with no card held this
// is her default hands and no capture, as before. Bookkeeping on her own act: it changes
// neither her framing nor what she is told.
let (settled, _) = crate::persona::card_hands::drive_in_held_card(
let (settled, _, sync) = crate::persona::card_hands::drive_in_held_card(
cycle,
ctx.identity.peer_id.as_uuid(),
&ctx.identity.agent_name,
Expand All @@ -117,6 +117,7 @@ pub(crate) async fn drive_initiative_turn(
conversation,
)
.await;
sync.run().await;
let acts = settled.acts;
let (step, _, _) = SettleStep::from_settled(settled);
let capacity = match &step {
Expand Down
16 changes: 13 additions & 3 deletions core/continuum-mind/src/persona/service_loop/turn_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub(super) struct SpokenTurn {
pub(super) response_text: String,
pub(super) turn_credit: Option<crate::persona::training_producer::CapturedCredit>,
pub(super) turn_generation_receipts: Vec<crate::ai::provenance::GenerationReceipt>,
/// Her held card's sync, run right after the reply is published.
pub(super) card_sync: crate::persona::card_hands::CardSync,
}

/// What the cycle needs beyond the trigger: the burst it reasons over and the engram of
Expand Down Expand Up @@ -178,12 +180,12 @@ pub(super) async fn run_turn_cycle(
Some(ctx.identity.peer_id.to_string()),
);
let turn_act_count;
let (step, turn_metrics, settled_receipts, turn_credit) = {
let (step, turn_metrics, settled_receipts, turn_credit, card_sync) = {
// A citizen holding a card works at that repo, in a ROOM turn too: the turn
// is bound to her card claim's hands (card fc9e9fd7), its credit captured on
// her act, and its work synced to the card's branch, by the one owner her
// own-time turn uses as well (card 959f3d5c).
let (outcome, credit) = crate::persona::card_hands::drive_in_held_card(
let (outcome, credit, card_sync) = crate::persona::card_hands::drive_in_held_card(
&cycle,
ctx.identity.peer_id.as_uuid(),
&ctx.identity.agent_name,
Expand All @@ -202,7 +204,7 @@ pub(super) async fn run_turn_cycle(
// The driver stays a driver: no learning policy at any call site.
turn_act_count = outcome.acts;
let (step, metrics, receipts) = crate::cognition::act_observe::SettleStep::from_settled(outcome);
(step, metrics, receipts, credit)
(step, metrics, receipts, credit, card_sync)
};
// The credit is the turn's held CARD, captured with it, NOT read back out of
// `acting_card_of`, a mutable persona-global that a later focus rebind would
Expand Down Expand Up @@ -241,6 +243,13 @@ pub(super) async fn run_turn_cycle(
deferred_input.settle(msg, wake_engram, attention, &step);
// The room that chose this turn's cycle above, so the notices it read are the ones answered.
deferred_input.notices_read(turn_room, &turn_generation_receipts);
// A turn with a reply hands its card sync to the publish; any other runs it now.
let card_sync = if matches!(step, crate::cognition::act_observe::SettleStep::Spoke(_)) {
card_sync
} else {
card_sync.run().await;
crate::persona::card_hands::CardSync::done()
};
match step {
crate::cognition::act_observe::SettleStep::CapacityRefused { .. }
| crate::cognition::act_observe::SettleStep::DeferredBeforeDispatch => {
Expand All @@ -251,6 +260,7 @@ pub(super) async fn run_turn_cycle(
response_text: text,
turn_credit,
turn_generation_receipts,
card_sync,
})
}
crate::cognition::act_observe::SettleStep::Acted { calls, intent } => {
Expand Down
6 changes: 6 additions & 0 deletions core/continuum-mind/src/persona/service_loop/turn_settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(super) async fn finish_spoken_turn(
response_text,
turn_credit,
turn_generation_receipts,
card_sync,
} = spoken;
// Invariant: a raw tool-call envelope must NEVER reach the room — from ANY
// path. The cycle's verdict text is sometimes just `{"tool_call":…}` (an
Expand All @@ -33,6 +34,7 @@ pub(super) async fn finish_spoken_turn(
"verdict was a raw tool-call envelope — not broadcasting"
);
outcome.turns_skipped += 1;
card_sync.run().await;
return SpeechMark::Done;
}

Expand All @@ -53,6 +55,7 @@ pub(super) async fn finish_spoken_turn(
"her reply could not be recorded before posting, so it was not posted (fail closed)"
);
outcome.turns_errored += 1;
card_sync.run().await;
return SpeechMark::Refused;
}
// Per #195 slice 1: time the airc publish + downstream ack.
Expand All @@ -68,6 +71,9 @@ pub(super) async fn finish_spoken_turn(
)
.await;
phase_timings.say_ms = say_started.elapsed().as_millis() as u64;
// Her reply is out; the card's commit and push follow it, before the act question below
// touches the same checkout.
card_sync.run().await;
if let Err(e) = say_result {
tracing::warn!(
lamport = msg.lamport,
Expand Down
Loading