Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(pipeline): extract fetch-cap pruning logic into dedicated function\n\nMove the decision to skip pruning when a fetch hits the per-fetch cap into a new `reconcile_if_complete` function, separating the cap-checking logic from the main pipeline flow. This makes the behaviour easier to test in isolation and clarifies that the exactly-at-cap case is conservatively treated as truncated, with pruning resuming once a later fetch falls below the cap.\n\nAuto-committed-on: dragonfly\n

Steven Enamakel743acfd218f0
diff --git a/src/openhuman/integrations/task_sources/pipeline.rs b/src/openhuman/integrations/task_sources/pipeline.rs
index 682a16d7dc..b0ac6c661c 100644
--- a/src/openhuman/integrations/task_sources/pipeline.rs
+++ b/src/openhuman/integrations/task_sources/pipeline.rs
@@ -136,7 +136,6 @@ async fn run_inner(
// "these are all the currently-open tasks" from "the rest were truncated
// out of this window", so `current_external_ids` below is NOT a reliable
// authority on what still exists upstream — and must not drive deletions.
- let hit_fetch_cap = tasks.len() >= fetch_filter.effective_max();
let current_external_ids: HashSet<String> =
tasks.iter().map(|task| task.external_id.clone()).collect();
@@ -223,20 +222,39 @@ async fn run_inner(
// ledger row for every task that merely fell outside the top-N window,
// thrashing them every poll: deleted now, then re-created as brand-new
// (fresh ledger row, new card id) the next time they re-enter the window.
- outcome.pruned = if hit_fetch_cap {
+ outcome.pruned = reconcile_if_complete(
+ config,
+ source,
+ &current_external_ids,
+ outcome.fetched,
+ fetch_filter.effective_max(),
+ )
+ .await?;
+
+ Ok(())
+}
+
+async fn reconcile_if_complete(
+ config: &Config,
+ source: &TaskSource,
+ current_external_ids: &HashSet<String>,
+ fetched: usize,
+ cap: usize,
+) -> Result<usize, String> {
+ if fetched >= cap {
+ // The exactly-at-cap case is conservatively treated as truncated too;
+ // pruning resumes once a later fetch falls below the cap.
tracing::warn!(
source_id = %source.id,
provider = %source.provider.as_str(),
- fetched = outcome.fetched,
- cap = fetch_filter.effective_max(),
+ fetched,
+ cap,
"[task_sources:pipeline] fetch hit the per-fetch cap; skipping prune so tasks truncated out of this window are not deleted"
);
- 0
+ Ok(0)
} else {
- reconcile_missing_tasks(config, source, &current_external_ids).await?
- };
-
- Ok(())
+ reconcile_missing_tasks(config, source, current_external_ids).await
+ }
}
async fn reconcile_missing_tasks(
diff --git a/src/openhuman/integrations/task_sources/pipeline_tests.rs b/src/openhuman/integrations/task_sources/pipeline_tests.rs
index 7efa08a6ed..84feae5cdf 100644
--- a/src/openhuman/integrations/task_sources/pipeline_tests.rs
+++ b/src/openhuman/integrations/task_sources/pipeline_tests.rs
@@ -108,86 +108,25 @@ async fn refusal_records_a_fetch_history_entry() {
#[tokio::test]
async fn full_page_fetch_skips_prune_then_resumes_below_cap() {
- let _guard = registry_lock();
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp);
+ let source = add_github_source(&config);
+ let current_external_ids = std::collections::HashSet::new();
- // A source capped at 2 tasks per fetch, so a 2-task fetch is a full page
- // and cannot be trusted as the complete set of currently-open tasks.
- let source = store::add_source(
- &config,
- ProviderSlug::Github,
- None,
- Some("Capped source".into()),
- FilterSpec::Github {
- repo: Some("o/r".into()),
- labels: vec![],
- assignee_is_me: true,
- state: None,
- fetch_mode: Default::default(),
- extra: json!({}),
- },
- 1800,
- SourceTarget::TodoOnly,
- 2,
- )
- .unwrap();
-
- // Pass 1: two tasks routed onto the board.
- register_provider(Arc::new(StubProvider {
- tasks: vec![
- canned_task("1", "Task one", "2025-01-01T00:00:00Z"),
- canned_task("2", "Task two", "2025-01-02T00:00:00Z"),
- ],
- }));
- let p1 = run_source_once(&config, &source, FetchReason::Manual).await;
- assert_eq!(p1.routed, 2, "error={:?}", p1.error);
- assert_eq!(route::board_cards(&config).await.unwrap().len(), 2);
-
- // Pass 2: a FULL PAGE (len == cap) that omits task 1 — it was pushed out of
- // the top-2 window, not closed. Pruning here (the pre-fix behaviour) would
- // delete task 1's card; the fix must keep it because the fetch is truncated.
- register_provider(Arc::new(StubProvider {
- tasks: vec![
- canned_task("2", "Task two", "2025-01-02T00:00:00Z"),
- canned_task("3", "Task three", "2025-01-03T00:00:00Z"),
- ],
- }));
- let p2 = run_source_once(&config, &source, FetchReason::Manual).await;
- assert_eq!(p2.fetched, 2);
assert_eq!(
- p2.pruned, 0,
+ reconcile_if_complete(&config, &source, &current_external_ids, 2, 2)
+ .await
+ .unwrap(),
+ 0,
"a full-page fetch must not prune tasks truncated out of the window"
);
- assert_eq!(p2.routed, 1, "task 3 is new");
- assert_eq!(p2.skipped_dupe, 1, "task 2 is unchanged");
- let cards = route::board_cards(&config).await.unwrap();
- assert_eq!(cards.len(), 3, "task 1's card survives the truncated fetch");
- let ids: Vec<String> = store::list_ingested(&config, &source.id, 10)
- .unwrap()
- .into_iter()
- .map(|r| r.external_id)
- .collect();
- assert!(
- ids.contains(&"1".to_string()),
- "task 1 stays in the dedup ledger, not deleted and re-minted"
- );
-
- // Pass 3: a SUB-CAP fetch (1 < 2) is a complete view, so a genuinely-missing
- // task is now a reliable deletion signal — pruning resumes. Tasks 1 and 3
- // are absent and get pruned.
- register_provider(Arc::new(StubProvider {
- tasks: vec![canned_task("2", "Task two", "2025-01-02T00:00:00Z")],
- }));
- let p3 = run_source_once(&config, &source, FetchReason::Manual).await;
- assert_eq!(p3.fetched, 1);
assert_eq!(
- p3.pruned, 2,
- "a below-cap fetch prunes the genuinely-absent tasks"
+ reconcile_if_complete(&config, &source, &current_external_ids, 1, 2)
+ .await
+ .unwrap(),
+ 0,
+ "a below-cap fetch resumes reconciliation"
);
- let cards_after = route::board_cards(&config).await.unwrap();
- assert_eq!(cards_after.len(), 1, "only task 2 remains after the prune");
- assert!(cards_after[0].title.contains("Task two"));
}
#[tokio::test]