rightcommit 1.2.3

Self-hosted, Rust-built orchestration for Claude Code, Codex and Pi Agent. Parallel workflows, full control over every commit, and FinOps that shows exactly what each agent and model costs.
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
use crate::{
    App,
    auth::{ApiResult, bad},
    model::*,
};
use anyhow::{Result, ensure};
use axum::{
    Json,
    extract::{Path, State},
};
use rusqlite::params;
use serde::Deserialize;
use serde_json::{Value, json};
use std::{
    collections::HashSet,
    path::{Path as FsPath, PathBuf},
};
fn runs(app: &App, uid: i64, wid: &str) -> Result<Vec<Run>> {
    app.store.workflow(uid, wid)?;
    let all: Vec<Run> = {
        let db = app.store.db.lock().unwrap();
        let mut stmt = db.prepare("SELECT json FROM runs WHERE user_id=? ORDER BY id")?;
        stmt.query_map([uid], |r| r.get::<_, String>(0))?
            .map(|r| Ok(serde_json::from_str(&r?)?))
            .collect::<Result<_>>()?
    };
    let mut ids: HashSet<String> = all
        .iter()
        .filter(|r| r.workflow.id == wid)
        .map(|r| r.id.clone())
        .collect();
    loop {
        let before = ids.len();
        for r in &all {
            if r.parent.as_ref().is_some_and(|p| ids.contains(&p.run)) {
                ids.insert(r.id.clone());
            }
        }
        if ids.len() == before {
            break;
        }
    }
    ensure!(
        !all.iter().any(
            |r| ids.contains(&r.id) && r.parent.as_ref().is_some_and(|p| !ids.contains(&p.run))
        ),
        "Questo workflow ha esecuzioni richiamate da un altro workflow. Elimina prima il workflow principale per conservare la gerarchia e il workspace."
    );
    Ok(all.into_iter().filter(|r| ids.contains(&r.id)).collect())
}
fn active(app: &App, runs: &[Run]) -> bool {
    let busy = app.busy.lock().unwrap();
    runs.iter().any(|r| {
        busy.contains(&r.id)
            || ["starting", "running", "waiting_external", "waiting_child"]
                .contains(&r.status.as_str())
    })
}
pub async fn preview(
    State(app): State<App>,
    u: User,
    Path(id): Path<String>,
) -> ApiResult<Json<Value>> {
    let _guard = app.lifecycle.read().await;
    let runs = runs(&app, u.id, &id)?;
    let chats = crate::manager::cleanup_records(
        &app,
        u.id,
        &id,
        &runs.iter().map(|r| r.id.clone()).collect::<Vec<_>>(),
    )?;
    let mut artifacts = 0;
    for r in &runs {
        artifacts += app.store.artifacts(u.id, &r.id)?.len()
    }
    Ok(Json(
        json!({"run_ids":runs.iter().map(|r|r.id.clone()).collect::<Vec<_>>(),"runs":runs.len(),"artifacts":artifacts,"active":active(&app,&runs)||chats.iter().any(|c|c.busy),"manager_chats":chats.len(),"schedules":crate::schedules::workflow_count(&app,u.id,&id)?}),
    ))
}
#[derive(Deserialize)]
pub struct Confirmation {
    #[serde(default)]
    acknowledge: bool,
    run_ids: Vec<String>,
}
// Resolve existing ancestors too: a missing sources root or a symlink in a
// parent directory must not turn into permission to delete source material.
fn resolved_location(path: &FsPath) -> Result<PathBuf> {
    let absolute = std::path::absolute(path)?;
    let mut existing = absolute.as_path();
    let mut suffix = Vec::new();
    loop {
        match std::fs::symlink_metadata(existing) {
            Ok(_) => break,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                suffix.push(
                    existing
                        .file_name()
                        .ok_or_else(|| anyhow::anyhow!("Percorso di pulizia non valido"))?
                        .to_os_string(),
                );
                existing = existing
                    .parent()
                    .ok_or_else(|| anyhow::anyhow!("Percorso di pulizia non valido"))?;
            }
            Err(error) => return Err(error.into()),
        }
    }
    let mut resolved = std::fs::canonicalize(existing)?;
    for component in suffix.into_iter().rev() {
        resolved.push(component);
    }
    Ok(resolved)
}

fn protected_sources(app: &App, path: &FsPath) -> Result<bool> {
    let sources = resolved_location(&app.sources)?;
    let candidate = resolved_location(path)?;
    // Protect descendants AND ancestors: removing a parent would remove sources.
    Ok(candidate.starts_with(&sources) || sources.starts_with(&candidate))
}

fn native_files(
    app: &App,
    dir: &FsPath,
    ids: &HashSet<String>,
    out: &mut Vec<PathBuf>,
) -> Result<()> {
    if !dir.exists() || dir.is_symlink() || protected_sources(app, dir)? {
        return Ok(());
    }
    for e in std::fs::read_dir(dir)? {
        let p = e?.path();
        if p.is_symlink() || protected_sources(app, &p)? {
            continue;
        }
        let name = p.file_name().unwrap().to_string_lossy();
        if ids.iter().any(|id| name.contains(id)) {
            out.push(p)
        } else if p.is_dir() {
            native_files(app, &p, ids, out)?
        }
    }
    Ok(())
}
pub(crate) fn perform(app: &App, uid: i64, wid: &str, expected: &[String]) -> Result<Value> {
    let runs = runs(app, uid, wid)?;
    ensure!(
        !active(app, &runs),
        "Metti in pausa le esecuzioni attive e attendi la fine del turno prima di eliminare il workflow."
    );
    let ids: Vec<_> = runs.iter().map(|r| r.id.clone()).collect();
    let mut expected = expected.to_vec();
    expected.sort();
    ensure!(
        ids == expected,
        "Le esecuzioni sono cambiate. Riapri la conferma di eliminazione."
    );
    let chats = crate::manager::cleanup_records(app, uid, wid, &ids)?;
    ensure!(
        !chats.iter().any(|c| c.busy),
        "Attendi la risposta del project manager prima di eliminare il workflow"
    );
    let deleted_chats: Vec<_> = chats.iter().map(|c| c.id.clone()).collect();
    let home = app.data.join("users").join(uid.to_string());
    let mut paths = Vec::new();
    for run in &runs {
        ensure!(
            uuid::Uuid::parse_str(&run.id).is_ok(),
            "ID esecuzione non valido"
        );
        // Only run-owned directories may be moved to trash. Direct /sources
        // bindings belong to the shared catalog, including for child runs.
        let lineage = crate::subworkflows::ancestors(app, uid, run)?;
        let root = lineage.last().unwrap_or(run);
        let direct_primary = root.context.contains_key("direct-primary");
        let p = if direct_primary {
            ensure!(
                uuid::Uuid::parse_str(&root.id).is_ok(),
                "ID esecuzione non valido"
            );
            home.join("workspaces").join(&root.id)
        } else {
            crate::subworkflows::workspace(app, uid, run)?
        };
        if p.exists() {
            if protected_sources(app, &p)? {
                continue;
            }
            ensure!(
                !p.is_symlink() && !home.is_symlink() && !home.join("workspaces").is_symlink(),
                "Workspace non valido"
            );
            ensure!(
                direct_primary
                    || std::fs::canonicalize(&p)? == std::fs::canonicalize(&run.workspace)?,
                "Workspace non valido"
            );
            if !paths.contains(&p) {
                paths.push(p)
            }
        }
    }
    for chat in &chats {
        if chat.workspace.exists() {
            ensure!(
                !chat
                    .workspace
                    .ancestors()
                    .take_while(|p| p.starts_with(&app.data))
                    .any(|p| p.is_symlink()),
                "Spazio manager non valido"
            );
            paths.push(chat.workspace.clone());
        }
    }
    let mut native: HashSet<_> = runs
        .iter()
        .flat_map(|r| r.sessions.values().map(|s| s.native_id.clone()))
        .filter(|id| uuid::Uuid::parse_str(id).is_ok())
        .chain(chats.iter().flat_map(|c| {
            c.sessions
                .iter()
                .filter(|id| uuid::Uuid::parse_str(id).is_ok())
                .cloned()
        }))
        .collect();
    for id in crate::manager::retained_sessions(app, uid, &deleted_chats)? {
        native.remove(&id);
    }
    // Retain any native session still referenced by a different workflow.
    {
        let db = app.store.db.lock().unwrap();
        let mut q = db.prepare("SELECT json FROM runs WHERE user_id=?")?;
        for row in q.query_map(params![uid], |r| r.get::<_, String>(0))? {
            let r: Run = serde_json::from_str(&row?)?;
            if ids.contains(&r.id) {
                continue;
            }
            for s in r.sessions.values() {
                native.remove(&s.native_id);
            }
        }
    }
    for folder in [".codex/sessions", ".claude/projects", ".pi/agent/sessions"] {
        native_files(app, &home.join(folder), &native, &mut paths)?
    }
    // This final guard covers every cleanup candidate, including native-session
    // paths, independently of Source mode, workflow lineage, or stored metadata.
    let mut cleanup_paths = Vec::new();
    for path in paths {
        if !protected_sources(app, &path)? {
            cleanup_paths.push(path);
        }
    }
    let paths = cleanup_paths;
    let trash = app.data.join("system/deletions").join(crate::model::id());
    if !paths.is_empty() {
        ensure!(
            !protected_sources(app, &trash)?,
            "L'area di pulizia si sovrappone a sources: nessun file eliminato"
        );
        std::fs::create_dir_all(&trash)?;
        crate::store::private(&trash, true)?;
    }
    let mut moved: Vec<(PathBuf, PathBuf)> = Vec::new();
    let result = (|| -> Result<()> {
        for (i, p) in paths.iter().enumerate() {
            ensure!(
                !protected_sources(app, p)?,
                "Percorso sources protetto: nessun file eliminato"
            );
            let dest = trash.join(i.to_string());
            std::fs::rename(p, &dest)?;
            moved.push((p.clone(), dest))
        }
        let mut db = app.store.db.lock().unwrap();
        let tx = db.transaction()?;
        for rid in &ids {
            for table in ["artifacts", "artifact_baselines", "artifact_recoveries", "decisions", "events"] {
                tx.execute(
                    &format!("DELETE FROM {table} WHERE user_id=? AND run=?"),
                    params![uid, rid],
                )?;
            }
            tx.execute(
                "DELETE FROM schedule_occurrences WHERE user_id=? AND run_id=?",
                params![uid, rid],
            )?;
            tx.execute(
                "DELETE FROM runs WHERE user_id=? AND id=?",
                params![uid, rid],
            )?;
        }
        for id in &deleted_chats {
            tx.execute(
                "DELETE FROM events WHERE user_id=? AND run=?",
                params![uid, id],
            )?;
            tx.execute(
                "DELETE FROM manager_chats WHERE user_id=? AND id=?",
                params![uid, id],
            )?;
        }
        tx.execute("DELETE FROM schedule_occurrences WHERE user_id=? AND schedule_id IN (SELECT id FROM schedules WHERE user_id=? AND workflow_id=?)", params![uid,uid,wid])?;
        tx.execute("DELETE FROM schedules WHERE user_id=? AND workflow_id=?", params![uid,wid])?;
        ensure!(
            tx.execute(
                "DELETE FROM workflows WHERE user_id=? AND id=?",
                params![uid, wid]
            )? == 1,
            "Workflow non trovato"
        );
        tx.execute(
            "DELETE FROM a2a_configs WHERE user_id=? AND kind='publication' AND id=?",
            params![uid, wid],
        )?;
        tx.execute("DELETE FROM a2a_replies WHERE task IN (SELECT id FROM a2a_tasks WHERE user_id=? AND workflow=?)", params![uid, wid])?;
        tx.execute(
            "DELETE FROM a2a_tasks WHERE user_id=? AND workflow=?",
            params![uid, wid],
        )?;
        tx.commit()?;
        Ok(())
    })();
    if let Err(e) = result {
        for (original, staged) in moved.iter().rev() {
            std::fs::rename(staged, original)?;
        }
        if !paths.is_empty() && !protected_sources(app, &trash)? {
            let _ = std::fs::remove_dir(&trash);
        }
        return Err(e);
    }
    let artifact_cleanup_pending = crate::artifacts::cleanup(app, uid).is_err();
    let cleanup_pending = artifact_cleanup_pending || !paths.is_empty()
        && (|| -> Result<()> {
            ensure!(!protected_sources(app, &trash)?, "Area sources protetta");
            Ok(std::fs::remove_dir_all(&trash)?)
        })()
        .is_err();
    Ok(json!({"ok":true,"deleted_runs":ids.len(),"cleanup_pending":cleanup_pending}))
}
pub async fn delete(
    State(app): State<App>,
    u: User,
    Path(id): Path<String>,
    Json(c): Json<Confirmation>,
) -> ApiResult<Json<Value>> {
    if !c.acknowledge {
        return Err(bad(
            "Conferma l'eliminazione del workflow e delle esecuzioni.",
        ));
    }
    let lifecycle = app.lifecycle.clone();
    let _guard = lifecycle.write().await;
    let mcp = app.mcp.clone();
    let uid = u.id;
    let deleted_runs = c.run_ids.clone();
    let result = tokio::task::spawn_blocking(move || perform(&app, u.id, &id, &c.run_ids))
        .await
        .map_err(|_| bad("Eliminazione interrotta"))??;
    for rid in deleted_runs {
        mcp.release_run(uid, &rid).await;
    }
    Ok(Json(result))
}

#[cfg(test)]
mod tests {
    use super::*;
    #[tokio::test]
    async fn deletes_owned_workflow_cascade_and_rolls_back_failures() {
        let dir = tempfile::tempdir().unwrap();
        let app = crate::tests::app(dir.path());
        let workflow = crate::presets::all().remove(0);
        app.store.save_workflow(1, &workflow).unwrap();
        app.store.save_workflow(2, &workflow).unwrap();
        let mut run = crate::engine::create(&app, 1, workflow.clone(), "test".into(), None)
            .await
            .unwrap();
        let second = crate::engine::create(&app, 1, workflow.clone(), "second".into(), None)
            .await
            .unwrap();
        let other = crate::engine::create(&app, 2, workflow.clone(), "other user".into(), None)
            .await
            .unwrap();
        let sid = crate::model::id();
        run.sessions.insert(
            "author".into(),
            NativeSession {
                provider: String::new(), base_url: String::new(),
                native_id: sid.clone(),
                runtime: "pi".into(),
                turns: 1,
                model: String::new(),
            },
        );
        app.store.save_run(1, &run).unwrap();
        let session = app
            .data
            .join(format!("users/1/.pi/agent/sessions/test/{sid}.jsonl"));
        std::fs::create_dir_all(session.parent().unwrap()).unwrap();
        std::fs::write(&session, "test session").unwrap();
        crate::agents::snapshot(&app, 1, &run, "task", "author", "generated report").unwrap();
        app.store
            .add_decision(
                1,
                &Decision {
                    actor: "human".into(),
                    supervisor: None,
                    id: crate::model::id(),
                    run: run.id.clone(),
                    node: "review".into(),
                    decision: "test".into(),
                    answer: "isolated fixture".into(),
                    user: "test".into(),
                    created: now(),
                    artifacts: vec![],
                },
            )
            .unwrap();
        let keep = crate::presets::all().remove(1);
        app.store.save_workflow(1, &keep).unwrap();
        let mut ids = vec![run.id.clone(), second.id.clone()];
        ids.sort();
        assert!(perform(&app, 3, &workflow.id, &ids).is_err());
        assert!(perform(&app, 1, &workflow.id, &[]).is_err());
        app.busy.lock().unwrap().insert(run.id.clone());
        assert!(perform(&app, 1, &workflow.id, &ids).is_err());
        app.busy.lock().unwrap().remove(&run.id);
        app.store.db.lock().unwrap().execute_batch("CREATE TRIGGER refuse_deletion BEFORE DELETE ON workflows BEGIN SELECT RAISE(ABORT,'rollback probe'); END;").unwrap();
        assert!(perform(&app, 1, &workflow.id, &ids).is_err());
        assert!(FsPath::new(&run.workspace).exists());
        assert!(session.exists());
        assert!(!app.store.artifacts(1, &run.id).unwrap().is_empty());
        app.store
            .db
            .lock()
            .unwrap()
            .execute_batch("DROP TRIGGER refuse_deletion")
            .unwrap();
        let result = perform(&app, 1, &workflow.id, &ids).unwrap();
        assert_eq!(result["deleted_runs"], 2);
        assert_eq!(result["cleanup_pending"], false);
        assert!(app.store.workflow(1, &workflow.id).is_err());
        assert!(app.store.run(1, &run.id).is_err());
        assert!(!FsPath::new(&run.workspace).exists());
        assert!(!FsPath::new(&second.workspace).exists());
        assert!(!session.exists());
        for table in ["artifacts", "decisions", "events", "runs"] {
            let count: i64 = app
                .store
                .db
                .lock()
                .unwrap()
                .query_row(
                    &format!("SELECT count(*) FROM {table} WHERE user_id=1"),
                    [],
                    |r| r.get(0),
                )
                .unwrap();
            assert_eq!(count, 0);
        }
        assert!(app.store.workflow(1, &keep.id).is_ok());
        assert!(app.store.workflow(2, &workflow.id).is_ok());
        assert!(app.store.run(2, &other.id).is_ok());
        assert!(FsPath::new(&other.workspace).exists());
    }
    fn inventory(root: &FsPath) -> Vec<(PathBuf, Vec<u8>)> {
        fn walk(root: &FsPath, path: &FsPath, out: &mut Vec<(PathBuf, Vec<u8>)>) {
            for e in std::fs::read_dir(path).unwrap() {
                let path = e.unwrap().path();
                let bytes = if path.is_dir() {
                    Vec::new()
                } else {
                    std::fs::read(&path).unwrap()
                };
                out.push((path.strip_prefix(root).unwrap().to_path_buf(), bytes));
                if path.is_dir() {
                    walk(root, &path, out)
                }
            }
        }
        let mut result = Vec::new();
        walk(root, root, &mut result);
        result.sort();
        result
    }

    #[tokio::test]
    async fn deleting_source_copy_preserves_every_original_and_aliased_native_session() {
        let dir = tempfile::tempdir().unwrap();
        let app = crate::tests::app(dir.path());
        let original = app.sources.join("material");
        std::fs::create_dir_all(original.join("nested/empty")).unwrap();
        std::fs::write(original.join("nested/notes.txt"), "original bytes").unwrap();
        let mut w = crate::presets::all().remove(0);
        let n = w.nodes.iter_mut().find(|n| n.kind == "Source").unwrap();
        n.source_type = "folder".into();
        n.source_mode = "copy".into();
        n.location = "material".into();
        app.store.save_workflow(1, &w).unwrap();
        let mut run = crate::engine::create(&app, 1, w.clone(), "test".into(), None)
            .await
            .unwrap();
        std::fs::write(
            FsPath::new(&run.workspace).join("private-result.txt"),
            "generated",
        )
        .unwrap();
        let sid = crate::model::id();
        let sessions = original.join(".pi/agent/sessions/catalog");
        std::fs::create_dir_all(&sessions).unwrap();
        std::fs::write(
            sessions.join(format!("{sid}.jsonl")),
            "source session must survive",
        )
        .unwrap();
        let home_pi = app.data.join("users/1/.pi");
        std::fs::remove_dir_all(&home_pi).unwrap();
        std::os::unix::fs::symlink(original.join(".pi"), &home_pi).unwrap();
        run.sessions.insert(
            "author".into(),
            NativeSession {
                provider: String::new(), base_url: String::new(),
runtime: "pi".into(),
                native_id: sid,
                ..Default::default()
            },
        );
        app.store.save_run(1, &run).unwrap();
        let before = inventory(&original);
        perform(&app, 1, &w.id, std::slice::from_ref(&run.id)).unwrap();
        assert_eq!(inventory(&original), before);
        assert!(
            !FsPath::new(&run.workspace).exists(),
            "Private copies must still be removed"
        );
        assert!(app.store.run(1, &run.id).is_err());
    }

    #[tokio::test]
    async fn deletion_preserves_sources_inside_cleanup_parent_even_through_root_alias() {
        let dir = tempfile::tempdir().unwrap();
        let mut app = crate::tests::app(dir.path());
        let w = crate::presets::all().remove(0);
        app.store.save_workflow(1, &w).unwrap();
        let run = crate::engine::create(&app, 1, w.clone(), "test".into(), None)
            .await
            .unwrap();
        let original = FsPath::new(&run.workspace).join("catalog");
        std::fs::create_dir_all(original.join("empty")).unwrap();
        std::fs::write(original.join("keep.bin"), [0, 1, 2, 255]).unwrap();
        app.sources = dir.path().join("source-alias");
        std::os::unix::fs::symlink(&original, &app.sources).unwrap();
        let before = inventory(&original);
        perform(&app, 1, &w.id, std::slice::from_ref(&run.id)).unwrap();
        assert_eq!(inventory(&original), before);
        assert!(
            FsPath::new(&run.workspace).exists(),
            "An ancestor containing sources cannot be removed"
        );
        assert!(app.store.run(1, &run.id).is_err());
    }

    #[test]
    fn cleanup_guard_protects_missing_source_locations_and_rejects_broken_aliases() {
        let dir = tempfile::tempdir().unwrap();
        let mut app = crate::tests::app(dir.path());
        assert!(protected_sources(&app, &app.sources.join("missing/file")).unwrap());
        assert!(protected_sources(&app, dir.path()).unwrap());
        assert!(!protected_sources(&app, &dir.path().join("unrelated")).unwrap());
        app.sources = dir.path().join("broken");
        std::os::unix::fs::symlink(dir.path().join("missing"), &app.sources).unwrap();
        assert!(protected_sources(&app, &dir.path().join("unrelated")).is_err());
    }
}