use serde::Serialize;
use crate::project::Project;
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Contributor {
pub agent_id: String,
pub agent_kind: String,
pub role: String,
pub unit: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct CreditView {
pub finding: String,
pub author_of_record: Vec<String>,
pub contributors: Vec<Contributor>,
pub originating_agents: Vec<Contributor>,
pub statement: String,
}
#[must_use]
pub fn credit(project: &Project, finding_id: &str) -> Option<CreditView> {
let finding = project.findings.iter().find(|f| f.id == finding_id)?;
let mut author_of_record: Vec<String> = Vec::new();
for e in &project.events {
if e.target.id != finding_id || e.actor.r#type != "human" {
continue;
}
let accepting = match e.kind.as_str() {
"finding.asserted" => true,
"finding.reviewed" => {
e.payload.get("status").and_then(|v| v.as_str()) == Some("accepted")
}
_ => false,
};
if !accepting || author_of_record.contains(&e.actor.id) {
continue;
}
let Some(actor) = project.actors.iter().find(|a| a.id == e.actor.id) else {
continue; };
if actor.is_revoked_at(e.timestamp.as_str()) {
continue; }
if matches!(
crate::sign::verify_event_signature(e, &actor.public_key),
Ok(true)
) {
author_of_record.push(e.actor.id.clone());
}
}
let mut contributors: Vec<Contributor> = Vec::new();
for c in &finding.provenance.contributions {
contributors.push(Contributor {
agent_id: c.agent_id.clone(),
agent_kind: c.agent_kind.as_str().to_string(),
role: c.role.as_str().to_string(),
unit: c.unit.clone(),
});
}
if let Some(model) = finding
.provenance
.extraction
.model
.as_deref()
.filter(|m| !m.trim().is_empty())
{
push_unique(
&mut contributors,
Contributor {
agent_id: model.to_string(),
agent_kind: "model".to_string(),
role: "extracted".to_string(),
unit: "whole".to_string(),
},
);
}
for e in &project.events {
if e.target.id == finding_id
&& e.kind.as_str() == "finding.asserted"
&& e.actor.r#type == "agent"
{
push_unique(
&mut contributors,
Contributor {
agent_id: e.actor.id.clone(),
agent_kind: "agent".to_string(),
role: "drafted".to_string(),
unit: "whole".to_string(),
},
);
}
}
let originating_agents: Vec<Contributor> = contributors
.iter()
.filter(|c| c.role == "originated")
.cloned()
.collect();
let statement = render_statement(&author_of_record, &contributors);
Some(CreditView {
finding: finding_id.to_string(),
author_of_record,
contributors,
originating_agents,
statement,
})
}
fn push_unique(v: &mut Vec<Contributor>, c: Contributor) {
if !v.contains(&c) {
v.push(c);
}
}
fn render_statement(authors: &[String], contributors: &[Contributor]) -> String {
let authored = if authors.is_empty() {
"No accountable author yet".to_string()
} else {
format!("Authored by {}", authors.join(", "))
};
if contributors.is_empty() {
return format!("{authored}.");
}
let parts: Vec<String> = contributors
.iter()
.map(|c| format!("{} {} {}", c.agent_id, c.role, c.unit))
.collect();
format!("{authored}. Contributions: {}.", parts.join("; "))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bundle::{AgentKind, Contribution, ContributionRole, ContributionUnitType};
use crate::events::{FindingEventInput, StateEvent, compute_event_id, new_finding_event};
use crate::project::assemble;
use crate::project::reverse_dep_index_tests::synth_finding;
use crate::sign::ActorRecord;
use ed25519_dalek::SigningKey;
fn keypair(seed: u8) -> (SigningKey, String) {
let sk = SigningKey::from_bytes(&[seed; 32]);
let pubkey = hex::encode(sk.verifying_key().to_bytes());
(sk, pubkey)
}
fn actor(id: &str, pubkey: &str) -> ActorRecord {
ActorRecord {
id: id.to_string(),
public_key: pubkey.to_string(),
algorithm: "ed25519".to_string(),
created_at: "2025-01-01T00:00:00Z".to_string(),
tier: None,
orcid: None,
access_clearance: None,
revoked_at: None,
revoked_reason: None,
}
}
fn signed_human_review(finding: &str, human: &str, key: &SigningKey) -> StateEvent {
let mut e = new_finding_event(FindingEventInput {
kind: "finding.reviewed",
finding_id: finding,
actor_id: human,
actor_type: "human",
reason: "accept",
before_hash: "",
after_hash: "",
payload: serde_json::json!({ "status": "accepted" }),
caveats: vec![],
timestamp: Some("2026-01-01T00:00:00Z"),
});
e.id = compute_event_id(&e);
e.signature = Some(crate::sign::sign_event(&e, key).unwrap());
e
}
#[test]
fn credit_names_human_signer_and_discloses_model_originator() {
let mut f = synth_finding(0, vec![]);
f.provenance.contributions = vec![Contribution {
unit: "number-field-towers lemma".into(),
unit_type: ContributionUnitType::Step,
agent_kind: AgentKind::Model,
agent_id: "openai/o5".into(),
model: Some("openai/o5".into()),
model_version: None,
role: ContributionRole::Originated,
basis: String::new(),
}];
let fid = f.id.clone();
let mut project = assemble("credit", vec![f], 0, 0, "test");
let (key, pubkey) = keypair(7);
project.actors.push(actor("reviewer:will-blair", &pubkey));
project
.events
.push(signed_human_review(&fid, "reviewer:will-blair", &key));
let view = credit(&project, &fid).expect("finding exists");
assert_eq!(
view.author_of_record,
vec!["reviewer:will-blair".to_string()]
);
assert!(!view.author_of_record.iter().any(|a| a.contains("openai")));
assert!(
view.originating_agents
.iter()
.any(|c| c.agent_id == "openai/o5" && c.agent_kind == "model"),
"model must appear as an originating agent: {view:?}"
);
assert!(view.statement.contains("Authored by reviewer:will-blair"));
}
#[test]
fn credit_invents_no_author_without_a_signature() {
let f = synth_finding(1, vec![]);
let fid = f.id.clone();
let mut project = assemble("credit", vec![f], 0, 0, "test");
let (key, pubkey) = keypair(9);
project.actors.push(actor("reviewer:x", &pubkey));
let mut unsigned = signed_human_review(&fid, "reviewer:x", &key);
unsigned.signature = None;
project.events.push(unsigned);
let view = credit(&project, &fid).expect("finding exists");
assert!(view.author_of_record.is_empty());
assert!(view.statement.contains("No accountable author yet"));
}
#[test]
fn credit_rejects_a_forged_signature_from_an_unregistered_actor() {
let f = synth_finding(2, vec![]);
let fid = f.id.clone();
let mut project = assemble("credit", vec![f], 0, 0, "test");
let (key, _pubkey) = keypair(11);
project
.events
.push(signed_human_review(&fid, "reviewer:ghost", &key));
let view = credit(&project, &fid).expect("finding exists");
assert!(
view.author_of_record.is_empty(),
"an unregistered signer is not an accountable author: {view:?}"
);
}
#[test]
fn credit_rejects_a_signature_that_does_not_verify() {
let f = synth_finding(3, vec![]);
let fid = f.id.clone();
let mut project = assemble("credit", vec![f], 0, 0, "test");
let (wrong_key, _) = keypair(13);
let (_right_key, registered_pubkey) = keypair(14);
project.actors.push(actor("reviewer:y", ®istered_pubkey));
project
.events
.push(signed_human_review(&fid, "reviewer:y", &wrong_key));
let view = credit(&project, &fid).expect("finding exists");
assert!(
view.author_of_record.is_empty(),
"a signature that does not verify against the registered key confers no authorship"
);
}
}