| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use maka_agent_facts::{ | |
| accounting::Outcome, | |
| context::{ | |
| CheckpointContent, CompactedContext, CompactionOptions, ModelClosureProtocol, | |
| ModelOperation, ModelPurpose, RequestContextEvidence, SummaryOrigin, SummaryResult, | |
| }, | |
| durable::*, | |
| history_snapshot::RequestSource, | |
| model::ModelFinishReason, | |
| }; | |
| use maka_event_log::{StoreError, connection::Query}; | |
| use rusqlite::{Connection, OptionalExtension, params}; | |
| use serde::de::DeserializeOwned; | |
| use super::{decode, identity, model_attempt, owned_content, pin, request}; | |
| use crate::{ | |
| fold::{Attempt, RunView}, | |
| store::{fallback::valid_summary, invalid}, | |
| }; | |
| pub(super) fn ends_run(fact: &Fact) -> bool { | |
| matches!( | |
| fact, | |
| Fact::RunTerminated { | |
| terminal: Terminal::Completed | Terminal::Cancelled, | |
| .. | |
| } | |
| ) | |
| } | |
| fn original<T: DeserializeOwned>( | |
| c: &Connection, | |
| run: RunId, | |
| reference: &ContentRef, | |
| ) -> Result<T, StoreError> { | |
| Ok(serde_json::from_slice(&owned_content(c, run, reference)?)?) | |
| } | |
| pub(super) fn validate_proactive_request( | |
| c: &Connection, | |
| view: &RunView, | |
| summary: &RequestSpec, | |
| surface: &maka_agent_facts::composition::CompositionSurface, | |
| ) -> Result<(), StoreError> { | |
| let main = view | |
| .current_step | |
| .ok_or_else(|| invalid("proactive Summary has no original Main Step"))?; | |
| let main = request(c, view.run, main)?; | |
| if main.accounting.purpose != ModelPurpose::Main | |
| || summary.accounting.purpose != ModelPurpose::Summary | |
| || summary.compaction_recovery.is_some() | |
| || summary.summary_repair_of.is_some() | |
| || !summary.steering.is_empty() | |
| || !surface.tools.is_empty() | |
| || !matches!(view.spec.origin, RunOrigin::Local) | |
| { | |
| return Err(invalid("invalid proactive Summary request")); | |
| } | |
| Ok(()) | |
| } | |
| pub(crate) struct ReadCompactionOptions(pub RunId, pub ContentRef, pub ToolKey); | |
| impl Query for ReadCompactionOptions { | |
| type Output = CompactionOptions; | |
| fn execute(self, c: &Connection) -> Result<Self::Output, StoreError> { | |
| compaction_options(c, self.0, &self.1, &self.2) | |
| } | |
| } | |
| fn compaction_options( | |
| c: &Connection, | |
| run: RunId, | |
| capture: &ContentRef, | |
| key: &ToolKey, | |
| ) -> Result<CompactionOptions, StoreError> { | |
| let source: RequestSource = original(c, run, capture)?; | |
| let call = crate::session_history::captured_tool_call(c, &source.current, run, key)?; | |
| Ok(CompactionOptions::from_input(&call.input)?) | |
| } | |
| pub(crate) struct CountMatchingFailedProactiveSummaries { | |
| pub run: RunId, | |
| pub main_step: StepId, | |
| pub options: CompactionOptions, | |
| pub stop_after: u64, | |
| } | |
| fn attempt_failure_applies( | |
| c: &Connection, | |
| run: RunId, | |
| attempt: &Attempt, | |
| main_provenance: &RequestContextEvidence, | |
| ) -> Result<bool, StoreError> { | |
| let protocol: Option<maka_agent_facts::context::ModelFailureProtocol> = | |
| if let Some(reference) = &attempt.source_failure { | |
| Some(original(c, run, reference)?) | |
| } else if let Some(closed) = attempt | |
| .closed | |
| .as_ref() | |
| .filter(|closed| closed.outcome == Outcome::Error) | |
| && closed | |
| .protocol | |
| .get("failure") | |
| .is_some_and(|failure| !failure.is_null()) | |
| { | |
| Some(serde_json::from_value(closed.protocol.clone())?) | |
| } else { | |
| None | |
| }; | |
| let Some(protocol) = protocol else { | |
| return Ok(true); | |
| }; | |
| Ok(match &protocol.failure { | |
| maka_agent_facts::model::error::ModelError::Cancelled | |
| | maka_agent_facts::model::error::ModelError::LocalAuthorization(_) | |
| | maka_agent_facts::model::error::ModelError::CleanupUnconfirmed => false, | |
| maka_agent_facts::model::error::ModelError::RequestEncodingRejected { | |
| encoding, .. | |
| } => main_provenance.encoding_matches_compression(encoding), | |
| _ => true, | |
| }) | |
| } | |
| impl Query for CountMatchingFailedProactiveSummaries { | |
| type Output = u64; | |
| fn execute(self, c: &Connection) -> Result<Self::Output, StoreError> { | |
| if self.stop_after == 0 { | |
| return Ok(0); | |
| } | |
| let main_request = request(c, self.run, self.main_step)?; | |
| let main_provenance: RequestContextEvidence = | |
| original(c, self.run, &main_request.provenance)?; | |
| let (namespace_id, admission_sequence): (i64, i64) = c | |
| .prepare_cached( | |
| "SELECT n.id, r.admission_sequence | |
| FROM agent_runs r | |
| JOIN agent_namespaces n ON n.owner=r.owner AND n.session=r.session | |
| WHERE r.run=?1", | |
| )? | |
| .query_row([self.run.to_string()], |row| Ok((row.get(0)?, row.get(1)?))) | |
| .optional()? | |
| .ok_or_else(|| invalid("proactive summary history namespace missing"))?; | |
| let mut statement = c.prepare_cached( | |
| "SELECT a.body | |
| FROM agent_session_history h INDEXED BY agent_session_history_kind | |
| JOIN agent_attempts a ON a.run=h.run AND a.key=h.attempt | |
| WHERE h.namespace_id=?1 | |
| AND h.kind='proactive_summary_prepared' | |
| AND h.run=?2 | |
| AND h.sequence>=?3 | |
| AND (json_extract(a.body,'$.sourceFailure') IS NOT NULL | |
| OR json_extract(a.body,'$.closed.outcome')='error') | |
| ORDER BY h.sequence DESC", | |
| )?; | |
| let mut rows = statement.query(params![ | |
| namespace_id, | |
| self.run.to_string(), | |
| admission_sequence | |
| ])?; | |
| let mut count = 0u64; | |
| while let Some(row) = rows.next()? { | |
| let body: String = row.get(0)?; | |
| let attempt: Attempt = serde_json::from_str(&body)?; | |
| let summary_request = request(c, self.run, attempt.step)?; | |
| if summary_request.accounting.purpose != ModelPurpose::Summary | |
| || summary_request.compaction_recovery.is_some() | |
| || summary_request.summary_repair_of.is_some() | |
| { | |
| continue; | |
| } | |
| let summary_provenance: RequestContextEvidence = | |
| original(c, self.run, &summary_request.provenance)?; | |
| if summary_provenance.previous_checkpoint_id != main_provenance.previous_checkpoint_id { | |
| break; | |
| } | |
| if main_provenance.accepts_background_summary(&summary_provenance, &self.options) | |
| && main_provenance.same_summary_route( | |
| &main_request, | |
| &summary_request, | |
| &summary_provenance, | |
| ) | |
| && attempt_failure_applies(c, self.run, &attempt, &main_provenance)? | |
| { | |
| count = count.saturating_add(1); | |
| if count >= self.stop_after { | |
| break; | |
| } | |
| } | |
| } | |
| Ok(count) | |
| } | |
| } | |
| pub(crate) struct PreviousProactiveSummary(pub RunId, pub StepId); | |
| impl Query for PreviousProactiveSummary { | |
| type Output = Option<SummaryOrigin>; | |
| fn execute(self, c: &Connection) -> Result<Self::Output, StoreError> { | |
| previous_proactive_summary(c, self.0, self.1) | |
| } | |
| } | |
| fn previous_proactive_summary( | |
| c: &Connection, | |
| run: RunId, | |
| step: StepId, | |
| ) -> Result<Option<SummaryOrigin>, StoreError> { | |
| let candidate: Option<(String, String)> = c | |
| .query_row( | |
| "SELECT r.run,r.proactive_summary FROM agent_runs current | |
| JOIN agent_runs r ON r.owner=current.owner AND r.session=current.session | |
| JOIN agent_attempts a ON a.run=r.run AND a.key=r.proactive_summary | |
| WHERE current.run=? AND r.admission_sequence<current.admission_sequence | |
| AND r.local=1 AND r.cancelled_by_risk=0 AND r.proactive_summary IS NOT NULL | |
| AND json_extract(r.terminal,'$') IN ('completed','cancelled') | |
| AND json_extract(a.body,'$.closed.outcome')='success' | |
| AND json_extract(a.body,'$.cleanup.kind')='confirmed' | |
| ORDER BY r.admission_sequence DESC LIMIT 1", | |
| [run.to_string()], | |
| |row| Ok((row.get(0)?, row.get(1)?)), | |
| ) | |
| .optional()?; | |
| let Some((origin_run, attempt)) = candidate else { | |
| return Ok(None); | |
| }; | |
| let origin = SummaryOrigin { | |
| run: identity(origin_run)?, | |
| attempt: identity(attempt)?, | |
| }; | |
| let attempt = model_attempt(c, origin.run, origin.attempt)?; | |
| let summary = request(c, origin.run, attempt.step)?; | |
| let main = request(c, run, step)?; | |
| if !valid_summary(c, origin.run, &summary, &attempt)? | |
| || !extends_summary(c, origin.run, &summary, run, &main)? | |
| { | |
| return Ok(None); | |
| } | |
| Ok(Some(origin)) | |
| } | |
| fn extends_summary( | |
| c: &Connection, | |
| producer: RunId, | |
| summary: &RequestSpec, | |
| run: RunId, | |
| main: &RequestSpec, | |
| ) -> Result<bool, StoreError> { | |
| if main.accounting.purpose != ModelPurpose::Main | |
| || summary.accounting.purpose != ModelPurpose::Summary | |
| { | |
| return Ok(false); | |
| } | |
| let summary_context: RequestContextEvidence = original(c, producer, &summary.provenance)?; | |
| let main_context: RequestContextEvidence = original(c, run, &main.provenance)?; | |
| let options = match &main_context.compaction_request { | |
| Some(key) => compaction_options(c, run, &main.source_capture, key)?, | |
| None => CompactionOptions::default(), | |
| }; | |
| if !main_context.accepts_background_summary(&summary_context, &options) { | |
| return Ok(false); | |
| } | |
| if producer != run { | |
| let prefix: RequestSource = original(c, producer, &summary.source_capture)?; | |
| let pending: RequestSource = original(c, run, &main.source_capture)?; | |
| return Ok(prefix.current.through_sequence <= pending.current.through_sequence); | |
| } | |
| // The same-Run Summary may include the pending Main's StepPrepared fact, | |
| // after the Main froze its input. Replay checks their logical message prefix. | |
| Ok(true) | |
| } | |
| fn proactive_summary( | |
| c: &Connection, | |
| view: &RunView, | |
| material: &CompactedContext, | |
| at: UnixMillis, | |
| ) -> Result<Option<(RunId, RequestSpec, Attempt)>, StoreError> { | |
| let step = material.checkpoint.summary_step_id; | |
| let (producer, attempt) = if let Some(origin) = material.summary_origin { | |
| let earlier: bool = c.query_row( | |
| "SELECT EXISTS(SELECT 1 FROM agent_run_specs current | |
| JOIN agent_facts admitted ON admitted.run=current.run AND admitted.revision=1 | |
| JOIN agent_runs r ON r.owner=current.owner AND r.session=current.session | |
| WHERE current.run=? AND r.run=? AND r.admission_sequence<admitted.sequence | |
| AND r.local=1 AND r.cancelled_by_risk=0 AND r.proactive_summary=? | |
| AND json_extract(r.terminal,'$') IN ('completed','cancelled'))", | |
| params![ | |
| view.run.to_string(), | |
| origin.run.to_string(), | |
| origin.attempt.to_string() | |
| ], | |
| |row| row.get(0), | |
| )?; | |
| if !earlier { | |
| return Err(invalid( | |
| "compaction does not name an earlier proactive Summary in this Session", | |
| )); | |
| } | |
| (origin.run, model_attempt(c, origin.run, origin.attempt)?) | |
| } else if let Some((_, attempt)) = view.proactive_summary_attempt() | |
| && attempt.step == step | |
| { | |
| (view.run, attempt.clone()) | |
| } else { | |
| return Ok(None); | |
| }; | |
| let summary = request(c, producer, step)?; | |
| if attempt.step != step | |
| || attempt.cleanup != maka_plugin_core::operation::OperationCleanup::Confirmed | |
| || !valid_summary(c, producer, &summary, &attempt)? | |
| { | |
| return Err(invalid( | |
| "compaction requires its successful proactive Summary and cleanup", | |
| )); | |
| } | |
| let main = view | |
| .current_step | |
| .ok_or_else(|| invalid("proactive adoption has no pending Main"))?; | |
| let main = request(c, view.run, main)?; | |
| if !extends_summary(c, producer, &summary, view.run, &main)? { | |
| return Err(invalid( | |
| "proactive Summary has another checkpoint predecessor or source", | |
| )); | |
| } | |
| let adopt = if let Some(id) = view.current_attempt | |
| && let Some(attempt) = view.attempts.get(&id) | |
| && attempt.step == main.step | |
| { | |
| attempt.admission.is_some() | |
| && attempt.compactable_overflow() | |
| && !attempt | |
| .closed | |
| .as_ref() | |
| .is_some_and(|closed| closed.outcome == Outcome::Unknown) | |
| } else { | |
| let evidence: RequestContextEvidence = original(c, view.run, &main.provenance)?; | |
| evidence.needs_compaction() | |
| }; | |
| if !adopt | |
| || view | |
| .spec | |
| .budget | |
| .deadline | |
| .is_some_and(|deadline| at >= deadline) | |
| || view | |
| .spec | |
| .budget | |
| .tokens | |
| .is_some_and(|limit| view.tokens >= limit) | |
| { | |
| return Err(invalid( | |
| "proactive adoption has not reached the frozen threshold or no-output overflow", | |
| )); | |
| } | |
| Ok(Some((producer, summary, attempt))) | |
| } | |
| pub(super) fn validate( | |
| c: &Connection, | |
| view: &RunView, | |
| reference: &ContentRef, | |
| at: UnixMillis, | |
| saving_for_next_run: bool, | |
| ) -> Result<(), StoreError> { | |
| if view.stopped && !saving_for_next_run { | |
| return Err(invalid( | |
| "stopped Run can only save compaction for the next Run", | |
| )); | |
| } | |
| let material: CompactedContext = decode(c, reference)?; | |
| material.validate().map_err(invalid)?; | |
| let checkpoint = &material.checkpoint; | |
| let deferred; | |
| let proactive; | |
| let (producer, request, attempt) = | |
| if let Some(candidate) = proactive_summary(c, view, &material, at)? { | |
| proactive = candidate; | |
| (proactive.0, proactive.1, &proactive.2) | |
| } else if view.current_step == Some(checkpoint.summary_step_id) { | |
| let attempt_id = view | |
| .current_attempt | |
| .ok_or_else(|| invalid("compaction has no original summary attempt"))?; | |
| let attempt = view | |
| .attempts | |
| .get(&attempt_id) | |
| .ok_or_else(|| invalid("compaction summary attempt projection is absent"))?; | |
| if attempt.step != checkpoint.summary_step_id || attempt.admission.is_none() { | |
| return Err(invalid( | |
| "compaction does not name the current admitted summary attempt", | |
| )); | |
| } | |
| (view.run, request(c, view.run, attempt.step)?, attempt) | |
| } else { | |
| deferred = crate::store::fallback::deferred_summary( | |
| c, | |
| view, | |
| checkpoint.summary_step_id, | |
| at, | |
| saving_for_next_run, | |
| )?; | |
| (view.run, deferred.0, &deferred.1) | |
| }; | |
| let closed = attempt | |
| .closed | |
| .as_ref() | |
| .ok_or_else(|| invalid("compaction summary has no original provider closure"))?; | |
| if closed.outcome != Outcome::Success || closed.diagnostic.is_some() { | |
| return Err(invalid( | |
| "compaction requires an actual successful summary closure", | |
| )); | |
| } | |
| request.accounting.validate().map_err(invalid)?; | |
| if request.accounting.purpose != ModelPurpose::Summary { | |
| return Err(invalid( | |
| "compaction source request is not a summary request", | |
| )); | |
| } | |
| if material.source_capture != request.source_capture | |
| || checkpoint.source_digest != request.source_digest | |
| { | |
| return Err(invalid( | |
| "compaction differs from the original frozen summary source", | |
| )); | |
| } | |
| let source: RequestSource = original(c, producer, &request.source_capture)?; | |
| crate::session_history::validate_cut(c, &view.spec.owner, &view.spec.session_id, &source.cut)?; | |
| crate::session_history::validate_cut( | |
| c, | |
| &view.spec.owner, | |
| &view.spec.session_id, | |
| &source.current, | |
| )?; | |
| if source.cut.through_sequence > source.current.through_sequence | |
| || checkpoint.covered_through != source.current.through_sequence | |
| { | |
| return Err(invalid( | |
| "compaction coverage differs from the original canonical source cut", | |
| )); | |
| } | |
| let provenance: RequestContextEvidence = original(c, producer, &request.provenance)?; | |
| provenance.breakdown.validate().map_err(invalid)?; | |
| if let Some(context) = &provenance.model_context { | |
| context.validate().map_err(invalid)?; | |
| } | |
| if checkpoint.previous_checkpoint_id != provenance.previous_checkpoint_id { | |
| return Err(invalid( | |
| "compaction predecessor differs from the request's frozen selected marker", | |
| )); | |
| } | |
| if let Some(marker) = &provenance.previous_checkpoint_id { | |
| let marker = serde_json::from_value::<FactId>(serde_json::Value::String(marker.clone()))?; | |
| if !crate::session_history::snapshot::checkpoint_member(c, view, &source, marker)? | |
| && !(provenance.codex_preflight.is_some() | |
| && provenance | |
| .model_context | |
| .as_ref() | |
| .is_some_and(|context| context.operation == ModelOperation::CodexCompaction) | |
| && same_preturn_dependency(c, view, &source, marker)?) | |
| { | |
| return Err(invalid( | |
| "compaction predecessor is outside its actual canonical source closure", | |
| )); | |
| } | |
| } | |
| let protocol: ModelClosureProtocol = serde_json::from_value(closed.protocol.clone())?; | |
| if protocol.finish_reason != ModelFinishReason::Stop { | |
| return Err(invalid( | |
| "compaction summary has an invalid original finish reason", | |
| )); | |
| } | |
| if matches!( | |
| &protocol.summary, | |
| Some(SummaryResult::ProviderEffectsUnknown { .. }) | |
| ) { | |
| return Err(invalid( | |
| "compaction cannot adopt a summary with unknown provider effects", | |
| )); | |
| } | |
| let native_operation = provenance | |
| .model_context | |
| .as_ref() | |
| .is_some_and(|context| context.operation == ModelOperation::CodexCompaction); | |
| if native_operation { | |
| let native = material | |
| .native | |
| .as_ref() | |
| .ok_or_else(|| invalid("native compaction omitted its complete original material"))?; | |
| let item = protocol | |
| .compaction | |
| .as_ref() | |
| .ok_or_else(|| invalid("native compaction has no original provider item"))?; | |
| let producer = provenance | |
| .model_context | |
| .as_ref() | |
| .and_then(|context| context.codex.as_ref()) | |
| .ok_or_else(|| invalid("native compaction has no original producer provenance"))?; | |
| let tokens = closed | |
| .usage | |
| .output_tokens | |
| .ok_or_else(|| invalid("native compaction has no original output-token observation"))?; | |
| if native.item != *item | |
| || native.producer != *producer | |
| || native.continuation.as_ref() | |
| != provenance.codex_preflight.as_ref().map(|plan| &plan.target) | |
| || checkpoint.summary != CheckpointContent::native(item, tokens) | |
| { | |
| return Err(invalid( | |
| "native compaction differs from original item, producer or observed usage", | |
| )); | |
| } | |
| } else { | |
| if protocol.compaction.is_some() || material.native.is_some() { | |
| return Err(invalid( | |
| "text summary cannot adopt a native item without original native-operation provenance", | |
| )); | |
| } | |
| let CheckpointContent::Text(expected) = &checkpoint.summary else { | |
| return Err(invalid( | |
| "text compaction requires original textual summary evidence", | |
| )); | |
| }; | |
| if expected.trim().is_empty() { | |
| return Err(invalid("compaction summary text is empty")); | |
| } | |
| let Some(SummaryResult::Text { summary }) = &protocol.summary else { | |
| return Err(invalid( | |
| "compaction has no original validated textual summary", | |
| )); | |
| }; | |
| if &summary.text != expected { | |
| return Err(invalid( | |
| "compaction text differs from the original validated summary", | |
| )); | |
| } | |
| } | |
| pin(c, view.run, &material.source_capture)?; | |
| Ok(()) | |
| } | |
| // A second pre-turn compaction consumes the installed first result without | |
| // extending raw-history coverage across this turn's still-pending user input. | |
| fn same_preturn_dependency( | |
| c: &Connection, | |
| view: &RunView, | |
| source: &RequestSource, | |
| marker: FactId, | |
| ) -> Result<bool, StoreError> { | |
| if source.current_revision != 0 { | |
| return Ok(false); | |
| } | |
| let Some(baseline) = &view.baseline else { | |
| return Ok(false); | |
| }; | |
| let Some(recorded) = super::fact(c, view.run, marker)? else { | |
| return Ok(false); | |
| }; | |
| if recorded.revision > view.revision | |
| || !matches!(&recorded.fact, Fact::ContextCompacted { baseline: actual } if actual == baseline) | |
| { | |
| return Ok(false); | |
| } | |
| let previous: CompactedContext = original(c, view.run, baseline)?; | |
| let producer = previous | |
| .summary_origin | |
| .map_or(view.run, |origin| origin.run); | |
| let captured: RequestSource = original(c, producer, &previous.source_capture)?; | |
| Ok(captured.current_revision == 0 | |
| && captured.cut == source.cut | |
| && captured.current == source.current) | |
| } |