Skip to content

Commit aa191fc

Browse files
committed
perf(agent): deduplicate step metadata, tool contracts, external descriptors, and return payloads
Eliminate structural duplication in newly written durable fact payloads while preserving full read compatibility with historical payloads: - Reference-share repeated tool contracts, external effect definitions (StoredToolDefinition::{Inline, Ref}), and external instructions (instructions_ref) via content-addressed ContentRef blobs. - Deduplicate redundant projection text on ('text', 'text') and ('cell', 'text') tool returns via StoredToolResult::FromOutput and strip redundant inline CellReply.observation on Completed outputs (keeping observation on Interrupted for cross-run external wait reads). - Switch accept_cell_with_tool to StoredToolResult so compact CellCompleted returns round-trip cleanly across live runs and upgrade_compat capsules. Refs #7272 Generated-by: Antigravity
1 parent 71ee1cf commit aa191fc

30 files changed

Lines changed: 2483 additions & 245 deletions

File tree

‎plugins/agent/engine/src/driver/cells.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::*;
2121
use maka_agent_facts::cells::{
2222
CellEvent, CellId, CellNotification, CellNotificationBody, CellReplyState, CellStatus,
2323
};
24-
use maka_agent_facts::tool_output::{ReturnedToolResult, ToolOutput};
24+
use maka_agent_facts::tool_output::{StoredToolResult, ToolOutput};
2525
impl Engine {
2626
pub(super) async fn resolve_recovered_cells(
2727
&self,
@@ -153,9 +153,14 @@ impl Engine {
153153
.store
154154
.content(view.run, returned.result.clone())
155155
.await?;
156-
let original: maka_agent_facts::artifact::ToolReturn<ReturnedToolResult> =
156+
let original: maka_agent_facts::artifact::ToolReturn<StoredToolResult> =
157157
serde_json::from_slice(&bytes).map_err(StoreError::from)?;
158-
let ToolOutput::Cell(reply) = &original.body.output else {
158+
let output = match &original.body {
159+
StoredToolResult::Inline(body) => &body.output,
160+
StoredToolResult::CellCompleted(body) => &body.output,
161+
StoredToolResult::Operation(_) => return Ok(vec![]),
162+
};
163+
let ToolOutput::Cell(reply) = output else {
159164
return Ok(vec![]);
160165
};
161166
let CellReplyState::Completed { result } = &reply.state else {

‎plugins/agent/engine/src/reconcile/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ pub(crate) fn model_closure(
305305
source_digest: source.digest.clone(),
306306
effective_source_digest: source.digest.clone(),
307307
provenance: source.clone(),
308+
tool_contracts: None,
308309
projection: None,
309310
adjudications: Vec::new(),
310311
steering: Vec::new(),
@@ -1508,6 +1509,7 @@ fn fallback_model_level(step: StepId, request: &ContentRef) -> ModelLevel {
15081509
source_digest: request.digest.clone(),
15091510
effective_source_digest: request.digest.clone(),
15101511
provenance: request.clone(),
1512+
tool_contracts: None,
15111513
projection: None,
15121514
adjudications: vec![],
15131515
steering: vec![],

‎plugins/agent/facts/src/cells.rs‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,14 +1116,57 @@ pub enum CellObservation {
11161116

11171117
/// Shared Cell child result body. The surrounding ToolReturn owns exact media
11181118
/// manifests; standalone plain tools may omit a model projection entirely.
1119-
#[derive(Clone, Debug, Serialize, Deserialize)]
1120-
#[serde(rename_all = "camelCase", deny_unknown_fields)]
1119+
#[derive(Clone, Debug)]
11211120
pub struct CellReturnedResult {
11221121
pub output: crate::tool_output::ToolOutput,
11231122
pub projection: Option<crate::tool_output::DurableToolProjection>,
11241123
pub is_error: bool,
11251124
}
11261125

1126+
#[derive(Serialize, Deserialize)]
1127+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
1128+
struct StoredCellReturnedResult<O, P> {
1129+
output: O,
1130+
projection: Option<crate::tool_output::StoredProjection<P>>,
1131+
is_error: bool,
1132+
}
1133+
1134+
impl Serialize for CellReturnedResult {
1135+
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1136+
let projection = self
1137+
.projection
1138+
.as_ref()
1139+
.map(|projection| crate::tool_output::store_projection(&self.output, projection));
1140+
StoredCellReturnedResult {
1141+
output: &self.output,
1142+
projection,
1143+
is_error: self.is_error,
1144+
}
1145+
.serialize(serializer)
1146+
}
1147+
}
1148+
1149+
impl<'de> Deserialize<'de> for CellReturnedResult {
1150+
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1151+
let StoredCellReturnedResult {
1152+
output,
1153+
projection,
1154+
is_error,
1155+
} = StoredCellReturnedResult::<
1156+
crate::tool_output::ToolOutput,
1157+
crate::tool_output::DurableToolProjection,
1158+
>::deserialize(deserializer)?;
1159+
let projection = projection
1160+
.map(|projection| crate::tool_output::resolve_projection(&output, projection))
1161+
.transpose()?;
1162+
Ok(Self {
1163+
output,
1164+
projection,
1165+
is_error,
1166+
})
1167+
}
1168+
}
1169+
11271170
/// Local and historical Cell returns use the original body. Host returns can
11281171
/// share the same stored operation envelope as ordinary Agent tools.
11291172
#[derive(Serialize, Deserialize)]
@@ -1141,3 +1184,52 @@ impl StoredCellReturn {
11411184
}
11421185
}
11431186
}
1187+
1188+
#[cfg(test)]
1189+
mod tests {
1190+
use super::*;
1191+
use crate::tool_output::{DurableToolProjection, ToolOutput};
1192+
use serde_json::json;
1193+
1194+
#[test]
1195+
fn cell_returned_result_shares_matching_json_projection_and_reads_legacy_inline() {
1196+
let value = json!({"status": "ok", "items": [1, 2, 3]});
1197+
let result = CellReturnedResult {
1198+
output: ToolOutput::Json(value.clone()),
1199+
projection: Some(DurableToolProjection::Json {
1200+
value: value.clone(),
1201+
}),
1202+
is_error: false,
1203+
};
1204+
let wire = serde_json::to_value(&result).unwrap();
1205+
assert_eq!(wire["projection"], json!({"kind": "from_output"}));
1206+
1207+
let roundtrip: CellReturnedResult = serde_json::from_value(wire).unwrap();
1208+
assert_eq!(roundtrip.output, ToolOutput::Json(value.clone()));
1209+
assert_eq!(
1210+
roundtrip.projection,
1211+
Some(DurableToolProjection::Json {
1212+
value: value.clone(),
1213+
})
1214+
);
1215+
assert!(!roundtrip.is_error);
1216+
1217+
let legacy = json!({
1218+
"output": { "format": "json", "value": value },
1219+
"projection": { "kind": "json", "value": value },
1220+
"isError": false
1221+
});
1222+
let from_legacy: CellReturnedResult = serde_json::from_value(legacy).unwrap();
1223+
assert_eq!(from_legacy.projection, roundtrip.projection);
1224+
1225+
let omitted = CellReturnedResult {
1226+
output: ToolOutput::Text("plain".into()),
1227+
projection: None,
1228+
is_error: true,
1229+
};
1230+
let omitted_roundtrip: CellReturnedResult =
1231+
serde_json::from_value(serde_json::to_value(&omitted).unwrap()).unwrap();
1232+
assert!(omitted_roundtrip.projection.is_none());
1233+
assert!(omitted_roundtrip.is_error);
1234+
}
1235+
}

‎plugins/agent/facts/src/durable.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ pub struct RequestSpec {
219219
pub source_digest: String,
220220
pub effective_source_digest: String,
221221
pub provenance: ContentRef,
222+
#[serde(default, skip_serializing_if = "Option::is_none")]
223+
pub tool_contracts: Option<ContentRef>,
222224
pub projection: Option<ContentRef>,
223225
pub adjudications: Vec<AdjudicationBasis>,
224226
pub steering: Vec<ContentRef>,
@@ -867,6 +869,7 @@ impl RequestSpec {
867869
&self.request,
868870
&self.recovery_contract,
869871
];
872+
refs.extend(self.tool_contracts.iter());
870873
refs.extend(self.projection.iter());
871874
refs.extend(self.adjudications.iter().map(|basis| &basis.evidence));
872875
refs.extend(self.steering.iter());

0 commit comments

Comments
 (0)