pub mod journal;
pub mod lease;
pub mod namespace;
pub mod observer;
use onetaskgraph_linear::live_artifact::{Scope, Slot, classify_retained, retained_title};
use serde_json::{Value, json};
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
pub const MODE_VARIABLE: &str = "ONETASKGRAPH_LINEAR_DOCUMENT_MODE";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
Ordinary,
Fresh,
Initialize,
}
impl Mode {
pub fn name(self) -> &'static str {
match self {
Self::Ordinary => "ordinary",
Self::Fresh => "fresh",
Self::Initialize => "initialize",
}
}
pub fn read() -> Result<Self, String> {
match std::env::var(MODE_VARIABLE).as_deref() {
Err(std::env::VarError::NotPresent) | Ok("ordinary") => Ok(Self::Ordinary),
Ok("fresh") => Ok(Self::Fresh),
Ok("initialize") => Ok(Self::Initialize),
_ => Err(format!(
"{MODE_VARIABLE} must be ordinary, fresh or initialize"
)),
}
}
}
pub struct Standing {
pub titles: BTreeMap<&'static str, String>,
pub ids: BTreeMap<&'static str, String>,
pub parent: Option<String>,
}
pub fn slot_key(slot: Slot) -> &'static str {
match slot {
Slot::Filed => "filed",
Slot::Loose => "loose",
Slot::FollowUp => "follow-up",
Slot::Assets => "assets",
Slot::Parent => "parent",
}
}
pub async fn send(
endpoint: &str,
key: &str,
query: &str,
variables: Value,
) -> Result<Value, String> {
let body: Value = reqwest::Client::new()
.post(endpoint)
.header("Authorization", key)
.json(&json!({"query":query,"variables":variables}))
.send()
.await
.map_err(|e| {
format!("Linear lifecycle request failed; discover ownership before retry: {e}")
})?
.error_for_status()
.map_err(|e| e.to_string())?
.json()
.await
.map_err(|e| e.to_string())?;
if body
.get("errors")
.is_some_and(|e| !e.as_array().is_some_and(Vec::is_empty))
{
return Err("Linear rejected lifecycle request; discover ownership before retry".into());
}
body.get("data")
.cloned()
.ok_or("Linear lifecycle response omitted data".into())
}
pub(crate) const DOCUMENTS: &str = "query($after:String,$filter:DocumentFilter!){documents(first:100,after:$after,filter:$filter){nodes{id title team{key} project{teams(first:2){nodes{key} pageInfo{hasNextPage}}}} pageInfo{hasNextPage endCursor}}}";
pub(crate) const PROJECTS: &str = "query($after:String,$filter:ProjectFilter!){projects(first:100,after:$after,filter:$filter){nodes{id name teams(first:2){nodes{key} pageInfo{hasNextPage}}} pageInfo{hasNextPage endCursor}}}";
fn in_team(node: &Value, team: &str, project: bool) -> bool {
if !project {
match node.get("team") {
Some(Value::Null) => {}
Some(Value::Object(direct)) => {
return direct
.get("key")
.and_then(Value::as_str)
.is_some_and(|key| key.eq_ignore_ascii_case(team));
}
_ => return false,
}
}
let teams = if project {
node.get("teams")
} else {
node.pointer("/project/teams")
};
teams.is_some_and(|teams| {
teams.pointer("/pageInfo/hasNextPage") == Some(&json!(false))
&& teams["nodes"].as_array().is_some_and(|nodes| {
!nodes.is_empty()
&& nodes.iter().all(|n| {
n["key"]
.as_str()
.is_some_and(|key| key.eq_ignore_ascii_case(team))
})
})
})
}
async fn discover(
endpoint: &str,
key: &str,
title: &str,
team: &str,
project: bool,
) -> Result<Option<String>, String> {
let root = if project { "projects" } else { "documents" };
let name = if project { "name" } else { "title" };
let query = if project { PROJECTS } else { DOCUMENTS };
let expected = classify_retained(title).ok_or("invalid requested standing identity")?;
let mut after = Value::Null;
let mut found = Vec::new();
for _ in 0..50 {
let filter = json!({name:{"containsIgnoreCase":format!("{}retained",onetaskgraph_linear::LIVE_ARTIFACT_TITLE_PREFIX)}});
let data = send(endpoint, key, query, json!({"after":after,"filter":filter})).await?;
let page = &data[root];
for node in page["nodes"]
.as_array()
.ok_or("standing discovery omitted nodes")?
{
let actual_title = node[name]
.as_str()
.ok_or("standing discovery omitted title")?;
let Some(actual) = classify_retained(actual_title) else {
continue;
};
if actual.scope != expected.scope {
continue;
}
if !in_team(node, team, project) {
return Err("standing ownership or nominated-team membership cannot be verified; no update sent".into());
}
if actual.slot != expected.slot {
continue;
}
found.push(
node["id"]
.as_str()
.filter(|s| !s.is_empty())
.ok_or("standing discovery omitted id")?
.to_owned(),
);
}
match page
.pointer("/pageInfo/hasNextPage")
.and_then(Value::as_bool)
{
Some(false) => {
return match found.len() {
0 => Ok(None),
1 => Ok(found.pop()),
_ => Err("duplicate standing ownership; no update sent".into()),
};
}
Some(true) => {
let next = page
.pointer("/pageInfo/endCursor")
.and_then(Value::as_str)
.ok_or("standing discovery omitted cursor")?;
if after.as_str() == Some(next) {
return Err("standing discovery cursor did not advance".into());
}
after = json!(next);
}
None => return Err("standing discovery omitted pagination".into()),
}
}
Err("standing discovery did not terminate".into())
}
#[derive(Clone, Default)]
pub struct Created {
ids: Arc<Mutex<Vec<(Slot, String)>>>,
panic_after: Option<usize>,
}
impl Created {
pub fn panicking_after(n: usize) -> Self {
Self {
panic_after: Some(n),
..Self::default()
}
}
fn record(&self, slot: Slot, id: &str) {
let count = {
let mut ids = self.ids.lock().unwrap();
ids.push((slot, id.to_owned()));
ids.len()
};
if self.panic_after == Some(count) {
panic!("injected failure after standing create {count}");
}
}
#[allow(
dead_code,
reason = "this module is compiled into both lanes' test binaries, and only the asset lane's own cleanup retires slots by its standing record"
)]
pub fn forget(&self) {
self.ids.lock().unwrap().clear();
}
pub fn len(&self) -> usize {
self.ids.lock().unwrap().len()
}
}
pub async fn retire(
endpoint: &str,
key: &str,
scope: Scope<'_>,
created: &Created,
) -> Result<usize, String> {
if scope.is_ci() {
return Ok(0);
}
let held = std::mem::take(&mut *created.ids.lock().unwrap());
let mut refused = Vec::new();
let mut retired = 0;
for (slot, id) in held.into_iter().rev() {
let (query, root) = if slot == Slot::Parent {
(
onetaskgraph_linear::graphql::PROJECT_DELETE,
"projectDelete",
)
} else {
(
onetaskgraph_linear::graphql::DOCUMENT_DELETE,
"documentDelete",
)
};
match send(endpoint, key, query, json!({"id":id})).await {
Ok(data) if data[root]["success"] == true => retired += 1,
Ok(_) => refused.push(format!(
"Linear did not confirm retiring {} slot {id}",
slot_key(slot)
)),
Err(error) => refused.push(format!("{} slot {id}: {error}", slot_key(slot))),
}
}
if refused.is_empty() {
Ok(retired)
} else {
Err(format!(
"invocation-owned standing slots were not all retired: {}",
refused.join("; ")
))
}
}
pub async fn unwinding<F: std::future::Future>(future: F) -> std::thread::Result<F::Output> {
let mut future = std::pin::pin!(future);
std::future::poll_fn(|cx| {
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| future.as_mut().poll(cx))) {
Ok(std::task::Poll::Pending) => std::task::Poll::Pending,
Ok(std::task::Poll::Ready(output)) => std::task::Poll::Ready(Ok(output)),
Err(panic) => std::task::Poll::Ready(Err(panic)),
}
})
.await
}
#[allow(
clippy::too_many_arguments,
reason = "prepare's seven lane identities plus the tracker its caller must hold for cleanup; a struct would only restate them"
)]
pub async fn prepare_owned(
endpoint: &str,
key: &str,
team: &str,
team_id: &str,
scope: Scope<'_>,
mode: Mode,
slots: &[Slot],
created: &Created,
) -> Result<Standing, String> {
let outcome = unwinding(prepare_tracked(
endpoint, key, team, team_id, scope, mode, slots, created,
))
.await;
if !matches!(outcome, Ok(Ok(_))) {
discover_stranded(endpoint, key, team, scope, slots, created).await;
}
match outcome {
Ok(Ok(standing)) => Ok(standing),
Ok(Err(error)) => Err(match retire(endpoint, key, scope, created).await {
Ok(_) => error,
Err(retired) => format!("{error}; additionally, {retired}"),
}),
Err(panic) => {
if let Err(retired) = retire(endpoint, key, scope, created).await {
eprintln!("{retired}");
}
std::panic::resume_unwind(panic)
}
}
}
async fn discover_stranded(
endpoint: &str,
key: &str,
team: &str,
scope: Scope<'_>,
slots: &[Slot],
created: &Created,
) {
if scope.is_ci() {
return;
}
let mut wanted = slots.to_vec();
if slots.contains(&Slot::Filed) {
wanted.insert(0, Slot::Parent);
}
for slot in wanted {
if created
.ids
.lock()
.unwrap()
.iter()
.any(|(held, _)| *held == slot)
{
continue;
}
let Ok(title) = retained_title(scope, slot) else {
continue;
};
match discover(endpoint, key, &title, team, slot == Slot::Parent).await {
Ok(Some(id)) => {
let mut ids = created.ids.lock().unwrap();
if slot == Slot::Parent {
ids.insert(0, (slot, id));
} else {
ids.push((slot, id));
}
}
Ok(None) => {}
Err(error) => eprintln!(
"{} slot could not be discovered for retirement: {error}",
slot_key(slot)
),
}
}
}
pub async fn prepare(
endpoint: &str,
key: &str,
team: &str,
team_id: &str,
scope: Scope<'_>,
mode: Mode,
slots: &[Slot],
) -> Result<Standing, String> {
prepare_tracked(
endpoint,
key,
team,
team_id,
scope,
mode,
slots,
&Created::default(),
)
.await
}
#[allow(
clippy::too_many_arguments,
reason = "the same lane identities as prepare_owned, which forwards each one unchanged"
)]
async fn prepare_tracked(
endpoint: &str,
key: &str,
team: &str,
team_id: &str,
scope: Scope<'_>,
mode: Mode,
slots: &[Slot],
created: &Created,
) -> Result<Standing, String> {
let nominated = scope.team();
if !nominated.eq_ignore_ascii_case(team)
|| team_id.trim().is_empty()
|| team_id.chars().any(char::is_control)
{
return Err("standing scope and nominated team disagree or team id is unverifiable".into());
}
let mut standing = Standing {
titles: BTreeMap::new(),
ids: BTreeMap::new(),
parent: None,
};
if mode == Mode::Fresh {
return Ok(standing);
}
let mut wanted = slots.to_vec();
if slots.contains(&Slot::Filed) {
wanted.insert(0, Slot::Parent);
}
for slot in &wanted {
let title = retained_title(scope, *slot)?;
if let Some(id) = discover(endpoint, key, &title, team, *slot == Slot::Parent).await? {
standing.ids.insert(slot_key(*slot), id);
}
standing.titles.insert(slot_key(*slot), title);
}
if mode == Mode::Ordinary
&& !scope.is_ci()
&& wanted
.iter()
.any(|s| !standing.ids.contains_key(slot_key(*s)))
{
return Err(format!(
"invocation-owned Linear document set absent or incomplete; non-CI ordinary reuse would create documents; run {MODE_VARIABLE}=initialize for an explicit creation pass; no documentCreate sent"
));
}
for slot in wanted {
let name = slot_key(slot);
if standing.ids.contains_key(name) {
continue;
}
let title = &standing.titles[name];
let (query, input, root, field) = if slot == Slot::Parent {
(
"mutation($input:ProjectCreateInput!){projectCreate(input:$input){success project{id}}}",
json!({"name":title,"teamIds":[team_id]}),
"projectCreate",
"project",
)
} else {
let mut input =
json!({"title":title,"content":"Explicit standing-set initialization."});
if slot == Slot::Filed {
input["projectId"] = json!(standing.ids["parent"]);
} else {
input["teamId"] = json!(team_id);
}
(
"mutation($input:DocumentCreateInput!){documentCreate(input:$input){success document{id}}}",
input,
"documentCreate",
"document",
)
};
let data = send(endpoint, key, query, json!({"input":input}))
.await
.map_err(|error| {
format!(
"uncertain standing create; discover exact identity before recovery: {error}"
)
})?;
if data[root]["success"] != true {
return Err("uncertain standing create; discover exact identity before recovery; no automatic retry".into());
}
let id=data[root][field]["id"].as_str().filter(|s| !s.is_empty()).ok_or("uncertain standing create acknowledgement; discover exact identity before recovery; no automatic retry")?;
standing.ids.insert(name, id.to_owned());
created.record(slot, id);
}
standing.parent = standing.ids.get("parent").cloned();
Ok(standing)
}
pub const POOL_VARIABLE: &str = "ONETASKGRAPH_LINEAR_POOL";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PoolSetup {
DryRun,
Initialize,
}
impl PoolSetup {
pub fn read() -> Result<Option<Self>, String> {
match std::env::var(POOL_VARIABLE) {
Err(std::env::VarError::NotPresent) => Ok(None),
Ok(value) => Self::parse(&value).map(Some),
Err(std::env::VarError::NotUnicode(_)) => {
Err(format!("{POOL_VARIABLE} must be dry-run or initialize"))
}
}
}
pub fn parse(value: &str) -> Result<Self, String> {
match value {
"dry-run" => Ok(Self::DryRun),
"initialize" => Ok(Self::Initialize),
_ => Err(format!(
"{POOL_VARIABLE} must be dry-run or initialize, not {value:?}"
)),
}
}
}
#[allow(
clippy::too_many_arguments,
reason = "prepare's lane identities plus the lease's holder and the setup asked for"
)]
pub async fn setup_pool(
endpoint: &str,
key: &str,
team: &str,
team_id: &str,
lane: onetaskgraph_linear::live_artifact::LeaseLane,
slots: &[Slot],
holder: &str,
setup: PoolSetup,
) -> Result<Vec<String>, String> {
use onetaskgraph_linear::live_artifact::POOL_SIZE;
let mut wanted = slots.to_vec();
if slots.contains(&Slot::Filed) {
wanted.insert(0, Slot::Parent);
}
let mut report = Vec::new();
for index in 1..=POOL_SIZE {
let scope = Scope::pooled(team, index)?;
if setup == PoolSetup::DryRun {
for slot in &wanted {
let title = retained_title(scope, *slot)?;
let present = discover(endpoint, key, &title, team, *slot == Slot::Parent).await?;
report.push(match present {
Some(id) => format!("present {title} ({id})"),
None => format!(
"would create {} {title}",
if *slot == Slot::Parent {
"project"
} else {
"document"
}
),
});
}
continue;
}
let Some(lease) = lease::claim_member(
endpoint,
key,
team,
team_id,
lane,
holder,
onetaskgraph_live::artifact::now_micros,
index,
)
.await?
else {
report.push(format!(
"pool member {index} is leased by a run still going; skipped"
));
continue;
};
let prepared = match lease::verify_exclusive(endpoint, key, team_id, &lease).await {
Ok(()) => prepare(endpoint, key, team, team_id, scope, Mode::Ordinary, slots).await,
Err(error) => Err(error),
};
let released = lease.release(endpoint, key).await;
let standing = prepared?;
released?;
for slot in &wanted {
let name = slot_key(*slot);
report.push(format!(
"ready {} ({})",
standing.titles[name],
standing.ids.get(name).map_or("?", String::as_str)
));
}
}
Ok(report)
}
pub const LOOPBACK_VARIABLE: &str = "LINEAR_LIVE_TEST_GRAPHQL";
pub fn loopback_endpoint() -> Option<String> {
let url = std::env::var(LOOPBACK_VARIABLE).ok()?;
let parsed = reqwest::Url::parse(&url)
.unwrap_or_else(|error| panic!("{LOOPBACK_VARIABLE} is not a URL: {error}"));
let loopback = parsed
.host_str()
.and_then(|host| {
host.trim_matches(['[', ']'])
.parse::<std::net::IpAddr>()
.ok()
})
.is_some_and(|host| host.is_loopback());
assert!(
parsed.scheme() == "http" && loopback && parsed.port().is_some(),
"{LOOPBACK_VARIABLE} must be a plain-HTTP loopback URL with an explicit port"
);
Some(url)
}
pub fn linear_endpoint() -> String {
loopback_endpoint().unwrap_or_else(|| "https://api.linear.app/graphql".to_owned())
}