From 66c107449d4a6a4cf8d46c65b2ed3a86052e67f9 Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Thu, 29 Oct 2026 16:18:09 +0100 Subject: [PATCH] fix: preserve intent classification in service listings --- atomic-cli/src/commands/intent/list.rs | 18 ++-- atomic-cli/src/commands/intent/new.rs | 8 +- atomic-cli/src/commands/rpc.rs | 25 ++++- atomic-cli/tests/it/routing_parity_test.rs | 94 +++++++++++++++++++ atomic-repository/src/repository/vault.rs | 3 + .../src/repository/vault_intent.rs | 69 +++++++++++--- .../services/vault/vault_messages.proto | 3 + libatomic/src/daemon/services_agent.rs | 44 +++++++-- 8 files changed, 229 insertions(+), 35 deletions(-) diff --git a/atomic-cli/src/commands/intent/list.rs b/atomic-cli/src/commands/intent/list.rs index 8c2c7cf4..492b24cf 100644 --- a/atomic-cli/src/commands/intent/list.rs +++ b/atomic-cli/src/commands/intent/list.rs @@ -54,8 +54,8 @@ pub struct IntentList { pub identity: Option, /// Only list intents of this classification (one of feature, review, bug, - /// chore, remediation). Read from the manifest `IntentSummary.kind` — no - /// lift. Mutually exclusive with `--review`. + /// chore, remediation). Read from intent frontmatter or the legacy manifest + /// fallback. Mutually exclusive with `--review`. #[arg(long, value_name = "KIND", conflicts_with = "review")] pub kind: Option, @@ -238,7 +238,7 @@ pub(crate) struct Row { /// name an intent without a second lookup. The table stays id-only. pub(crate) title: String, pub(crate) status: String, - /// Classification tag, read from the manifest `IntentSummary.kind` (no lift). + /// Classification from the stored frontmatter, falling back to the manifest. pub(crate) kind: String, pub(crate) attested: Attested, pub(crate) verifies: Verifies, @@ -351,9 +351,9 @@ fn compute_row( /// Build the (optionally kind-filtered) rows for the listing. /// -/// Enumeration reuses [`Repository::vault_intent_list`]; the `kind` tag is read -/// straight from the manifest `IntentSummary.kind` (a manifest read, never a -/// lift). Factored out of [`Command::run`] so the kind tag + filter are +/// Enumeration reuses [`Repository::vault_intent_list`]; the `kind` tag prefers +/// stored frontmatter over the manifest fallback. Factored out of [`Command::run`] +/// so the kind tag + filter are /// unit-testable without a global identity store. A row whose id is missing from /// the manifest (should not happen — the same manifest backs both) degrades to /// the default `feature`. @@ -369,7 +369,7 @@ fn build_rows( let kind = manifest .intents .get(&info.id) - .map(|s| s.kind.clone()) + .map(|s| repo.vault_intent_effective_kind(&s.vault_path, &s.kind)) .unwrap_or_else(|| "feature".to_string()); if let Some(f) = filter { if kind != f { @@ -449,7 +449,7 @@ impl IntentList { } /// One row reconstructed from the wire's per-entry classification — the -/// routed twin of [`compute_row`]: the handler computed the manifest kind +/// routed twin of [`compute_row`]: the handler computed the effective kind /// and the attestation/verifies state with the same domain logic, so the /// row maps straight off the wire columns. pub(crate) fn wire_row(entry: &atomic_client::proto::VaultEntry) -> Row { @@ -511,7 +511,7 @@ impl Command for IntentList { // Enumerate via the SAME source `atomic vault intent list` uses; already // sorted by id. Attestation entries are never in `manifest.intents`. The - // kind tag is read from the manifest summary (no lift). + // kind tag uses stored frontmatter with a manifest fallback. let mut rows = build_rows(&repo, verifier.as_ref(), filter.as_deref())?; sort_rows(&mut rows, self.sort, self.reverse); diff --git a/atomic-cli/src/commands/intent/new.rs b/atomic-cli/src/commands/intent/new.rs index 147558d2..2eccd534 100644 --- a/atomic-cli/src/commands/intent/new.rs +++ b/atomic-cli/src/commands/intent/new.rs @@ -141,10 +141,6 @@ fn create_intent( impl Command for IntentNew { fn run(&self) -> CliResult<()> { - if crate::commands::rpc::intent_new(self)? { - return Ok(()); - } - if self.template != "feature" { return Err(CliError::InvalidArgument { message: format!( @@ -154,6 +150,10 @@ impl Command for IntentNew { }); } + if crate::commands::rpc::intent_new(self)? { + return Ok(()); + } + let root = find_repository_root()?; let repo = Repository::open(&root).map_err(CliError::Repository)?; diff --git a/atomic-cli/src/commands/rpc.rs b/atomic-cli/src/commands/rpc.rs index decb63dd..c38b9d82 100644 --- a/atomic-cli/src/commands/rpc.rs +++ b/atomic-cli/src/commands/rpc.rs @@ -666,15 +666,34 @@ pub fn intent_new(args: &super::intent::new::IntentNew) -> CliResult { developer: None, intent: None, model: None, + intent_kind: Some(args.kind.clone()), + review_target: args.review.clone(), view: None, expected_snapshot: None, }; let response = session.create_vault_entity(request)?; let entry = response.entry.expect("created entry"); + let expected_kind = if args.review.is_some() { + "review" + } else { + &args.kind + }; + if expected_kind != "feature" && entry.manifest_kind.as_deref() != Some(expected_kind) { + return Err(CliError::Internal(anyhow::anyhow!( + "intent {} was created, but the service did not confirm kind '{}'; upgrade the service and inspect the intent", + entry.id, expected_kind + ))); + } println!("Created intent: {}", entry.id); println!(" file: {}", entry.vault_path.clone().unwrap_or_default()); println!(" template: {}", args.template); - println!(" kind: {}", args.kind); + println!( + " kind: {}", + entry.manifest_kind.as_deref().unwrap_or(&args.kind) + ); + if let Some(target) = &args.review { + println!(" reviews: {target}"); + } Ok(true) } @@ -1104,6 +1123,8 @@ pub fn memory_new(args: &super::memory::new::MemoryNew) -> CliResult { developer: None, intent: None, model: None, + intent_kind: None, + review_target: None, view: None, expected_snapshot: None, }; @@ -5983,6 +6004,8 @@ pub fn vault_goal_start(args: &super::vault::goal::GoalStart) -> CliResult developer: args.developer.clone(), intent: args.intent.clone(), model: args.model.clone(), + intent_kind: None, + review_target: None, view: None, expected_snapshot: None, }; diff --git a/atomic-cli/tests/it/routing_parity_test.rs b/atomic-cli/tests/it/routing_parity_test.rs index b0bd8453..56c53319 100644 --- a/atomic-cli/tests/it/routing_parity_test.rs +++ b/atomic-cli/tests/it/routing_parity_test.rs @@ -111,6 +111,100 @@ fn intent_list_json_carries_attestation_columns_over_the_wire() { assert!(table.contains(&id)); } +#[test] +fn intent_kind_is_preserved_and_stale_summaries_are_reconciled() { + let (repo, home) = fixture("intent-kind"); + let (root, home_dir) = (repo.path(), home.path()); + let create = |args: &[&str]| { + let output = run(root, home_dir, args); + output + .lines() + .find_map(|s| s.strip_prefix("Created intent: ")) + .unwrap() + .to_owned() + }; + let feature = create(&["intent", "new", "Feature"]); + let bug = create(&["intent", "new", "Bug", "--kind", "bug"]); + let review = create(&["intent", "new", "Review", "--review", &bug]); + let bug_show = run(root, home_dir, &["intent", "show", &bug]); + assert!(bug_show.contains("Kind: bug"), "{bug_show}"); + let review_show = run(root, home_dir, &["intent", "show", &review]); + assert!( + review_show.contains("Kind: review") && review_show.contains(&bug), + "{review_show}" + ); + for args in [ + vec!["intent", "new", "bad", "--kind", "nonsense"], + vec!["intent", "new", "bad", "--template", "nonsense"], + ] { + assert!(!atomic(root, home_dir, &args).status.success(), "{args:?}"); + } + let rows = |args: &[&str]| -> Vec { + serde_json::from_str(&run(root, home_dir, args)).unwrap() + }; + let all = rows(&["intent", "list", "--json"]); + assert_eq!(all.len(), 3, "invalid requests must not create intents"); + assert!(all + .iter() + .any(|row| row["id"] == feature && row["kind"] == "feature")); + assert!(all + .iter() + .any(|row| row["id"] == review && row["kind"] == "review")); + let bugs = rows(&["intent", "list", "--kind", "bug", "--json"]); + assert_eq!(bugs.len(), 1); + assert_eq!(bugs[0]["id"], bug); + + // Simulate an already-stored corrected document with a stale manifest, + // without rewriting the document during listing or reconciliation. + { + let repository = atomic_repository::Repository::open_existing(root).unwrap(); + let summary = repository.vault_manifest().unwrap().intents[&feature].clone(); + let entry = repository + .vault_retrieve(&summary.vault_path) + .unwrap() + .unwrap(); + let mut fm: serde_json::Value = serde_json::from_str(&entry.frontmatter_json).unwrap(); + fm["kind"] = "bug".into(); + repository + .vault_store( + &summary.vault_path, + atomic_core::pristine::VaultEntryType::Intent, + entry.content_bytes, + serde_json::to_string(&fm).unwrap(), + ) + .unwrap(); + repository.vault_materialize(&summary.vault_path).unwrap(); + assert_eq!( + repository.vault_manifest().unwrap().intents[&feature].kind, + "feature" + ); + } + let bugs = rows(&["intent", "list", "--kind", "bug", "--json"]); + assert_eq!(bugs.len(), 2); + assert!(bugs.iter().any(|row| row["id"] == feature)); + let path = { + let repository = atomic_repository::Repository::open_existing(root).unwrap(); + repository.vault_manifest().unwrap().intents[&feature] + .vault_path + .clone() + }; + let before = std::fs::read(root.join(".vault").join(path)).unwrap(); + run(root, home_dir, &["vault", "sync"]); + let repository = atomic_repository::Repository::open_existing(root).unwrap(); + assert_eq!( + repository.vault_manifest().unwrap().intents[&feature].kind, + "bug" + ); + assert_eq!( + before, + std::fs::read( + root.join(".vault") + .join(&repository.vault_manifest().unwrap().intents[&feature].vault_path) + ) + .unwrap() + ); +} + #[test] fn intent_human_key_sort_is_natural_in_service_output() { let repo = TempDir::new().unwrap(); diff --git a/atomic-repository/src/repository/vault.rs b/atomic-repository/src/repository/vault.rs index 29af909e..73180722 100644 --- a/atomic-repository/src/repository/vault.rs +++ b/atomic-repository/src/repository/vault.rs @@ -879,6 +879,9 @@ impl Repository { if let Some(status) = fm.get("status").and_then(|v| v.as_str()) { summary.status = status.to_string(); } + if let Some(kind) = fm.get("kind").and_then(|v| v.as_str()) { + summary.kind = kind.to_string(); + } // Same story as status: the title lives in the frontmatter, and // the summary is what `intent list` and the intents API render. // Unmirrored, an intent that plainly has a title listed with none. diff --git a/atomic-repository/src/repository/vault_intent.rs b/atomic-repository/src/repository/vault_intent.rs index dde369d2..eff780c1 100644 --- a/atomic-repository/src/repository/vault_intent.rs +++ b/atomic-repository/src/repository/vault_intent.rs @@ -336,15 +336,14 @@ impl Repository { }) } - /// Rebuild missing `manifest.intents` summaries from stored intent entries. + /// Rebuild missing summaries and reconcile explicit intent classifications. /// /// Intent entries can reach the vault without `vault_intent_create` ever /// running: clone bootstrap and `vault sync` deflate inherited /// `.vault/intents/**` files straight into redb, and /// `update_manifest_for_store` deliberately leaves intent summaries to /// "higher-level methods". This is that higher-level method for the - /// ingestion path. Existing summaries are never overwritten — the - /// authoring path stays the source of truth for intents it created. + /// ingestion path. Existing summaries retain their other fields. /// /// Also adopts the authoring repo's intent prefix when the local one is /// unset, and advances the per-author sequence allocator past inherited @@ -369,18 +368,9 @@ impl Repository { .get_vault_manifest() .map_err(|e| RepositoryError::Database(e.to_string()))?; let mut added = 0usize; + let mut changed = false; for meta in &entries { - // Already indexed (the authoring path stores vault_path on every - // summary) — leave it alone. - if manifest - .intents - .values() - .any(|summary| summary.vault_path == meta.path) - { - continue; - } - let Some(entry) = txn .get_vault_entry(&meta.path) .map_err(|e| RepositoryError::Database(e.to_string()))? @@ -394,6 +384,19 @@ impl Repository { ); continue; }; + if let Some(summary) = manifest + .intents + .values_mut() + .find(|s| s.vault_path == meta.path) + { + if let Some(kind) = fm.get("kind").and_then(|v| v.as_str()) { + if summary.kind != kind { + summary.kind = kind.to_owned(); + changed = true; + } + } + continue; + } // `id` is the human key; an entry without one is not a canonical // intent scaffold and has nothing to index. let Some(human_key) = fm.get("id").and_then(|v| v.as_str()).map(str::to_owned) else { @@ -456,7 +459,7 @@ impl Repository { added += 1; } - if added > 0 { + if added > 0 || changed { manifest.updated_at = chrono::Utc::now().to_rfc3339(); txn.put_vault_manifest(&manifest) .map_err(|e| RepositoryError::Database(e.to_string()))?; @@ -466,6 +469,19 @@ impl Repository { Ok(added) } + /// Prefer an explicit stored classification over a stale manifest summary. + /// Legacy entries without a kind continue to use their manifest value. + pub fn vault_intent_effective_kind(&self, path: &str, fallback: &str) -> String { + self.vault_retrieve(path) + .ok() + .flatten() + .and_then(|entry| { + serde_json::from_str::(&entry.frontmatter_json).ok() + }) + .and_then(|fm| fm.get("kind").and_then(|v| v.as_str()).map(str::to_owned)) + .unwrap_or_else(|| fallback.to_owned()) + } + /// List vault intents. /// /// Optionally filtered by status. Pass `Some("all")` or `None` for all intents. @@ -1516,6 +1532,31 @@ mod tests { assert_eq!(rev_fm["kind"], "review"); } + #[test] + fn sync_mirrors_edited_kind_and_preserves_legacy_fallback() { + let dir = tempdir().unwrap(); + let repo = init_repo_with_vault(dir.path()); + let created = repo + .vault_intent_create(create_opts_manual("Kind edit")) + .unwrap(); + assert_eq!( + repo.vault_intent_effective_kind(&created.intent_file, "review"), + "review" + ); + let path = repo.vault_dir().join(&created.intent_file); + let content = std::fs::read_to_string(&path).unwrap(); + std::fs::write(&path, content.replacen("---\n", "---\nkind: bug\n", 1)).unwrap(); + repo.vault_record_working_copy().unwrap(); + assert_eq!( + repo.vault_manifest().unwrap().intents[&created.id].kind, + "bug" + ); + assert_eq!( + repo.vault_intent_effective_kind(&created.intent_file, "feature"), + "bug" + ); + } + /// Reproduces the clone-inheritance gap: an intent file that arrives as /// bytes (clone materializes it; `vault sync` deflates it) must become /// listable and resolvable — `vault_intent_create` never runs on the diff --git a/libatomic/proto/atomic/services/vault/vault_messages.proto b/libatomic/proto/atomic/services/vault/vault_messages.proto index 2fda4c07..ffa506cc 100644 --- a/libatomic/proto/atomic/services/vault/vault_messages.proto +++ b/libatomic/proto/atomic/services/vault/vault_messages.proto @@ -244,6 +244,9 @@ message CreateVaultEntityRequest { optional string developer = 9; optional string intent = 10; optional string model = 11; + // Intent classification and optional reviewed target (add-only). + optional string intent_kind = 12; + optional string review_target = 13; ViewRef view = 20; ViewSnapshot expected_snapshot = 21; } diff --git a/libatomic/src/daemon/services_agent.rs b/libatomic/src/daemon/services_agent.rs index a148e424..d22d8a3f 100644 --- a/libatomic/src/daemon/services_agent.rs +++ b/libatomic/src/daemon/services_agent.rs @@ -1213,6 +1213,21 @@ impl vault_service_server::VaultService for VaultImpl { let response = match kind { Kind::Intent => { let title = request.title.clone(); + let intent_kind = if request.review_target.is_some() { + "review".to_string() + } else { + request + .intent_kind + .clone() + .unwrap_or_else(|| "feature".into()) + }; + if !atomic_canonical::vocab::is_known_intent_kind(&intent_kind) { + return Err(Status::invalid_argument(format!( + "unknown intent kind '{intent_kind}'" + ))); + } + let review_target = request.review_target.clone(); + let created_kind = intent_kind.clone(); let handle_for_task = handle.clone(); let result = tokio::task::spawn_blocking({ let handle = handle_for_task; @@ -1226,15 +1241,25 @@ impl vault_service_server::VaultService for VaultImpl { labels: Vec::new(), session_id: None, turn_id: None, - kind: None, + kind: (intent_kind != "feature").then_some(intent_kind), }) .map_err(repository_error)?; // The CLI's `intent new` overwrites the legacy // positional scaffold with the directive scaffold // through the same update path — the daemon must // produce the identical artifact. - let scaffold = - atomic_repository::FEATURE_SCAFFOLD.replace("{id}", &created.uid); + let scaffold = if let Some(target) = review_target { + let target = target.trim(); + let target = if target.starts_with("urn:") { + target.to_string() + } else { + format!("urn:atomic:intent:{target}") + }; + atomic_repository::REVIEW_SCAFFOLD.replace("{target}", &target) + } else { + atomic_repository::FEATURE_SCAFFOLD.to_string() + } + .replace("{id}", &created.uid); repo.vault_intent_update( &created.id, IntentUpdateOptions { @@ -1261,6 +1286,7 @@ impl vault_service_server::VaultService for VaultImpl { linked: Vec::new(), vault_path: Some(format!(".vault/{}", result.intent_file)), priority: Some("medium".to_string()), + manifest_kind: Some(created_kind), ..Default::default() }), meta: response_meta(&meta), @@ -1926,13 +1952,17 @@ impl vault_service_server::VaultService for VaultImpl { let mut entries = infos .into_iter() .map(|info| { - // The manifest kind tag (a manifest read, never a - // lift); a row missing from the manifest degrades - // to the default "feature". + // Prefer the stored intent's explicit kind over a + // stale manifest; missing data falls back to feature. let manifest_kind = manifest .intents .get(&info.id) - .map(|summary| summary.kind.clone()) + .map(|summary| { + repo.vault_intent_effective_kind( + &summary.vault_path, + &summary.kind, + ) + }) .unwrap_or_else(|| "feature".to_string()); // The attestation columns: a read/attestation // failure for a SINGLE intent degrades that row's