|
| 1 | +# Design & Implementation Plan: Add Action Metadata to NormalizedTerm |
| 2 | + |
| 3 | +**Issue**: #735 — `feat(types): add action metadata to NormalizedTerm beyond url field` |
| 4 | +**Author**: Agent |
| 5 | +**Date**: 2026-04-22 |
| 6 | +**Phase**: 2 — Design |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 1. Summary of Target Behavior |
| 11 | + |
| 12 | +After implementation, `NormalizedTerm` carries four additional optional metadata fields: |
| 13 | + |
| 14 | +| Field | Type | Description | |
| 15 | +|-------|------|-------------| |
| 16 | +| `action` | `Option<String>` | CLI action template with `{{ model }}` and `{{ prompt }}` placeholders | |
| 17 | +| `priority` | `Option<u8>` | Routing tiebreaking priority (higher = preferred) | |
| 18 | +| `trigger` | `Option<String>` | Pattern or alias that activates this term | |
| 19 | +| `pinned` | `bool` | Whether the term is pinned (default `false`) | |
| 20 | + |
| 21 | +`AutocompleteMetadata` (in `terraphim_automata`) receives the same four fields in the same PR to stay in sync. |
| 22 | + |
| 23 | +All new fields are `Option<T>` (or `bool` with `#[serde(default)]`), ensuring backward-compatible JSON deserialization: existing serialised `NormalizedTerm` data remains valid. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 2. Key Invariants and Acceptance Criteria |
| 28 | + |
| 29 | +### Invariants |
| 30 | +- `NormalizedTerm` JSON can be deserialised regardless of which optional fields are present |
| 31 | +- Adding fields does not change the meaning of existing `id`, `value`, `display_value`, or `url` fields |
| 32 | +- `AutocompleteMetadata` and `NormalizedTerm` remain structurally aligned for their shared fields |
| 33 | + |
| 34 | +### Acceptance Criteria |
| 35 | + |
| 36 | +| # | Criterion | Test Type | Location | |
| 37 | +|---|-----------|-----------|----------| |
| 38 | +| AC1 | `NormalizedTerm::new()`, `with_auto_id()`, `with_display_value()`, `with_url()` still compile and produce terms without the new fields set | Unit | New test in `terraphim_types` | |
| 39 | +| AC2 | `NormalizedTerm` deserialises from JSON missing all new fields | Unit | New test in `terraphim_types` | |
| 40 | +| AC3 | `NormalizedTerm` serialises with all four new fields present | Unit | New test in `terraphim_types` | |
| 41 | +| AC4 | `NormalizedTerm::action()` / `::priority()` / `::trigger()` / `::pinned()` builder methods work | Unit | New test in `terraphim_types` | |
| 42 | +| AC5 | `AutocompleteMetadata` gains the same four fields | Unit | New test in `terraphim_automata` | |
| 43 | +| AC6 | `cargo build --workspace` succeeds with no new warnings | Build | CI | |
| 44 | +| AC7 | All existing tests pass (`cargo test --workspace`) | Integration | CI | |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## 3. High-Level Design and Boundaries |
| 49 | + |
| 50 | +### Components |
| 51 | + |
| 52 | +``` |
| 53 | +crates/terraphim_types/src/lib.rs |
| 54 | + └── NormalizedTerm struct (MODIFY) |
| 55 | + + action: Option<String> |
| 56 | + + priority: Option<u8> |
| 57 | + + trigger: Option<String> |
| 58 | + + pinned: bool |
| 59 | + + builder methods: with_action(), with_priority(), with_trigger(), with_pinned() |
| 60 | + + getter methods: action(), priority(), trigger(), pinned() |
| 61 | +
|
| 62 | +crates/terraphim_automata/src/autocomplete.rs |
| 63 | + └── AutocompleteMetadata struct (MODIFY) |
| 64 | + + action: Option<String> |
| 65 | + + priority: Option<u8> |
| 66 | + + trigger: Option<String> |
| 67 | + + pinned: bool |
| 68 | +``` |
| 69 | + |
| 70 | +### Boundaries |
| 71 | +- **Inside**: Adding fields to `NormalizedTerm` and `AutocompleteMetadata`, including serde attributes and builder/getter methods |
| 72 | +- **Outside**: No changes to `MarkdownDirectives`, `RouteDirective`, `RoutingRule`, `Thesaurus`, or any storage layer |
| 73 | +- **New dependencies**: None — only standard library + existing serde/ahash imports |
| 74 | + |
| 75 | +### Anti-Patterns Avoided |
| 76 | +- No wrapper struct (per user decision) |
| 77 | +- No changes to JSON schema for existing persisted data (backward-compatible `Option` fields) |
| 78 | +- No migration of existing data |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## 4. File/Module-Level Change Plan |
| 83 | + |
| 84 | +| File/Module | Action | Before | After | Dependencies | |
| 85 | +|-------------|--------|--------|-------|--------------| |
| 86 | +| `crates/terraphim_types/src/lib.rs` | Modify | `NormalizedTerm` with 4 fields | `NormalizedTerm` with 8 fields; 4 new builder methods; 4 getter methods | None new | |
| 87 | +| `crates/terraphim_types/src/lib.rs` | Modify | `AutocompleteMetadata` with 4 fields | `AutocompleteMetadata` with 8 fields | None new | |
| 88 | +| `crates/terraphim_automata/src/autocomplete.rs` | Modify | `AutocompleteMetadata` with 4 fields | `AutocompleteMetadata` with 8 fields; serde attributes updated | `terraphim_types` re-export | |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## 5. Step-by-Step Implementation Sequence |
| 93 | + |
| 94 | +### Step 1: Add fields to `NormalizedTerm` in `terraphim_types/src/lib.rs` |
| 95 | +- Add `action: Option<String>` with `#[serde(default, skip_serializing_if = "Option::is_none")]` |
| 96 | +- Add `priority: Option<u8>` with same serde attributes |
| 97 | +- Add `trigger: Option<String>` with same serde attributes |
| 98 | +- Add `pinned: bool` with `#[serde(default)]` |
| 99 | +- Add builder methods: `with_action(self, String)`, `with_priority(self, u8)`, `with_trigger(self, String)`, `with_pinned(self, bool)` |
| 100 | +- Add getter methods: `action(&self) -> Option<&String>`, `priority(&self) -> Option<&u8>`, `trigger(&self) -> Option<&String>`, `pinned(&self) -> bool` |
| 101 | +- Update `new()` to initialise all new fields to default values |
| 102 | +- Update `with_auto_id()` similarly |
| 103 | +- Update existing `with_url()` return type to `Self` (already correct for builder pattern) |
| 104 | + |
| 105 | +**Deployable state**: Yes — compiles, all old code path-compatible |
| 106 | + |
| 107 | +### Step 2: Add tests for `NormalizedTerm` in `terraphim_types` |
| 108 | +- Test deserialisation from JSON missing all new fields |
| 109 | +- Test serialisation with all fields set |
| 110 | +- Test builder chain with all new methods |
| 111 | +- Test `display()` still works with new fields |
| 112 | + |
| 113 | +**Deployable state**: Yes — tests only |
| 114 | + |
| 115 | +### Step 3: Update `AutocompleteMetadata` in `terraphim_automata/src/autocomplete.rs` |
| 116 | +- Add the same four fields with identical serde attributes |
| 117 | +- Add identical builder and getter methods |
| 118 | + |
| 119 | +**Deployable state**: Yes — compiles, structurally aligned with NormalizedTerm |
| 120 | + |
| 121 | +### Step 4: Add tests for `AutocompleteMetadata` |
| 122 | +- Test serialisation and deserialisation round-trip |
| 123 | +- Test builder methods |
| 124 | + |
| 125 | +**Deployable state**: Yes — tests only |
| 126 | + |
| 127 | +### Step 5: Run full workspace build and test |
| 128 | +- `cargo build --workspace` |
| 129 | +- `cargo test --workspace` |
| 130 | +- `cargo clippy -- -D warnings` |
| 131 | + |
| 132 | +**Deployable state**: Yes — verified green |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 6. Testing & Verification Strategy |
| 137 | + |
| 138 | +| Acceptance Criteria | Test Type | Test Location | |
| 139 | +|---------------------|-----------|---------------| |
| 140 | +| AC1: Constructors produce unset new fields | Unit | `crates/terraphim_types/src/lib.rs` (inline tests or `tests/` dir) | |
| 141 | +| AC2: JSON backward compat (missing fields) | Unit | Same | |
| 142 | +| AC3: JSON serialisation with all fields | Unit | Same | |
| 143 | +| AC4: Builder/getter methods | Unit | Same | |
| 144 | +| AC5: AutocompleteMetadata alignment | Unit | `crates/terraphim_automata/src/autocomplete.rs` | |
| 145 | +| AC6: No new warnings | Build | CI (`cargo clippy`) | |
| 146 | +| AC7: All tests pass | Integration | CI (`cargo test --workspace`) | |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## 7. Risk & Complexity Review |
| 151 | + |
| 152 | +| Risk | Mitigation | Residual Risk | |
| 153 | +|------|------------|--------------| |
| 154 | +| Forgetting to update a construction site causes compiler warning | Run `cargo build --workspace` which will warn on missing fields; all test crates updated in same PR | Low | |
| 155 | +| JSON output change breaks external consumers | New fields are optional; consumers that ignore unknown fields are unaffected | Low | |
| 156 | +| `AutocompleteMetadata` and `NormalizedTerm` drift out of sync in future | Document that they must be updated together; consider a shared test | Low | |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## 8. Open Questions / Decisions for Human Review |
| 161 | + |
| 162 | +1. **Field defaults for `priority`**: `Option<u8>` means `None` means no priority set. Should `priority: Option<u8>` be replaced with `priority: u8` with `#[serde(default = "default_priority")]` where `default_priority()` returns `50` as a sensible neutral priority? (The current plan uses `Option<u8>` — confirm this is preferred or choose the default-value approach.) |
| 163 | + |
| 164 | +2. **Should `trigger` be a `Vec<String>` instead of `Option<String>`** to support multiple trigger patterns per term? (Current plan uses `Option<String>` — single trigger per term. Multi-trigger would require `Option<Vec<String>>`.) |
| 165 | + |
| 166 | +3. **`pinned: bool` with `#[serde(default)]`** — should pinned terms default to `false` (current plan) or should the serde default be a specific behaviour (e.g., pinned = false is never serialised)? |
| 167 | + |
| 168 | +**Note**: All three questions above are clarifications on default values and representation — the four-field structure is fixed per the approved research. The default-value approach for `priority` and the single-string `trigger` are recommended for simplicity. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Appendix: Target Struct Shapes |
| 173 | + |
| 174 | +### `NormalizedTerm` (after) |
| 175 | +```rust |
| 176 | +pub struct NormalizedTerm { |
| 177 | + pub id: u64, |
| 178 | + pub value: NormalizedTermValue, |
| 179 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 180 | + pub display_value: Option<String>, |
| 181 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 182 | + pub url: Option<String>, |
| 183 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 184 | + pub action: Option<String>, |
| 185 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 186 | + pub priority: Option<u8>, |
| 187 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 188 | + pub trigger: Option<String>, |
| 189 | + #[serde(default)] |
| 190 | + pub pinned: bool, |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### `AutocompleteMetadata` (after) |
| 195 | +```rust |
| 196 | +pub struct AutocompleteMetadata { |
| 197 | + pub id: u64, |
| 198 | + pub normalized_term: NormalizedTermValue, |
| 199 | + pub original_term: String, |
| 200 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 201 | + pub url: Option<String>, |
| 202 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 203 | + pub action: Option<String>, |
| 204 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 205 | + pub priority: Option<u8>, |
| 206 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 207 | + pub trigger: Option<String>, |
| 208 | + #[serde(default)] |
| 209 | + pub pinned: bool, |
| 210 | +} |
| 211 | +``` |
0 commit comments