agentty 0.14.4

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! App construction and startup helpers for the core module.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use ag_git::GitClient;
use tokio::sync::mpsc;

use super::events::AppEvent;
use super::state::{App, AppClients};
use crate::app::service::{AppServiceDeps, AppServices};
use crate::app::session::{SessionManager, migrate_active_sessions_off_retired_models};
use crate::app::setting::SettingsManager;
use crate::app::startup::{AppStartup, StartupProjectContext, StartupSessionLoadContext};
use crate::app::{AppError, review, sync, task};
use crate::domain::agent::{AgentCliInfo, AgentKind, AgentModel};
use crate::infra::clock::{self, Clock};
use crate::infra::db;
use crate::infra::db::AppRepositories;
use crate::infra::fs::FsClient;
#[cfg(test)]
use crate::infra::project_discovery::ProjectDiscoveryClient;

impl App {
    /// Builds the app state from persisted data and starts background
    /// housekeeping tasks.
    ///
    /// When `auto_update` is `true`, a background `npm i -g agentty@latest`
    /// runs automatically after detecting a newer version.
    ///
    /// # Errors
    /// Returns an error if startup project metadata cannot be persisted,
    /// required startup state cannot be loaded from the database, or restart
    /// recovery cannot complete.
    pub async fn new(
        auto_update: bool,
        base_path: PathBuf,
        working_dir: PathBuf,
        git_branch: Option<String>,
        repositories: impl Into<AppRepositories>,
    ) -> Result<Self, AppError> {
        let clients = AppClients::new();

        let mut app = Self::new_with_options(
            auto_update,
            base_path,
            working_dir,
            git_branch,
            repositories,
            clients,
        )
        .await?;
        app.refresh_assigned_issues_if_issues_tab(false);

        Ok(app)
    }

    /// Builds app state from persisted data with explicit external clients.
    ///
    /// Auto-update is disabled by default; use [`App::new`] with an explicit
    /// `auto_update` flag for production startup.
    ///
    /// # Errors
    /// Returns an error if startup project metadata cannot be persisted,
    /// required startup state cannot be loaded from the database, or restart
    /// recovery cannot complete.
    #[cfg(test)]
    pub(crate) async fn new_with_clients(
        base_path: PathBuf,
        working_dir: PathBuf,
        git_branch: Option<String>,
        repositories: impl Into<AppRepositories>,
        clients: AppClients,
    ) -> Result<Self, AppError> {
        Self::new_with_options(
            false,
            base_path,
            working_dir,
            git_branch,
            repositories,
            clients,
        )
        .await
    }

    /// Core constructor with all options explicit.
    ///
    /// # Errors
    /// Returns an error if startup project metadata cannot be persisted,
    /// required startup state cannot be loaded from the database, or restart
    /// recovery cannot complete.
    async fn new_with_options(
        auto_update: bool,
        base_path: PathBuf,
        working_dir: PathBuf,
        git_branch: Option<String>,
        repositories: impl Into<AppRepositories>,
        clients: AppClients,
    ) -> Result<Self, AppError> {
        let repositories = repositories.into();
        let StartupProjectContext {
            active_project_id,
            active_project_name,
            initial_tab,
            project_items,
            startup_git_branch,
            startup_git_upstream_ref,
            startup_working_dir,
        } = Self::load_startup_project_state(
            working_dir.as_path(),
            git_branch,
            &repositories,
            &clients,
        )
        .await?;

        let clock: Arc<dyn Clock> = clock::from_environment();
        let (event_tx, event_rx) = mpsc::unbounded_channel();
        let services = Self::build_services(
            base_path.clone(),
            Arc::clone(&clock),
            event_tx.clone(),
            repositories.clone(),
            &clients,
        )
        .await?;
        Self::recover_startup_operations(
            repositories.clone(),
            base_path,
            services.git_client(),
            clock,
        )
        .await?;
        let projects = crate::app::project::ProjectManager::new(
            active_project_id,
            active_project_name,
            startup_git_branch,
            startup_git_upstream_ref,
            project_items,
            startup_working_dir.clone(),
        );
        let settings = Self::load_settings(&repositories, &services, active_project_id).await;
        let mut sessions = Self::load_and_restack_startup_sessions(
            &services,
            active_project_id,
            startup_working_dir.as_path(),
        )
        .await;
        let (review_cache, recoverable_focused_review_session_ids) =
            Self::load_startup_focused_reviews(
                &repositories,
                active_project_id,
                &mut sessions,
                settings.default_review_selection.model(),
            )
            .await?;

        let sync_context = Self::sync_context_for(&projects, &services, &sessions);
        let sync_handle = sync::SyncHandle::spawn(event_tx.clone(), sync_context);
        Self::spawn_startup_background_tasks(auto_update, &event_tx, &services, &clients);
        let sync_main_runner = clients
            .sync_main_runner
            .unwrap_or_else(|| sync_handle.sync_main_runner());
        let mut app = Self {
            mode: crate::presentation::app_mode::AppMode::List,
            needs_redraw: true,
            settings,
            settings_presentation:
                crate::presentation::settings::SettingsPresentationState::default(),
            tabs: crate::app::tab::TabManager::new(initial_tab),
            assigned_issue_generation: 0,
            assigned_issue_selected_index: None,
            assigned_issues: crate::app::AssignedIssueState::default(),
            prompt_progress: std::collections::HashMap::new(),
            projects,
            services,
            sessions: sessions.into(),
            question_progress: std::collections::HashMap::new(),
            question_reconcile_reload_attempted: None,
            requested_review_generation: 0,
            requested_review_comment_fetches: std::collections::HashSet::new(),
            requested_review_selected_index: None,
            requested_reviews: crate::app::RequestedReviewState::default(),
            event_rx,
            review_cache,
            latest_available_version: None,
            last_seen_session_update_versions: std::collections::HashMap::new(),
            merge_queue: crate::app::merge_queue::MergeQueue::default(),
            session_progress_messages: std::collections::HashMap::new(),
            update_status: None,
            sync_handle,
            sync_main_runner,
            tmux_client: clients.tmux_client,
        };
        app.recover_startup_focused_reviews(recoverable_focused_review_session_ids)
            .await;

        Ok(app)
    }

    /// Loads focused-review cache state and the managed reviews that must be
    /// regenerated during startup recovery.
    async fn load_startup_focused_reviews(
        repositories: &AppRepositories,
        active_project_id: i64,
        sessions: &mut SessionManager,
        review_model: AgentModel,
    ) -> Result<
        (
            std::collections::HashMap<crate::domain::session::SessionId, review::ReviewCacheEntry>,
            Vec<String>,
        ),
        AppError,
    > {
        let review_cache = Self::load_focused_review_cache(repositories, active_project_id).await;
        let recoverable_session_ids = repositories
            .orchestrations()
            .load_recoverable_focused_review_session_ids(active_project_id)
            .await?;
        review::hydrate_review_transients(&review_cache, sessions.state_mut(), review_model);

        Ok((review_cache, recoverable_session_ids))
    }

    /// Restarts focused review for durable managed tasks whose persistence was
    /// interrupted before a terminal review result was stored.
    pub(super) async fn recover_startup_focused_reviews(&mut self, session_ids: Vec<String>) {
        let session_ids = session_ids
            .into_iter()
            .map(crate::domain::session::SessionId::from)
            .collect();

        self.auto_start_reviews(&session_ids).await;
    }

    /// Loads active-project settings from the feature-scoped dependencies.
    async fn load_settings(
        repositories: &AppRepositories,
        services: &AppServices,
        project_id: i64,
    ) -> SettingsManager {
        SettingsManager::from_repositories(
            repositories.clone(),
            services.available_agent_kinds(),
            project_id,
        )
        .await
    }

    /// Loads persisted focused reviews for one project into the render cache.
    pub(super) async fn load_focused_review_cache(
        repositories: &AppRepositories,
        active_project_id: i64,
    ) -> std::collections::HashMap<crate::domain::session::SessionId, review::ReviewCacheEntry>
    {
        let focused_review_rows = repositories
            .sessions()
            .load_session_focused_reviews_for_project(active_project_id)
            .await
            .unwrap_or_default();

        review::review_cache_from_rows(focused_review_rows)
    }

    /// Loads startup sessions and requeues durable post-merge stack restacks
    /// that were interrupted after persistence had recorded the stack base.
    async fn load_and_restack_startup_sessions(
        services: &AppServices,
        active_project_id: i64,
        startup_working_dir: &Path,
    ) -> SessionManager {
        let default_session_model = SessionManager::load_default_session_model(
            services,
            Some(active_project_id),
            AgentKind::Antigravity.default_model(),
        )
        .await;
        let mut sessions = AppStartup::load_startup_sessions(
            services,
            StartupSessionLoadContext {
                active_project_id,
                default_session_model,
                startup_working_dir,
            },
        )
        .await;
        let failures = sessions
            .rebase_pending_stack_restacks_for_project(services, active_project_id)
            .await;
        sessions
            .append_stacked_rebase_failure_notices(failures, "Pending stacked child sync failed");

        sessions
    }

    /// Returns the optional agent availability probe used by the background
    /// CLI version task.
    fn startup_agent_cli_version_probe(
        clients: &AppClients,
    ) -> Option<Arc<dyn ag_agent::AgentAvailabilityProbe>> {
        clients
            .agent_cli_version_task_enabled
            .then(|| Arc::clone(&clients.agent_availability_probe))
    }

    /// Spawns startup background tasks with the initial availability snapshot
    /// needed for CLI version fallback rows.
    fn spawn_startup_background_tasks(
        auto_update: bool,
        event_tx: &mpsc::UnboundedSender<AppEvent>,
        services: &AppServices,
        clients: &AppClients,
    ) {
        let agent_cli_version_probe = Self::startup_agent_cli_version_probe(clients);
        AppStartup::spawn_background_tasks(
            auto_update,
            event_tx,
            agent_cli_version_probe,
            services.available_agent_kinds(),
        );
    }

    /// Migrates project-independent session state before loading the startup
    /// project context.
    ///
    /// # Errors
    /// Returns an error if startup project metadata cannot be persisted or
    /// loaded from storage.
    async fn load_startup_project_state(
        working_dir: &Path,
        git_branch: Option<String>,
        repositories: &AppRepositories,
        clients: &AppClients,
    ) -> Result<StartupProjectContext, AppError> {
        migrate_active_sessions_off_retired_models(repositories).await;
        let current_project_id =
            AppStartup::persist_startup_project(repositories, working_dir, git_branch.as_deref())
                .await?;
        let startup_project_context = AppStartup::load_startup_project_context(
            repositories,
            clients.fs_client.as_ref(),
            &clients.git_client,
            clients.project_discovery_client.as_ref(),
            working_dir,
            git_branch,
            current_project_id,
        )
        .await?;

        Ok(startup_project_context)
    }

    /// Builds the shared app services after validating startup agent
    /// availability.
    ///
    /// # Errors
    /// Returns an error when no supported agent backend is available.
    async fn build_services(
        base_path: PathBuf,
        clock: Arc<dyn Clock>,
        event_tx: mpsc::UnboundedSender<AppEvent>,
        repositories: AppRepositories,
        clients: &AppClients,
    ) -> Result<AppServices, AppError> {
        let available_agent_kinds = task::TaskService::load_agent_availability(Arc::clone(
            &clients.agent_availability_probe,
        ))
        .await;
        AppStartup::validate_startup_agent_availability(&available_agent_kinds)?;
        let available_agent_clis = AgentCliInfo::loading_from_kinds(&available_agent_kinds);

        Ok(AppServices::new_with_agent_clis(
            base_path,
            clock,
            event_tx,
            AppServiceDeps {
                app_server_client_override: clients
                    .app_server_client_override
                    .as_ref()
                    .map(Arc::clone),
                available_agent_kinds,
                clipboard_image_client_override: None,
                fs_client: Arc::clone(&clients.fs_client),
                git_client: Arc::clone(&clients.git_client),
                one_shot_client_override: None,
                personality_catalog_client_override: Some(Arc::clone(
                    &clients.personality_catalog_client,
                )),
                repositories,
                review_request_client: Arc::clone(&clients.review_request_client),
            },
            available_agent_clis,
        ))
    }

    /// Completes durable operation recovery before startup admits sessions.
    ///
    /// # Errors
    /// Returns an actionable error when recovery cannot complete.
    async fn recover_startup_operations(
        repositories: AppRepositories,
        base_path: PathBuf,
        git_client: Arc<dyn GitClient>,
        clock: Arc<dyn Clock>,
    ) -> Result<(), AppError> {
        SessionManager::fail_unfinished_operations_from_previous_run(
            repositories,
            base_path,
            git_client,
            clock,
        )
        .await
        .map_err(Self::startup_recovery_error)
    }

    /// Converts a failed startup recovery into an actionable application error.
    fn startup_recovery_error(error: impl std::fmt::Display) -> AppError {
        AppError::Workflow(format!(
            "Startup recovery did not complete. Resolve the underlying storage or Git error and \
             restart Agentty: {error}"
        ))
    }

    /// Resolves the configured upstream reference for one project branch.
    pub(super) async fn load_git_upstream_ref(
        git_client: &dyn GitClient,
        working_dir: &Path,
        git_branch: Option<&str>,
    ) -> Option<String> {
        AppStartup::load_git_upstream_ref(git_client, working_dir, git_branch).await
    }

    /// Resolves startup active project id from settings, falling back to the
    /// current working directory when the stored project row is stale.
    #[cfg(test)]
    pub(super) async fn resolve_startup_active_project_id(
        db: &AppRepositories,
        fs_client: &dyn FsClient,
        current_project_id: i64,
    ) -> i64 {
        AppStartup::resolve_startup_active_project_id(db, fs_client, current_project_id).await
    }

    /// Loads project list entries for the projects tab.
    ///
    /// Agentty-managed session worktrees, missing project directories, and
    /// non-git folders are excluded so the list keeps only user-facing
    /// repository roots that still exist on disk.
    pub(super) async fn load_project_items(
        db: &AppRepositories,
        fs_client: &dyn FsClient,
    ) -> Vec<crate::domain::project::ProjectListItem> {
        AppStartup::load_project_items(db, fs_client).await
    }

    /// Loads project list entries with one caller-provided session worktree
    /// root for filtering.
    #[cfg(test)]
    pub(super) async fn load_project_items_with_session_worktree_root(
        db: &AppRepositories,
        fs_client: &dyn FsClient,
        session_worktree_root: &Path,
    ) -> Vec<crate::domain::project::ProjectListItem> {
        AppStartup::load_project_items_with_session_worktree_root(
            db,
            fs_client,
            session_worktree_root,
        )
        .await
    }

    /// Refreshes the persisted project catalog from the user's home directory
    /// during startup before the first project list render.
    #[cfg(test)]
    pub(super) async fn load_projects_from_home_directory(
        db: &AppRepositories,
        git_client: &dyn GitClient,
        project_discovery_client: &dyn ProjectDiscoveryClient,
        session_worktree_root: &Path,
        home_directory: Option<&Path>,
    ) {
        AppStartup::load_projects_from_home_directory(
            db,
            git_client,
            project_discovery_client,
            session_worktree_root,
            home_directory,
        )
        .await;
    }

    /// Returns git repository roots discovered under the user home directory.
    ///
    /// A repository root is identified by a direct `.git` marker inside the
    /// directory and discovery stops after `HOME_PROJECT_SCAN_MAX_RESULTS`.
    #[cfg(test)]
    pub(super) fn discover_home_project_paths(
        home_directory: &Path,
        session_worktree_root: &Path,
    ) -> Vec<PathBuf> {
        AppStartup::discover_home_project_paths(home_directory, session_worktree_root)
    }

    /// Returns whether a persisted project path points to an agentty session
    /// worktree under `~/.agentty/wt`.
    #[cfg(test)]
    pub(super) fn is_session_worktree_project_path(
        project_path: &str,
        session_worktree_root: &Path,
    ) -> bool {
        AppStartup::is_session_worktree_project_path(project_path, session_worktree_root)
    }

    /// Filters persisted project rows down to git repository entries that
    /// should remain visible in the Projects tab.
    #[cfg(test)]
    pub(super) fn visible_project_rows(
        project_rows: Vec<db::ProjectListRow>,
        fs_client: &dyn FsClient,
        session_worktree_root: &Path,
    ) -> Vec<db::ProjectListRow> {
        AppStartup::visible_project_rows(project_rows, fs_client, session_worktree_root)
    }

    /// Returns whether one persisted project path still resolves to a
    /// directory on disk.
    #[cfg(test)]
    pub(super) fn is_existing_project_path(fs_client: &dyn FsClient, project_path: &str) -> bool {
        AppStartup::is_existing_project_path(fs_client, project_path)
    }

    /// Converts a project row into domain project model.
    pub(super) fn project_from_row(project_row: db::ProjectRow) -> crate::domain::project::Project {
        AppStartup::project_from_row(project_row)
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::os::unix::fs::PermissionsExt;
    use std::process::Command;

    use tempfile::tempdir;

    use super::*;
    use crate::domain::session::Status;
    use crate::infra::db::{AppRepositories, Database};

    const PUBLIC_CONSTRUCTOR_COVERAGE_ENV: &str = "AGENTTY_PUBLIC_CONSTRUCTOR_COVERAGE";

    #[tokio::test]
    /// Verifies the public constructor starts with its production client
    /// bundle in an isolated environment.
    async fn test_new_uses_production_client_bundle() {
        if std::env::var_os(PUBLIC_CONSTRUCTOR_COVERAGE_ENV).is_some() {
            // Arrange
            let base_dir = tempdir().expect("failed to create temp dir");
            let base_path = base_dir.path().to_path_buf();
            let database = Database::open_in_memory()
                .await
                .expect("failed to open in-memory database");

            // Act
            let app = App::new(false, base_path.clone(), base_path, None, database)
                .await
                .expect("public constructor should build app");

            // Assert
            assert!(app.selected_session().is_none());

            return;
        }

        // Arrange
        let base_dir = tempdir().expect("failed to create temp dir");
        let stub_bin = base_dir.path().join("stub-bin");
        fs::create_dir_all(&stub_bin).expect("failed to create stub bin directory");
        let codex_stub = stub_bin.join("codex");
        fs::write(&codex_stub, "#!/bin/sh\nexit 0\n").expect("failed to write codex stub");
        fs::set_permissions(&codex_stub, fs::Permissions::from_mode(0o755))
            .expect("failed to mark codex stub executable");
        let test_binary = std::env::current_exe().expect("failed to locate test binary");
        let child_path = format!("{}:/usr/bin:/bin", stub_bin.display());
        let child_coverage_profile = std::env::var_os("LLVM_PROFILE_FILE").map(|profile| {
            profile
                .to_string_lossy()
                .replace(".profraw", "-child.profraw")
        });

        // Act
        let mut child = Command::new(test_binary);
        child
            .arg("--exact")
            .arg("app::core::new::tests::test_new_uses_production_client_bundle")
            .arg("--nocapture")
            .env(PUBLIC_CONSTRUCTOR_COVERAGE_ENV, "1")
            .env("HOME", base_dir.path())
            .env("PATH", child_path);
        if let Some(profile) = child_coverage_profile {
            child.env("LLVM_PROFILE_FILE", profile);
        }
        let child_status = child
            .status()
            .expect("failed to run isolated constructor test");

        // Assert
        assert!(child_status.success());
    }

    #[tokio::test]
    /// Verifies incomplete recovery prevents startup from admitting sessions.
    async fn test_new_with_clients_returns_actionable_error_when_recovery_fails() {
        // Arrange
        let base_dir = tempdir().expect("failed to create temp dir");
        let base_path = base_dir.path().to_path_buf();
        let (database, pool) = AppRepositories::in_memory_with_pool().await;
        sqlx::query("DROP TABLE session_operation")
            .execute(&pool)
            .await
            .expect("failed to remove session operation table");

        // Act
        let error = App::new_with_clients(
            base_path.clone(),
            base_path,
            None,
            database,
            crate::test_support::test_app_clients(),
        )
        .await
        .err();

        // Assert
        assert!(
            error.is_some(),
            "incomplete recovery should prevent app startup"
        );
        if let Some(error) = error {
            assert!(matches!(error, AppError::Workflow(_)));
            assert!(
                error
                    .to_string()
                    .contains("Startup recovery did not complete")
            );
            assert!(error.to_string().contains("restart Agentty"));
        }
    }

    #[tokio::test]
    /// Verifies a subsequent startup admits sessions after recovery is
    /// retried following a transient operation-update failure.
    async fn test_new_with_clients_retries_recovery_after_operation_update_failure() {
        // Arrange
        let base_dir = tempdir().expect("failed to create temp dir");
        let base_path = base_dir.path().to_path_buf();
        let (database, pool) = AppRepositories::in_memory_with_pool().await;
        let project_id = database
            .projects()
            .upsert_project(&base_path.to_string_lossy(), None)
            .await
            .expect("failed to upsert project");
        database
            .sessions()
            .insert_session(
                "sess1",
                "gemini-3.6-flash",
                "main",
                &Status::InProgress.to_string(),
                project_id,
            )
            .await
            .expect("failed to insert session");
        database
            .operations()
            .insert_session_operation("op-1", "sess1", "reply")
            .await
            .expect("failed to insert session operation");
        sqlx::query(
            "CREATE TRIGGER fail_startup_recovery BEFORE UPDATE OF status ON session_operation \
             BEGIN SELECT RAISE(FAIL, 'operation update failed'); END",
        )
        .execute(&pool)
        .await
        .expect("failed to create recovery trigger");

        // Act
        let failed_startup = App::new_with_clients(
            base_path.clone(),
            base_path.clone(),
            None,
            database.clone(),
            crate::test_support::test_app_clients(),
        )
        .await;
        sqlx::query("DROP TRIGGER fail_startup_recovery")
            .execute(&pool)
            .await
            .expect("failed to remove recovery trigger");
        let retried_startup = App::new_with_clients(
            base_path.clone(),
            base_path,
            None,
            database,
            crate::test_support::test_app_clients(),
        )
        .await;

        // Assert
        assert!(matches!(failed_startup, Err(AppError::Workflow(_))));
        assert!(retried_startup.is_ok());
    }
}