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
43 changes: 34 additions & 9 deletions apps/signalboxd/src/context_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use signalbox_application::{
};
use signalbox_domain::{
AcceptedInputTurnActivationIdentities, ContextFrontierId, FailedModelCallTurnIdentities,
ModelCallId, SemanticTranscriptEntryId, SessionId, TurnAttemptId, TurnId,
ModelCallId, ResolvedContextFrontierSnapshot, SemanticTranscriptEntryId, SessionId,
TurnAttemptId, TurnId,
};
use signalbox_model_provider_runtime::{ContextCompactionModel, RuntimeModelCatalog};
use signalbox_persistence::{
Expand Down Expand Up @@ -331,15 +332,15 @@ impl ReportedUsageCompaction {
});
let failure_requires_compaction = if reported_requires_compaction {
false
} else {
} else if let Some(persisted_prefix) =
persisted_preflight_prefix(preview.prepared().starting_snapshot())
{
self.model_calls
.request_too_large_requires_compaction(
session,
target,
operation.request().call().frontier().snapshot(),
)
.request_too_large_requires_compaction(session, target, persisted_prefix)
.await
.map_err(|source| ReportedUsageCompactionError::Model { turn, source })?
} else {
false
};
if !reported_requires_compaction && !failure_requires_compaction {
return Ok(None);
Expand Down Expand Up @@ -868,6 +869,14 @@ fn activation_identities() -> AcceptedInputTurnActivationIdentities {
)
}

fn persisted_preflight_prefix(
prospective: &ResolvedContextFrontierSnapshot,
) -> Option<ContextFrontierId> {
prospective
.immediate_semantic_prefix()
.map(|prefix| prefix.snapshot())
}

async fn close_failed_compaction_turn(
activation: &StartEligibleTurnRepository,
model_calls: &PostgresModelCallRepository,
Expand Down Expand Up @@ -903,7 +912,10 @@ mod tests {
};

use signalbox_application::{ClassifyOperatorFailure, OperatorFailureClass};
use signalbox_domain::{ActivatedTurn, TurnId};
use signalbox_domain::{
ActivatedTurn, ContextFrontierId, ResolvedContextFrontierReconstitutionInput, SessionId,
TurnId,
};
use signalbox_persistence::{
context_compaction::ContextCompactionRepositoryError,
model_execution::{ModelCallIdentityCollision, ModelCallRepositoryError},
Expand All @@ -915,7 +927,7 @@ mod tests {

use super::{
ContextGuardedTurnPassError, compaction_failure_closure_collision_is_retryable,
guarded_failure_stage, report_guarded_ambiguity,
guarded_failure_stage, persisted_preflight_prefix, report_guarded_ambiguity,
};
use crate::{
ActivatedTurnExecution, FatalExecutionSignal, FatalExecutionSupervisor,
Expand Down Expand Up @@ -992,6 +1004,19 @@ mod tests {
TurnId::from_uuid(uuid::Uuid::from_u128(1))
}

#[test]
fn request_failure_preflight_uses_the_durable_immediate_prefix() {
let session = SessionId::from_uuid(uuid::Uuid::from_u128(2));
let persisted = ContextFrontierId::from_uuid(uuid::Uuid::from_u128(3));
let prospective = ContextFrontierId::from_uuid(uuid::Uuid::from_u128(4));
let snapshot = ResolvedContextFrontierReconstitutionInput::new(session, persisted, vec![])
.derive_appending(prospective, vec![])
.reconstitute()
.expect("derived prospective frontier is valid");

assert_eq!(persisted_preflight_prefix(&snapshot), Some(persisted));
}

/// The exact lost-acknowledgement failure the activation repository reports
/// when a guarded counted commit cannot be proven.
fn ambiguous_activation() -> GuardedFailure {
Expand Down
6 changes: 4 additions & 2 deletions crates/persistence/src/model_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,13 @@ impl PostgresModelCallRepository {
/// A successor can use that typed terminal evidence to compact once, but a
/// later provider-accepted ordinary call or completed compaction on the
/// prospective lineage supersedes the failure so it cannot trigger forever.
/// The supplied frontier is the durable immediate prefix of an uncommitted
/// activation preview, so lineage checks never depend on a speculative ID.
pub async fn request_too_large_requires_compaction(
&self,
session: SessionId,
target: ResolvedProviderTarget,
prospective_frontier: ContextFrontierId,
persisted_prospective_prefix: ContextFrontierId,
) -> Result<bool, ModelCallRepositoryError> {
let requires_compaction = sqlx::query_scalar(
"WITH latest_failure AS MATERIALIZED (
Expand Down Expand Up @@ -1056,7 +1058,7 @@ impl PostgresModelCallRepository {
)
.bind(session_id_to_uuid(session))
.bind(target.identity().into_uuid())
.bind(prospective_frontier.into_uuid())
.bind(persisted_prospective_prefix.into_uuid())
.fetch_one(&self.pool)
.await?;
Ok(requires_compaction)
Expand Down
Loading