reviewloop 0.2.1

Reproducible, guardrailed automation for academic review workflows on paperreview.ai
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::{
    config::{Config, PaperConfig},
    db::Db,
    email_account::resolve_submission_email,
    model::{Job, JobStatus, NewJob},
    util::sha256_file,
};
use anyhow::{Context, Result};
use chrono::Utc;
use regex::Regex;
use serde_json::json;
use std::{
    collections::HashSet,
    path::Path,
    process::Command,
    sync::{Mutex, OnceLock},
};
use tracing::warn;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedTag {
    pub backend: String,
    pub paper_id: Option<String>,
}

pub fn parse_review_tag(tag: &str) -> Option<ParsedTag> {
    // Canonical: review-<backend>/<paper-id>/<anything>
    // Shorthand: review-<backend>/<anything>
    if !tag.starts_with("review-") {
        return None;
    }

    let body = tag.trim_start_matches("review-");
    let parts: Vec<&str> = body.split('/').collect();
    if parts.len() < 2 {
        return None;
    }

    let backend = parts[0].trim();
    if backend.is_empty() {
        return None;
    }

    let paper_id = if parts.len() >= 3 {
        Some(parts[1].trim().to_string())
    } else {
        None
    };

    Some(ParsedTag {
        backend: backend.to_string(),
        paper_id,
    })
}

pub fn run_git_tag_trigger(config: &Config, db: &Db) -> Result<()> {
    if !config.trigger.git.enabled {
        return Ok(());
    }
    let repo_dir = config.trigger.git.repo_dir.trim();
    let repo_dir = if repo_dir.is_empty() { "." } else { repo_dir };

    let output = Command::new("git")
        .args(["-C", repo_dir, "tag", "--list", "review-*"])
        .output()
        .with_context(|| format!("failed to list git tags in repo_dir={repo_dir}"))?;

    if !output.status.success() {
        return Ok(());
    }

    let tags = String::from_utf8_lossy(&output.stdout);
    for tag in tags.lines().map(str::trim).filter(|v| !v.is_empty()) {
        let commit = resolve_tag_commit(repo_dir, tag).unwrap_or_else(|| "unknown".to_string());
        let processed = process_tag_entry(config, db, tag, &commit)?;
        if processed
            && config.trigger.git.auto_delete_processed_tags
            && let Err(err) = delete_local_tag(repo_dir, tag)
        {
            warn!(tag, error = %err, "failed to auto-delete processed git tag");
        }
    }

    Ok(())
}

// Per-process set of paper IDs for which a missing-PDF warning has already
// been emitted.  We log the warning and write the `pdf_missing` event only
// once per paper per process lifetime (option b from the design notes) to
// avoid spamming the event table every 30-second tick.
//
// Trade-off: if the file reappears and then goes missing again without a
// daemon restart, the second disappearance will be silent.  Acceptable given
// the use-case (reorganised repo); a `daemon stop && daemon start` resets the
// set.
static PDF_MISSING_WARNED: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();

pub fn run_pdf_trigger(config: &Config, db: &Db) -> Result<()> {
    if !config.trigger.pdf.enabled {
        return Ok(());
    }

    for paper in config
        .papers
        .iter()
        .filter(|paper| config.is_paper_watched(&paper.id))
        .take(config.trigger.pdf.max_scan_papers)
    {
        let path = Path::new(&paper.pdf_path);
        if !path.exists() {
            let guard = PDF_MISSING_WARNED.get_or_init(|| Mutex::new(HashSet::new()));
            let mut seen = guard.lock().unwrap_or_else(|e| e.into_inner());
            if seen.insert(paper.id.clone()) {
                tracing::warn!(
                    paper_id = %paper.id,
                    path = %paper.pdf_path,
                    "configured PDF not found; skipping until file appears"
                );
                db.add_event(
                    Some(&config.project_id),
                    None,
                    "pdf_missing",
                    json!({"paper_id": paper.id, "path": paper.pdf_path}),
                )?;
            }
            continue;
        }

        let hash = sha256_file(path)?;
        let (version_source, version_key) = version_identity(None, &hash);
        if let Some(existing) = db.find_duplicate_covering_job(
            &config.project_id,
            &paper.id,
            &paper.backend,
            &hash,
            &version_key,
        )? {
            record_duplicate_skip(DuplicateSkipContext {
                config,
                db,
                paper,
                pdf_hash: &hash,
                version_source: &version_source,
                version_key: &version_key,
                existing: &existing,
                source: "pdf_trigger",
            })?;
            continue;
        }

        let latest_hash =
            db.latest_hash_for_paper(&config.project_id, &paper.id, &paper.backend)?;
        if latest_hash.as_deref() == Some(hash.as_str()) {
            continue;
        }

        let status = if config.trigger.pdf.auto_submit_on_change {
            JobStatus::Queued
        } else {
            JobStatus::PendingApproval
        };

        let (auto_tag, auto_commit) = match maybe_create_auto_tag(config, paper) {
            Ok(v) => v.unwrap_or((None, None)),
            Err(err) => {
                warn!(
                    paper_id = %paper.id,
                    backend = %paper.backend,
                    error = %err,
                    "failed to create auto git tag; continuing without git tag metadata"
                );
                (None, None)
            }
        };

        enqueue_for_paper(
            config,
            db,
            paper,
            status,
            auto_tag,
            auto_commit,
            "pdf_change_trigger",
        )?;
    }

    Ok(())
}

fn select_paper<'a>(config: &'a Config, parsed: &ParsedTag) -> Option<&'a PaperConfig> {
    if let Some(paper_id) = &parsed.paper_id
        && let Some(paper) = config.find_paper(paper_id)
        && paper.backend == parsed.backend
    {
        return Some(paper);
    }

    config.first_paper_for_backend(&parsed.backend)
}

fn resolve_tag_commit(repo_dir: &str, tag: &str) -> Option<String> {
    let output = Command::new("git")
        .args(["-C", repo_dir, "rev-list", "-n", "1", tag])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }

    let commit = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if commit.is_empty() {
        None
    } else {
        Some(commit)
    }
}

fn process_tag_entry(config: &Config, db: &Db, tag: &str, commit: &str) -> Result<bool> {
    let scoped_tag = scoped_tag_name(&config.project_id, tag);
    if db.is_tag_seen(&scoped_tag)? {
        return Ok(false);
    }

    if let Some(paper) = select_paper_for_tag(config, tag) {
        enqueue_for_paper(
            config,
            db,
            paper,
            JobStatus::Queued,
            Some(tag.to_string()),
            Some(commit.to_string()),
            "git_tag_trigger",
        )?;
    }

    db.mark_tag_seen(&scoped_tag, commit)?;
    Ok(true)
}

fn select_paper_for_tag<'a>(config: &'a Config, tag: &str) -> Option<&'a PaperConfig> {
    if let Some(parsed) = parse_review_tag(tag)
        && let Some(paper) = select_paper(config, &parsed)
    {
        return Some(paper);
    }

    config.papers.iter().find(|paper| {
        config
            .paper_tag_trigger(&paper.id)
            .is_some_and(|pattern| matches_tag_pattern(pattern, tag))
    })
}

fn matches_tag_pattern(pattern: &str, tag: &str) -> bool {
    let trimmed = pattern.trim();
    if trimmed.is_empty() {
        return false;
    }

    let mut regex_pattern = String::from("^");
    for part in trimmed.split('*') {
        regex_pattern.push_str(&regex::escape(part));
        regex_pattern.push_str(".*");
    }
    if !trimmed.ends_with('*') {
        regex_pattern.truncate(regex_pattern.len().saturating_sub(2));
    }
    regex_pattern.push('$');

    Regex::new(&regex_pattern)
        .map(|re| re.is_match(tag))
        .unwrap_or(false)
}

fn maybe_create_auto_tag(
    config: &Config,
    paper: &PaperConfig,
) -> Result<Option<(Option<String>, Option<String>)>> {
    if !config.trigger.git.auto_create_tags_on_pdf_change {
        return Ok(None);
    }

    let repo_dir = config.trigger.git.repo_dir.trim();
    let repo_dir = if repo_dir.is_empty() { "." } else { repo_dir };
    let tag = format!(
        "review-{}/{}/auto-{}",
        paper.backend,
        paper.id,
        Utc::now().timestamp_millis()
    );

    let output = Command::new("git")
        .args(["-C", repo_dir, "tag", &tag])
        .output()
        .with_context(|| format!("failed to create auto git tag: {tag}"))?;

    if !output.status.success() {
        anyhow::bail!(
            "auto git tag command failed for {tag}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    let commit = resolve_tag_commit(repo_dir, &tag);
    Ok(Some((Some(tag), commit)))
}

fn delete_local_tag(repo_dir: &str, tag: &str) -> Result<()> {
    let output = Command::new("git")
        .args(["-C", repo_dir, "tag", "-d", tag])
        .output()
        .with_context(|| format!("failed to run git tag -d for tag={tag}"))?;
    if !output.status.success() {
        anyhow::bail!(
            "git tag -d failed for {tag}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
    Ok(())
}

fn enqueue_for_paper(
    config: &Config,
    db: &Db,
    paper: &PaperConfig,
    status: JobStatus,
    git_tag: Option<String>,
    git_commit: Option<String>,
    source: &str,
) -> Result<()> {
    let pdf_hash = sha256_file(Path::new(&paper.pdf_path))?;
    let (version_source, version_key) = version_identity(git_commit.as_deref(), &pdf_hash);

    if let Some(existing) = db.find_duplicate_covering_job(
        &config.project_id,
        &paper.id,
        &paper.backend,
        &pdf_hash,
        &version_key,
    )? {
        record_duplicate_skip(DuplicateSkipContext {
            config,
            db,
            paper,
            pdf_hash: &pdf_hash,
            version_source: &version_source,
            version_key: &version_key,
            existing: &existing,
            source,
        })?;
        return Ok(());
    }

    let job = db.create_job(&NewJob {
        project_id: config.project_id.clone(),
        paper_id: paper.id.clone(),
        backend: paper.backend.clone(),
        pdf_path: paper.pdf_path.clone(),
        pdf_hash,
        status,
        email: provider_email(config, &paper.backend)?,
        venue: provider_venue(config, paper),
        git_tag,
        git_commit,
        next_poll_at: None,
    })?;

    db.add_event(
        None,
        Some(&job.id),
        "job_enqueued",
        json!({
            "source": source,
            "status": job.status.as_str(),
            "paper_id": job.paper_id,
            "backend": job.backend,
        }),
    )?;

    Ok(())
}

fn scoped_tag_name(project_id: &str, tag: &str) -> String {
    format!("{project_id}::{tag}")
}

fn version_identity(git_commit: Option<&str>, pdf_hash: &str) -> (String, String) {
    if let Some(commit) = git_commit.map(str::trim).filter(|value| !value.is_empty()) {
        ("git_commit".to_string(), commit.to_string())
    } else {
        ("pdf_hash".to_string(), pdf_hash.to_string())
    }
}

struct DuplicateSkipContext<'a> {
    config: &'a Config,
    db: &'a Db,
    paper: &'a PaperConfig,
    pdf_hash: &'a str,
    version_source: &'a str,
    version_key: &'a str,
    existing: &'a Job,
    source: &'a str,
}

fn record_duplicate_skip(ctx: DuplicateSkipContext<'_>) -> Result<()> {
    warn!(
        project_id = %ctx.config.project_id,
        paper_id = %ctx.paper.id,
        backend = %ctx.paper.backend,
        source = %ctx.source,
        existing_job_id = %ctx.existing.id,
        existing_status = %ctx.existing.status.as_str(),
        "skipped duplicate trigger enqueue"
    );
    ctx.db.add_event(
        Some(&ctx.config.project_id),
        None,
        "duplicate_skipped",
        json!({
            "project_id": ctx.config.project_id,
            "paper_id": ctx.paper.id,
            "backend": ctx.paper.backend,
            "pdf_hash": ctx.pdf_hash,
            "version_no": ctx.existing.version_no,
            "round_no": ctx.existing.round_no,
            "version_source": ctx.version_source,
            "version_key": ctx.version_key,
            "existing_job_id": ctx.existing.id,
            "existing_job_status": ctx.existing.status.as_str(),
            "source": ctx.source
        }),
    )?;
    Ok(())
}

fn provider_email(config: &Config, backend: &str) -> Result<String> {
    resolve_submission_email(config, backend, None)
}

fn provider_venue(config: &Config, paper: &PaperConfig) -> Option<String> {
    config.venue_for(paper)
}

#[cfg(test)]
mod tests {
    use super::{matches_tag_pattern, parse_review_tag, process_tag_entry, run_pdf_trigger};
    use crate::{
        config::{Config, PaperConfig},
        db::Db,
        model::JobStatus,
    };
    use anyhow::Context;
    use std::{fs, path::Path};

    fn setup_simulation_context() -> anyhow::Result<(tempfile::TempDir, Config, Db)> {
        let tmp = tempfile::tempdir()?;
        let state_dir = tmp.path().join("state");
        fs::create_dir_all(&state_dir)?;

        let pdf_path = tmp.path().join("main.pdf");
        fs::write(&pdf_path, b"%PDF-1.4\n%%EOF\n")?;

        let mut config = Config {
            project_id: "project-main".to_string(),
            ..Config::default()
        };
        config.core.state_dir = state_dir.to_string_lossy().to_string();
        config.trigger.git.enabled = false;
        config.trigger.pdf.enabled = false;
        config.providers.stanford.email = "test@example.edu".to_string();
        config.papers = vec![PaperConfig {
            id: "main".to_string(),
            pdf_path: pdf_path.to_string_lossy().to_string(),
            backend: "stanford".to_string(),
            venue: None,
        }];

        let db = Db::new(Path::new(&config.core.state_dir));
        db.ensure_schema()?;
        Ok((tmp, config, db))
    }

    #[test]
    fn parses_full_tag_format() {
        let parsed = parse_review_tag("review-stanford/main/v1").unwrap();
        assert_eq!(parsed.backend, "stanford");
        assert_eq!(parsed.paper_id.as_deref(), Some("main"));
    }

    #[test]
    fn parses_shorthand_tag_format() {
        let parsed = parse_review_tag("review-stanford/v2").unwrap();
        assert_eq!(parsed.backend, "stanford");
        assert_eq!(parsed.paper_id, None);
    }

    #[test]
    fn rejects_non_review_tag() {
        assert!(parse_review_tag("v1.2.3").is_none());
    }

    #[test]
    fn simulated_tag_entry_enqueues_once_and_deduplicates() -> anyhow::Result<()> {
        let (_tmp, config, db) = setup_simulation_context()?;
        let tag = "review-stanford/main/sim";

        let processed = process_tag_entry(&config, &db, tag, "deadbeef")?;
        assert!(processed);

        let job = db
            .find_latest_open_job_for_paper(&config.project_id, "main")?
            .context("expected queued job for simulated tag")?;
        assert_eq!(job.status, JobStatus::Queued);
        assert_eq!(job.git_tag.as_deref(), Some(tag));
        assert_eq!(job.git_commit.as_deref(), Some("deadbeef"));
        assert!(db.is_tag_seen(&format!("{}::{}", config.project_id, tag))?);

        let processed = process_tag_entry(&config, &db, tag, "deadbeef")?;
        assert!(!processed);
        let rows = db.list_status_views(&config.project_id, Some("main"))?;
        assert_eq!(
            rows.len(),
            1,
            "simulated duplicate tag should not enqueue twice"
        );

        Ok(())
    }

    #[test]
    fn simulated_custom_tag_trigger_enqueues_target_paper() -> anyhow::Result<()> {
        let (_tmp, mut config, db) = setup_simulation_context()?;
        config.set_paper_tag_trigger("main", Some("custom/main/*".to_string()));

        let processed = process_tag_entry(&config, &db, "custom/main/v3", "beadfeed")?;
        assert!(processed);

        let job = db
            .find_latest_open_job_for_paper(&config.project_id, "main")?
            .context("expected queued job for custom tag trigger")?;
        assert_eq!(job.status, JobStatus::Queued);
        assert_eq!(job.git_tag.as_deref(), Some("custom/main/v3"));
        assert_eq!(job.git_commit.as_deref(), Some("beadfeed"));
        Ok(())
    }

    #[test]
    fn pattern_match_supports_wildcard() {
        assert!(matches_tag_pattern(
            "review-stanford/main/*",
            "review-stanford/main/v1"
        ));
        assert!(matches_tag_pattern("custom-*", "custom-build-123"));
        assert!(!matches_tag_pattern(
            "review-stanford/main/*",
            "review-stanford/other/v1"
        ));
    }

    #[test]
    fn pdf_trigger_skips_unwatched_paper() -> anyhow::Result<()> {
        let (_tmp, mut config, db) = setup_simulation_context()?;
        config.trigger.pdf.enabled = true;
        config.set_paper_watch("main", false);

        run_pdf_trigger(&config, &db)?;
        assert!(
            db.list_status_views(&config.project_id, Some("main"))?
                .is_empty()
        );
        Ok(())
    }

    /// U4 regression: a missing PDF must emit a `pdf_missing` event exactly
    /// once per paper per process lifetime, no matter how many ticks fire.
    #[test]
    fn pdf_trigger_emits_pdf_missing_event_once_for_missing_file() -> anyhow::Result<()> {
        // Use a project/paper ID that is unique to this test to avoid
        // colliding with the process-global PDF_MISSING_WARNED HashSet that
        // other tests might have already populated.
        let tmp = tempfile::tempdir()?;
        let state_dir = tmp.path().join("state");
        fs::create_dir_all(&state_dir)?;

        let mut config = Config {
            project_id: "proj-u4-pdf-missing".to_string(),
            ..Config::default()
        };
        config.core.state_dir = state_dir.to_string_lossy().to_string();
        config.trigger.pdf.enabled = true;
        config.providers.stanford.email = "test@example.edu".to_string();

        // Point to a path that will never exist.
        let missing = tmp.path().join("does-not-exist-u4.pdf");
        config.papers = vec![PaperConfig {
            id: "u4-missing-paper".to_string(),
            pdf_path: missing.to_string_lossy().to_string(),
            backend: "stanford".to_string(),
            venue: None,
        }];

        let db = Db::new(Path::new(&config.core.state_dir));
        db.ensure_schema()?;

        // Simulate three daemon ticks.
        run_pdf_trigger(&config, &db)?;
        run_pdf_trigger(&config, &db)?;
        run_pdf_trigger(&config, &db)?;

        // The `pdf_missing` event must have been recorded.
        let ev = db
            .most_recent_event_of_type(&config.project_id, "pdf_missing")?
            .expect("expected a pdf_missing event after ticks with missing file");
        assert_eq!(
            ev.payload.get("paper_id").and_then(|v| v.as_str()),
            Some("u4-missing-paper"),
        );

        // Only one event must have been written (HashSet dedup).
        let timeline = db.list_timeline_events(&config.project_id, "u4-missing-paper")?;
        let missing_events: Vec<_> = timeline
            .iter()
            .filter(|e| e.event_type == "pdf_missing")
            .collect();
        assert_eq!(
            missing_events.len(),
            1,
            "pdf_missing must be written exactly once per process lifetime per paper; got {}",
            missing_events.len()
        );

        Ok(())
    }
}