Preserve selected member agency in gestalt projections

This commit is contained in:
Meta Vens 2026-08-18 20:45:33 +02:00
commit 7916ab0493

View file

@ -576,8 +576,12 @@ fn constrain_cell_projection_schema(
.pointer_mut("/properties/segments")
.and_then(serde_json::Value::as_object_mut)
.ok_or_else(|| anyhow!("cell projection schema has no segment array"))?;
segments.insert("minItems".into(), 1.into());
segments.insert("maxItems".into(), slice.max_actions.max(1).into());
let required_count = required_projection_subject_ids(slice).len();
segments.insert("minItems".into(), required_count.max(1).into());
segments.insert(
"maxItems".into(),
slice.max_actions.max(required_count).max(1).into(),
);
let segment = schema
.pointer_mut("/$defs/CellPerspectiveSegment/properties")
.and_then(serde_json::Value::as_object_mut)
@ -611,7 +615,10 @@ fn bind_cell_projection(
slice: &PermittedCellSlice,
proposal: CellProjectionProposal,
) -> Result<(String, BTreeSet<String>)> {
if proposal.segments.is_empty() || proposal.segments.len() > slice.max_actions.max(1) {
let required_subject_ids = required_projection_subject_ids(slice);
if proposal.segments.is_empty()
|| proposal.segments.len() > slice.max_actions.max(required_subject_ids.len()).max(1)
{
return Err(anyhow!(
"cell Projector emitted an invalid perspective count"
));
@ -641,20 +648,12 @@ fn bind_cell_projection(
));
}
}
if let Some(focus) = slice.detail_focus_subject_id.as_deref()
&& (slice
.constituents
.iter()
.any(|subject| subject.subject_id == focus)
|| slice
.member_exceptions
.iter()
.any(|member| member.subject_id == focus))
&& !segments.contains_key(focus)
{
return Err(anyhow!(
"cell Projector omitted the debt-selected perspective owner"
));
for subject_id in required_subject_ids {
if !segments.contains_key(&subject_id) {
return Err(anyhow!(
"cell Projector omitted required perspective owner {subject_id}"
));
}
}
let active_subject_ids = segments.keys().cloned().collect::<BTreeSet<_>>();
let mut lowered = Vec::with_capacity(segments.len());
@ -700,6 +699,30 @@ fn bind_cell_projection(
Ok((lowered.join("\n\n"), active_subject_ids))
}
fn required_projection_subject_ids(slice: &PermittedCellSlice) -> BTreeSet<String> {
slice
.detail_focus_subject_id
.iter()
.filter(|focus| {
slice
.constituents
.iter()
.any(|subject| &subject.subject_id == *focus)
|| slice
.member_exceptions
.iter()
.any(|member| &member.subject_id == *focus)
})
.cloned()
.chain(
slice
.member_exceptions
.iter()
.map(|member| member.subject_id.clone()),
)
.collect()
}
fn allowed_effect_type(kind: &crate::domain::AgencySubjectKind) -> &'static str {
match kind {
crate::domain::AgencySubjectKind::Actor => "actor_move",
@ -746,8 +769,13 @@ impl CellProjectionEngine {
let mode_guidance = format!(
"{mode_guidance} Treat already_committed_posture as an institutional course already in force, not a pressure, option, or fresh decision. Continuing it is holding steady; only a materially different commitment is a new posture choice. Each perceived event names the exact constituents that can perceive it; do not teach it to anyone else. Only supplied constituents and member_exceptions may own an internal perspective or choice. A person merely mentioned in an event is external observation when absent from those lists: never voice them. Every supplied member_exception was selected because that person has an actionable decision in this horizon. Render each selected person explicitly by name, with only their own footing and choices."
);
let word_budget = (120 + 45 * slice.constituents.len()).min(360);
let perspective_limit = slice.max_actions.max(1);
let required_projection_subject_ids = required_projection_subject_ids(&slice);
let word_budget =
(120 + 45 * (slice.constituents.len() + slice.member_exceptions.len())).min(360);
let perspective_limit = slice
.max_actions
.max(required_projection_subject_ids.len())
.max(1);
let mut projection_schema = serde_json::to_value(schema_for!(CellProjectionProposal))?;
constrain_cell_projection_schema(&mut projection_schema, &slice)?;
let mut projection_request = ModelStageRequest {
@ -755,7 +783,9 @@ impl CellProjectionEngine {
model: self.projector_model.clone(),
snapshot_binding: slice.snapshot_binding.clone(),
lived_stream: format!(
"<!-- membrane:{MEMBRANE_SCHEMA}:cell-projector -->\nYou are a private cell Projector. Convert only the permitted typed context and visible stimulus into compact lived narrative segments. Each segment belongs to exactly one supplied subject_id and contains only that subject's perceptions, memories, wants, fears, knowledge, and explicit uncertainty. Mentioned outsiders remain external observations: never give them an internal viewpoint. Do not choose actions or claim world effects. Omit decorative recap. Return between 1 and {perspective_limit} unique segments; do not narrate every supplied subject. If detail_focus_subject_id is present, include it first. Spend the remaining slots only on subjects facing a materially different decision in this horizon.\n\nReturn exactly one JSON object matching this stable shape:\n{CELL_PROJECTION_OUTPUT_CONTRACT}\n\nDomain guidance:\n{mode_guidance}\n\nIdentity:\n{}\n\nPermitted typed context:\n{projector_context}\n\nVisible stimulus:\n{visible_stimulus}\n\nUse no more than {word_budget} narrative words across all segments.",
"<!-- membrane:{MEMBRANE_SCHEMA}:cell-projector -->\nYou are a private cell Projector. Convert only the permitted typed context and visible stimulus into compact lived narrative segments. Each segment belongs to exactly one supplied subject_id and contains only that subject's perceptions, memories, wants, fears, knowledge, and explicit uncertainty. Mentioned outsiders remain external observations: never give them an internal viewpoint. Do not choose actions or claim world effects. Omit decorative recap. Return between {} and {perspective_limit} unique segments; do not narrate every ordinary constituent. Include every exact subject ID in REQUIRED PERSPECTIVE OWNERS, because each named member exception or debt focus has an actionable decision that must not disappear inside the aggregate. Put detail_focus_subject_id first when present. Spend any remaining slots only on subjects facing a materially different decision in this horizon.\n\nREQUIRED PERSPECTIVE OWNERS:\n{}\n\nReturn exactly one JSON object matching this stable shape:\n{CELL_PROJECTION_OUTPUT_CONTRACT}\n\nDomain guidance:\n{mode_guidance}\n\nIdentity:\n{}\n\nPermitted typed context:\n{projector_context}\n\nVisible stimulus:\n{visible_stimulus}\n\nUse no more than {word_budget} narrative words across all segments.",
required_projection_subject_ids.len().max(1),
serde_json::to_string(&required_projection_subject_ids)?,
slice.cell_id
),
output_schema: Some(projection_schema),
@ -1724,7 +1754,11 @@ mod tests {
.lived_stream
.contains("between 1 and 1 unique segments")
);
assert!(request.lived_stream.contains("include it first"));
assert!(
request
.lived_stream
.contains("Put detail_focus_subject_id first")
);
Ok(serde_json::json!({
"segments":[{
"subject_id":"faction-06",
@ -2586,7 +2620,44 @@ mod tests {
},
)
.unwrap_err();
assert!(error.to_string().contains("debt-selected"));
assert!(error.to_string().contains("required perspective owner"));
}
#[test]
fn cell_projection_cannot_erase_a_selected_member_exception() {
let mut slice = fixture_cell_slice();
slice.max_actions = 2;
slice.member_exceptions.push(CellMemberSlice {
subject_id: "member:mira".into(),
member_id: "mira".into(),
name: "Mira".into(),
source_gestalt_id: "refugees".into(),
source_location_id: "forum".into(),
knowledge: BTreeSet::new(),
capabilities: BTreeSet::new(),
resources: BTreeSet::new(),
information_channels: BTreeSet::new(),
permitted_state_references: BTreeSet::from(["member:mira".into()]),
migration_destinations: BTreeMap::new(),
activity_target_ids: BTreeSet::new(),
goals: vec!["choose whether to leave".into()],
pressures: vec!["the final ferry is boarding".into()],
relationships: BTreeMap::new(),
memories: vec![],
});
let error = bind_cell_projection(
&slice,
CellProjectionProposal {
segments: vec![CellPerspectiveSegment {
subject_id: "faction-06".into(),
narrative: "The institution watches the ferry clock.".into(),
}],
},
)
.unwrap_err();
assert!(error.to_string().contains("member:mira"));
}
}