Watch
0
0
Fork
You've already forked hyperhive
0

add shared forge-client crate; hive-forge and hive-forge-notify use it

This commit is contained in:
atlas 2026-11-15 19:49:02 +01:00 • committed by mara
commit e59e41ec90
13 changed files with 502 additions and 214 deletions

View file

@ -110,6 +110,9 @@ Wire types + socket plumbing:
in-container producers (todos, reminders); also holds the `extra_mcp`
spec. ⚠️ **This socket never leaves the container** — `hive-c0re` is not
in the path, unlike `hive-core-agent-sock`.
- **`forge-client/`** — the shared Forgejo client construction (`async_client`,
`sync_client`), `TimeoutPolicy`, `TokenSource`, and the one drift-tolerant
`NotificationThread`; used by `hive-forge` and `hive-forge-notify`.
- **`hive-sock-client/`** — the shared JSON-line unix-socket client; retry
is a policy value (`Retry::None` / `Retry::RideOutRestart`). Also holds
the bounded request-line reader (`read_request_line`) the socket servers

15
Cargo.lock generated
View file

@ -1400,6 +1400,19 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "forge-client"
version = "0.1.0"
dependencies = [
"forgejo-api",
"reqwest",
"serde",
"serde_json",
"time",
"tokio",
"url",
]
[[package]]
name = "forgejo-api"
version = "0.11.1"
@ -1869,6 +1882,7 @@ dependencies = [
"anyhow",
"clap",
"clap-markdown",
"forge-client",
"forgejo-api",
"hive-types",
"libc",
@ -1886,6 +1900,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"backon",
"forge-client",
"forgejo-api",
"hive-agent-sock",
"hive-sock-client",

View file

@ -1,6 +1,7 @@
[workspace]
resolver = "3"
members = [
"forge-client",
"hive-agent",
"hive-agent-mcp",
"hive-agent-sock",
@ -116,6 +117,7 @@ hive-claude = "0.1.4"
hive-host-sock = { path = "hive-host-sock" }
hive-priv-sock = { path = "hive-priv-sock" }
hive-runtime = { path = "hive-runtime" }
forge-client = { path = "forge-client" }
hive-sock-client = { path = "hive-sock-client" }
hive-template = { path = "hive-template" }
hive-types = { path = "hive-types" }

30
forge-client/Cargo.toml Normal file
View file

@ -0,0 +1,30 @@
# `forge-client` — the one place hyperhive builds a Forgejo client and
# parses a Forgejo notification. Shared by `hive-forge` (blocking CLI) and
# `hive-forge-notify` (async poller) so that forgejo-api schema drift is
# handled once.
[package]
name = "forge-client"
edition.workspace = true
version.workspace = true
readme = "README.md"
[lints]
workspace = true
[features]
# The blocking `forgejo_api::sync` client and the blocking raw reqwest
# client. Only the CLI wants these.
sync = ["forgejo-api/sync", "reqwest/blocking"]
[dependencies]
forgejo-api.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
time.workspace = true
tokio = { workspace = true, features = ["time"] }
url.workspace = true
[dev-dependencies]
serde_json.workspace = true
tokio = { workspace = true, features = ["test-util", "macros", "rt"] }

26
forge-client/README.md Normal file
View file

@ -0,0 +1,26 @@
# forge-client
The one place hyperhive builds a Forgejo client and reads a Forgejo
notification. `hive-forge` (the blocking CLI) and `hive-forge-notify` (the
async poller) share it, so a Forgejo schema change that `forgejo-api` does not
yet model is fixed here once.
## When to use it
- Building a `forgejo_api` client from a base URL and a token: `async_client`,
and with the `sync` feature `sync_client`.
- Building the raw `reqwest` client for Forgejo's web-router-only routes (the
`sync` feature): `raw_blocking_client`.
- Deciding how long a forge call may take: `TimeoutPolicy`. A caller names its
policy (`TimeoutPolicy::CLI`, or its own value) and reads its bounds from it.
- Parsing a notification thread from the forge: `NotificationThread`. Every
field is optional and lenient, so an unknown or reshaped field cannot fail
the page the way `forgejo_api::structs::NotificationThread` does.
- Supplying the token: `TokenSource`. `StaticToken` is a fixed string; a
caller whose token can change implements the trait and rebuilds its client
when `current()` returns a different value.
## Not here
Anything that decides what a notification means (delivery, read-state,
filtering): that stays in `hive-forge-notify` and `hive-forge`.

214
forge-client/src/lib.rs Normal file
View file

@ -0,0 +1,214 @@
//! Forgejo client construction, timeout policy and the drift-tolerant
//! notification thread, shared by `hive-forge` and `hive-forge-notify`.
//!
//! Construction takes a base URL and a [`TokenSource`]; the bounds on how
//! long a call may take come from a [`TimeoutPolicy`] the caller names.
mod notification;
#[cfg(feature = "sync")]
use std::fmt;
use std::future::Future;
use std::time::Duration;
use forgejo_api::{Auth, Forgejo, ForgejoError};
pub use notification::{NotificationRepo, NotificationSubject, NotificationThread, parse_rfc3339};
/// Where a client's token comes from.
///
/// [`current`](TokenSource::current) is read each time a client is built. A
/// source whose token can change (re-read from a secret store after the
/// forge answers 401) rebuilds its client when `current()` returns a value
/// different from the one the client was built with; building is cheap and
/// pure, so a rebuild is a second call to [`async_client`].
pub trait TokenSource {
/// The token to send now.
fn current(&self) -> String;
}
/// A token that never changes.
#[derive(Clone)]
pub struct StaticToken(String);
impl StaticToken {
#[must_use]
pub fn new(token: impl Into<String>) -> Self {
Self(token.into())
}
}
impl TokenSource for StaticToken {
fn current(&self) -> String {
self.0.clone()
}
}
/// How long a caller lets forge calls take. Each caller picks one policy and
/// keeps it; the `forgejo_api` clients carry no timeout of their own, so a
/// bound exists only where the caller applies it from here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeoutPolicy {
/// Bound on reaching the forge, applied to the raw client. `None`: unbounded.
pub connect: Option<Duration>,
/// Whole-request budget for a small JSON call.
pub json: Duration,
/// Whole-request budget for a call that downloads a file body.
pub download: Duration,
}
impl TimeoutPolicy {
/// The `hive-forge` CLI: 5 s to connect, 15 s per JSON call, 10 minutes
/// for a download. `reqwest::blocking` has no idle timeout, so the
/// download figure bounds the whole transfer.
pub const CLI: Self = Self {
connect: Some(Duration::from_secs(5)),
json: Duration::from_secs(15),
download: Duration::from_mins(10),
};
/// A policy bounding JSON calls only (the notifier: no connect bound, no
/// downloads).
#[must_use]
pub const fn json_only(json: Duration) -> Self {
Self {
connect: None,
json,
download: json,
}
}
/// Run `call`, giving up after [`json`](Self::json).
///
/// # Errors
///
/// `Elapsed` when the budget ran out first.
pub async fn bound_json<F: Future>(
&self,
call: F,
) -> Result<F::Output, tokio::time::error::Elapsed> {
tokio::time::timeout(self.json, call).await
}
}
/// The async typed client for the forge at `url`, sending `token`'s current
/// value. No timeout is set on it: bound each call with
/// [`TimeoutPolicy::bound_json`].
///
/// # Errors
///
/// `url` is not http(s), or the token is not ASCII.
pub fn async_client(url: url::Url, token: &impl TokenSource) -> Result<Forgejo, ForgejoError> {
Forgejo::new(Auth::Token(&token.current()), url)
}
/// The blocking typed client for the forge at `url`. No timeout is set on it.
///
/// # Errors
///
/// `url` is not http(s), or the token is not ASCII.
#[cfg(feature = "sync")]
pub fn sync_client(
url: url::Url,
token: &impl TokenSource,
) -> Result<forgejo_api::sync::Forgejo, ForgejoError> {
forgejo_api::sync::Forgejo::new(Auth::Token(&token.current()), url)
}
/// Why [`raw_blocking_client`] failed.
#[cfg(feature = "sync")]
#[derive(Debug)]
pub enum RawClientError {
/// The token cannot be sent as a header value.
AuthHeader(reqwest::header::InvalidHeaderValue),
/// `reqwest` refused to build the client.
Build(reqwest::Error),
}
#[cfg(feature = "sync")]
impl fmt::Display for RawClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AuthHeader(e) => write!(f, "auth header: {e}"),
Self::Build(e) => write!(f, "build reqwest client: {e}"),
}
}
}
#[cfg(feature = "sync")]
impl std::error::Error for RawClientError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::AuthHeader(e) => Some(e),
Self::Build(e) => Some(e),
}
}
}
/// A blocking `reqwest` client carrying the forge token and asking for JSON,
/// with `policy.connect` as its connect timeout. For the routes Forgejo
/// serves only through its web router, which `forgejo_api` cannot reach.
///
/// # Errors
///
/// [`RawClientError`] when the token is not a valid header value or the
/// client cannot be built.
#[cfg(feature = "sync")]
pub fn raw_blocking_client(
token: &impl TokenSource,
policy: &TimeoutPolicy,
) -> Result<reqwest::blocking::Client, RawClientError> {
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue};
let mut headers = HeaderMap::new();
let mut auth = HeaderValue::from_str(&format!("token {}", token.current()))
.map_err(RawClientError::AuthHeader)?;
auth.set_sensitive(true);
headers.insert(AUTHORIZATION, auth);
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
let mut builder = reqwest::blocking::Client::builder().default_headers(headers);
if let Some(connect) = policy.connect {
builder = builder.connect_timeout(connect);
}
builder.build().map_err(RawClientError::Build)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cli_policy_keeps_the_hive_forge_bounds() {
assert_eq!(TimeoutPolicy::CLI.connect, Some(Duration::from_secs(5)));
assert_eq!(TimeoutPolicy::CLI.json, Duration::from_secs(15));
assert_eq!(TimeoutPolicy::CLI.download, Duration::from_mins(10));
}
#[tokio::test(start_paused = true)]
async fn bound_json_gives_up_after_the_json_budget() {
let policy = TimeoutPolicy {
download: Duration::from_hours(1),
..TimeoutPolicy::json_only(Duration::from_secs(10))
};
let slow = policy.bound_json(tokio::time::sleep(Duration::from_secs(11)));
assert!(slow.await.is_err());
let quick = policy.bound_json(tokio::time::sleep(Duration::from_secs(9)));
assert!(quick.await.is_ok());
}
#[test]
fn async_client_sends_the_token_source_value_and_rejects_non_http() {
let ok = url::Url::parse("http://forge.example/").unwrap();
assert!(async_client(ok, &StaticToken::new("t")).is_ok());
let bad = url::Url::parse("ftp://forge.example/").unwrap();
assert!(async_client(bad, &StaticToken::new("t")).is_err());
}
#[cfg(feature = "sync")]
#[test]
fn raw_client_builds_and_rejects_an_unsendable_token() {
assert!(raw_blocking_client(&StaticToken::new("t"), &TimeoutPolicy::CLI).is_ok());
let err = raw_blocking_client(&StaticToken::new("a\nb"), &TimeoutPolicy::CLI).unwrap_err();
assert!(matches!(err, RawClientError::AuthHeader(_)));
}
}

View file

@ -0,0 +1,179 @@
//! The drift-tolerant notification thread.
//!
//! Deliberately NOT `forgejo_api::structs::NotificationThread`: that struct
//! lags the running Forgejo, and one reshaped field fails the whole parse, so
//! every notification on the page is dropped. Every field here is optional
//! and degrades to `None` on an unknown or reshaped value, so a schema change
//! cannot break a notification call. Fields are the union of what
//! `hive-forge` and `hive-forge-notify` read.
use forgejo_api::structs::NotifySubjectType;
use serde::{Deserialize, Deserializer};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
/// One notification thread.
#[derive(Debug, Deserialize)]
pub struct NotificationThread {
/// Thread id as a string, whichever host sent it: Forgejo sends a JSON
/// number, GitHub a quoted string. Only ever an opaque key.
#[serde(default, deserialize_with = "de_opt_id")]
pub id: Option<String>,
#[serde(default, deserialize_with = "de_opt_rfc3339")]
pub updated_at: Option<OffsetDateTime>,
#[serde(default)]
pub subject: Option<NotificationSubject>,
#[serde(default)]
pub repository: Option<NotificationRepo>,
}
#[derive(Debug, Deserialize)]
pub struct NotificationSubject {
#[serde(default)]
pub title: Option<String>,
/// `Pull` and GitHub's `PullRequest` both map to
/// [`NotifySubjectType::Pull`]; an unknown type is `None`.
#[serde(rename = "type", default, deserialize_with = "de_opt_subject_type")]
pub r#type: Option<NotifySubjectType>,
#[serde(default, deserialize_with = "de_opt_url")]
pub html_url: Option<url::Url>,
/// The API URL; ends in the issue/PR number.
#[serde(default, deserialize_with = "de_opt_url")]
pub url: Option<url::Url>,
#[serde(default, deserialize_with = "de_opt_url")]
pub latest_comment_url: Option<url::Url>,
#[serde(default, deserialize_with = "de_opt_url")]
pub latest_comment_html_url: Option<url::Url>,
}
#[derive(Debug, Deserialize)]
pub struct NotificationRepo {
#[serde(default)]
pub full_name: Option<String>,
}
/// Parse an RFC 3339 timestamp as Forgejo emits them (`+02:00` or `Z`,
/// optionally with fractional seconds). `None` on any shape `time` does not
/// recognise.
#[must_use]
pub fn parse_rfc3339(s: &str) -> Option<OffsetDateTime> {
OffsetDateTime::parse(s, &Rfc3339).ok()
}
fn de_opt_subject_type<'de, D>(d: D) -> Result<Option<NotifySubjectType>, D::Error>
where
D: Deserializer<'de>,
{
Ok(
Option::<String>::deserialize(d)?.and_then(|s| match s.as_str() {
"Pull" | "PullRequest" => Some(NotifySubjectType::Pull),
"Issue" => Some(NotifySubjectType::Issue),
"Commit" => Some(NotifySubjectType::Commit),
"Repository" => Some(NotifySubjectType::Repository),
_ => None,
}),
)
}
/// A JSON number or a non-empty JSON string, as a `String`; anything else is
/// `None`.
fn de_opt_id<'de, D>(d: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
Ok(match Option::<serde_json::Value>::deserialize(d)? {
Some(serde_json::Value::String(s)) if !s.is_empty() => Some(s),
Some(serde_json::Value::Number(n)) => Some(n.to_string()),
_ => None,
})
}
/// A blank or unparseable URL is `None` (Forgejo marshals empty strings for
/// absent URLs).
fn de_opt_url<'de, D>(d: D) -> Result<Option<url::Url>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Option::<String>::deserialize(d)?
.filter(|s| !s.is_empty())
.and_then(|s| url::Url::parse(&s).ok()))
}
fn de_opt_rfc3339<'de, D>(d: D) -> Result<Option<OffsetDateTime>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Option::<String>::deserialize(d)?
.as_deref()
.and_then(parse_rfc3339))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn full_payload_parses_every_field() {
let t: NotificationThread = serde_json::from_value(json!({
"id": 42,
"updated_at": "2026-06-13T11:18:42+02:00",
"subject": {
"title": "t",
"type": "Pull",
"html_url": "http://f/o/r/pulls/7",
"url": "http://f/api/v1/repos/o/r/pulls/7",
"latest_comment_url": "http://f/api/v1/repos/o/r/issues/comments/1",
"latest_comment_html_url": "http://f/o/r/pulls/7/files",
},
"repository": { "full_name": "o/r" },
}))
.unwrap();
assert_eq!(t.id.as_deref(), Some("42"));
assert!(t.updated_at.is_some());
let s = t.subject.unwrap();
assert_eq!(s.r#type, Some(NotifySubjectType::Pull));
assert_eq!(s.url.unwrap().path(), "/api/v1/repos/o/r/pulls/7");
assert!(s.latest_comment_html_url.is_some());
assert_eq!(t.repository.unwrap().full_name.as_deref(), Some("o/r"));
}
#[test]
fn drifted_payload_still_parses_with_the_fields_it_can() {
// Reshaped values the struct tolerates: blank or malformed URLs, nulls, a new
// subject type, unparseable timestamp, extra keys, GitHub-style string id.
let t: NotificationThread = serde_json::from_value(json!({
"id": "abc",
"updated_at": "yesterday",
"unread": "maybe",
"subject": {
"title": null,
"type": "Discussion",
"state": "merged",
"url": "",
"html_url": "not a url",
"latest_comment_url": null,
},
"repository": { "full_name": null, "new_field": {} },
"new_top_level": [1, 2],
}))
.unwrap();
assert_eq!(t.id.as_deref(), Some("abc"));
assert!(t.updated_at.is_none());
let s = t.subject.unwrap();
assert!(s.r#type.is_none() && s.url.is_none() && s.html_url.is_none());
}
#[test]
fn empty_object_parses() {
let t: NotificationThread = serde_json::from_value(json!({})).unwrap();
assert!(t.id.is_none() && t.subject.is_none() && t.repository.is_none());
}
#[test]
fn github_pull_request_type_maps_to_pull() {
let s: NotificationSubject =
serde_json::from_value(json!({ "type": "PullRequest" })).unwrap();
assert_eq!(s.r#type, Some(NotifySubjectType::Pull));
}
}

View file

@ -18,6 +18,7 @@ anyhow.workspace = true
backon.workspace = true
forgejo-api.workspace = true
hive-agent-sock.workspace = true
forge-client.workspace = true
hive-sock-client.workspace = true
reqwest.workspace = true
serde.workspace = true

View file

@ -9,8 +9,9 @@
use std::time::Duration;
use forge_client::{StaticToken, TimeoutPolicy};
use forgejo_api::structs::{NotifyGetListQuery, NotifyReadThreadQuery};
use forgejo_api::{Auth, Forgejo, ForgejoError};
use forgejo_api::{Forgejo, ForgejoError};
use hive_forge_notify::notify::{HTTP_TIMEOUT_SECS, UNREAD_FETCH_LIMIT};
use hive_forge_notify::source::Source;
use tracing::{debug, warn};
@ -22,6 +23,7 @@ pub struct ForgejoSource {
/// out over plain `reqwest`.
pub base_url: String,
pub token: String,
timeouts: TimeoutPolicy,
}
impl ForgejoSource {
@ -36,11 +38,12 @@ impl ForgejoSource {
return None;
}
};
match Forgejo::new(Auth::Token(token), parsed) {
match forge_client::async_client(parsed, &StaticToken::new(token)) {
Ok(forge) => Some(Self {
forge: Box::new(forge),
base_url: base_url.to_owned(),
token: token.to_owned(),
timeouts: TimeoutPolicy::json_only(Duration::from_secs(HTTP_TIMEOUT_SECS)),
}),
Err(e) => {
warn!("forge_notify: failed to build forge client: {e}");
@ -93,9 +96,7 @@ impl Source for ForgejoSource {
.notify_get_list(query)
.page_size(u32::try_from(UNREAD_FETCH_LIMIT).unwrap_or(u32::MAX))
.response_type::<String>();
let raw = match tokio::time::timeout(Duration::from_secs(HTTP_TIMEOUT_SECS), request.send())
.await
{
let raw = match self.timeouts.bound_json(request.send()).await {
Ok(Ok(raw)) => raw,
Ok(Err(ForgejoError::UnexpectedStatusCode(status))) => {
debug!("forge_notify: poll status {status}");
@ -135,7 +136,7 @@ impl Source for ForgejoSource {
.forge
.notify_read_thread(thread_id, NotifyReadThreadQuery { to_status: None })
.response_type::<String>();
match tokio::time::timeout(Duration::from_secs(HTTP_TIMEOUT_SECS), request.send()).await {
match self.timeouts.bound_json(request.send()).await {
Err(_) => {
warn!(%id, "forge_notify: mark-read request failed — notification will resurface");
}

View file

@ -31,10 +31,9 @@ use std::path::Path;
use std::time::Duration;
use backon::{ConstantBuilder, Retryable as _};
use forge_client::{NotificationThread, parse_rfc3339};
use forgejo_api::structs::NotifySubjectType;
use serde::{Deserialize, Deserializer};
use time::OffsetDateTime;
use time::format_description::well_known::Rfc3339;
use tracing::{debug, warn};
use crate::source::Source;
@ -1073,122 +1072,6 @@ async fn fetch_inline_review_tail<S: Source>(
render_inline_comments(pr_number, &groups)
}
/// Parse an RFC 3339 timestamp as Forgejo emits them
/// (`2026-06-13T11:18:42+02:00` or `...Z`, optionally with fractional
/// seconds). Returns `None` on any shape `time` doesn't recognise so
/// callers can fall back gracefully.
fn parse_rfc3339(s: &str) -> Option<OffsetDateTime> {
OffsetDateTime::parse(s, &Rfc3339).ok()
}
/// Minimal, drift-tolerant view of a Forgejo notification thread —
/// deliberately NOT `forgejo_api::structs::NotificationThread`. That crate
/// (0.11.0) lags the running `pkgs.forgejo` release, and one field
/// type/shape drift in the upstream struct fails the WHOLE parse, so every
/// notification is dropped, never dedup'd / marked-read, and redelivered
/// forever. This local struct carries only the fields `forge_notify` reads,
/// each optional + lenient, so unknown or reshaped upstream fields can't
/// break notification read-state again.
#[derive(Debug, Deserialize)]
struct NotificationThread {
/// Thread id as a **string**, whichever host it came from: Forgejo
/// sends a JSON number, GitHub sends a quoted string. It is only ever
/// used as an opaque key (dedupe map, todo key, mark-read path
/// segment), so normalising to `String` at the parse boundary is
/// cheaper than carrying the difference through every call site.
#[serde(default, deserialize_with = "de_opt_id")]
id: Option<String>,
#[serde(default, deserialize_with = "de_opt_rfc3339")]
updated_at: Option<OffsetDateTime>,
#[serde(default)]
subject: Option<NotificationSubject>,
#[serde(default)]
repository: Option<NotificationRepo>,
}
#[derive(Debug, Deserialize)]
struct NotificationSubject {
#[serde(default)]
title: Option<String>,
#[serde(rename = "type", default, deserialize_with = "de_opt_subject_type")]
r#type: Option<NotifySubjectType>,
#[serde(default, deserialize_with = "de_opt_url")]
html_url: Option<url::Url>,
#[serde(default, deserialize_with = "de_opt_url")]
url: Option<url::Url>,
#[serde(default, deserialize_with = "de_opt_url")]
latest_comment_url: Option<url::Url>,
#[serde(default, deserialize_with = "de_opt_url")]
latest_comment_html_url: Option<url::Url>,
}
#[derive(Debug, Deserialize)]
struct NotificationRepo {
#[serde(default)]
full_name: Option<String>,
}
/// Deserialize an optional Forgejo subject `type`, mapping any unknown or
/// missing value to `None` instead of failing the parse — so a new Forgejo
/// subject type can never break notification read-state.
fn de_opt_subject_type<'de, D>(d: D) -> Result<Option<NotifySubjectType>, D::Error>
where
D: Deserializer<'de>,
{
Ok(
Option::<String>::deserialize(d)?.and_then(|s| match s.as_str() {
// Forgejo says `Pull`; GitHub says `PullRequest` for the same
// thing. Both map to the same label, so a GitHub PR doesn't
// render as the `?` unknown-type fallback.
"Pull" | "PullRequest" => Some(NotifySubjectType::Pull),
"Issue" => Some(NotifySubjectType::Issue),
"Commit" => Some(NotifySubjectType::Commit),
"Repository" => Some(NotifySubjectType::Repository),
_ => None,
}),
)
}
/// Deserialize an optional notification thread id from either a JSON
/// number (Forgejo) or a JSON string (GitHub), normalising both to
/// `String`. Anything else — including a `null` or an unexpected shape —
/// degrades to `None` rather than failing the parse, keeping the "one bad
/// field can never break notification read-state" property the rest of
/// this struct is built around.
fn de_opt_id<'de, D>(d: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
Ok(match Option::<serde_json::Value>::deserialize(d)? {
Some(serde_json::Value::String(s)) if !s.is_empty() => Some(s),
Some(serde_json::Value::Number(n)) => Some(n.to_string()),
_ => None,
})
}
/// Deserialize an optional URL, mapping a blank or unparseable value to
/// `None` (Forgejo marshals empty strings for absent URLs, which a plain
/// `Option<Url>` would reject).
fn de_opt_url<'de, D>(d: D) -> Result<Option<url::Url>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Option::<String>::deserialize(d)?
.filter(|s| !s.is_empty())
.and_then(|s| url::Url::parse(&s).ok()))
}
/// Deserialize an optional RFC 3339 timestamp, mapping any unrecognised
/// shape to `None` via the module's tolerant [`parse_rfc3339`].
fn de_opt_rfc3339<'de, D>(d: D) -> Result<Option<OffsetDateTime>, D::Error>
where
D: Deserializer<'de>,
{
Ok(Option::<String>::deserialize(d)?
.as_deref()
.and_then(parse_rfc3339))
}
/// One notification from the poll page: the lenient thread plus two raw
/// fields the typed struct can't carry faithfully.
struct PolledNotification {

View file

@ -19,6 +19,9 @@ clap-markdown = "0.1"
# not about the workspace. See the pin note on `forgejo-api` in the root
# `Cargo.toml`.
forgejo-api = { workspace = true, features = ["sync"] }
# Client construction, the CLI timeout policy and the drift-tolerant
# notification thread, shared with hive-forge-notify.
forge-client = { workspace = true, features = ["sync"] }
# The review rule `pr assign-reviewer` shares with swarm-controller.
hive-types.workspace = true
url.workspace = true

View file

@ -9,26 +9,20 @@
use std::path::PathBuf;
use anyhow::{Context, Result, anyhow, bail};
use forgejo_api::{Auth, ForgejoError};
use forge_client::{StaticToken, TimeoutPolicy};
use forgejo_api::ForgejoError;
use reqwest::blocking::{Client as HttpClient, Response};
use reqwest::header::{
ACCEPT, AUTHORIZATION, CONTENT_DISPOSITION, CONTENT_TYPE, HeaderMap, HeaderValue,
};
use reqwest::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Bound on reaching the forge.
const HTTP_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
/// Whole-request budget for a small JSON call (an `/api/v1` GET, or the
/// run-view log streamer's snapshot POST).
const JSON_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);
/// Whole-request budget for a GET that downloads a file body — an
/// attachment, an Actions artifact zip, or a persisted job log. Sized
/// for a large-but-live transfer rather than a JSON call:
/// `reqwest::blocking` has no separate read/idle timeout, so this bounds
/// the whole download rather than cutting it off partway through like
/// [`JSON_TIMEOUT`] would.
pub(crate) const DOWNLOAD_TIMEOUT: std::time::Duration = std::time::Duration::from_mins(10);
/// The CLI's timeout policy: connect bound, JSON-call budget (an `/api/v1`
/// GET, or the run-view log streamer's snapshot POST) and download budget
/// (an attachment, an Actions artifact zip, or a persisted job log; it bounds
/// the whole transfer because `reqwest::blocking` has no idle timeout).
const TIMEOUTS: TimeoutPolicy = TimeoutPolicy::CLI;
const JSON_TIMEOUT: std::time::Duration = TIMEOUTS.json;
pub(crate) const DOWNLOAD_TIMEOUT: std::time::Duration = TIMEOUTS.download;
/// Forgejo client pair: the typed `/api/v1` client plus a raw
/// `reqwest` client for web-router-only routes.
@ -94,20 +88,9 @@ impl Client {
.or_else(|| std::env::var("HIVE_FORGE_REPO").ok());
let url = url::Url::parse(&base).with_context(|| format!("parse HIVE_FORGE_URL {base}"))?;
let api = forgejo_api::sync::Forgejo::new(Auth::Token(&token), url)
.context("build forgejo client")?;
let mut headers = HeaderMap::new();
let auth = format!("token {token}");
let mut auth_val = HeaderValue::from_str(&auth).context("auth header")?;
auth_val.set_sensitive(true);
headers.insert(AUTHORIZATION, auth_val);
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
let web = HttpClient::builder()
.default_headers(headers)
.connect_timeout(HTTP_CONNECT_TIMEOUT)
.build()
.context("build reqwest client")?;
let token = StaticToken::new(token);
let api = forge_client::sync_client(url, &token).context("build forgejo client")?;
let web = forge_client::raw_blocking_client(&token, &TIMEOUTS)?;
Ok(Self {
api,
@ -576,7 +559,8 @@ impl std::error::Error for WebStatusError {}
/// `op` failed with `e`; a timeout names the bound that fired.
fn http_context(op: &str, e: &reqwest::Error, budget: std::time::Duration) -> String {
if e.is_connect() && e.is_timeout() {
format!("{op}: connect timed out after {HTTP_CONNECT_TIMEOUT:?}")
let connect = TIMEOUTS.connect.unwrap_or_default();
format!("{op}: connect timed out after {connect:?}")
} else if e.is_timeout() {
format!("{op}: timed out after {budget:?}")
} else {

View file

@ -13,6 +13,7 @@
//! `forge cli: agents keep commenting without reading prev comments`.)
use anyhow::Result;
use forge_client::NotificationThread;
use forgejo_api::structs::NotifyReadThreadQuery;
use crate::client::{Client, index, split_repo};
@ -33,38 +34,13 @@ const MAX_PAGES: u32 = 5;
/// notification (nothing to read / already read). Repo-scoped so a bare
/// number can't false-match another repo. Errors only on transport /
/// non-2xx — callers degrade open on `Err`.
/// Minimal, drift-tolerant view of a Forgejo notification thread — only
/// the two fields the read-before-comment guard needs: `id` (to mark the
/// thread read) and `subject.url` (to match the thread to an issue/PR
/// number). Both `#[serde(default)]` so a schema change anywhere else in
/// the notification object can't break this deserialize. This is the
/// deliberate replacement for `forgejo-api`'s strict `NotificationThread`,
/// whose schema drift vs the running (latest-line) Forgejo was breaking
/// the whole notifications call — silently disabling the guard and
/// wedging notification read-state (so wakes kept redelivering).
#[derive(serde::Deserialize)]
struct Notification {
#[serde(default)]
id: Option<i64>,
#[serde(default)]
subject: Option<Subject>,
}
/// The one notification-subject field the guard reads: the API `url`,
/// which ends in the issue/PR number (see [`subject_matches`]).
#[derive(serde::Deserialize)]
struct Subject {
#[serde(default)]
url: Option<String>,
}
pub fn unread_thread_id(client: &Client, repo: &str, number: u64) -> Result<Option<u64>> {
let (owner, name) = split_repo(repo)?;
let limit = PAGE_SIZE.to_string();
let path = format!("/repos/{owner}/{name}/notifications");
for page in 1..=MAX_PAGES {
let page_str = page.to_string();
let threads: Vec<Notification> = client.get_api_json(
let threads: Vec<NotificationThread> = client.get_api_json(
&path,
&[("all", "false"), ("page", &page_str), ("limit", &limit)],
)?;
@ -72,10 +48,10 @@ pub fn unread_thread_id(client: &Client, repo: &str, number: u64) -> Result<Opti
let subject_url = n
.subject
.as_ref()
.and_then(|s| s.url.as_deref())
.unwrap_or("");
.and_then(|s| s.url.as_ref())
.map_or("", url::Url::as_str);
if subject_matches(subject_url, number) {
return Ok(n.id.and_then(|id| u64::try_from(id).ok()));
return Ok(n.id.as_deref().and_then(|id| id.parse().ok()));
}
}
// Last (short) page reached — stop.
@ -123,36 +99,7 @@ pub fn subject_matches(subject_url: &str, number: u64) -> bool {
#[cfg(test)]
mod tests {
use super::{Notification, subject_matches};
#[test]
fn notification_deserializes_leniently_from_drifted_json() {
// A payload carrying fields the guard doesn't model (which would
// break `forgejo-api`'s strict struct) plus a missing subject —
// must still yield `id` + `subject.url` and never fail to parse.
let json = serde_json::json!([
{
"id": 42,
"unread": true,
"pinned": false,
"subject": {
"title": "whatever",
"url": "https://forge/api/v1/repos/o/r/issues/7", // lint:allow: test fixture, not a real forge link
"type": "SomeFutureSubjectTypeWeDoNotKnow"
},
"some_new_field": { "nested": 1 }
},
{ "id": 43 }
]);
let ns: Vec<Notification> = serde_json::from_value(json).expect("lenient deser");
assert_eq!(ns[0].id, Some(42));
assert_eq!(
ns[0].subject.as_ref().and_then(|s| s.url.as_deref()),
Some("https://forge/api/v1/repos/o/r/issues/7") // lint:allow: test fixture, not a real forge link
);
assert_eq!(ns[1].id, Some(43));
assert!(ns[1].subject.is_none());
}
use super::subject_matches;
#[test]
fn matches_issue_subject_url() {