fix(forge): consume a decided account notification

The account feed is at-least-once in a way the dedup ids do not cover.
`since` is inclusive at the boundary and `NotificationPage::finish`
advances its cursor only to the newest row seen, so a row the poll cannot
get past — anything at or before the cursor, which is every poll — comes
back for as long as the forge still considers it unread. Nothing marked
it read, and the daemon's cursor map is in memory, so a restart asked for
the whole unread feed again.

The result is a re-dispatch loop: the same notification id re-triaged
every pass, a fresh turn for a thread that already has one, and a second
comment on that thread if the first turn ever finished inside its lease.
Observed live on `vhaudiquet/orcaslicer-snap#10`: key revision 1 → 2 at
the 00:35 poll, `inbox_depth: 0`, no outcome — a re-dispatch of a turn
that was already running.

So consume what the poll decides:

- `Forge::mark_read` — a new default no-op on the trait, implemented for
  GitHub as `PUT /notifications/threads/{id}`;
- `Notification::read_key` carries what that route keys on (GitHub keys
  on the notification id, which the dedup id embeds only as text), and
  defaults on deserialize so a payload already in an inbox still decodes;
- `poll_account` marks each row read once it is triaged — dispatched,
  surfaced, or ignored; best-effort, since an unread row costs a repeat
  and never a lost event.

Gitea and GitLab keep the no-op: neither has an account watch here yet.
This commit is contained in:
kosmos authored and Valentin Haudiquet committed 2026-11-22 01:34:11 +00:00
1 parent 78fdec4f49
commit f779415b2b
9 files changed
+235 -3

No files matched your search

+77
View File
@@ -565,6 +565,22 @@ impl ForgeSource {
fed += 1;
}
}
// The row is decided — dispatched, surfaced, or ignored — so
// consume it: `mark_read` is what stops the feed returning
// it. Deciding without consuming is a loop: the poll's cursor
// never passes a row the forge still considers unread, so
// every pass re-decides it and re-dispatches a turn for a
// thread that already has one, and a restart (the cursor is
// in memory) replays the whole unread feed.
//
// Best-effort: an unread row costs a repeat, never a lost
// event, so a failed call is logged and the poll carries on.
if let Err(err) = client.mark_read(notification).await {
eprintln!(
"[claw] warning: mark-read of {} failed: {err}",
notification.id
);
}
}
if let Some(cursor) = page.cursor {
self.account_cursors
@@ -689,6 +705,65 @@ mod tests {
);
}
/// The account poll consumes what it decides. The feed is
/// at-least-once — the cursor never passes a row the forge still
/// considers unread, and a restart asks for the whole unread feed
/// again — so a row that is triaged but not marked read comes back
/// with the same dedup id on the next pass, and every pass dispatches
/// a fresh turn for a thread that already has one.
#[tokio::test]
async fn a_triaged_account_notification_is_marked_read() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/notifications"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([
{
"id": 42188,
"unread": true,
"reason": "mention",
"updated_at": "2026-10-05T12:00:00Z",
"subject": {
"title": "Add the widget flange",
"url": "https://api.github.com/repos/acme/widgets/pulls/142",
"type": "PullRequest"
},
"repository": { "full_name": "acme/widgets" }
}
])))
.mount(&server)
.await;
let store = Arc::new(ClawStore::in_memory().expect("store opens"));
let (dispatcher, accounts, notifications) = test_parts(store.clone());
// The store mints the id; the watch row is what names it.
let account = accounts
.put(rhema_identity::AccountDraft::new(
"forge bot",
"github",
rhema_identity::Secret::new(rhema_identity::SecretKind::Token, "acct-tok"),
))
.expect("account lands");
store
.add_account_watch("github", Some(&server.uri()), &account.id)
.expect("account watch lands");
let source = ForgeSource::new(&dispatcher, "github", store, accounts, notifications);
assert_eq!(
source.detect().await.expect("the pass runs"),
1,
"a mention dispatches a turn"
);
// The read route keys on the notification, not the thread.
let requests = server.received_requests().await.expect("recording is on");
let reads: Vec<_> = requests
.iter()
.filter(|request| request.method.as_str() == "PUT")
.collect();
assert_eq!(reads.len(), 1, "one mark-read per triaged row");
assert_eq!(reads[0].url.path(), "/notifications/threads/42188");
}
/// The parts a test source polls through, all in memory.
fn test_parts(
store: Arc<ClawStore>,
@@ -972,6 +1047,7 @@ mod tests {
title: Some("Add the flange".to_owned()),
url: None,
timestamp: Timestamp::parse("2026-10-08T00:00:00Z").expect("parses"),
read_key: Some("1".to_owned()),
raw: serde_json::json!({}),
};
let payload = |notification: &Notification| {
@@ -1016,6 +1092,7 @@ mod tests {
title: Some("a title".to_owned()),
url: None,
timestamp: Timestamp::parse("2026-11-21T00:00:00Z").expect("parses"),
read_key: Some("1".to_owned()),
raw: serde_json::json!({}),
}
}
+1
View File
@@ -977,6 +977,7 @@ mod tests {
"title": "Add the widget flange",
"url": null,
"timestamp": "2026-10-03T09:00:00Z",
"read_key": "42188",
"raw": {},
}))
.expect("notification fixture parses")