|
| 1 | +# Implementation Plan: Cursor-Based Mention Polling |
| 2 | + |
| 3 | +**Research doc:** `.docs/research-mention-replay-storm.md` |
| 4 | +**Gitea issue:** #186 |
| 5 | +**Closes:** #249 (superseded) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 5/25 Elimination |
| 10 | + |
| 11 | +**IN (top 5):** |
| 12 | +1. Cursor-based repo-wide polling (single API call) |
| 13 | +2. Persistent cursor via terraphim_persistence |
| 14 | +3. max_dispatches_per_tick rate limit |
| 15 | +4. Startup guard (cursor=now on first run) |
| 16 | +5. Backward compat deprecation of watch_issues |
| 17 | + |
| 18 | +**AVOID:** |
| 19 | +- Per-issue depth tracking (remove, cursor makes it unnecessary) |
| 20 | +- Complex deduplication logic |
| 21 | +- Webhook-driven mentions (Issue #149, deferred) |
| 22 | +- Processed HashSet persistence (cursor replaces it) |
| 23 | +- Custom SQLite table for cursor (use existing KV store) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## File Changes |
| 28 | + |
| 29 | +### Step 1: New `MentionCursor` type + persistence |
| 30 | + |
| 31 | +**File:** `crates/terraphim_orchestrator/src/mention.rs` |
| 32 | + |
| 33 | +```rust |
| 34 | +/// Persistent cursor for mention polling. |
| 35 | +/// Stored via terraphim_persistence as JSON at key "adf/mention_cursor". |
| 36 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 37 | +pub struct MentionCursor { |
| 38 | + /// ISO 8601 timestamp of the last processed comment. |
| 39 | + pub last_seen_at: String, |
| 40 | + /// Counter of dispatches in current tick (reset each poll cycle). |
| 41 | + #[serde(skip)] |
| 42 | + pub dispatches_this_tick: u32, |
| 43 | +} |
| 44 | + |
| 45 | +impl MentionCursor { |
| 46 | + /// Create a cursor set to "now" (skip all historical mentions). |
| 47 | + pub fn now() -> Self { |
| 48 | + Self { |
| 49 | + last_seen_at: chrono::Utc::now().to_rfc3339(), |
| 50 | + dispatches_this_tick: 0, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Load from persistence or create "now" cursor. |
| 55 | + pub async fn load_or_now() -> Self { ... } |
| 56 | + |
| 57 | + /// Save to persistence. |
| 58 | + pub async fn save(&self) { ... } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +**Remove:** `MentionTracker.processed` HashSet (no longer needed). |
| 63 | +**Keep:** `MentionTracker.depth_counters` renamed to `dispatches_per_issue: HashMap<u64, u32>` (flood protection). |
| 64 | + |
| 65 | +### Step 2: New `fetch_repo_comments` in tracker |
| 66 | + |
| 67 | +**File:** `crates/terraphim_tracker/src/gitea.rs` |
| 68 | + |
| 69 | +```rust |
| 70 | +/// Fetch comments across all issues in a repo since a timestamp. |
| 71 | +pub async fn fetch_repo_comments( |
| 72 | + &self, |
| 73 | + since: &str, |
| 74 | + limit: u32, |
| 75 | +) -> Result<Vec<IssueComment>> { |
| 76 | + // GET /api/v1/repos/{owner}/{repo}/issues/comments?since={since}&limit={limit} |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +The `IssueComment` struct needs an `issue_number` field extracted from `issue_url`. |
| 81 | + |
| 82 | +### Step 3: Update `MentionConfig` |
| 83 | + |
| 84 | +**File:** `crates/terraphim_orchestrator/src/config.rs` |
| 85 | + |
| 86 | +```rust |
| 87 | +pub struct MentionConfig { |
| 88 | + /// DEPRECATED: Ignored when cursor polling is active. |
| 89 | + #[serde(default)] |
| 90 | + pub watch_issues: Vec<u64>, |
| 91 | + |
| 92 | + /// Max mentions to dispatch per poll tick. |
| 93 | + #[serde(default = "default_max_dispatches_per_tick")] |
| 94 | + pub max_dispatches_per_tick: u32, // default: 3 |
| 95 | + |
| 96 | + /// Max concurrent mention-spawned agents. |
| 97 | + #[serde(default = "default_max_concurrent_mention_agents")] |
| 98 | + pub max_concurrent_mention_agents: u32, // default: 5 |
| 99 | + |
| 100 | + /// Poll every N reconciliation ticks. |
| 101 | + #[serde(default = "default_poll_modulo")] |
| 102 | + pub poll_modulo: u64, |
| 103 | + |
| 104 | + /// Persistence key prefix for cursor. |
| 105 | + #[serde(default)] |
| 106 | + pub cursor_key: String, // default: "adf/mention_cursor" |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +Remove `max_mention_depth` (replaced by `max_dispatches_per_tick`). |
| 111 | + |
| 112 | +### Step 4: Rewrite `poll_mentions()` |
| 113 | + |
| 114 | +**File:** `crates/terraphim_orchestrator/src/lib.rs` (lines 1055-1170) |
| 115 | + |
| 116 | +```rust |
| 117 | +async fn poll_mentions(&mut self) { |
| 118 | + let cfg = match self.config.mentions.clone() { ... }; |
| 119 | + let gitea_cfg = match self.config.gitea.clone() { ... }; |
| 120 | + |
| 121 | + if self.tick_count % cfg.poll_modulo != 0 { return; } |
| 122 | + |
| 123 | + // Count currently active mention-spawned agents |
| 124 | + let active_mention_agents = self.active_agents.values() |
| 125 | + .filter(|a| a.spawned_by_mention) |
| 126 | + .count() as u32; |
| 127 | + if active_mention_agents >= cfg.max_concurrent_mention_agents { return; } |
| 128 | + |
| 129 | + // Load cursor |
| 130 | + let mut cursor = MentionCursor::load_or_now().await; |
| 131 | + cursor.dispatches_this_tick = 0; |
| 132 | + |
| 133 | + // Single API call: all comments since cursor |
| 134 | + let comments = tracker.fetch_repo_comments(&cursor.last_seen_at, 50).await?; |
| 135 | + |
| 136 | + for comment in &comments { |
| 137 | + if cursor.dispatches_this_tick >= cfg.max_dispatches_per_tick { break; } |
| 138 | + |
| 139 | + let mentions = parse_mentions(comment, comment.issue_number, &agents, &personas); |
| 140 | + for m in mentions { |
| 141 | + if cursor.dispatches_this_tick >= cfg.max_dispatches_per_tick { break; } |
| 142 | + |
| 143 | + // Spawn |
| 144 | + self.spawn_agent(&mention_def).await?; |
| 145 | + cursor.dispatches_this_tick += 1; |
| 146 | + } |
| 147 | + |
| 148 | + // Advance cursor past this comment |
| 149 | + cursor.last_seen_at = comment.created_at.clone(); |
| 150 | + } |
| 151 | + |
| 152 | + cursor.save().await; |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +### Step 5: Track mention-spawned agents |
| 157 | + |
| 158 | +**File:** `crates/terraphim_orchestrator/src/lib.rs` |
| 159 | + |
| 160 | +Add `spawned_by_mention: bool` to `ManagedAgent` struct. Set to `true` when spawned from `poll_mentions()`. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## Test Strategy |
| 165 | + |
| 166 | +| Test | Type | What it verifies | |
| 167 | +|---|---|---| |
| 168 | +| `test_cursor_load_or_now` | Unit | Fresh cursor uses current time | |
| 169 | +| `test_cursor_persistence_roundtrip` | Unit | Save + load preserves last_seen_at | |
| 170 | +| `test_poll_mentions_respects_max_dispatches` | Unit | Stops after N dispatches per tick | |
| 171 | +| `test_poll_mentions_advances_cursor` | Unit | Cursor advances to latest comment | |
| 172 | +| `test_poll_mentions_concurrent_limit` | Unit | Skips poll when max concurrent reached | |
| 173 | +| `test_no_replay_on_restart` | Integration | Restart with persisted cursor doesn't re-dispatch | |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Step Sequence |
| 178 | + |
| 179 | +| Step | Description | Files | Depends on | |
| 180 | +|---|---|---|---| |
| 181 | +| 1 | MentionCursor + persistence | mention.rs | — | |
| 182 | +| 2 | fetch_repo_comments API | terraphim_tracker/gitea.rs | — | |
| 183 | +| 3 | Update MentionConfig | config.rs | — | |
| 184 | +| 4 | Rewrite poll_mentions() | lib.rs | 1, 2, 3 | |
| 185 | +| 5 | spawned_by_mention tracking | lib.rs | 4 | |
| 186 | +| 6 | Tests | mention.rs, lib.rs tests | 1-5 | |
| 187 | +| 7 | Deploy + verify on bigbox | orchestrator.toml | 6 | |
| 188 | + |
| 189 | +Steps 1-3 are independent and can be done in parallel. |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## Rollback |
| 194 | + |
| 195 | +If cursor polling fails, re-enable `[mentions]` with `watch_issues` — the old code path still works. The cursor is additive. |
| 196 | + |
| 197 | +--- |
| 198 | + |
| 199 | +## Config migration |
| 200 | + |
| 201 | +```toml |
| 202 | +# OLD (remove) |
| 203 | +[mentions] |
| 204 | +watch_issues = [107, 108, ...] |
| 205 | +max_mention_depth = 10 |
| 206 | +poll_modulo = 2 |
| 207 | + |
| 208 | +# NEW |
| 209 | +[mentions] |
| 210 | +max_dispatches_per_tick = 3 |
| 211 | +max_concurrent_mention_agents = 5 |
| 212 | +poll_modulo = 2 |
| 213 | +``` |
0 commit comments