use std::{collections::BTreeMap, env};
use onetaskgraph_live::artifact::{Run, Sweep, now_micros};
use onetaskgraph_live::{Credential, Exclusivity, Session, missing, required};
use onetaskgraph_plugin_api::{
Capabilities, DependencyEdge, DependencyEndpoint, DependencyKind, DependencySupport, Direction,
Document, DocumentQuery, ItemKind, ItemWrite, Label, LabelFilter, Location, MetadataMatch,
NativeId, PageRequest, Priority, Project, ProjectFilter, SecretResolver, SourceError,
SourceName, SourcePlugin, Status, StatusCategory, Support, Task, TaskQuery, TaskSource,
TextFields, TextQuery,
};
use secrecy::SecretString;
mod document_lifecycle;
use document_lifecycle::{Mode, Standing};
use onetaskgraph_linear::live_artifact::{LeaseLane, Scope, Slot};
use serde_json::{Value, json};
struct Environment;
impl SecretResolver for Environment {
fn get(&self, var: &str) -> Option<SecretString> {
env::var(var).ok().map(SecretString::from)
}
}
macro_rules! ensure {
($condition:expr, $($message:tt)+) => {
if !$condition {
return Err(format!($($message)+));
}
};
}
#[allow(dead_code)]
mod cleanup;
use cleanup::{
ISSUE_PAGE_PROBE, LABEL_CREATE, PROJECT_STATUSES, SESSION_NAME, TEAM_STATES, artifact_label,
artifact_title, is_this_runs, linear, remove_artifacts, run_then_cleanup, sweep_orphans,
};
mod settle;
use settle::{
Bound, LINEAR_DOCUMENT_LISTING, LINEAR_INDEX, LINEAR_REPLICA, resolving_labels, settled,
settled_content, settled_document_absent, settled_documents, settled_label, settled_projects,
settled_tasks, settled_walk, task_titles, token_moved,
};
async fn fixture_states(key: &str, team_key: &str) -> Result<(String, String), String> {
let data = linear(
key,
TEAM_STATES,
json!({"key":team_key}),
"live workflow state discovery",
)
.await?;
let states = data
.pointer("/teams/nodes/0/states/nodes")
.and_then(Value::as_array)
.ok_or_else(|| {
format!("LINEAR_WRITE_TEAM={team_key} names no team this credential can see")
})?;
let named = |wanted: &str| {
states
.iter()
.find(|state| state.get("type").and_then(Value::as_str) == Some(wanted))
.and_then(|state| state.get("name").and_then(Value::as_str))
.map(str::to_owned)
};
match (named("unstarted"), named("completed")) {
(Some(open), Some(done)) => Ok((open, done)),
_ => Err(format!(
"team {team_key} has no workflow state of type unstarted and one of type \
completed, which this lane needs to file two issues a status filter can \
separate; add them to that team or point LINEAR_WRITE_TEAM at a scratch team \
that has them"
)),
}
}
async fn fixture_project_status(key: &str) -> Result<String, String> {
let data = linear(
key,
PROJECT_STATUSES,
json!({}),
"live project status discovery",
)
.await?;
let statuses = data
.pointer("/projectStatuses/nodes")
.and_then(Value::as_array)
.ok_or_else(|| "live project status discovery returned no statuses".to_owned())?;
let names = statuses
.iter()
.filter_map(|status| status.get("name").and_then(Value::as_str))
.collect::<Vec<_>>();
names
.iter()
.find(|name| {
names
.iter()
.filter(|other| other.eq_ignore_ascii_case(name))
.count()
== 1
})
.map(|name| (*name).to_owned())
.ok_or_else(|| {
"this workspace has no project status name that resolves uniquely, and the \
source refuses one two statuses answer to"
.to_owned()
})
}
async fn create_label(key: &str, team: &str, name: &str) -> Result<(), String> {
let data = linear(
key,
LABEL_CREATE,
json!({"input":{"name":name,"teamId":team,"color":"#bec2c8"}}),
"live label creation",
)
.await?;
if data
.pointer("/issueLabelCreate/issueLabel/name")
.and_then(Value::as_str)
!= Some(name)
{
return Err(format!(
"Linear did not confirm creating the label {name:?}"
));
}
Ok(())
}
fn label(name: &str) -> Label {
Label {
id: NativeId("live-source-label".into()),
name: name.to_owned(),
color: None,
}
}
fn blocks(far: &NativeId, kind: ItemKind) -> DependencyEdge {
DependencyEdge {
from: DependencyEndpoint::from_native(NativeId("live-source-item".into()), kind),
to: DependencyEndpoint::from_native(far.clone(), kind),
kind: DependencyKind::Blocks,
}
}
fn page(limit: u32) -> PageRequest {
PageRequest {
cursor: None,
limit,
}
}
fn sorted(mut titles: Vec<String>) -> Vec<String> {
titles.sort();
titles
}
async fn write_labelled_task(
bound: Bound,
source: &dyn TaskSource,
write: &ItemWrite<Task>,
) -> Result<NativeId, SourceError> {
resolving_labels(bound, || source.write_task(write)).await
}
struct LiveRun {
endpoint: String,
key: String,
id: Run,
stamp_micros: u64,
open_state: String,
done_state: String,
project_status: String,
document_mode: Mode,
standing: Standing,
}
impl LiveRun {
async fn linear(&self, query: &str, variables: Value, _what: &str) -> Result<Value, String> {
document_lifecycle::send(&self.endpoint, &self.key, query, variables).await
}
}
async fn drive_every_declared_capability(
run: &LiveRun,
source: &dyn TaskSource,
team_id: &str,
) -> Result<(), String> {
let title = |offset: u64| artifact_title(run.id, run.stamp_micros + offset);
let (alpha, beta) = (title(0), title(1));
let (first, second, orphan) = (title(2), title(3), title(4));
let run_label = artifact_label(run.id, run.stamp_micros);
let only_label = artifact_label(run.id, run.stamp_micros + 1);
create_label(&run.key, team_id, &run_label).await?;
create_label(&run.key, team_id, &only_label).await?;
for name in [&run_label, &only_label] {
settled_label(LINEAR_INDEX, name, |query, variables| {
run.linear(query, variables, "live label lookup")
})
.await?;
}
let open = Status {
category: StatusCategory::Todo,
name: run.open_state.clone(),
};
let done = Status {
category: StatusCategory::Done,
name: run.done_state.clone(),
};
let project_status = Status {
category: StatusCategory::Todo,
name: run.project_status.clone(),
};
let missing = source
.set_task_status(
&NativeId("00000000-0000-0000-0000-000000000000".into()),
StatusCategory::Todo,
)
.await
.map_err(|error| format!("the live missing-issue status contract drifted: {error}"))?;
ensure!(
missing.is_none(),
"a status mutation to a nonexistent issue was not not-found"
);
let missing = source
.set_project_status(
&NativeId("00000000-0000-0000-0000-000000000000".into()),
StatusCategory::Todo,
)
.await
.map_err(|error| format!("the live missing-project status contract drifted: {error}"))?;
ensure!(
missing.is_none(),
"a status mutation to a nonexistent project was not not-found"
);
let scoped = || TaskQuery {
labels: LabelFilter {
any_of: vec![run_label.clone()],
..LabelFilter::default()
},
..TaskQuery::default()
};
let project = |name: &str| Project {
version: None,
id: NativeId("live-source-item".into()),
title: name.to_owned(),
content: Some("temporary credentialed write; the live lane removes this".into()),
status: project_status.clone(),
labels: vec![],
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: BTreeMap::new(),
repositories: vec![],
classification: Default::default(),
};
let task = |name: &str, status: &Status, under: Option<&NativeId>, labels: Vec<Label>| Task {
version: None,
id: NativeId("live-source-item".into()),
key: None,
title: name.to_owned(),
content: Some("temporary credentialed write; the live lane removes this".into()),
status: status.clone(),
labels,
priority: Priority::None,
project: under.cloned(),
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: BTreeMap::new(),
repositories: vec![],
delivers: Vec::new(),
delivered_by: Vec::new(),
classification: Default::default(),
assignees: None,
};
let alpha_id = source
.write_project(&ItemWrite {
target: None,
item: project(&alpha),
depends_on: vec![],
})
.await
.map_err(|error| format!("live project write of {alpha:?} failed: {error}"))?;
let beta_id = source
.write_project(&ItemWrite {
target: None,
item: project(&beta),
depends_on: vec![blocks(&alpha_id, ItemKind::Project)],
})
.await
.map_err(|error| format!("live project write of {beta:?} failed: {error}"))?;
let first_id = write_labelled_task(
LINEAR_INDEX,
source,
&ItemWrite {
target: None,
item: task(
&first,
&open,
Some(&alpha_id),
vec![label(&run_label), label(&only_label)],
),
depends_on: vec![],
},
)
.await
.map_err(|error| format!("live task write of {first:?} failed: {error}"))?;
let second_id = write_labelled_task(
LINEAR_INDEX,
source,
&ItemWrite {
target: None,
item: task(&second, &open, Some(&beta_id), vec![label(&run_label)]),
depends_on: vec![blocks(&first_id, ItemKind::Task)],
},
)
.await
.map_err(|error| format!("live task write of {second:?} failed: {error}"))?;
let orphan_id = write_labelled_task(
LINEAR_INDEX,
source,
&ItemWrite {
target: None,
item: task(&orphan, &done, None, vec![label(&run_label)]),
depends_on: vec![],
},
)
.await
.map_err(|error| format!("live task write of {orphan:?} failed: {error}"))?;
let all_three = sorted(vec![first.clone(), second.clone(), orphan.clone()]);
settled_tasks(
LINEAR_INDEX,
source,
&scoped(),
&format!("the three issues this run created, labelled {run_label},"),
&all_three,
)
.await?;
let listed = settled_projects(LINEAR_INDEX, source, &[alpha.clone(), beta.clone()]).await?;
ensure!(
listed.contains(&alpha) && listed.contains(&beta),
"the two projects this run created are not in Linear's own project listing"
);
let under = |id: &NativeId| TaskQuery {
project: ProjectFilter::Is(id.clone()),
..TaskQuery::default()
};
settled_tasks(
LINEAR_INDEX,
source,
&under(&alpha_id),
"the issues of one of this run's two projects",
std::slice::from_ref(&first),
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&under(&beta_id),
"the issues of the other of this run's two projects",
std::slice::from_ref(&second),
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
project: ProjectFilter::Orphans,
..scoped()
},
"this run's issues belonging to no project",
std::slice::from_ref(&orphan),
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
labels: LabelFilter {
any_of: vec![only_label.clone()],
..LabelFilter::default()
},
..TaskQuery::default()
},
"this run's issues carrying its second label",
std::slice::from_ref(&first),
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
labels: LabelFilter {
any_of: vec![run_label.clone()],
none_of: vec![only_label.clone()],
..LabelFilter::default()
},
..TaskQuery::default()
},
"this run's issues not carrying its second label",
&[second.clone(), orphan.clone()],
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
statuses: vec![StatusCategory::Todo],
..scoped()
},
"this run's unstarted issues",
&[first.clone(), second.clone()],
)
.await?;
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
statuses: vec![StatusCategory::Done],
..scoped()
},
"this run's completed issue",
std::slice::from_ref(&orphan),
)
.await?;
for (terms, fields, expected) in [
(first.clone(), TextFields::Title, vec![first.clone()]),
(first.clone(), TextFields::Content, Vec::new()),
(
first.clone(),
TextFields::TitleOrContent,
vec![first.clone()],
),
(
"TEMPORARY CREDENTIALED WRITE".to_owned(),
TextFields::Content,
all_three.clone(),
),
] {
settled_tasks(
LINEAR_INDEX,
source,
&TaskQuery {
text: Some(TextQuery { terms, fields }),
..scoped()
},
&format!("a {fields:?} search this source narrows itself"),
&expected,
)
.await?;
}
let task_edge = DependencyEdge {
from: DependencyEndpoint::from_native(second_id.clone(), ItemKind::Task),
to: DependencyEndpoint::from_native(first_id.clone(), ItemKind::Task),
kind: DependencyKind::Blocks,
};
let project_edge = DependencyEdge {
from: DependencyEndpoint::from_native(beta_id.clone(), ItemKind::Project),
to: DependencyEndpoint::from_native(alpha_id.clone(), ItemKind::Project),
kind: DependencyKind::Blocks,
};
for (near, direction, expected, level) in [
(&second_id, Direction::DependsOn, &task_edge, "task"),
(&first_id, Direction::DependedOnBy, &task_edge, "task"),
(&beta_id, Direction::DependsOn, &project_edge, "project"),
(&alpha_id, Direction::DependedOnBy, &project_edge, "project"),
] {
let read = if level == "task" {
source.task_dependencies(near, direction, &page(50)).await
} else {
source
.project_dependencies(near, direction, &page(50))
.await
}
.map_err(|error| format!("live {level} {direction:?} dependency read failed: {error}"))?;
ensure!(
read.items == vec![expected.clone()],
"the {level} {direction:?} read of {} returned {:?}",
near.0,
read.items
);
}
let walk = "a walk in pages of one over this run's own three issues";
settled_walk(LINEAR_INDEX, source, &scoped(), 10, walk, &all_three).await?;
stale_cursor_is_refused_as_the_source_reads_it(run).await?;
let ceiling = source.capabilities().max_page_size;
let over_the_ceiling = scoped();
let what = "a read at a limit one above the declared ceiling, which this source clamps \
rather than refuses,";
settled(LINEAR_INDEX, what, &all_three, || {
task_titles(source, &over_the_ceiling, ceiling + 1, what)
})
.await?;
run.linear(
ISSUE_PAGE_PROBE,
json!({"first":ceiling}),
"page size probe at the declared maximum",
)
.await?;
let written = source
.get_task(&first_id)
.await
.map_err(|error| format!("live task read-back failed: {error}"))?
.ok_or_else(|| "the written issue was not readable by its own id".to_owned())?;
ensure!(
written.title == first && written.status.category == StatusCategory::Todo,
"the live write did not round-trip its title and status: {written:?}"
);
let closed_back = source
.get_task(&orphan_id)
.await
.map_err(|error| format!("live completed-issue read-back failed: {error}"))?
.ok_or_else(|| "the completed issue was not readable by its own id".to_owned())?;
ensure!(
closed_back.status.category == StatusCategory::Done,
"the live write filed under done read back as {:?}",
closed_back.status.category
);
drive_document_paths(run, source, &alpha_id, None).await?;
every_field_change_moves_a_linear_tasks_token(
source,
(&second_id, &first_id, &orphan_id),
(&title(11), &label(&only_label)),
)
.await
}
async fn stale_cursor_is_refused_as_the_source_reads_it(run: &LiveRun) -> Result<(), String> {
const NOTHING: &str = "00000000-0000-4000-8000-000000000000";
let body: Value = reqwest::Client::new()
.post(&run.endpoint)
.header("Authorization", &run.key)
.json(&json!({"query": onetaskgraph_linear::graphql::ISSUES,
"variables": {"first": 1, "after": NOTHING}}))
.send()
.await
.map_err(|error| format!("the stale-cursor probe was not answered: {error}"))?
.json()
.await
.map_err(|error| format!("the stale-cursor probe's answer is not JSON: {error}"))?;
let error = &body["errors"][0];
let presentable = error["extensions"]["userPresentableMessage"]
.as_str()
.unwrap_or_default();
ensure!(
error["message"].as_str() == Some(onetaskgraph_linear::graphql::CURSOR_NOT_FOUND_MESSAGE)
&& presentable.starts_with(&format!(
"{} '{NOTHING}'",
onetaskgraph_linear::graphql::CURSOR_NOT_FOUND_PRESENTABLE
)),
"Linear answered a page after a cursor it does not hold with {body}, which the Linear \
source no longer reads as a stale cursor: update graphql::CURSOR_NOT_FOUND_MESSAGE and \
CURSOR_NOT_FOUND_PRESENTABLE to what it says"
);
Ok(())
}
async fn drive_document_paths(
run: &LiveRun,
source: &dyn TaskSource,
alpha_id: &NativeId,
journal: Option<&document_lifecycle::journal::Journal>,
) -> Result<(), String> {
let title = |offset: u64| artifact_title(run.id, run.stamp_micros + offset);
let document_parent = run.standing.parent.as_ref().map(|id| NativeId(id.clone()));
let document_parent = document_parent.as_ref().unwrap_or(alpha_id);
let filed_title = run
.standing
.titles
.get("filed")
.cloned()
.unwrap_or_else(|| title(5));
let loose_title = run
.standing
.titles
.get("loose")
.cloned()
.unwrap_or_else(|| title(6));
if let Some(journal) = journal {
journal.journey("drive_documents");
}
drive_documents(run, source, document_parent, &filed_title, &loose_title).await?;
let follow_up_title = run
.standing
.titles
.get("follow-up")
.cloned()
.unwrap_or_else(|| title(9));
if let Some(journal) = journal {
journal.journey("drive_follow_ups");
}
drive_follow_ups(
run,
source,
&title(7),
&title(8),
&follow_up_title,
&title(10),
)
.await
}
async fn every_field_change_moves_a_linear_tasks_token(
source: &dyn TaskSource,
(task, first, orphan): (&NativeId, &NativeId, &NativeId),
(renamed, extra_label): (&str, &Label),
) -> Result<(), String> {
use onetaskgraph_plugin_api::{CommentBody, MetadataKey, NewComment, TaskUpdate};
let read = || async {
let held = source
.get_task(task)
.await
.map_err(|error| format!("live read of the task failed: {error}"))?
.ok_or_else(|| "the task was not there".to_owned())?;
ensure!(
held.version
.as_deref()
.is_some_and(|token| token.starts_with("lin1|")),
"Linear gave the task {:?} rather than a `lin1` change token, which \
docs/plugin-protocol.md §4.13d says every whole read carries",
held.version
);
Ok::<Task, String>(held)
};
let pause = || tokio::time::sleep(std::time::Duration::from_millis(1_100));
let mut evidence = Vec::new();
let mut before = read().await?;
let mut note = |kind: &str, before: &Task, after: &Task| {
evidence.push(format!(
"{kind}: updatedAt {} → {}, token {}",
before
.updated_at
.map_or("none".to_owned(), |at| at.to_rfc3339()),
after
.updated_at
.map_or("none".to_owned(), |at| at.to_rfc3339()),
if before.version == after.version {
"unmoved"
} else {
"moved"
},
));
};
let body = |text: &str| {
CommentBody::new(text).map_err(|error| format!("a live comment body: {error}"))
};
pause().await;
let comment = source
.add_comment(
task,
&NewComment {
body: body("temporary credentialed comment; the live lane removes it")?,
author: None,
},
)
.await
.map_err(|error| format!("live comment write failed: {error}"))?
.ok_or_else(|| "the task to comment on was not there".to_owned())?;
let after = read().await?;
note("a comment added", &before, &after);
before = after;
pause().await;
source
.edit_comment(
task,
&comment.id,
&body("temporary credentialed comment, edited")?,
)
.await
.map_err(|error| format!("live comment edit failed: {error}"))?
.ok_or_else(|| "the comment to edit was not there".to_owned())?;
let after = read().await?;
note("a comment edited", &before, &after);
before = after;
pause().await;
source
.delete_comment(task, &comment.id)
.await
.map_err(|error| format!("live comment deletion failed: {error}"))?
.ok_or_else(|| "the comment to delete was not there".to_owned())?;
let after = read().await?;
note("a comment deleted", &before, &after);
before = after;
pause().await;
source
.set_task_content(task, "temporary credentialed write, its body edited")
.await
.map_err(|error| format!("live content write failed: {error}"))?
.ok_or_else(|| "the task to edit was not there".to_owned())?;
let after = token_moved(source, LINEAR_REPLICA, "a body edit", &before, &read).await?;
note("a body edit", &before, &after);
before = after;
pause().await;
source
.update_task(
task,
&TaskUpdate {
title: Some(renamed.to_owned()),
..TaskUpdate::default()
},
)
.await
.map_err(|error| format!("live title write failed: {error}"))?
.ok_or_else(|| "the task to rename was not there".to_owned())?;
let after = token_moved(source, LINEAR_REPLICA, "a title edit", &before, &read).await?;
note("a title edit", &before, &after);
before = after;
pause().await;
let mut labelled = before.clone();
labelled.labels.push(extra_label.clone());
let edges = source
.task_dependencies(task, Direction::DependsOn, &page(50))
.await
.map_err(|error| format!("live dependency read failed: {error}"))?
.items;
write_labelled_task(
LINEAR_INDEX,
source,
&ItemWrite {
target: Some(task.clone()),
item: labelled,
depends_on: edges,
},
)
.await
.map_err(|error| format!("live label write failed: {error}"))?;
let after = token_moved(source, LINEAR_REPLICA, "a label change", &before, &read).await?;
note("a label change", &before, &after);
before = after;
pause().await;
source
.set_task_metadata(
task,
&MetadataKey::new("live.change_kind").map_err(|error| format!("a key: {error}"))?,
&json!("metadata"),
)
.await
.map_err(|error| format!("live metadata write failed: {error}"))?
.ok_or_else(|| "the task to annotate was not there".to_owned())?;
let after = token_moved(source, LINEAR_REPLICA, "a metadata change", &before, &read).await?;
note("a metadata change", &before, &after);
before = after;
pause().await;
source
.set_task_priority(task, Priority::Low)
.await
.map_err(|error| format!("live priority write failed: {error}"))?
.ok_or_else(|| "the task to prioritise was not there".to_owned())?;
let after = token_moved(source, LINEAR_REPLICA, "a priority change", &before, &read).await?;
note("a priority change", &before, &after);
before = after;
for (kind, on) in [
("a dependency added", vec![first, orphan]),
("a dependency removed", vec![first]),
] {
pause().await;
source
.update_task(
task,
&TaskUpdate {
depends_on: Some(
on.into_iter()
.map(|far| blocks(far, ItemKind::Task))
.collect(),
),
..TaskUpdate::default()
},
)
.await
.map_err(|error| format!("live dependency write failed: {error}"))?
.ok_or_else(|| "the task to make depend was not there".to_owned())?;
let after = read().await?;
note(kind, &before, &after);
before = after;
}
pause().await;
source
.set_task_status(task, StatusCategory::Done)
.await
.map_err(|error| format!("live status write failed: {error}"))?
.ok_or_else(|| "the task to finish was not there".to_owned())?;
let after = token_moved(source, LINEAR_REPLICA, "a status change", &before, &read).await?;
note("a status change", &before, &after);
eprintln!(
"Linear's Issue.updatedAt and lin1 token across every change a copy has to see:\n {}",
evidence.join("\n ")
);
Ok(())
}
async fn raw_description(run: &LiveRun, id: &NativeId) -> Result<String, String> {
run.linear(
"query($id:String!){ issue(id:$id){ description } }",
json!({"id":id.0}),
"live raw issue read",
)
.await?
.pointer("/issue/description")
.and_then(Value::as_str)
.map(str::to_owned)
.ok_or_else(|| format!("issue {} carried no description", id.0))
}
async fn raw_content(run: &LiveRun, id: &NativeId, expected: &str) -> Result<String, String> {
settled_content(LINEAR_REPLICA, expected, || async {
run.linear(
"query($id:String!){ document(id:$id){ content } }",
json!({"id":id.0}),
"live raw document read",
)
.await?
.pointer("/document/content")
.and_then(Value::as_str)
.map(str::to_owned)
.ok_or_else(|| format!("document {} carried no content", id.0))
})
.await
}
const PROBE: &str = "P.\n\n<!-- probe {\"site\":\"example.com\",\"list\":[1]} -->\n\n<!-- probe\n{\"k\":\"v\"}\n-->\n\n<!-- probe `{\"site\":\"example.com\",\"list\":[1]}` -->";
const PROBE_STORED: &str = "P.\n\n<!-- probe {\"site\":\"[example.com](<http://example.com>)\",\"list\":\\[1\\]} -->\n\n<!-- probe\n{\"k\":\"v\"}\n\\-->\n\n<!-- probe `{\"site\":\"example.com\",\"list\":[1]}` -->";
const MARKDOWN_PROBE: &str = "- bullet\n| a | b |\n| --- | --- |\nsee https://example.com/a.\nexample.com and \"caller.live\"\n[x] done ~5 _y_\nsay \\\"hi\\\" a \\d b\n-->\n```\n- [x] _y_ example.com ~\n```\nkept `- [x] _y_ example.com ~` span\n\n\n\n| a | b |\n| --- | --- |\n";
const MARKDOWN_PROBE_STORED: &str = "* bullet\n | a | b |\n | --- | --- |\n see [https://example.com/a](<https://example.com/a>).\n [example.com](<http://example.com>) and \"[caller.live](<http://caller.live>)\"\n \\[x\\] done \\~5 *y*\n say \"hi\" a \\\\d b\n \\-->\n\n```\n- [x] _y_ example.com ~\n```\n\nkept `- [x] _y_ example.com ~` span\n\n| a | b |\n| -- | -- |\n";
async fn drive_follow_ups(
run: &LiveRun,
source: &dyn TaskSource,
matching: &str,
decoy: &str,
document_title: &str,
probe_title: &str,
) -> Result<(), String> {
let tag = format!("otg-live-{}", run.stamp_micros);
let origin = format!("elsewhere:{tag}");
let issue = |title: &str, content: String, metadata: Value, priority: Priority| Task {
version: None,
id: NativeId("live-source-item".into()),
key: None,
title: title.to_owned(),
content: Some(content),
status: Status {
category: StatusCategory::Todo,
name: run.open_state.clone(),
},
labels: Vec::new(),
priority,
project: None,
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: serde_json::from_value(metadata).unwrap_or_default(),
repositories: Vec::new(),
delivers: Vec::new(),
delivered_by: Vec::new(),
classification: Default::default(),
assignees: None,
};
let hostile = json!(["github.com/a/b", "_y_", "a --> b"]);
let matching_metadata = json!({"caller.tag": tag, "caller.key": tag, "caller.live": tag,
"caller.site": hostile, "onetaskgraph.origin": origin});
let matching_id = source
.write_task(&ItemWrite {
target: None,
item: issue(
matching,
"A follow-up the lane searches for.".to_owned(),
matching_metadata.clone(),
Priority::High,
),
depends_on: Vec::new(),
})
.await
.map_err(|error| format!("live write of {matching:?} failed: {error}"))?;
let decoy_id = source
.write_task(&ItemWrite {
target: None,
item: issue(
decoy,
format!(
"Quotes \"caller.key\":\"{tag}\" and \"onetaskgraph.origin\":\"{origin}\" \
in its prose, and {tag}-prose."
),
json!({"caller.tag": tag, "caller.key": format!("{tag}-other")}),
Priority::Low,
),
depends_on: Vec::new(),
})
.await
.map_err(|error| format!("live write of {decoy:?} failed: {error}"))?;
let read_back = source
.get_task(&matching_id)
.await
.map_err(|error| format!("live read of {matching:?} failed: {error}"))?
.ok_or_else(|| format!("{matching:?} was not readable by its own id"))?;
for key in ["caller.tag", "caller.key", "caller.live", "caller.site"] {
ensure!(
read_back.metadata.get(key) == matching_metadata.get(key),
"the metadata slot did not keep {key}: wrote {}, read {:?}",
matching_metadata[key],
read_back.metadata.get(key)
);
}
let held = raw_description(run, &matching_id).await?;
ensure!(
held.starts_with("A follow-up the lane searches for.\n\n+++ onetaskgraph metadata\n\n`")
&& held.ends_with("`\n\n+++")
&& !held.contains("<!--"),
"the metadata did not come back as the collapsed block: {held:?}"
);
source
.add_comment(
&matching_id,
&onetaskgraph_plugin_api::NewComment {
body: onetaskgraph_plugin_api::CommentBody::new(
"A comment the activity search finds.".to_owned(),
)
.map_err(|error| error.to_string())?,
author: None,
},
)
.await
.map_err(|error| format!("live comment add failed: {error}"))?
.ok_or_else(|| "the matching issue had no comments".to_owned())?;
let probe_id = source
.write_task(&ItemWrite {
target: None,
item: issue(probe_title, "probe".to_owned(), json!({}), Priority::None),
depends_on: Vec::new(),
})
.await
.map_err(|error| format!("live write of {probe_title:?} failed: {error}"))?;
run.linear("mutation($id:String!,$input:IssueUpdateInput!){ issueUpdate(id:$id,input:$input){success} }",
json!({"id":probe_id.0,"input":{"description":PROBE}}),
"live description probe",
)
.await?;
let stored = raw_description(run, &probe_id).await?;
ensure!(
stored == PROBE_STORED,
"an issue description's HTML comments came back as {stored:?}, not as recorded"
);
let added = source
.add_comment(
&probe_id,
&onetaskgraph_plugin_api::NewComment {
body: onetaskgraph_plugin_api::CommentBody::new(PROBE.to_owned())
.map_err(|error| error.to_string())?,
author: None,
},
)
.await
.map_err(|error| format!("live comment add failed: {error}"))?
.ok_or_else(|| "the probe issue had no comments".to_owned())?;
ensure!(
added.body.as_str() == PROBE,
"a comment body's HTML comments did not survive byte for byte: {:?}",
added.body.as_str()
);
let document_id = source
.write_document(&ItemWrite {
target: run.standing.ids.get("follow-up").cloned().map(NativeId),
item: Document {
version: None,
id: NativeId("live-source-item".into()),
title: document_title.to_owned(),
content: Some("A probed document.".to_owned()),
project: None,
labels: Vec::new(),
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: serde_json::from_value(json!({"caller.live": tag})).unwrap_or_default(),
repositories: Vec::new(),
classification: Default::default(),
},
depends_on: Vec::new(),
})
.await
.map_err(|error| format!("live document write failed: {error}"))?;
let document = source
.get_document(&document_id)
.await
.map_err(|error| format!("live document read failed: {error}"))?
.ok_or_else(|| "the probed document was not readable by its own id".to_owned())?;
ensure!(
document.metadata.get("caller.live") == Some(&json!(tag))
&& document.content.as_deref() == Some("A probed document."),
"a document's slot did not keep its metadata: {document:?}"
);
let updated = run.linear(
"mutation($id:String!,$input:DocumentUpdateInput!){ documentUpdate(id:$id,input:$input){success document{content}} }",
json!({"id":document_id.0,"input":{"content":PROBE}}),
"live content probe",
)
.await?;
ensure!(
updated
.pointer("/documentUpdate/success")
.and_then(Value::as_bool)
== Some(true)
&& updated
.pointer("/documentUpdate/document/content")
.and_then(Value::as_str)
== Some(PROBE_STORED),
"the document update did not acknowledge the exact stored content: {updated:?}"
);
let content = raw_content(run, &document_id, PROBE_STORED).await?;
ensure!(
content == PROBE_STORED,
"a document's HTML comments came back as {content:?}, not as recorded"
);
run.linear("mutation($id:String!,$input:IssueUpdateInput!){ issueUpdate(id:$id,input:$input){success} }",
json!({"id":probe_id.0,"input":{"description":MARKDOWN_PROBE}}),
"live Markdown description probe",
)
.await?;
let stored = raw_description(run, &probe_id).await?;
ensure!(
stored == MARKDOWN_PROBE_STORED,
"an issue description's Markdown came back as {stored:?}, not as the ruling declares"
);
let read = source
.get_task(&probe_id)
.await
.map_err(|error| format!("the Markdown probe's issue could not be read: {error}"))?
.and_then(|task| task.content);
ensure!(
read.as_deref() == Some(MARKDOWN_PROBE_STORED),
"a read of the Markdown probe's issue handed back {read:?}, not its stored form"
);
let updated = run.linear(
"mutation($id:String!,$input:DocumentUpdateInput!){ documentUpdate(id:$id,input:$input){success document{content}} }",
json!({"id":document_id.0,"input":{"content":MARKDOWN_PROBE}}),
"live Markdown content probe",
)
.await?;
ensure!(
updated
.pointer("/documentUpdate/success")
.and_then(Value::as_bool)
== Some(true)
&& updated
.pointer("/documentUpdate/document/content")
.and_then(Value::as_str)
== Some(MARKDOWN_PROBE_STORED),
"the document update did not acknowledge the exact stored content: {updated:?}"
);
let content = raw_content(run, &document_id, MARKDOWN_PROBE_STORED).await?;
ensure!(
content == MARKDOWN_PROBE_STORED,
"a document's Markdown came back as {content:?}, not as the ruling declares"
);
let read = Some(
settled_content(LINEAR_REPLICA, MARKDOWN_PROBE_STORED, || async {
source
.get_document(&document_id)
.await
.map_err(|error| {
format!("the Markdown probe's document could not be read: {error}")
})?
.and_then(|document| document.content)
.ok_or_else(|| "the Markdown probe's document carried no content".to_owned())
})
.await?,
);
ensure!(
read.as_deref() == Some(MARKDOWN_PROBE_STORED),
"a read of the Markdown probe's document handed back {read:?}, not its stored form"
);
let tabled = "Owners:\n\n| a | b |\n| -- | -- |\n".to_owned();
source
.write_document(&ItemWrite {
target: Some(document_id.clone()),
item: Document {
version: None,
id: document_id.clone(),
title: document_title.to_owned(),
content: Some(tabled.clone()),
project: None,
labels: Vec::new(),
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: serde_json::from_value(json!({"caller.live": tag})).unwrap_or_default(),
repositories: Vec::new(),
classification: Default::default(),
},
depends_on: Vec::new(),
})
.await
.map_err(|error| format!("live table document write failed: {error}"))?;
let read = source
.get_document(&document_id)
.await
.map_err(|error| format!("the table document could not be read: {error}"))?;
ensure!(
read.as_ref()
.and_then(|document| document.content.as_deref())
== Some(tabled.as_str())
&& read
.as_ref()
.and_then(|document| document.metadata.get("caller.live"))
== Some(&json!(tag)),
"a document ending in a table, written with a slot, read back as {read:?}, not as {tabled:?}"
);
let both = vec![decoy.to_owned(), matching.to_owned()];
let only = vec![matching.to_owned()];
let tag_match = MetadataMatch::new("caller.tag", Vec::new(), tag.clone())?;
let base = TaskQuery {
metadata: vec![tag_match.clone()],
..TaskQuery::default()
};
settled_tasks(LINEAR_INDEX, source, &base, "the two tagged issues", &both).await?;
for (query, what, expected) in [
(
TaskQuery {
metadata: vec![
tag_match.clone(),
MetadataMatch::new("caller.key", Vec::new(), tag.clone())?,
],
..TaskQuery::default()
},
"a metadata match the decoy carries only in prose",
only.clone(),
),
(
TaskQuery {
origin: Some(origin.clone()),
..base.clone()
},
"an origin the decoy carries only in prose",
only.clone(),
),
(
TaskQuery {
priorities: vec![Priority::High],
..base.clone()
},
"a priority",
only.clone(),
),
(
TaskQuery {
commented_since: chrono::Utc::now()
.checked_sub_signed(chrono::Duration::minutes(10)),
..base.clone()
},
"comment activity in the last ten minutes",
only.clone(),
),
(
TaskQuery {
commented_since: chrono::Utc::now().checked_add_signed(chrono::Duration::days(1)),
..base.clone()
},
"comment activity tomorrow",
Vec::new(),
),
(
TaskQuery {
text: Some(TextQuery {
terms: format!("{tag}-PROSE"),
fields: TextFields::Content,
}),
..base.clone()
},
"a content search, any case",
vec![decoy.to_owned()],
),
] {
settled_tasks(LINEAR_INDEX, source, &query, what, &expected).await?;
}
let narrowed = |filter: Value| async move {
let data = run
.linear(
"query($filter:IssueFilter){ issues(first:10,filter:$filter){ nodes{ title } } }",
json!({ "filter": filter }),
"live comparator probe",
)
.await?;
let mut titles = data
.pointer("/issues/nodes")
.and_then(Value::as_array)
.ok_or_else(|| "the comparator probe answered no issues".to_owned())?
.iter()
.filter_map(|node| node["title"].as_str().map(str::to_owned))
.collect::<Vec<_>>();
titles.sort();
Ok::<_, String>(titles)
};
let phrase = format!("\"caller.tag\":\"{tag}\"");
for (filter, what, expected) in [
(
json!({"description":{"contains":phrase}}),
"`contains` of a phrase both slots hold",
both.clone(),
),
(
json!({"description":{"contains":phrase.to_uppercase()}}),
"`contains` of the same phrase upper-cased",
Vec::new(),
),
(
json!({"description":{"containsIgnoreCase":phrase.to_uppercase()}}),
"`containsIgnoreCase` of it upper-cased",
both.clone(),
),
] {
settled(LINEAR_INDEX, what, &expected, || narrowed(filter.clone())).await?;
}
let before = raw_description(run, &decoy_id).await?;
let key = onetaskgraph_plugin_api::MetadataKey::new("caller.review")?;
source
.set_task_metadata(&decoy_id, &key, &json!({"approved": true}))
.await
.map_err(|error| format!("live metadata write failed: {error}"))?
.ok_or_else(|| "the decoy was not held".to_owned())?;
let after = raw_description(run, &decoy_id).await?;
let above = |field: &str| {
field
.rfind("+++ onetaskgraph metadata")
.map(|start| field[..start].to_owned())
};
ensure!(
above(&before).is_some() && above(&before) == above(&after),
"a narrow metadata write moved bytes above the slot:\n{before:?}\n{after:?}"
);
ensure!(
after.contains("\"caller.review\":{\"approved\":true}"),
"the narrow metadata write did not land in the slot: {after:?}"
);
Ok(())
}
async fn drive_documents(
run: &LiveRun,
source: &dyn TaskSource,
under: &NativeId,
filed: &str,
loose: &str,
) -> Result<(), String> {
let document = |title: &str, project: Option<&NativeId>| Document {
version: None,
id: NativeId("live-source-item".into()),
title: title.to_owned(),
content: Some("temporary credentialed write; the live lane removes this".into()),
project: project.cloned(),
labels: vec![],
url: None,
location: None,
created_at: None,
updated_at: None,
metadata: [
("caller.count".to_owned(), json!(3)),
("caller.run".to_owned(), json!(run.stamp_micros)),
]
.into_iter()
.collect(),
repositories: vec![],
classification: Default::default(),
};
let write = |item: Document| ItemWrite {
target: if item.title == filed {
run.standing.ids.get("filed")
} else {
run.standing.ids.get("loose")
}
.cloned()
.map(NativeId),
item,
depends_on: vec![],
};
let filed_id = source
.write_document(&write(document(filed, Some(under))))
.await
.map_err(|error| format!("live document write of {filed:?} failed: {error}"))?;
let loose_id = source
.write_document(&write(document(loose, None)))
.await
.map_err(|error| format!("live document write of {loose:?} failed: {error}"))?;
let read = source
.get_document(&filed_id)
.await
.map_err(|error| format!("live document read-back failed: {error}"))?
.ok_or_else(|| "the written document was not readable by its own id".to_owned())?;
ensure!(
read.title == filed,
"the live document write did not round-trip its title: {read:?}"
);
ensure!(
read.content.as_deref() == Some("temporary credentialed write; the live lane removes this"),
"the visible body a read reports is the text a person wrote: {:?}",
read.content
);
ensure!(
read.metadata.get("caller.count") == Some(&json!(3)),
"a caller's key did not round-trip with its JSON type: {:?}",
read.metadata
);
ensure!(
read.metadata.get("caller.run") == Some(&json!(run.stamp_micros)),
"the retained document did not reset caller.run to this invocation: {:?}",
read.metadata.get("caller.run")
);
ensure!(
read.labels.is_empty(),
"Linear's own document type has no labels, so a read reports none: {:?}",
read.labels
);
ensure!(
matches!(&read.location, Some(Location::Url(url)) if url.starts_with("https://")),
"a document says where it is, as a link: {:?}",
read.location
);
ensure!(
read.project.as_ref() == Some(under),
"the document filed under this run's project read back filed under {:?}",
read.project
);
let refusal = source
.write_document(&write(Document {
labels: vec![label(&artifact_label(run.id, run.stamp_micros))],
..document(filed, Some(under))
}))
.await;
ensure!(
refusal
.as_ref()
.err()
.is_some_and(|error| error.to_string().contains("labels")),
"a document write carrying a label must be refused by name, and was {refusal:?}"
);
settled_documents(
LINEAR_INDEX,
LINEAR_DOCUMENT_LISTING,
source,
&DocumentQuery::default(),
&[filed_id.clone(), loose_id.clone()],
"the two documents this run created",
&[
(filed_id.clone(), filed.to_owned()),
(loose_id.clone(), loose.to_owned()),
],
)
.await?;
settled_documents(
LINEAR_INDEX,
LINEAR_DOCUMENT_LISTING,
source,
&DocumentQuery {
project: ProjectFilter::Is(under.clone()),
..DocumentQuery::default()
},
&[filed_id.clone(), loose_id.clone()],
"a document listing narrowed to this run's project",
&[(filed_id.clone(), filed.to_owned())],
)
.await?;
settled_documents(
LINEAR_INDEX,
LINEAR_DOCUMENT_LISTING,
source,
&DocumentQuery {
project: ProjectFilter::Orphans,
..DocumentQuery::default()
},
&[filed_id.clone(), loose_id.clone()],
"a document listing narrowed to the orphans",
&[(loose_id.clone(), loose.to_owned())],
)
.await?;
settled_documents(
LINEAR_INDEX,
LINEAR_DOCUMENT_LISTING,
source,
&DocumentQuery {
labels: LabelFilter {
any_of: vec![artifact_label(run.id, run.stamp_micros)],
..LabelFilter::default()
},
..DocumentQuery::default()
},
&[filed_id.clone(), loose_id.clone()],
"a document listing demanding a label, which no Linear document carries,",
&[],
)
.await?;
for (id, title) in [(&filed_id, filed), (&loose_id, loose)]
.into_iter()
.filter(|_| run.document_mode == Mode::Fresh)
{
source
.delete_document(id)
.await
.map_err(|error| format!("live document removal failed: {error}"))?;
settled_document_absent(LINEAR_INDEX, source, id, title).await?;
}
Ok(())
}
fn linear_graphql() -> String {
let url = document_lifecycle::linear_endpoint();
if document_lifecycle::loopback_endpoint().is_some() {
cleanup::against(&url);
}
url
}
#[tokio::test]
async fn real_linear_applies_every_declared_capability_and_leaves_no_residue() {
let live_required = required(
env::var(onetaskgraph_live::REQUIRED_VARIABLE)
.ok()
.as_deref(),
)
.unwrap_or_else(|error| panic!("the Linear live lane cannot run: {error}"));
let skip = |reason: &str| -> Option<String> {
match missing(live_required, SESSION_NAME, reason) {
Ok(reason) => {
eprintln!("skipped live Linear journey: {reason}");
None
}
Err(error) => panic!("the Linear live lane cannot run: {error}"),
}
};
let Some(key) = env::var("LINEAR_API_KEY").ok().and_then(Credential::new) else {
skip("LINEAR_API_KEY is not set");
return;
};
let Some(team) = env::var("LINEAR_WRITE_TEAM")
.ok()
.filter(|value| !value.trim().is_empty())
else {
skip(
"LINEAR_WRITE_TEAM is not set, and this lane writes only to the scratch team that \
name gives rather than discovering one; no mutation was sent. Set it to that \
team's key — in CI it is the LINEAR_WRITE_TEAM repository variable, and locally \
it is an environment variable",
);
return;
};
let session = Session::open(SESSION_NAME, key, Exclusivity::OneAtATime)
.unwrap_or_else(|declined| declined.refuse());
let key = session.credential().expose().to_owned();
let document_mode = Mode::read().unwrap_or_else(|e| panic!("{e}"));
let owner = Run::current();
let stamp = onetaskgraph_live::artifact::Stamp::new(owner, now_micros()).to_string();
let team_key = team.to_ascii_uppercase();
let ci = env::var("GITHUB_ACTIONS").as_deref() == Ok("true");
let effective_mode = document_mode;
if !ci && document_mode == Mode::Initialize {
eprintln!(
"non-CI creation pass: initialize invocation-owned plugin document slots, exercise reuse assertions, and retire the set"
);
}
let linear_graphql = linear_graphql();
let team_id = linear(&key, TEAM_STATES, json!({"key":team}), "live team lookup")
.await
.and_then(|data| {
data.pointer("/teams/nodes/0/id")
.and_then(Value::as_str)
.map(str::to_owned)
.ok_or_else(|| {
format!("LINEAR_WRITE_TEAM={team} names no team this credential can see")
})
})
.unwrap_or_else(|error| {
panic!("the Linear live lane cannot reach its scratch team: {error}")
});
if let Some(setup) = document_lifecycle::PoolSetup::read().unwrap_or_else(|e| panic!("{e}")) {
let report = document_lifecycle::setup_pool(
&linear_graphql,
&key,
&team_key,
&team_id,
LeaseLane::Plugin,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&stamp,
setup,
)
.await
.unwrap_or_else(|error| panic!("the Linear plugin pool setup failed: {error}"));
for line in report {
println!("{line}");
}
return;
}
let journal = document_lifecycle::journal::Journal::new("plugin", effective_mode.name())
.durable(
std::env::current_exe()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("telemetry/onetaskgraph-linear/live")
.join(&stamp),
)
.unwrap();
let namespace = std::sync::Arc::new(document_lifecycle::namespace::Namespace::new(
owner, &team_key,
));
let endpoint = document_lifecycle::observer::guarded(
&linear_graphql,
&key,
journal.clone(),
std::sync::Arc::clone(&namespace),
);
let (open_state, done_state) = fixture_states(&key, &team)
.await
.unwrap_or_else(|error| panic!("the Linear live lane cannot file its fixture: {error}"));
let project_status = fixture_project_status(&key)
.await
.unwrap_or_else(|error| panic!("the Linear live lane cannot file its projects: {error}"));
let source = onetaskgraph_linear::Plugin
.build(
&SourceName::new("live").unwrap(),
&json!({"endpoint":endpoint,"team":team,"status_mapping":{
"todo":{"task":open_state,"project":project_status},
"done":{"task":done_state},
}}),
&Environment,
)
.unwrap_or_else(|error| panic!("the Linear live lane cannot use this team: {error}"));
assert!(source.health().await.unwrap().reachable);
assert_eq!(
source.capabilities(),
Capabilities {
projects: Support::Native,
documents: Support::Native,
comments: Support::Native,
assets: Support::Native,
priority: Support::Native,
filter_by_priority: Support::Native,
filter_by_comment_activity: Support::Native,
filter_by_metadata: Support::Native,
filter_by_origin: Support::Native,
reconcile_status: Support::Unsupported,
complete_by_issue: Support::Unsupported,
orphan_tasks: Support::Native,
filter_by_label: Support::Native,
filter_by_status: Support::Native,
search_title: Support::Native,
search_content: Support::Native,
task_dependencies: DependencySupport::BothDirections,
project_dependencies: DependencySupport::BothDirections,
max_page_size: onetaskgraph_linear::MAX_PAGE_SIZE,
assignees: Support::Unsupported,
}
);
let lease = if ci {
Some(
document_lifecycle::lease::claim(
&linear_graphql,
&key,
&team_key,
&team_id,
LeaseLane::Plugin,
&stamp,
now_micros,
)
.await
.unwrap_or_else(|error| panic!("the Linear live lane cannot lease its slots: {error}")),
)
} else {
None
};
let scope = match &lease {
Some(lease) => {
namespace.leased(lease.index);
lease.scope()
}
None => Scope::local(&team_key, &stamp).unwrap(),
};
let created = &document_lifecycle::Created::default();
let lane_endpoint = endpoint.clone();
let lane_key = key.clone();
document_lifecycle::lease::holding(lease.as_ref(), &linear_graphql, &lane_key, async {
if let Some(lease) = &lease {
document_lifecycle::lease::verify_exclusive(
&linear_graphql,
&lane_key,
&team_id,
lease,
)
.await
.unwrap_or_else(|error| panic!("{error}"));
}
guard_plugin_invocation(
&lane_endpoint,
&lane_key,
&team_key,
&team_id.clone(),
scope,
effective_mode,
created,
|standing| async move {
if ci && document_mode == Mode::Initialize {
journal.finish().unwrap();
return;
}
journal.journey("capability-and-follow-up");
let run = LiveRun {
endpoint,
key: key.clone(),
id: Run::current(),
stamp_micros: now_micros(),
open_state,
done_state,
project_status,
document_mode,
standing,
};
let id = run.id;
let panicked = std::sync::Mutex::new(None);
let result = run_then_cleanup(
|| async {
match document_lifecycle::unwinding(drive_every_declared_capability(
&run,
source.as_ref(),
&team_id,
))
.await
{
Ok(outcome) => outcome,
Err(panic) => {
*panicked.lock().unwrap() = Some(panic);
Err("the Linear live journey panicked".into())
}
}
},
|| async {
let slots =
document_lifecycle::retire(&run.endpoint, &key, scope, created).await;
let mine =
remove_artifacts(&key, &|prefix, name| is_this_runs(id, prefix, name))
.await
.and(slots.map(drop));
let orphans = sweep_orphans(&key, &Sweep::of(id, now_micros())).await;
match (mine, orphans) {
(Ok(()), Ok(())) => Ok(()),
(Err(mine), Ok(())) => Err(mine),
(Ok(()), Err(orphans)) => Err(format!(
"residue left by an earlier interrupted run could not be \
cleared: {orphans}"
)),
(Err(mine), Err(orphans)) => Err(format!(
"{mine}; additionally, residue left by an earlier interrupted \
run could not be cleared: {orphans}"
)),
}
},
)
.await;
journal.finish().unwrap();
if let Some(panic) = panicked.into_inner().unwrap() {
if let Err(error) = result {
eprintln!("{error}");
}
std::panic::resume_unwind(panic);
}
result.unwrap_or_else(|error| {
panic!("Linear live capability journey failed: {error}")
});
},
)
.await
})
.await;
let refused = namespace.refusals();
assert!(
refused.is_empty(),
"the Linear live lane named records outside its run's namespace: {refused:?}"
);
}
#[allow(
clippy::too_many_arguments,
reason = "prepare_owned's lane identities plus the invocation; the offline journey passes \
each one separately to drive this guard as the live lane does"
)]
async fn guard_plugin_invocation<F, Fut>(
endpoint: &str,
key: &str,
team_key: &str,
team_id: &str,
scope: Scope<'_>,
mode: Mode,
created: &document_lifecycle::Created,
invocation: F,
) where
F: FnOnce(Standing) -> Fut,
Fut: std::future::Future<Output = ()>,
{
let guarded = document_lifecycle::unwinding(async {
let standing = document_lifecycle::prepare_owned(
endpoint,
key,
team_key,
team_id,
scope,
mode,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
created,
)
.await
.unwrap_or_else(|e| panic!("{e}"));
invocation(standing).await;
})
.await;
if let Err(panic) = guarded {
if let Err(error) = document_lifecycle::retire(endpoint, key, scope, created).await {
eprintln!("{error}");
}
std::panic::resume_unwind(panic);
}
}
#[path = "document_lifecycle/replay.rs"]
mod document_replay;
fn replay() -> document_replay::Replay {
document_replay::Replay::start(
document_lifecycle::journal::Journal::new("plugin", "initialize"),
[
(PROBE.to_owned(), PROBE_STORED.to_owned()),
(MARKDOWN_PROBE.to_owned(), MARKDOWN_PROBE_STORED.to_owned()),
]
.into_iter()
.collect(),
)
}
async fn replay_prepare(
replay: &document_replay::Replay,
scope: Scope<'_>,
mode: Mode,
) -> Result<document_lifecycle::Standing, String> {
document_lifecycle::prepare(
&replay.endpoint,
"synthetic",
"ENG",
"team",
scope,
mode,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
)
.await
}
async fn replay_drive(
replay: &document_replay::Replay,
standing: document_lifecycle::Standing,
mode: Mode,
stamp: u64,
) -> Result<(), String> {
replay_drive_at(&replay.endpoint, &replay.journal, standing, mode, stamp).await
}
async fn replay_drive_at(
endpoint: &str,
journal: &document_lifecycle::journal::Journal,
standing: document_lifecycle::Standing,
mode: Mode,
stamp: u64,
) -> Result<(), String> {
replay_drive_as(endpoint, journal, standing, mode, stamp, Run::current()).await
}
async fn replay_drive_as(
endpoint: &str,
journal: &document_lifecycle::journal::Journal,
standing: document_lifecycle::Standing,
mode: Mode,
stamp: u64,
owner: Run,
) -> Result<(), String> {
let source = onetaskgraph_linear::Plugin.build(&SourceName::new("live").unwrap(),
&json!({"endpoint":endpoint,"team":"ENG","status_mapping":{"todo":{"task":"Todo","project":"Planned"},"done":{"task":"Done"}}}),&ReplaySecrets).unwrap();
let parent = if let Some(parent) = &standing.parent {
parent.clone()
} else {
let data = document_lifecycle::send(endpoint, "synthetic",
"mutation($input:ProjectCreateInput!){projectCreate(input:$input){success project{id}}}",
json!({"input":{"name":artifact_title(owner, stamp),"teamIds":["team"]}})).await?;
data["projectCreate"]["project"]["id"]
.as_str()
.ok_or("fresh parent create omitted id")?
.to_owned()
};
let run = LiveRun {
endpoint: endpoint.to_owned(),
key: "synthetic".into(),
id: owner,
stamp_micros: stamp,
open_state: "Todo".into(),
done_state: "Done".into(),
project_status: "Planned".into(),
document_mode: mode,
standing,
};
drive_document_paths(&run, source.as_ref(), &NativeId(parent), Some(journal)).await
}
async fn replay_ordinary_ci_pass(
replay: &document_replay::Replay,
stamp: u64,
standing_ids: &std::sync::Mutex<Option<BTreeMap<&'static str, String>>>,
) {
guard_plugin_invocation(
&replay.endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
Mode::Ordinary,
&document_lifecycle::Created::default(),
|standing| async move {
*standing_ids.lock().unwrap() = Some(standing.ids.clone());
replay_drive(replay, standing, Mode::Ordinary, stamp)
.await
.unwrap();
},
)
.await;
}
async fn replay_cleanup_documents(replay: &document_replay::Replay) -> usize {
let query = "query($filter:DocumentFilter!){documents(first:100,filter:$filter){nodes{id title} pageInfo{hasNextPage endCursor}}}";
let read = || {
document_lifecycle::send(
&replay.endpoint,
"synthetic",
query,
json!({"filter":{"title":{"containsIgnoreCase":onetaskgraph_linear::LIVE_ARTIFACT_TITLE_PREFIX}}}),
)
};
let listing = read().await.unwrap();
let owned = |row: &Value| {
row["title"].as_str().is_some_and(|title| {
is_this_runs(
Run::current(),
onetaskgraph_linear::LIVE_ARTIFACT_TITLE_PREFIX,
title,
)
})
};
for document in listing["documents"]["nodes"]
.as_array()
.unwrap()
.iter()
.filter(|row| owned(row))
{
document_lifecycle::send(
&replay.endpoint,
"synthetic",
onetaskgraph_linear::graphql::DOCUMENT_DELETE,
json!({"id":document["id"]}),
)
.await
.unwrap();
}
let after = read().await.unwrap();
after["documents"]["nodes"]
.as_array()
.unwrap()
.iter()
.filter(|row| owned(row))
.count()
}
#[tokio::test]
async fn document_modes_replay_the_live_plugin_paths_over_http() {
let replay = replay();
let scope = Scope::ci("ENG").unwrap();
let local = Scope::local("ENG", "1-2-900").unwrap();
assert!(
replay_prepare(&replay, local, Mode::Ordinary)
.await
.err()
.unwrap()
.contains("initialize")
);
assert_eq!(replay.journal.count(), 0);
let created = document_lifecycle::Created::default();
let standing = document_lifecycle::prepare_owned(
&replay.endpoint,
"synthetic",
"ENG",
"team",
local,
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&created,
)
.await
.unwrap();
assert_eq!(
replay.journal.count(),
3,
"one owned document per missing plugin slot"
);
let again = replay_prepare(&replay, local, Mode::Initialize)
.await
.unwrap();
assert_eq!(standing.ids, again.ids);
assert_eq!(replay.journal.count(), 3);
replay_drive(&replay, standing, Mode::Ordinary, now_micros())
.await
.unwrap();
assert_eq!(replay.journal.count(), 3);
document_lifecycle::retire(&replay.endpoint, "synthetic", local, &created)
.await
.unwrap();
replay.journal.finish().unwrap();
replay.journal.begin("ordinary");
let stamp = now_micros();
let rebuilt = std::sync::Mutex::new(None);
replay_ordinary_ci_pass(&replay, stamp, &rebuilt).await;
let rebuilt = rebuilt.into_inner().unwrap().unwrap();
assert_eq!(rebuilt.len(), 4, "a parent and three document slots");
assert_eq!(
replay
.journal
.count_in(document_lifecycle::journal::COLD_REBUILD),
3,
"exactly the three missing documents, filed as a cold rebuild"
);
assert_eq!(
replay.journal.count(),
3,
"the reuse assertions create none"
);
let warm = std::sync::Mutex::new(None);
replay_ordinary_ci_pass(&replay, stamp + 1, &warm).await;
assert_eq!(warm.into_inner().unwrap().unwrap(), rebuilt);
assert_eq!(
replay.journal.count(),
3,
"a warm ordinary pass creates none"
);
replay.journal.finish().unwrap();
replay.journal.begin("fresh");
let standing = replay_prepare(&replay, scope, Mode::Fresh).await.unwrap();
replay_drive(&replay, standing, Mode::Fresh, stamp + 2)
.await
.unwrap();
assert_eq!(
replay.journal.count(),
3,
"two document writes and the follow-up document write"
);
replay.journal.finish().unwrap();
let fresh_ids: Vec<_> = replay
.state
.lock()
.unwrap()
.documents
.iter()
.filter(|(_, row)| {
row["title"].as_str().is_some_and(|t| {
is_this_runs(
Run::current(),
onetaskgraph_linear::LIVE_ARTIFACT_TITLE_PREFIX,
t,
)
})
})
.map(|(id, _)| id.clone())
.collect();
assert_eq!(fresh_ids.len(), 1);
for id in fresh_ids {
document_lifecycle::send(
&replay.endpoint,
"synthetic",
onetaskgraph_linear::graphql::DOCUMENT_DELETE,
json!({"id":id}),
)
.await
.unwrap();
}
assert_eq!(
replay.journal.count(),
3,
"deletion never removes successful creations from the journal"
);
replay.journal.begin("failure");
replay.state.lock().unwrap().corrupt_document_create_ack = true;
let standing = replay_prepare(&replay, scope, Mode::Fresh).await.unwrap();
let error = replay_drive(&replay, standing, Mode::Fresh, stamp + 3)
.await
.unwrap_err();
assert!(error.contains("visible body"), "{error}");
assert_eq!(
replay.journal.count(),
2,
"both writes before the assertion failure remain counted"
);
let residue = replay_cleanup_documents(&replay).await;
assert_eq!(
residue, 0,
"failed fresh documents retire while standing CI slots survive"
);
replay.journal.residue(residue);
replay.journal.finish().unwrap();
replay.state.lock().unwrap().corrupt_document_create_ack = false;
let standing = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
replay_drive(&replay, standing, Mode::Ordinary, stamp + 4)
.await
.unwrap();
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn document_lifecycle_replay_refuses_unsafe_ownership_and_recovers_partial_initialization() {
let replay = replay();
let scope = Scope::ci("ENG").unwrap();
replay.state.lock().unwrap().uncertain = true;
let error = replay_prepare(&replay, scope, Mode::Initialize)
.await
.err()
.unwrap();
assert!(error.contains("discover"), "{error}");
assert_eq!(
replay.journal.count(),
1,
"uncertain acknowledgement still records the stored create"
);
let recovered = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
assert_eq!(
replay.journal.count(),
3,
"discovery reuses the uncertain first create and fills only missing slots"
);
assert_eq!(recovered.ids.len(), 4);
let again = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
assert_eq!(again.ids, recovered.ids);
assert_eq!(
replay.journal.count(),
3,
"a warm pass creates no duplicate"
);
replay_drive(&replay, recovered, Mode::Ordinary, now_micros())
.await
.unwrap();
for defect in ["wrong-team", "unverifiable-team", "ambiguous"] {
let before = {
let mut state = replay.state.lock().unwrap();
state.wrong_team = defect == "wrong-team";
state.unverifiable_team = defect == "unverifiable-team";
state.ambiguous = defect == "ambiguous";
state.requests.len()
};
let error = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.err()
.unwrap();
assert!(
error.contains("ownership") || error.contains("membership"),
"{defect}: {error}"
);
let state = replay.state.lock().unwrap();
assert!(
state.requests[before..]
.iter()
.all(|r| !r["query"].as_str().unwrap().contains("mutation")),
"{defect} sent a write"
);
}
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
fn replay_mutations(
replay: &document_replay::Replay,
from: usize,
) -> BTreeMap<&'static str, usize> {
let state = replay.state.lock().unwrap();
let mut counted = BTreeMap::new();
for request in &state.requests[from..] {
let query = request["query"].as_str().unwrap();
for root in [
"projectCreate",
"documentCreate",
"projectDelete",
"documentDelete",
] {
if query.contains(&format!("{root}(")) {
*counted.entry(root).or_default() += 1;
}
}
}
counted
}
#[tokio::test]
async fn a_panic_after_each_local_slot_create_retires_what_the_invocation_created() {
let replay = replay();
let ci = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Initialize)
.await
.unwrap();
for after in 1..=4 {
let stamp = format!("1-2-{after}");
let scope = Scope::local("ENG", &stamp).unwrap();
let before = replay.state.lock().unwrap().requests.len();
let creates_before = replay.journal.count();
let created = document_lifecycle::Created::panicking_after(after);
let outcome = document_lifecycle::unwinding(document_lifecycle::prepare_owned(
&replay.endpoint,
"synthetic",
"ENG",
"team",
scope,
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&created,
))
.await;
let panic = outcome.err().expect("the injected failure panics the lane");
assert_eq!(
panic.downcast_ref::<String>().map(String::as_str),
Some(format!("injected failure after standing create {after}").as_str())
);
let sent = replay_mutations(&replay, before);
let projects = 1;
let documents = after - projects;
assert_eq!(
sent.get("projectCreate").copied().unwrap_or(0),
projects,
"after {after}"
);
assert_eq!(
sent.get("documentCreate").copied().unwrap_or(0),
documents,
"after {after}"
);
assert_eq!(
sent.get("projectDelete").copied().unwrap_or(0),
projects,
"after {after}: the parent created is the parent retired"
);
assert_eq!(
sent.get("documentDelete").copied().unwrap_or(0),
documents,
"after {after}: every document created is retired"
);
assert_eq!(
replay.journal.count() - creates_before,
documents,
"after {after}: retired documents stay counted as created"
);
let state = replay.state.lock().unwrap();
assert!(
state
.documents
.values()
.chain(state.projects.values())
.all(|row| !row["title"]
.as_str()
.or(row["name"].as_str())
.unwrap_or("")
.contains(&stamp)),
"after {after}: nothing of the invocation's set survives"
);
assert_eq!(created.len(), 0);
}
let survived = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Ordinary)
.await
.unwrap();
assert_eq!(survived.ids, ci.ids, "the standing CI set is untouched");
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn a_panic_straight_after_local_preparation_is_retired_by_the_plugin_invocation_guard() {
let replay = replay();
let ci = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Initialize)
.await
.unwrap();
let unrelated_document = document_lifecycle::send(
&replay.endpoint,
"synthetic",
"mutation($input:DocumentCreateInput!){documentCreate(input:$input){success document{id}}}",
json!({"input":{"title":"Unrelated synthetic document","content":"synthetic","teamId":"team"}}),
)
.await
.unwrap()["documentCreate"]["document"]["id"]
.as_str()
.unwrap()
.to_owned();
let unrelated_project = document_lifecycle::send(
&replay.endpoint,
"synthetic",
"mutation($input:ProjectCreateInput!){projectCreate(input:$input){success project{id}}}",
json!({"input":{"name":"Unrelated synthetic project","teamIds":["team"]}}),
)
.await
.unwrap()["projectCreate"]["project"]["id"]
.as_str()
.unwrap()
.to_owned();
let stamp = "1-2-9";
let scope = Scope::local("ENG", stamp).unwrap();
let before = replay.state.lock().unwrap().requests.len();
let creates_before = replay.journal.count();
let created = document_lifecycle::Created::default();
let prepared = std::sync::Mutex::new(None);
let outcome = document_lifecycle::unwinding(guard_plugin_invocation(
&replay.endpoint,
"synthetic",
"ENG",
"team",
scope,
Mode::Initialize,
&created,
|standing| {
*prepared.lock().unwrap() = Some(standing.ids.clone());
async {
panic!("injected failure straight after local preparation");
}
},
))
.await;
let panic = outcome.expect_err("the injected failure still fails the lane");
assert_eq!(
panic.downcast_ref::<&str>().copied(),
Some("injected failure straight after local preparation")
);
let prepared = prepared
.into_inner()
.unwrap()
.expect("preparation succeeded first");
assert_eq!(
prepared.len(),
4,
"the parent and all three local documents were created"
);
let sent = replay_mutations(&replay, before);
assert_eq!(sent.get("projectCreate"), Some(&1));
assert_eq!(sent.get("documentCreate"), Some(&3));
assert_eq!(sent.get("projectDelete"), Some(&1), "the parent is retired");
assert_eq!(
sent.get("documentDelete"),
Some(&3),
"every document is retired"
);
assert_eq!(
replay.journal.count() - creates_before,
3,
"retired creations stay journalled"
);
{
let state = replay.state.lock().unwrap();
for id in prepared.values() {
assert!(
!state.documents.contains_key(id) && !state.projects.contains_key(id),
"{id} survived"
);
}
assert!(state.documents.contains_key(&unrelated_document));
assert!(state.projects.contains_key(&unrelated_project));
}
assert_eq!(created.len(), 0);
let survived = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Ordinary)
.await
.unwrap();
assert_eq!(survived.ids, ci.ids, "the standing CI set is untouched");
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn an_uncertain_local_slot_create_is_discovered_and_retired_before_the_lane_fails() {
let replay = replay();
replay.state.lock().unwrap().uncertain = true;
let created = document_lifecycle::Created::default();
let error = document_lifecycle::prepare_owned(
&replay.endpoint,
"synthetic",
"ENG",
"team",
Scope::local("ENG", "1-2-3").unwrap(),
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&created,
)
.await
.err()
.unwrap();
assert!(error.contains("discover"), "{error}");
let sent = replay_mutations(&replay, 0);
assert_eq!(sent.get("projectCreate"), Some(&1));
assert_eq!(sent.get("documentCreate"), Some(&1));
assert_eq!(sent.get("projectDelete"), Some(&1));
assert_eq!(sent.get("documentDelete"), Some(&1));
assert_eq!(
replay.journal.count(),
1,
"the retired create stays counted"
);
{
let state = replay.state.lock().unwrap();
assert!(state.documents.is_empty() && state.projects.is_empty());
}
replay.state.lock().unwrap().uncertain = true;
let before = replay.state.lock().unwrap().requests.len();
document_lifecycle::prepare_owned(
&replay.endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&document_lifecycle::Created::default(),
)
.await
.err()
.unwrap();
let sent = replay_mutations(&replay, before);
assert_eq!(
sent.get("projectDelete"),
None,
"standing CI slots are never retired"
);
assert_eq!(
sent.get("documentDelete"),
None,
"standing CI slots are never retired"
);
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn local_document_sets_cannot_resolve_ci_or_another_invocation() {
let replay = replay();
let ci = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Initialize)
.await
.unwrap();
let first = Scope::local("ENG", "1-2-3").unwrap();
let second = Scope::local("ENG", "1-4-5").unwrap();
for scope in [first, second] {
assert!(
replay_prepare(&replay, scope, Mode::Ordinary)
.await
.err()
.unwrap()
.contains("initialize")
);
}
let local = replay_prepare(&replay, first, Mode::Initialize)
.await
.unwrap();
let other = replay_prepare(&replay, second, Mode::Initialize)
.await
.unwrap();
for key in ["filed", "loose", "follow-up", "parent"] {
assert_ne!(ci.ids[key], local.ids[key]);
assert_ne!(local.ids[key], other.ids[key]);
}
assert_eq!(
replay.journal.count(),
9,
"each independently owned set has three real document creates"
);
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn cancelled_document_initialization_discovers_its_stored_create_before_recovery() {
use std::{sync::atomic::Ordering, time::Duration};
let replay = replay();
replay.state.lock().unwrap().pause_next_create = true;
let endpoint = replay.endpoint.clone();
let initializing = tokio::spawn(async move {
document_lifecycle::prepare(
&endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
)
.await
});
tokio::time::timeout(Duration::from_secs(5), async {
while !replay.paused.load(Ordering::Acquire) {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("create reached HTTP transport before cancellation");
initializing.abort();
assert!(matches!(initializing.await, Err(error) if error.is_cancelled()));
replay.release.store(true, Ordering::Release);
tokio::time::timeout(Duration::from_secs(5), async {
while replay.journal.count() != 1 {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("forwarding observer records the upstream acknowledgement after cancellation");
assert_eq!(
replay.journal.count(),
1,
"a committed create remains counted after the caller is cancelled"
);
let scope = Scope::ci("ENG").unwrap();
let restored = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
assert_eq!(
replay.journal.count(),
3,
"recovery discovers the cancelled create and creates only missing identities"
);
replay_drive(&replay, restored, Mode::Ordinary, now_micros())
.await
.unwrap();
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn document_writer_child() {
use std::time::Duration;
let Ok(role) = env::var("ONETASKGRAPH_LINEAR_WRITER_CHILD") else {
return;
};
let root = std::path::PathBuf::from(env::var_os("ONETASKGRAPH_LINEAR_WRITER_ROOT").unwrap());
let session = Session::open(
SESSION_NAME,
Credential::new("synthetic").unwrap(),
Exclusivity::OneAtATime,
);
if role == "decline" {
assert!(
session.is_err(),
"a concurrent writer unexpectedly acquired the Linear seat"
);
println!("declined before HTTP");
return;
}
let _session = session
.unwrap_or_else(|error| panic!("isolated writer could not acquire its seat: {error}"));
std::fs::write(root.join("held"), b"held").unwrap();
tokio::time::timeout(Duration::from_secs(20), async {
while !root.join("release").exists() {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("parent releases the writer it started");
let endpoint = env::var("ONETASKGRAPH_LINEAR_WRITER_ENDPOINT").unwrap();
let standing = document_lifecycle::prepare(
&endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
Mode::Ordinary,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
)
.await
.unwrap();
let journal = document_lifecycle::journal::Journal::new("plugin", "ordinary");
replay_drive_at(&endpoint, &journal, standing, Mode::Ordinary, now_micros())
.await
.unwrap();
}
#[tokio::test]
async fn document_ci_writers_share_the_real_machine_seat_before_http_updates() {
use std::{
process::{Child, Command, Stdio},
time::Duration,
};
struct HeldChild(Option<Child>);
impl Drop for HeldChild {
fn drop(&mut self) {
if let Some(child) = self.0.as_mut() {
let _ = child.kill();
let _ = child.wait();
}
}
}
let replay = replay();
let standing = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Initialize)
.await
.unwrap();
replay.journal.begin("ordinary");
let root = env::var_os("ONEPIPELINE_NODE_SCRATCH_DIR")
.map(std::path::PathBuf::from)
.unwrap_or_else(env::temp_dir)
.join(format!(
"linear-writers-{}-{}",
std::process::id(),
now_micros()
));
std::fs::create_dir_all(root.join("seats")).unwrap();
let command = |role: &str| {
let mut command = Command::new(env::current_exe().unwrap());
command
.args(["--exact", "document_writer_child", "--nocapture"])
.env("ONETASKGRAPH_LINEAR_WRITER_CHILD", role)
.env("ONETASKGRAPH_LINEAR_WRITER_ROOT", &root)
.env("ONETASKGRAPH_LINEAR_WRITER_ENDPOINT", &replay.endpoint)
.env(
onetaskgraph_live::SEAT_DIRECTORY_VARIABLE,
root.join("seats"),
)
.env(
onetaskgraph_live::artifact::REGISTRY_DIRECTORY_VARIABLE,
root.join("registry"),
)
.env_remove("LINEAR_API_KEY")
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command
};
let mut first = HeldChild(Some(command("hold").spawn().unwrap()));
tokio::time::timeout(Duration::from_secs(10), async {
while !root.join("held").exists() {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("first child holds its isolated Linear seat");
let before = replay.state.lock().unwrap().requests.len();
let declined = command("decline").output().unwrap();
assert!(
declined.status.success(),
"{}",
String::from_utf8_lossy(&declined.stderr)
);
assert!(String::from_utf8_lossy(&declined.stdout).contains("declined before HTTP"));
assert_eq!(
replay.state.lock().unwrap().requests.len(),
before,
"declined writer sends no HTTP operation"
);
std::fs::write(root.join("release"), b"release").unwrap();
let completed = first.0.take().unwrap().wait_with_output().unwrap();
assert!(
completed.status.success(),
"{}",
String::from_utf8_lossy(&completed.stdout)
);
assert_eq!(replay.journal.count(), 0);
let requests = replay.state.lock().unwrap().requests[before..].to_vec();
assert!(
requests
.iter()
.filter(|request| request["query"]
.as_str()
.unwrap()
.contains("documentUpdate("))
.count()
>= 3,
"admitted child runs the actual shared ordinary document paths"
);
let after = replay_prepare(&replay, Scope::ci("ENG").unwrap(), Mode::Ordinary)
.await
.unwrap();
assert_eq!(standing.ids, after.ids);
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
std::fs::remove_dir_all(root).unwrap();
}
struct ReplaySecrets;
impl SecretResolver for ReplaySecrets {
fn get(&self, _: &str) -> Option<SecretString> {
Some(SecretString::from("synthetic"))
}
}
#[tokio::test]
async fn document_discovery_follows_advancing_http_pages_without_writes() {
let replay = replay();
let scope = Scope::ci("ENG").unwrap();
let initialized = replay_prepare(&replay, scope, Mode::Initialize)
.await
.unwrap();
let before = {
let mut state = replay.state.lock().unwrap();
state.discovery_pagination = Some("advance");
state.requests.len()
};
let discovered = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
assert_eq!(discovered.ids, initialized.ids);
let state = replay.state.lock().unwrap();
let requests = &state.requests[before..];
assert!(
requests
.iter()
.all(|request| !request["query"].as_str().unwrap().contains("mutation"))
);
let cursors: Vec<_> = requests
.iter()
.filter(|request| request["query"].as_str().unwrap().contains("documents("))
.map(|request| request["variables"]["after"].clone())
.collect();
assert_eq!(
cursors,
(0..3)
.flat_map(|_| [Value::Null, json!("1"), json!("2")])
.collect::<Vec<_>>(),
"each exact slot discovery reads all three actual stored document pages"
);
assert!(state.schema_errors.is_empty(), "{:?}", state.schema_errors);
}
#[tokio::test]
async fn document_discovery_refuses_broken_or_unbounded_http_pagination_before_writes() {
let replay = replay();
let scope = Scope::ci("ENG").unwrap();
replay_prepare(&replay, scope, Mode::Initialize)
.await
.unwrap();
for (fault, expected, pages) in [
("missing-cursor", "omitted cursor", 1),
("repeated-cursor", "did not advance", 2),
("missing-pagination", "omitted pagination", 1),
("exhausted", "did not terminate", 50),
] {
let before = {
let mut state = replay.state.lock().unwrap();
state.discovery_pagination = Some(fault);
state.requests.len()
};
let error = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.err()
.unwrap();
assert!(error.contains(expected), "{fault}: {error}");
let state = replay.state.lock().unwrap();
let requests = &state.requests[before..];
assert!(
requests
.iter()
.all(|request| !request["query"].as_str().unwrap().contains("mutation")),
"{fault} sent a write"
);
assert_eq!(
requests
.iter()
.filter(|request| request["query"].as_str().unwrap().contains("documents("))
.count(),
pages,
"{fault}"
);
assert!(
state.schema_errors.is_empty(),
"{fault}: {:?}",
state.schema_errors
);
}
assert_eq!(
replay.journal.count(),
3,
"failed ordinary discovery never creates a replacement"
);
}
#[tokio::test]
async fn document_discovery_requires_well_formed_direct_team_before_project_fallback() {
let replay = replay();
let prepare = |mode| {
document_lifecycle::prepare(
&replay.endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
mode,
&[Slot::Filed],
)
};
let initialized = prepare(Mode::Initialize).await.unwrap();
for malformed in [
json!("ENG"),
json!({}),
json!({"key":7}),
json!({"key":"OTHER"}),
] {
let before = {
let mut state = replay.state.lock().unwrap();
state.direct_team_override = Some(malformed.clone());
state.requests.len()
};
let error = prepare(Mode::Ordinary).await.err().unwrap();
assert!(error.contains("membership"), "{malformed}: {error}");
let state = replay.state.lock().unwrap();
assert!(
state.requests[before..]
.iter()
.all(|request| !request["query"].as_str().unwrap().contains("mutation")),
"{malformed} sent a write"
);
}
let before = {
let mut state = replay.state.lock().unwrap();
state.direct_team_override = None;
state.omit_direct_team = true;
state.requests.len()
};
assert!(
prepare(Mode::Ordinary)
.await
.err()
.unwrap()
.contains("membership")
);
assert!(
replay.state.lock().unwrap().requests[before..]
.iter()
.all(|request| !request["query"].as_str().unwrap().contains("mutation"))
);
{
let mut state = replay.state.lock().unwrap();
state.omit_direct_team = false;
state.direct_team_override = Some(Value::Null);
}
let recovered = prepare(Mode::Ordinary).await.unwrap();
assert_eq!(
recovered.ids, initialized.ids,
"explicit null uses the verified complete project membership"
);
assert_eq!(replay.journal.count(), 1);
}
#[tokio::test]
async fn ordinary_document_run_metadata_reset_is_read_back_and_recovers_after_stale_write() {
let replay = replay();
let scope = Scope::ci("ENG").unwrap();
let first = replay_prepare(&replay, scope, Mode::Initialize)
.await
.unwrap();
replay.journal.begin("ordinary");
let stamp = now_micros();
replay_drive(&replay, first, Mode::Ordinary, stamp)
.await
.unwrap();
replay.state.lock().unwrap().freeze_document_run_metadata = true;
let second = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
let error = replay_drive(&replay, second, Mode::Ordinary, stamp + 1)
.await
.unwrap_err();
assert!(error.contains("reset caller.run"), "{error}");
replay.state.lock().unwrap().freeze_document_run_metadata = false;
let recovered = replay_prepare(&replay, scope, Mode::Ordinary)
.await
.unwrap();
replay_drive(&replay, recovered, Mode::Ordinary, stamp + 1)
.await
.unwrap();
assert_eq!(
replay.journal.count(),
0,
"recovery updates the same retained documents"
);
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
#[tokio::test]
async fn a_task_write_naming_a_label_linear_holds_late_resolves_it_and_one_never_held_is_refused() {
const BOUND: Bound = Bound {
reads: 4,
interval: std::time::Duration::from_millis(20),
};
let replay = replay();
let stamp = now_micros();
let late = artifact_label(Run::current(), stamp);
let never = artifact_label(Run::current(), stamp + 1);
{
let mut state = replay.state.lock().unwrap();
state.labels.insert(late.clone(), ("label-late".into(), 2));
state
.labels
.insert(never.clone(), ("label-never".into(), u32::MAX));
}
let source = onetaskgraph_linear::Plugin
.build(
&SourceName::new("live").unwrap(),
&json!({"endpoint":replay.endpoint,"team":"ENG","status_mapping":{"todo":{"task":"Todo"}}}),
&ReplaySecrets,
)
.unwrap();
let write = |name: &str, offset: u64| -> ItemWrite<Task> {
let mut task: Task = serde_json::from_value(json!({
"id":"authored:labelled","title":artifact_title(Run::current(), stamp + offset),
"content":"Synthetic labelled task.","status":{"category":"todo","name":"Todo"},
"labels":[],"project":null,"repositories":[],"metadata":{}
}))
.unwrap();
task.labels = vec![label(name)];
ItemWrite {
target: None,
item: task,
depends_on: vec![],
}
};
let creates = |replay: &document_replay::Replay| -> Vec<Value> {
replay
.state
.lock()
.unwrap()
.requests
.iter()
.filter(|request| {
request["query"]
.as_str()
.is_some_and(|q| q.contains("issueCreate("))
})
.map(|request| request["variables"]["input"]["labelIds"].clone())
.collect()
};
write_labelled_task(BOUND, source.as_ref(), &write(&late, 10))
.await
.expect("a label the filter holds within the bound is resolved and written");
let lookups = |name: &str| replay.state.lock().unwrap().label_lookups[&name.to_lowercase()];
assert_eq!(
lookups(&late),
3,
"the write asks again until the label is held, and not once more"
);
assert_eq!(
creates(&replay),
vec![json!(["label-late"])],
"one issue is written, naming the label once it was resolved"
);
let refusal = write_labelled_task(BOUND, source.as_ref(), &write(&never, 11))
.await
.expect_err("a label the filter never holds is refused once the bound is spent");
assert!(
matches!(&refusal, SourceError::Refused { message }
if *message == format!("source live cannot resolve label {never:?}: found 0 matches")),
"the refusal past the bound is the plugin's own: {refusal:?}"
);
assert_eq!(
lookups(&never),
BOUND.reads,
"the bound is every lookup made"
);
assert_eq!(
creates(&replay).len(),
1,
"no issue is written for a label that was never resolved"
);
let state = replay.state.lock().unwrap();
assert!(
state.requests.iter().all(|request| {
let query = request["query"].as_str().unwrap_or_default();
!query.contains("issueLabel") || query == onetaskgraph_linear::graphql::ISSUE_LABEL
}),
"a label is only ever looked up, never created, renamed or removed"
);
assert!(state.schema_errors.is_empty(), "{:?}", state.schema_errors);
}
#[tokio::test]
async fn standing_set_preparation_and_a_queued_pass_leave_a_live_journeys_label_resolvable() {
let replay = replay();
let held = artifact_label(Run::current(), now_micros());
replay
.state
.lock()
.unwrap()
.labels
.insert(held.clone(), ("label-held".into(), 0));
let standing_ids = std::sync::Mutex::new(None);
replay_ordinary_ci_pass(&replay, now_micros(), &standing_ids).await;
let cold = replay.journal.count();
assert!(cold > 0, "the first pass prepared the standing set cold");
replay_ordinary_ci_pass(&replay, now_micros(), &standing_ids).await;
assert_eq!(
replay.journal.count(),
cold,
"the queued pass reused the standing set"
);
assert!(
replay
.state
.lock()
.unwrap()
.requests
.iter()
.all(|request| !request["query"]
.as_str()
.unwrap_or_default()
.contains("issueLabel")),
"preparation and the passes over the standing set touched no label"
);
let source = onetaskgraph_linear::Plugin
.build(
&SourceName::new("live").unwrap(),
&json!({"endpoint":replay.endpoint,"team":"ENG","status_mapping":{"todo":{"task":"Todo"}}}),
&ReplaySecrets,
)
.unwrap();
let mut task: Task = serde_json::from_value(json!({
"id":"authored:labelled","title":artifact_title(Run::current(), now_micros()),
"content":"Synthetic labelled task.","status":{"category":"todo","name":"Todo"},
"labels":[],"project":null,"repositories":[],"metadata":{}
}))
.unwrap();
task.labels = vec![label(&held)];
write_labelled_task(
Bound {
reads: 1,
interval: std::time::Duration::ZERO,
},
source.as_ref(),
&ItemWrite {
target: None,
item: task,
depends_on: vec![],
},
)
.await
.expect("the live journey's label still resolves on the first lookup after both passes");
assert!(replay.state.lock().unwrap().schema_errors.is_empty());
}
mod pool {
use super::*;
use document_lifecycle::lease::{self, Held, STALE_AFTER};
use document_lifecycle::namespace::Namespace;
use document_lifecycle::observer;
use onetaskgraph_linear::live_artifact::{POOL_SIZE, classify_lease};
use std::collections::BTreeSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
static HOLD_ORDER: AtomicU64 = AtomicU64::new(0);
async fn join_all<F: std::future::Future>(futures: Vec<F>) -> Vec<F::Output> {
let mut futures: Vec<_> = futures.into_iter().map(Box::pin).collect();
let mut outputs: Vec<Option<F::Output>> = futures.iter().map(|_| None).collect();
std::future::poll_fn(|cx| {
let mut pending = false;
for (future, output) in futures.iter_mut().zip(outputs.iter_mut()) {
if output.is_none() {
match future.as_mut().poll(cx) {
std::task::Poll::Ready(value) => *output = Some(value),
std::task::Poll::Pending => pending = true,
}
}
}
if pending {
std::task::Poll::Pending
} else {
std::task::Poll::Ready(())
}
})
.await;
outputs.into_iter().map(Option::unwrap).collect()
}
fn run(process: u32) -> Run {
Run::read(&format!("9-{process}")).unwrap()
}
fn holder(owner: Run) -> String {
onetaskgraph_live::artifact::Stamp::new(owner, now_micros()).to_string()
}
fn first_member(holder: &str) -> u8 {
lease::first_member(holder)
}
#[test]
fn lease_ids_and_first_members_are_the_ones_the_standing_pool_was_filled_under() {
assert_eq!(
lease::lease_id("TES2", LeaseLane::Plugin, 1, 1),
"f064e9d8-01f5-4f77-b19e-d25c82a04462"
);
assert_eq!(
lease::lease_id("TES2", LeaseLane::Assets, 16, 42),
"6cc370b1-021c-4280-aa3f-e18a5568aabe"
);
assert_eq!(first_member("ci-41229550836-1-1793471795575259"), 1);
}
async fn claim(
replay: &document_replay::Replay,
lane: LeaseLane,
holder: &str,
) -> Result<Held, String> {
lease::claim(
&replay.endpoint,
"synthetic",
"ENG",
"team",
lane,
holder,
now_micros,
)
.await
}
fn leases(replay: &document_replay::Replay) -> Vec<(String, String)> {
replay
.state
.lock()
.unwrap()
.issues
.iter()
.filter(|(_, row)| {
row["title"]
.as_str()
.is_some_and(|t| classify_lease(t).is_some())
})
.map(|(id, row)| (id.clone(), row["title"].as_str().unwrap().to_owned()))
.collect()
}
#[tokio::test]
async fn two_claimants_starting_at_one_member_each_hold_a_different_one() {
let replay = replay();
let first = holder(run(1));
let second = (2..)
.map(|process| holder(run(process)))
.find(|candidate| first_member(candidate) == first_member(&first))
.unwrap();
let (a, b) = tokio::join!(
claim(&replay, LeaseLane::Plugin, &first),
claim(&replay, LeaseLane::Plugin, &second)
);
let (a, b) = (a.unwrap(), b.unwrap());
assert_ne!(a.index, b.index, "two live claims share one pool member");
assert_eq!(leases(&replay).len(), 2, "one lease issue per claim");
let creates = replay
.state
.lock()
.unwrap()
.keyed
.iter()
.filter(|(_, request)| request["query"].as_str().unwrap().contains("issueCreate("))
.count();
assert!(
creates >= 3,
"the losing claimant tried the contended id first: {creates}"
);
}
#[tokio::test]
async fn a_released_or_ended_runs_lease_is_reclaimed_and_a_live_one_never_is() {
let replay = replay();
let owner = holder(run(1));
let held = claim(&replay, LeaseLane::Plugin, &owner).await.unwrap();
let mut others = Vec::new();
for process in 2..=u32::from(POOL_SIZE) {
others.push(
claim(&replay, LeaseLane::Plugin, &holder(run(process)))
.await
.unwrap(),
);
}
let refused = claim(&replay, LeaseLane::Plugin, &holder(run(100)))
.await
.unwrap_err();
assert!(refused.contains("still going"), "{refused}");
claim(&replay, LeaseLane::Assets, &holder(run(101)))
.await
.unwrap()
.release(&replay.endpoint, "synthetic")
.await
.unwrap();
held.release(&replay.endpoint, "synthetic").await.unwrap();
let next = claim(&replay, LeaseLane::Plugin, &holder(run(102)))
.await
.unwrap();
assert_eq!(
(next.index, next.generation),
(held.index, held.generation + 1)
);
assert!(replay.state.lock().unwrap().trashed.contains(&held.id));
let ended = &others[0];
let old = |age: std::time::Duration| {
chrono::DateTime::from_timestamp_micros(
i64::try_from(now_micros() - u64::try_from(age.as_micros()).unwrap()).unwrap(),
)
.unwrap()
.to_rfc3339()
};
replay
.state
.lock()
.unwrap()
.issues
.get_mut(&ended.id)
.unwrap()["createdAt"] = json!(old(STALE_AFTER - std::time::Duration::from_secs(60)));
assert!(
claim(&replay, LeaseLane::Plugin, &holder(run(103)))
.await
.is_err()
);
replay
.state
.lock()
.unwrap()
.issues
.get_mut(&ended.id)
.unwrap()["createdAt"] = json!(old(STALE_AFTER + std::time::Duration::from_secs(60)));
let reclaimed = claim(&replay, LeaseLane::Plugin, &holder(run(104)))
.await
.unwrap();
assert_eq!(
(reclaimed.index, reclaimed.generation),
(ended.index, ended.generation + 1)
);
}
struct Session {
key: String,
lease: Held,
standing: BTreeMap<&'static str, String>,
namespace: Arc<Namespace>,
result: Result<(), String>,
held: (u64, u64),
}
async fn ci_session(
replay: &document_replay::Replay,
lane: LeaseLane,
key: &str,
owner: Run,
) -> Session {
let journal = document_lifecycle::journal::Journal::new("plugin", "ordinary");
let namespace = Arc::new(Namespace::new(owner, "ENG"));
let raw = observer::start(&replay.endpoint, key, journal.clone());
let endpoint = observer::guarded(
&replay.endpoint,
key,
journal.clone(),
Arc::clone(&namespace),
);
let stamp = now_micros();
let lease = lease::claim(&raw, key, "ENG", "team", lane, &holder(owner), now_micros)
.await
.unwrap();
let claimed = HOLD_ORDER.fetch_add(1, Ordering::SeqCst);
namespace.leased(lease.index);
let slots: &[Slot] = match lane {
LeaseLane::Plugin => &[Slot::Filed, Slot::Loose, Slot::FollowUp],
LeaseLane::Assets => &[Slot::Assets],
};
let created = document_lifecycle::Created::default();
let result = async {
let standing = document_lifecycle::prepare_owned(
&endpoint, key, "ENG", "team", lease.scope(), Mode::Ordinary, slots, &created,
)
.await?;
let ids = standing.ids.clone();
match lane {
LeaseLane::Plugin => {
replay_drive_as(&endpoint, &journal, standing, Mode::Ordinary, stamp, owner)
.await?;
}
LeaseLane::Assets => {
let id = &ids["assets"];
document_lifecycle::send(
&endpoint,
key,
"mutation($id:String!,$input:DocumentUpdateInput!){documentUpdate(id:$id,input:$input){success document{id}}}",
json!({"id":id,"input":{"content":format!("asset destination of {key}")}}),
)
.await?;
}
}
Ok::<_, String>(ids)
}
.await;
let releasing = HOLD_ORDER.fetch_add(1, Ordering::SeqCst);
lease.release(&raw, key).await.unwrap();
let (standing, result) = match result {
Ok(ids) => (ids, Ok(())),
Err(error) => (BTreeMap::new(), Err(error)),
};
Session {
key: key.to_owned(),
lease,
standing,
namespace,
result,
held: (claimed, releasing),
}
}
fn named(request: &Value) -> BTreeSet<String> {
let mut found = Vec::new();
document_lifecycle::namespace::ids_named(&request["variables"], &mut found);
found.into_iter().collect()
}
fn assert_isolated(replay: &document_replay::Replay, sessions: &[Session]) {
let keyed = replay.state.lock().unwrap().keyed.clone();
for session in sessions {
assert_eq!(session.result, Ok(()), "{} failed", session.key);
assert_eq!(session.namespace.refusals(), Vec::<String>::new());
for other in sessions.iter().filter(|other| other.key != session.key) {
let theirs: BTreeSet<String> = other
.standing
.values()
.cloned()
.chain([other.lease.id.clone()])
.collect();
for (_, request) in keyed.iter().filter(|(key, _)| *key == session.key) {
let overlap: Vec<_> = named(request).intersection(&theirs).cloned().collect();
assert!(
overlap.is_empty(),
"{} named {}'s records {overlap:?} in {}",
session.key,
other.key,
request["query"]
);
}
}
}
for (n, a) in sessions.iter().enumerate() {
for b in &sessions[n + 1..] {
let same = a.lease.lane == b.lease.lane && a.lease.index == b.lease.index;
let overlap = a.held.0 < b.held.1 && b.held.0 < a.held.1;
assert!(
!(same && overlap),
"{} and {} held {:?} member {} at once",
a.key,
b.key,
a.lease.lane,
a.lease.index
);
}
}
}
#[tokio::test]
async fn two_concurrent_sessions_of_each_linear_lane_pass_and_touch_only_their_own_records() {
let replay = replay();
let sessions = {
let (a, b, c, d) = tokio::join!(
ci_session(&replay, LeaseLane::Plugin, "plugin-a", run(1)),
ci_session(&replay, LeaseLane::Plugin, "plugin-b", run(2)),
ci_session(&replay, LeaseLane::Assets, "assets-a", run(3)),
ci_session(&replay, LeaseLane::Assets, "assets-b", run(4)),
);
vec![a, b, c, d]
};
assert_isolated(&replay, &sessions);
{
let state = replay.state.lock().unwrap();
for session in sessions
.iter()
.filter(|s| s.lease.lane == LeaseLane::Assets)
{
assert_eq!(
state.documents[&session.standing["assets"]]["content"],
json!(format!("asset destination of {}", session.key))
);
}
}
assert!(
leases(&replay)
.iter()
.all(|(_, title)| title.ends_with(" released"))
);
}
#[tokio::test]
async fn eight_concurrent_ci_sessions_stay_inside_the_team_document_limit_and_the_pool() {
let replay = replay();
const LIMIT: usize = 1000;
let pool = usize::from(POOL_SIZE) * (3 + 1);
let initial = LIMIT - pool;
{
let mut state = replay.state.lock().unwrap();
state.document_limit = Some(LIMIT);
state.documents_ever = initial;
state.request_limit = Some(2_500);
state.complexity_limit = Some(3_000_000.0);
}
for round in 0..4u32 {
let handles: Vec<_> = (0..8u32)
.flat_map(|n| {
[
(
LeaseLane::Plugin,
format!("plugin-{round}-{n}"),
run(100 * round + n + 1),
),
(
LeaseLane::Assets,
format!("assets-{round}-{n}"),
run(100 * round + n + 50),
),
]
})
.collect();
let sessions = join_all(
handles
.iter()
.map(|(lane, key, owner)| ci_session(&replay, *lane, key, *owner))
.collect(),
)
.await;
assert_isolated(&replay, &sessions);
}
assert_eq!(replay.state.lock().unwrap().rate_limited, 0);
let created = replay.state.lock().unwrap().documents_ever - initial;
assert!(created <= pool, "{created} documents for a pool of {pool}");
let documents = replay.state.lock().unwrap().documents.len();
assert_eq!(
documents, created,
"every created document is a standing member's"
);
}
const UNPARSED_RETAINED: [&str; 4] = [
"onetaskgraph live cleanup retained-v2 ci ENG filed pool-1",
"onetaskgraph live cleanup retained-v1 ci ENG filed pool-x",
"onetaskgraph live cleanup retained-v1 ci ENG unknown pool-1",
"Onetaskgraph Live Cleanup retained-v1 ci ENG filed pool-1",
];
const UNPARSED_RETAINED_PROJECTS: [&str; 3] = [
"onetaskgraph live cleanup retained-v2 ci ENG parent pool-1",
"onetaskgraph live cleanup retained-v1 ci ENG parent pool-x",
"Onetaskgraph Live Cleanup retained-v1 ci ENG parent pool-1",
];
async fn seed_stray_retained(replay: &document_replay::Replay) -> BTreeMap<String, Value> {
document_lifecycle::prepare(
&replay.endpoint,
"synthetic",
"ENG",
"team",
Scope::ci("ENG").unwrap(),
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp, Slot::Assets],
)
.await
.unwrap();
for title in UNPARSED_RETAINED {
document_lifecycle::send(
&replay.endpoint,
"synthetic",
"mutation($input:DocumentCreateInput!){documentCreate(input:$input){success document{id}}}",
json!({"input":{"title":title,"content":"stray","teamId":"team"}}),
)
.await
.unwrap();
}
for name in UNPARSED_RETAINED_PROJECTS {
document_lifecycle::send(
&replay.endpoint,
"synthetic",
"mutation($input:ProjectCreateInput!){projectCreate(input:$input){success project{id}}}",
json!({"input":{"name":name,"teamIds":["team"]}}),
)
.await
.unwrap();
}
let state = replay.state.lock().unwrap();
let strays: BTreeMap<String, Value> = state
.documents
.iter()
.chain(state.projects.iter())
.map(|(id, row)| (id.clone(), row.clone()))
.collect();
assert_eq!(
strays.len(),
4 + 1 + UNPARSED_RETAINED.len() + UNPARSED_RETAINED_PROJECTS.len()
);
strays
}
#[tokio::test]
async fn every_linear_lane_passes_over_standing_identities_no_current_scheme_owns() {
let replay = replay();
let strays = seed_stray_retained(&replay).await;
let local = Scope::local("ENG", "1-2-900").unwrap();
let created = document_lifecycle::Created::default();
document_lifecycle::prepare_owned(
&replay.endpoint,
"synthetic",
"ENG",
"team",
local,
Mode::Initialize,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
&created,
)
.await
.unwrap();
let held = claim(&replay, LeaseLane::Plugin, &holder(run(9)))
.await
.unwrap();
let untouched: BTreeMap<String, Value> = {
let state = replay.state.lock().unwrap();
state
.documents
.iter()
.chain(state.projects.iter())
.chain(state.issues.iter())
.map(|(id, row)| (id.clone(), row.clone()))
.collect()
};
assert!(untouched.contains_key(&held.id));
let from = replay.state.lock().unwrap().keyed.len();
let sessions = {
let (a, b, c, d) = tokio::join!(
ci_session(&replay, LeaseLane::Plugin, "plugin-a", run(1)),
ci_session(&replay, LeaseLane::Plugin, "plugin-b", run(2)),
ci_session(&replay, LeaseLane::Assets, "assets-a", run(3)),
ci_session(&replay, LeaseLane::Assets, "assets-b", run(4)),
);
vec![a, b, c, d]
};
assert_isolated(&replay, &sessions);
assert!(
sessions
.iter()
.all(|s| s.lease.index != held.index || s.lease.lane != LeaseLane::Plugin)
);
{
let state = replay.state.lock().unwrap();
for (id, row) in &untouched {
let now = state
.documents
.get(id)
.or_else(|| state.projects.get(id))
.or_else(|| state.issues.get(id));
assert_eq!(now, Some(row), "{id} was changed or removed by a lane");
}
for (_, request) in &state.keyed[from..] {
let mut named = Vec::new();
document_lifecycle::namespace::ids_named(&request["variables"], &mut named);
assert!(
named.iter().all(|id| !strays.contains_key(id)),
"a lane named a stray in {request}"
);
}
}
replay_prepare(&replay, local, Mode::Ordinary)
.await
.unwrap();
assert_eq!(
document_lifecycle::retire(&replay.endpoint, "synthetic", local, &created)
.await
.unwrap(),
4
);
held.release(&replay.endpoint, "synthetic").await.unwrap();
}
#[tokio::test]
async fn pool_setup_dry_runs_without_writing_then_initializes_once_skipping_a_held_member() {
use document_lifecycle::{PoolSetup, setup_pool};
let replay = replay();
let slots = [Slot::Filed, Slot::Loose, Slot::FollowUp];
let setup = |mode| {
setup_pool(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&slots,
"1-1-1",
mode,
)
};
let mutations = || {
replay
.state
.lock()
.unwrap()
.keyed
.iter()
.filter(|(_, request)| request["query"].as_str().unwrap().contains("mutation"))
.count()
};
let planned = setup(PoolSetup::DryRun).await.unwrap();
assert_eq!(planned.len(), usize::from(POOL_SIZE) * 4);
assert!(
planned.iter().all(|line| line.starts_with("would create ")),
"{planned:?}"
);
assert_eq!(mutations(), 0, "a dry run sent a mutation");
let busy = claim(&replay, LeaseLane::Plugin, &holder(run(7)))
.await
.unwrap();
let done = setup(PoolSetup::Initialize).await.unwrap();
assert!(done.iter().any(|line| line
== &format!(
"pool member {} is leased by a run still going; skipped",
busy.index
)));
let created = replay.state.lock().unwrap().documents_ever;
assert_eq!(created, (usize::from(POOL_SIZE) - 1) * 3);
busy.release(&replay.endpoint, "synthetic").await.unwrap();
setup(PoolSetup::Initialize).await.unwrap();
assert_eq!(
replay.state.lock().unwrap().documents_ever,
usize::from(POOL_SIZE) * 3
);
setup(PoolSetup::Initialize).await.unwrap();
assert_eq!(
replay.state.lock().unwrap().documents_ever,
usize::from(POOL_SIZE) * 3
);
let present = setup(PoolSetup::DryRun).await.unwrap();
assert!(
present.iter().all(|line| line.starts_with("present ")),
"{present:?}"
);
}
#[tokio::test]
async fn a_claim_whose_create_acknowledgement_was_lost_holds_the_member_it_created() {
let replay = replay();
replay.state.lock().unwrap().lose_next_issue_create_ack = true;
let held = claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
{
let state = replay.state.lock().unwrap();
let creates = state
.keyed
.iter()
.filter(|(_, request)| request["query"].as_str().unwrap().contains("issueCreate("))
.count();
assert_eq!(creates, 1);
assert!(state.issues.contains_key(&held.id));
}
let next = claim(&replay, LeaseLane::Plugin, &holder(run(2)))
.await
.unwrap();
assert_ne!(next.index, held.index);
}
#[tokio::test]
async fn a_lane_that_panics_releases_its_lease_before_the_panic_leaves() {
let replay = replay();
let held = claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
let released = |id: &str| {
replay.state.lock().unwrap().issues[id]["title"]
.as_str()
.unwrap()
.ends_with(" released")
};
let panicked = document_lifecycle::unwinding(document_lifecycle::lease::holding(
Some(&held),
&replay.endpoint,
"synthetic",
async { panic!("injected journey failure") },
))
.await;
assert!(panicked.is_err(), "the panic was swallowed");
assert!(released(&held.id), "a panicking plugin lane kept its lease");
let assets = claim(&replay, LeaseLane::Assets, &holder(run(2)))
.await
.unwrap();
let endpoint = replay.endpoint.clone();
let id = assets.id.clone();
tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Runtime::new().unwrap();
let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
document_lifecycle::lease::holding_blocking(
&runtime,
Some(&assets),
&endpoint,
"synthetic",
|| panic!("injected asset journey failure"),
)
}));
assert!(panicked.is_err(), "the panic was swallowed");
})
.await
.unwrap();
assert!(released(&id), "a panicking asset lane kept its lease");
}
#[test]
fn the_pool_setup_reads_exactly_its_two_actions() {
use document_lifecycle::PoolSetup;
assert_eq!(PoolSetup::parse("dry-run"), Ok(PoolSetup::DryRun));
assert_eq!(PoolSetup::parse("initialize"), Ok(PoolSetup::Initialize));
for refused in ["", "fill", "Initialize", "dry_run"] {
assert!(PoolSetup::parse(refused).is_err(), "{refused:?}");
}
}
#[tokio::test]
async fn a_superseded_lease_linear_will_not_delete_leaves_the_claim_held_and_is_taken_later() {
let replay = replay();
let first = claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
first.release(&replay.endpoint, "synthetic").await.unwrap();
replay.state.lock().unwrap().refuse_issue_deletes = true;
let second = lease::claim_member(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&holder(run(2)),
now_micros,
first.index,
)
.await
.unwrap()
.expect("the released member is free");
assert_eq!(second.generation, first.generation + 1);
assert!(
replay.state.lock().unwrap().issues.contains_key(&first.id),
"the leftover stays"
);
assert!(
lease::claim_member(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&holder(run(3)),
now_micros,
first.index,
)
.await
.unwrap()
.is_none()
);
replay.state.lock().unwrap().refuse_issue_deletes = false;
second.release(&replay.endpoint, "synthetic").await.unwrap();
let third = lease::claim_member(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&holder(run(4)),
now_micros,
first.index,
)
.await
.unwrap()
.unwrap();
assert_eq!(third.generation, second.generation + 1);
let state = replay.state.lock().unwrap();
assert!(!state.issues.contains_key(&first.id) && !state.issues.contains_key(&second.id));
}
#[tokio::test]
async fn a_release_linear_refuses_never_hides_the_lanes_own_outcome() {
let replay = replay();
let held = claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
replay.state.lock().unwrap().refuse_issue_updates = true;
let answered =
document_lifecycle::lease::holding(Some(&held), &replay.endpoint, "synthetic", async {
7
})
.await;
assert_eq!(answered, 7);
let panicked = document_lifecycle::unwinding(document_lifecycle::lease::holding(
Some(&held),
&replay.endpoint,
"synthetic",
async { panic!("the journey's own failure") },
))
.await
.unwrap_err();
assert_eq!(
panicked.downcast_ref::<&str>(),
Some(&"the journey's own failure")
);
let title = replay.state.lock().unwrap().issues[&held.id]["title"]
.as_str()
.unwrap()
.to_owned();
assert!(!title.ends_with(" released"), "{title}");
}
#[tokio::test]
async fn a_lease_listing_that_does_not_read_soundly_refuses_the_claim_creating_nothing() {
for (fault, said) in [
("no-page-info", "the lease listing answered no pagination"),
(
"repeated-cursor",
"the lease listing cursor did not advance",
),
("unreadable-created-at", "unreadable createdAt"),
("untitled", "a lease issue answered no title"),
] {
let replay = replay();
claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
let before = replay.state.lock().unwrap().issues.len();
replay.state.lock().unwrap().lease_listing_fault = Some(fault);
let refused = claim(&replay, LeaseLane::Plugin, &holder(run(2)))
.await
.unwrap_err();
assert!(refused.contains(said), "{fault}: {refused}");
assert_eq!(
replay.state.lock().unwrap().issues.len(),
before,
"{fault}: it created"
);
}
}
#[tokio::test]
async fn an_issue_titled_as_a_lease_under_another_id_confers_no_lease() {
let replay = replay();
let stamp = holder(run(9));
let forged = onetaskgraph_linear::live_artifact::lease_title(
onetaskgraph_linear::live_artifact::Lease::new(
Scope::pooled("ENG", 1).unwrap(),
LeaseLane::Plugin,
std::num::NonZeroU32::MIN,
onetaskgraph_linear::live_artifact::LeaseHolder::Held(
onetaskgraph_linear::live_artifact::HolderStamp::new(&stamp).unwrap(),
),
)
.unwrap(),
);
replay.state.lock().unwrap().issues.insert(
"forged".into(),
json!({"id":"forged","title":forged,"createdAt":chrono::Utc::now().to_rfc3339()}),
);
let held = lease::claim_member(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&holder(run(1)),
now_micros,
1,
)
.await
.unwrap()
.expect("a forged lease holds nothing");
assert_eq!(held.generation, 1);
assert!(replay.state.lock().unwrap().issues.contains_key("forged"));
}
#[tokio::test]
async fn the_pool_variable_turns_the_lanes_entry_point_into_the_setup_and_skips_the_journey() {
let replay = replay();
let seats = std::env::temp_dir().join(format!("linear-pool-entry-{}", std::process::id()));
std::fs::create_dir_all(&seats).unwrap();
let output = tokio::task::spawn_blocking({
let endpoint = replay.endpoint.clone();
let seats = seats.clone();
move || {
std::process::Command::new(std::env::current_exe().unwrap())
.args([
"real_linear_applies_every_declared_capability_and_leaves_no_residue",
"--exact",
"--nocapture",
])
.env(document_lifecycle::POOL_VARIABLE, "dry-run")
.env("LINEAR_LIVE_TEST_GRAPHQL", endpoint)
.env("LINEAR_API_KEY", "synthetic")
.env("LINEAR_WRITE_TEAM", "ENG")
.env(onetaskgraph_live::SEAT_DIRECTORY_VARIABLE, &seats)
.env_remove("GITHUB_ACTIONS")
.env_remove(onetaskgraph_live::REQUIRED_VARIABLE)
.output()
.unwrap()
}
})
.await
.unwrap();
let _ = std::fs::remove_dir_all(&seats);
let said = String::from_utf8_lossy(&output.stdout);
assert!(
output.status.success(),
"{said}{}",
String::from_utf8_lossy(&output.stderr)
);
let planned = said
.lines()
.filter(|line| line.contains("would create "))
.count();
assert_eq!(planned, usize::from(POOL_SIZE) * 4, "{said}");
let state = replay.state.lock().unwrap();
let sent: Vec<_> = state
.keyed
.iter()
.map(|(_, request)| request["query"].as_str().unwrap().to_owned())
.collect();
assert!(
!sent.iter().any(|query| query.contains("mutation")),
"{sent:?}"
);
assert!(
!sent.iter().any(|query| query.contains("projectStatuses")),
"{sent:?}"
);
}
#[tokio::test]
async fn a_claim_proves_a_second_create_under_its_id_is_refused_and_refuses_to_run_if_not() {
let replay = replay();
let held = claim(&replay, LeaseLane::Plugin, &holder(run(1)))
.await
.unwrap();
lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.unwrap();
let title = replay.state.lock().unwrap().issues[&held.id]["title"]
.as_str()
.unwrap()
.to_owned();
assert!(!title.ends_with("duplicate-probe"), "{title}");
replay.state.lock().unwrap().accept_duplicate_ids = true;
let refused = lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.unwrap_err();
assert!(refused.contains("not exclusive"), "{refused}");
replay.state.lock().unwrap().accept_duplicate_ids = false;
replay.state.lock().unwrap().request_limit = Some(0);
let unproven = lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.unwrap_err();
assert!(
unproven.contains("not answered with Linear's duplicate-id refusal"),
"{unproven}"
);
replay.state.lock().unwrap().request_limit = None;
replay
.state
.lock()
.unwrap()
.duplicate_refusal_worded_otherwise = true;
let unproven = lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.unwrap_err();
assert!(
unproven.contains("not answered with Linear's duplicate-id refusal"),
"{unproven}"
);
let replay = self::replay();
let held = claim(&replay, LeaseLane::Plugin, &holder(run(2)))
.await
.unwrap();
let (generation, _) = held.title.rsplit_once(' ').unwrap();
let other = format!("{generation} {}", holder(run(3)));
replay
.state
.lock()
.unwrap()
.issues
.get_mut(&held.id)
.unwrap()["title"] = json!(other);
let refused = lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.unwrap_err();
assert!(
refused.contains("no longer reads as this run's"),
"{refused}"
);
replay.state.lock().unwrap().issues.remove(&held.id);
assert!(
lease::verify_exclusive(&replay.endpoint, "synthetic", "team", &held)
.await
.is_err()
);
}
#[tokio::test]
async fn a_pool_setup_that_fails_preparing_a_member_releases_its_lease_first() {
let replay = replay();
{
let mut state = replay.state.lock().unwrap();
state.document_limit = Some(0);
}
let failed = document_lifecycle::setup_pool(
&replay.endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&[Slot::Filed, Slot::Loose, Slot::FollowUp],
"1-1-1",
document_lifecycle::PoolSetup::Initialize,
)
.await;
assert!(failed.is_err(), "{failed:?}");
let leases = leases(&replay);
assert_eq!(leases.len(), 1, "{leases:?}");
assert!(leases[0].1.ends_with(" released"), "{leases:?}");
}
#[tokio::test]
async fn lease_record_child() {
let Ok(endpoint) = std::env::var("LEASE_RECORD_CHILD_ENDPOINT") else {
return;
};
lease::claim(
&endpoint,
"synthetic",
"ENG",
"team",
LeaseLane::Plugin,
&holder(run(1)),
now_micros,
)
.await
.unwrap();
}
#[tokio::test]
async fn a_claim_records_its_lease_where_the_cancellation_step_releases_it() {
let replay = replay();
let records = std::env::temp_dir().join(format!("lease-records-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&records);
let output = tokio::task::spawn_blocking({
let endpoint = replay.endpoint.clone();
let records = records.clone();
move || {
std::process::Command::new(std::env::current_exe().unwrap())
.args(["pool::lease_record_child", "--exact"])
.env("LEASE_RECORD_CHILD_ENDPOINT", endpoint)
.env(lease::RECORD_VARIABLE, &records)
.output()
.unwrap()
}
})
.await
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let written: Vec<_> = std::fs::read_dir(&records)
.unwrap()
.map(|entry| entry.unwrap().path())
.collect();
assert_eq!(written.len(), 1, "{written:?}");
let record: Value = serde_json::from_slice(&std::fs::read(&written[0]).unwrap()).unwrap();
let id = record["id"].as_str().unwrap();
assert_eq!(
replay.state.lock().unwrap().issues[id]["title"],
record["title"],
"the record does not name the lease as Linear holds it"
);
let _ = std::fs::remove_dir_all(&records);
}
#[tokio::test]
async fn a_claim_whose_record_cannot_be_written_releases_its_lease_and_refuses() {
let replay = replay();
let blocked =
std::env::temp_dir().join(format!("lease-records-blocked-{}", std::process::id()));
std::fs::write(&blocked, "not a directory").unwrap();
let output = tokio::task::spawn_blocking({
let endpoint = replay.endpoint.clone();
let blocked = blocked.join("leases");
move || {
std::process::Command::new(std::env::current_exe().unwrap())
.args(["pool::lease_record_child", "--exact"])
.env("LEASE_RECORD_CHILD_ENDPOINT", endpoint)
.env(lease::RECORD_VARIABLE, &blocked)
.output()
.unwrap()
}
})
.await
.unwrap();
let _ = std::fs::remove_file(&blocked);
let said = String::from_utf8_lossy(&output.stdout);
assert!(
!output.status.success(),
"a lease nothing could release was taken"
);
assert!(said.contains("could not be recorded"), "{said}");
let leases = leases(&replay);
assert_eq!(leases.len(), 1, "{leases:?}");
assert!(leases[0].1.ends_with(" released"), "{leases:?}");
}
#[tokio::test]
async fn the_namespace_guard_refuses_a_request_naming_another_runs_record() {
let replay = replay();
let theirs = ci_session(&replay, LeaseLane::Assets, "theirs", run(1)).await;
assert_eq!(theirs.result, Ok(()));
let journal = document_lifecycle::journal::Journal::new("assets", "ordinary");
let namespace = Arc::new(Namespace::new(run(2), "ENG"));
let endpoint = observer::guarded(&replay.endpoint, "mine", journal, Arc::clone(&namespace));
namespace.leased(theirs.lease.index % POOL_SIZE + 1);
document_lifecycle::send(
&endpoint,
"mine",
document_lifecycle::DOCUMENTS,
json!({"after":null,"filter":{"title":{"containsIgnoreCase":"retained"}}}),
)
.await
.unwrap();
let target = &theirs.standing["assets"];
let refused = document_lifecycle::send(
&endpoint,
"mine",
"mutation($id:String!,$input:DocumentUpdateInput!){documentUpdate(id:$id,input:$input){success document{id}}}",
json!({"id":target,"input":{"content":"overwritten by another run"}}),
)
.await
.unwrap_err();
assert!(refused.contains("rejected"), "{refused}");
assert_eq!(namespace.refusals().len(), 1, "{:?}", namespace.refusals());
assert!(namespace.refusals()[0].contains(target.as_str()));
assert!(
document_lifecycle::send(
&endpoint,
"mine",
document_lifecycle::DOCUMENTS,
json!({"after":null,"filter":{"id":{"in":[target]}}}),
)
.await
.is_err()
);
assert_eq!(namespace.refusals().len(), 2, "{:?}", namespace.refusals());
let title =
onetaskgraph_linear::live_artifact::retained_title(theirs.lease.scope(), Slot::Assets)
.unwrap();
assert!(document_lifecycle::send(
&endpoint,
"mine",
"mutation($input:DocumentCreateInput!){documentCreate(input:$input){success document{id}}}",
json!({"input":{"title":title,"content":"x","teamId":"team"}}),
)
.await
.is_err());
assert_eq!(namespace.refusals().len(), 3);
let state = replay.state.lock().unwrap();
assert!(!state.keyed.iter().any(|(key, request)| key == "mine"
&& request["query"].as_str().unwrap().contains("mutation")));
assert_eq!(
state.documents[target]["content"],
json!("asset destination of theirs")
);
}
}