diff --git a/server/src/agents/pool/auto_assign/story_checks.rs b/server/src/agents/pool/auto_assign/story_checks.rs index 151cc9ed..44b6f1f9 100644 --- a/server/src/agents/pool/auto_assign/story_checks.rs +++ b/server/src/agents/pool/auto_assign/story_checks.rs @@ -7,15 +7,23 @@ fn read_story_contents(_project_root: &Path, story_id: &str) -> Option { crate::db::read_content(story_id) } -/// Read the optional `agent:` field from the front matter of a story file. +/// Read the optional `agent:` pin for a story. /// -/// Returns `Some(agent_name)` if the front matter specifies an agent, or `None` -/// if the field is absent or the file cannot be read / parsed. +/// After story 871 the agent assignment lives in the CRDT typed register +/// (`PipelineItemView.agent`), not the YAML front matter. We check the CRDT +/// first; falling back to legacy YAML parsing keeps behaviour intact for any +/// stories whose CRDT entry doesn't yet have the field set. pub(super) fn read_story_front_matter_agent( project_root: &Path, _stage_dir: &str, story_id: &str, ) -> Option { + if let Some(view) = crate::crdt_state::read_item(story_id) + && let Some(agent) = view.agent.as_ref() + && !agent.is_empty() + { + return Some(agent.clone()); + } use crate::io::story_metadata::parse_front_matter; let contents = read_story_contents(project_root, story_id)?; parse_front_matter(&contents).ok()?.agent @@ -293,4 +301,29 @@ mod tests { check_archived_dependencies(tmp.path(), "1_backlog", "503_story_waiting"); assert!(archived_deps.is_empty()); } + + #[test] + fn read_story_front_matter_agent_prefers_crdt_typed_register_over_yaml() { + // Regression: after story 871 the agent pin lives in the CRDT typed + // register, not the YAML front matter. read_story_front_matter_agent + // must consult the register first so auto-assign honours the pin. + let tmp = tempfile::tempdir().unwrap(); + crate::db::ensure_content_store(); + + // Seed a story whose YAML has NO agent field. + crate::db::write_item_with_content( + "9971_story_pin_in_crdt", + "2_current", + "---\nname: Pin In CRDT\n---\n", + ); + + // Set the typed CRDT register (this is the path 871's migration uses). + let written = + crate::crdt_state::set_agent("9971_story_pin_in_crdt", Some("coder-opus")); + assert!(written, "set_agent should succeed for an existing item"); + + // The reader must return the CRDT register value, not None. + let agent = read_story_front_matter_agent(tmp.path(), "2_current", "9971_story_pin_in_crdt"); + assert_eq!(agent.as_deref(), Some("coder-opus")); + } } diff --git a/server/src/agents/pool/start/validation.rs b/server/src/agents/pool/start/validation.rs index eb800038..25b866ce 100644 --- a/server/src/agents/pool/start/validation.rs +++ b/server/src/agents/pool/start/validation.rs @@ -60,6 +60,15 @@ pub(super) fn read_front_matter_agent(story_id: &str, agent_name: Option<&str>) if agent_name.is_some() { return None; } + // After story 871 the pin lives in the CRDT typed register; fall back + // to legacy YAML parsing for stories whose CRDT entry doesn't yet have + // the field populated. + if let Some(view) = crate::crdt_state::read_item(story_id) + && let Some(agent) = view.agent.as_ref() + && !agent.is_empty() + { + return Some(agent.clone()); + } crate::db::read_content(story_id).and_then(|contents| { crate::io::story_metadata::parse_front_matter(&contents) .ok()?