agentty 0.15.1

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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! Orchestration campaign and managed-worker E2E tests.

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::Command;

use agentty::db::{Database, DbError};
use agentty::domain::session::{
    ForgeKind, ReviewRequest, ReviewRequestState, ReviewRequestSummary,
};
use agentty::domain::session_message::SessionMessageKind;
use agentty::test_support;
use testty::assertion;
use testty::frame::TerminalFrame;
use testty::region::Region;

use crate::common;
use crate::common::{BuilderEnv, FeatureTest, SessionSeed};

type E2eResult = Result<(), Box<dyn std::error::Error>>;

const CONTROLLER_ID: &str = "controller-0001";
const WORKER_ID: &str = "worker-a-0001";
const CONTROLLER_REVISION_PROMPT: &str = "Revise the plan without changing branches";
const CONTROLLER_REVISION_RESPONSE: &str = "Controller revision recorded.";

/// Gives the controller a strictly newer fixture timestamp than its worker.
async fn stabilize_controller_initial_selection(database: &Database) -> Result<(), DbError> {
    database
        .sessions()
        .update_session_updated_at(WORKER_ID, 1)
        .await?;
    database
        .sessions()
        .update_session_updated_at(CONTROLLER_ID, 2)
        .await
}

/// Seeds one parked campaign plus a linked managed worker for UI proofs.
fn seed_orchestration_campaign(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign_rows(env)?;

    std::fs::create_dir_all(env.agentty_root.join("wt").join("controll"))?;
    std::fs::create_dir_all(env.agentty_root.join("wt").join("worker-a"))?;

    Ok(())
}

/// Seeds a controller and managed worker with real diffs so startup and turn
/// completion exercise role-scoped automatic review.
fn seed_orchestrator_auto_review_scope(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign_rows(env)?;
    seed_orchestration_review_worktrees(env)?;
    install_auto_review_claude_stub(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        for session_id in [CONTROLLER_ID, WORKER_ID] {
            database
                .sessions()
                .update_session_model(session_id, "claude-haiku-4-5-20251001")
                .await?;
            database
                .sessions()
                .update_session_diff_stats(1, 0, true, session_id, "XS")
                .await?;
        }
        database
            .sessions()
            .update_session_status_with_timing_at(WORKER_ID, "Review", 0)
            .await?;

        sqlx::query(
            "UPDATE session_orchestration SET status = 'Running' WHERE controller_session_id = ?",
        )
        .bind(CONTROLLER_ID)
        .execute(database.pool())
        .await?;
        sqlx::query(
            "UPDATE session_orchestration_task SET status = CASE task_key WHEN 'protocol' THEN \
             'Reviewing' ELSE 'Failed' END",
        )
        .execute(database.pool())
        .await?;

        let project_id =
            sqlx::query_scalar::<_, i64>("SELECT project_id FROM session WHERE id = ?")
                .bind(CONTROLLER_ID)
                .fetch_one(database.pool())
                .await?;
        for (setting_name, setting_value) in [
            ("DefaultReviewAgent", "claude"),
            ("DefaultReviewModel", "claude-haiku-4-5-20251001"),
        ] {
            sqlx::query(
                "INSERT INTO project_setting (project_id, name, value) VALUES (?, ?, ?) ON \
                 CONFLICT(project_id, name) DO UPDATE SET value = excluded.value",
            )
            .bind(project_id)
            .bind(setting_name)
            .bind(setting_value)
            .execute(database.pool())
            .await?;
        }

        stabilize_controller_initial_selection(&database).await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

/// Seeds the persisted rows for one parked campaign and linked managed worker.
fn seed_orchestration_campaign_rows(env: &BuilderEnv) -> E2eResult {
    common::seed_session(
        env,
        SessionSeed::regular(CONTROLLER_ID, "gpt-5.6-sol", "main", "Review")
            .with_title("Managed feature delivery"),
    )?;
    common::seed_session(
        env,
        SessionSeed::regular(WORKER_ID, "gpt-5.6-sol", "main", "InProgress")
            .with_title("Implement protocol contract"),
    )?;

    let runtime = common::seed_runtime()?;
    let seed_result = runtime.block_on(async {
        let database = common::open_database(env)
            .await
            .map_err(|error| -> Box<dyn std::error::Error> { Box::new(error) })?;
        sqlx::query("UPDATE session SET role = 'Orchestrator' WHERE id = ?")
            .bind(CONTROLLER_ID)
            .execute(database.pool())
            .await?;
        let orchestration_id = sqlx::query_scalar::<_, i64>(
            "INSERT INTO session_orchestration (controller_session_id, goal_statement, status, \
             max_parallelism, created_at, updated_at) VALUES (?, 'Deliver the campaign safely', \
             'AwaitingApproval', 3, 1, 1) RETURNING id",
        )
        .bind(CONTROLLER_ID)
        .fetch_one(database.pool())
        .await?;
        let protocol_task_id = sqlx::query_scalar::<_, i64>(
            "INSERT INTO session_orchestration_task (session_orchestration_id, task_key, title, \
             prompt, touched_areas, acceptance_criteria, merge_position, status, \
             child_session_id, created_at, updated_at) VALUES (?, 'protocol', 'Protocol \
             contract', 'Implement the protocol contract', '[\"crates/ag-protocol/\"]', \
             '[\"Protocol tests pass\"]', 0, 'Proposed', ?, 1, 1) RETURNING id",
        )
        .bind(orchestration_id)
        .bind(WORKER_ID)
        .fetch_one(database.pool())
        .await?;
        sqlx::query(
            "INSERT INTO session_orchestration_task (session_orchestration_id, task_key, title, \
             prompt, touched_areas, acceptance_criteria, merge_position, status, created_at, \
             updated_at) VALUES (?, 'ui', 'Campaign board', 'Implement the campaign board', \
             '[\"crates/agentty/src/ui/\"]', '[\"Board is visible\"]', 1, 'Proposed', 1, 1)",
        )
        .bind(orchestration_id)
        .execute(database.pool())
        .await?;
        sqlx::query(
            "UPDATE session SET role = 'OrchestrationWorker', orchestration_task_id = ? WHERE id \
             = ?",
        )
        .bind(protocol_task_id)
        .bind(WORKER_ID)
        .execute(database.pool())
        .await?;
        database
            .reviews()
            .update_session_review_request(
                WORKER_ID,
                Some(ReviewRequest {
                    last_refreshed_at: 1,
                    summary: ReviewRequestSummary {
                        display_id: "#7".to_string(),
                        forge_kind: ForgeKind::GitHub,
                        source_branch: "wt/worker-a".to_string(),
                        state: ReviewRequestState::Open,
                        status_summary: None,
                        target_branch: "main".to_string(),
                        title: "Managed worker review".to_string(),
                        web_url: "https://github.com/example/project/pull/7".to_string(),
                    },
                }),
            )
            .await?;
        // Keep the controller as the deterministic initial raw selection so
        // one `j` press always reaches its grouped worker row. Explicit
        // timestamps avoid ties with the worker's later `created_at` value.
        stabilize_controller_initial_selection(&database).await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    });
    seed_result?;

    Ok(())
}

/// Creates linked controller and worker worktrees with one tracked change in
/// each branch.
fn seed_orchestration_review_worktrees(env: &BuilderEnv) -> E2eResult {
    std::fs::write(env.workdir.join("review-scope.txt"), "base\n")?;
    run_git_command(&env.workdir, &["add", "review-scope.txt"])?;
    run_git_command(&env.workdir, &["commit", "-m", "review scope base"])?;

    let worktree_root = env.agentty_root.join("wt");
    std::fs::create_dir_all(&worktree_root)?;
    for (session_id, branch_name, content) in [
        (CONTROLLER_ID, "wt/controll", "controller change\n"),
        (WORKER_ID, "wt/worker-a", "worker change\n"),
    ] {
        let session_folder = test_support::session_folder(&worktree_root, session_id);
        let session_folder_text = session_folder.to_string_lossy().into_owned();
        run_git_command(
            &env.workdir,
            &[
                "worktree",
                "add",
                "-b",
                branch_name,
                session_folder_text.as_str(),
            ],
        )?;
        std::fs::write(session_folder.join("review-scope.txt"), content)?;
    }

    Ok(())
}

/// Installs a prompt-aware Claude stub that completes controller chat quickly
/// while leaving focused review visibly in progress.
fn install_auto_review_claude_stub(env: &BuilderEnv) -> E2eResult {
    let claude_path = env.stub_bin.join("claude");
    let script = format!(
        r###"#!/bin/sh
if [ "$1" = "update" ]; then exit 0; fi
if [ "$1" = "--version" ]; then printf 'claude 0.0.0-test\n'; exit 0; fi
input=$(cat)
case "$input" in
  *"Review the Git diff for display in a terminal UI."*)
    sleep 30
    result='{{\"answer\":\"## Review\\n\\n### Project Impact\\n\\n- Managed worker review completed.\\n\\n### Suggestions\\n\\n- None\",\"questions\":[],\"summary\":null}}'
    ;;
  *)
    result='{{\"answer\":\"{CONTROLLER_REVISION_RESPONSE}\",\"questions\":[],\"review_comment_outcomes\":[],\"subtasks\":[],\"summary\":null,\"verification_verdicts\":[]}}'
    ;;
esac
printf '%s\n' '{{"type":"system","subtype":"init"}}'
printf '{{"type":"result","subtype":"success","result":"%s","usage":{{"input_tokens":5,"output_tokens":9}}}}\n' "$result"
"###,
    );
    std::fs::write(&claude_path, script)?;
    #[cfg(unix)]
    std::fs::set_permissions(&claude_path, std::fs::Permissions::from_mode(0o755))?;

    Ok(())
}

/// Runs one Git setup command inside the isolated E2E repository.
fn run_git_command(working_directory: &Path, args: &[&str]) -> E2eResult {
    let output = Command::new("git")
        .args(args)
        .current_dir(working_directory)
        .output()?;
    if !output.status.success() {
        return Err(std::io::Error::other(format!(
            "git {} failed: {}",
            args.join(" "),
            String::from_utf8_lossy(&output.stderr),
        ))
        .into());
    }

    Ok(())
}

/// Seeds a completed read-only research task whose temporary worktree had
/// edits so the campaign board proves both report capture and discard status.
fn seed_reported_research_task(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        sqlx::query(
            "UPDATE session_orchestration_task SET kind = 'Research', title = 'Architecture \
             review', prompt = 'Inspect architecture', touched_areas = '[]', status = 'Reported', \
             research_report = 'Architecture boundaries are documented', verification_verdict = \
             'Pass' WHERE task_key = 'protocol'",
        )
        .execute(database.pool())
        .await?;
        sqlx::query(
            "UPDATE session SET role = 'OrchestrationResearcher', has_diff = 1, status = \
             'Canceled', title = 'Inspect architecture boundaries', archived_diff = 'diff --git \
             a/policy.txt b/policy.txt\n+Unexpected research write\n' WHERE id = ?",
        )
        .bind(WORKER_ID)
        .execute(database.pool())
        .await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })?;

    Ok(())
}

/// Seeds one managed worker executing its first accepted review-remediation
/// continuation so the campaign board exposes stable remediation progress.
fn seed_orchestration_review_remediation(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        let task_id = sqlx::query_scalar::<_, i64>(
            "SELECT id FROM session_orchestration_task WHERE task_key = 'protocol'",
        )
        .fetch_one(database.pool())
        .await?;
        sqlx::query(
            "UPDATE session_orchestration_task SET status = 'ReviewApplying', review_iteration = \
             1, continuation_generation = 1, continuation_prompt = 'Verify then apply sensible \
             review suggestions' WHERE id = ?",
        )
        .bind(task_id)
        .execute(database.pool())
        .await?;
        sqlx::query(
            "UPDATE session SET status = 'InProgress', has_diff = 1, focused_review_status = \
             NULL, focused_review_diff_hash = NULL, focused_review_text = NULL WHERE id = ?",
        )
        .bind(WORKER_ID)
        .execute(database.pool())
        .await?;
        sqlx::query(
            "INSERT INTO session_operation (id, session_id, kind, status, queued_at, finished_at) \
             VALUES (?, ?, 'reply', 'done', 1, 1)",
        )
        .bind(format!("orchestration-continuation-{task_id}-1"))
        .bind(WORKER_ID)
        .execute(database.pool())
        .await?;
        sqlx::query("UPDATE session_orchestration SET status = 'Running'")
            .execute(database.pool())
            .await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })?;

    Ok(())
}

/// Seeds enough controller transcript output to exercise line-by-line scroll
/// bounds in the compact chat pane below the campaign board.
fn seed_scrollable_orchestration_campaign(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let output = (0..60)
        .map(|line_index| format!("Transcript line {line_index:02}"))
        .collect::<Vec<_>>()
        .join("\n");
    let output = format!("```text\n{output}\n```");
    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        database
            .sessions()
            .append_session_message(CONTROLLER_ID, SessionMessageKind::AssistantAnswer, &output)
            .await
    })?;

    Ok(())
}

/// Seeds one parked campaign whose managed worker is ready for review.
fn seed_review_ready_managed_worker(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        database
            .sessions()
            .update_session_status_with_timing_at(WORKER_ID, "Review", 0)
            .await?;
        // The worker status update touches its row, so restore the same stable
        // initial selection ordering used by the base campaign seed.
        stabilize_controller_initial_selection(&database).await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

/// Seeds a review-request campaign with one worker still awaiting its forge
/// merge and one already integrated sibling.
fn seed_review_request_campaign_awaiting_merge(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        sqlx::query!(
            "UPDATE session_orchestration SET status = 'Integrating', integration_approach = \
             'ReviewRequest'",
        )
        .execute(database.pool())
        .await?;
        sqlx::query!(
            "UPDATE session_orchestration_task SET status = CASE task_key WHEN 'protocol' THEN \
             'ReviewRequested' ELSE 'Integrated' END",
        )
        .execute(database.pool())
        .await?;
        sqlx::query!(
            "UPDATE session SET status = 'Review' WHERE id = ?",
            WORKER_ID
        )
        .execute(database.pool())
        .await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

/// Seeds a review-request campaign whose forge review was closed without a
/// merge so reconciliation must surface an integration failure.
fn seed_review_request_campaign_with_closed_worker(env: &BuilderEnv) -> E2eResult {
    seed_review_request_campaign_awaiting_merge(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        sqlx::query!(
            "UPDATE session SET status = 'Canceled' WHERE id = ?",
            WORKER_ID
        )
        .execute(database.pool())
        .await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

/// Returns the numbered transcript rows visible in one captured frame.
fn visible_transcript_line_indexes(frame: &TerminalFrame) -> Vec<u16> {
    let full = Region::full(frame.cols(), frame.rows());

    frame
        .text_in_region(&full)
        .lines()
        .filter_map(|line| {
            let suffix = line.split_once("Transcript line ")?.1;

            suffix.get(..2)?.parse().ok()
        })
        .collect()
}

/// Seeds one completed managed worker whose worktree-independent review diff
/// has already been archived by the merge workflow.
fn seed_archived_managed_worker(env: &BuilderEnv) -> E2eResult {
    seed_orchestration_campaign(env)?;

    let runtime = common::seed_runtime()?;
    runtime.block_on(async {
        let database = common::open_database(env).await?;
        database
            .sessions()
            .update_session_archived_diff(
                WORKER_ID,
                Some(
                    "diff --git a/worker.txt b/worker.txt\nnew file mode 100644\n+Archived worker \
                     result\n"
                        .to_string(),
                ),
            )
            .await?;
        sqlx::query("UPDATE session SET status = 'Done' WHERE id IN (?, ?)")
            .bind(WORKER_ID)
            .bind(CONTROLLER_ID)
            .execute(database.pool())
            .await?;
        sqlx::query("UPDATE session_orchestration SET status = 'Done'")
            .execute(database.pool())
            .await?;
        stabilize_controller_initial_selection(&database).await?;

        Ok::<(), Box<dyn std::error::Error>>(())
    })
}

#[test]
fn test_orchestration_campaign_board() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("orchestration_campaign_board")
        .with_git()
        .setup(seed_orchestration_campaign)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view())
                    .capture_labeled("campaign_board", "Parked orchestration campaign board")
            },
            |frame, _report| {
                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(
                    frame,
                    "Campaign: Managed feature delivery",
                    &full,
                );
                assertion::assert_text_in_region(frame, "Phase: AwaitingApproval", &full);
                assertion::assert_text_in_region(
                    frame,
                    "Parallel workers: 3 (global setting)",
                    &full,
                );
                assertion::assert_text_in_region(
                    frame,
                    "Protocol contract [protocol]: awaiting approval",
                    &full,
                );
                assertion::assert_text_in_region(frame, "a approve  Enter discuss/revise", &full);
            },
        )
}

#[test]
fn test_orchestrator_auto_review_scope() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("orchestrator_auto_review_scope")
        .with_git()
        .setup(seed_orchestrator_auto_review_scope)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Campaign: Managed feature delivery",
                    ]))
                    .press_key("Enter")
                    .wait_for_text("Tab: focus | Enter: send", 5000)
                    .write_text(CONTROLLER_REVISION_PROMPT)
                    .press_key("Enter")
                    .wait_for_text("Enter: reply", 30000)
                    .wait_for_text("Phase: Running", 5000)
                    .capture_labeled(
                        "controller_review_ready",
                        "Controller remains review-ready after its turn",
                    )
                    .compose(&common::return_to_session_list())
                    .press_key("j")
                    .compose(&common::open_selected_session_view())
                    .wait_for_text("Managed by controller-0001", 5000)
                    .wait_for_text("Reviewing changes with claude-haiku-4-5-20251001", 5000)
                    .capture_labeled(
                        "worker_auto_review",
                        "Managed worker starts focused review automatically",
                    )
            },
            |frame, report| {
                let controller_frame = common::frame_from_capture(&report.captures[0]);
                let controller_full =
                    Region::full(controller_frame.cols(), controller_frame.rows());
                assertion::assert_text_in_region(
                    &controller_frame,
                    "Enter: reply",
                    &controller_full,
                );
                assertion::assert_text_in_region(
                    &controller_frame,
                    "Phase: Running",
                    &controller_full,
                );
                assertion::assert_not_visible(&controller_frame, "Reviewing changes with");

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Managed by controller-0001", &full);
                assertion::assert_text_in_region(
                    frame,
                    "Reviewing changes with claude-haiku-4-5-20251001",
                    &full,
                );
            },
        )
}

#[test]
fn test_review_request_campaign_waits_for_worker_merge() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("review_request_campaign_waits_for_worker_merge")
        .with_git()
        .setup(seed_review_request_campaign_awaiting_merge)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view())
                    // Campaign reconciliation competes with up to four E2E
                    // processes on CI, so allow one longer render deadline.
                    .wait_for_text("Phase: Integrating", 10_000)
                    .capture_labeled(
                        "controller_waiting",
                        "Controller remains active while review request is open",
                    )
                    .compose(&common::return_to_session_list())
                    .press_key("j")
                    .wait_for_stable_frame(300, 3000)
                    .compose(&common::open_selected_session_view())
                    .wait_for_text("Managed by controller-0001", 5000)
                    .press_key("?")
                    .wait_for_stable_frame(300, 5000)
                    .capture_labeled(
                        "worker_attached",
                        "Review-request worker remains attached to controller",
                    )
            },
            |frame, report| {
                let controller_frame = common::frame_from_capture(&report.captures[0]);
                let controller_full =
                    Region::full(controller_frame.cols(), controller_frame.rows());
                assertion::assert_text_in_region(
                    &controller_frame,
                    "Phase: Integrating",
                    &controller_full,
                );
                assertion::assert_text_in_region(
                    &controller_frame,
                    "Protocol contract [protocol]: review requested",
                    &controller_full,
                );
                let controller_text = controller_frame.text_in_region(&controller_full);
                assert!(!controller_text.contains("Campaign complete"));

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Managed by controller-0001", &full);
                assertion::assert_text_in_region(frame, "Detach managed", &full);
            },
        )
}

#[test]
fn test_review_request_campaign_reports_closed_worker() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("review_request_campaign_reports_closed_worker")
        .with_git()
        .setup(seed_review_request_campaign_with_closed_worker)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view())
                    .wait_for_text("integration failed", 5000)
                    .capture_labeled(
                        "closed_worker_failure",
                        "Closed review request remains visible as integration failure",
                    )
            },
            |frame, _report| {
                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Phase: Integrating", &full);
                assertion::assert_text_in_region(
                    frame,
                    "Protocol contract [protocol]: integration failed",
                    &full,
                );
                let text = frame.text_in_region(&full);
                assert!(!text.contains("Campaign complete"));
            },
        )
}

#[test]
fn test_orchestration_review_remediation() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("orchestration_review_remediation")
        .with_git()
        .setup(seed_orchestration_review_remediation)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Campaign: Managed feature delivery",
                        "remediation 1/3",
                    ]))
                    .capture_labeled(
                        "review_remediation",
                        "Managed worker focused-review remediation progress",
                    )
            },
            |frame, _report| {
                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(
                    frame,
                    "Protocol contract [protocol]: applying review; remediation 1/3",
                    &full,
                );
            },
        )
}

#[test]
fn test_orchestration_research_report() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("orchestration_research_report")
        .with_git()
        .setup(seed_reported_research_task)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view())
                    .wait_for_text("[Research] Architecture review", 5000)
                    .capture_labeled(
                        "research_report",
                        "Read-only research report on campaign board",
                    )
                    .press_key("q")
                    .press_key("j")
                    .press_key("Enter")
                    .wait_for_text("Managed by controller-0001", 5000)
                    .wait_for_text("Inspect architecture boundaries", 5000)
                    .press_key("d")
                    .wait_for_text("Unexpected research write", 5000)
                    .capture_labeled(
                        "research_archived_diff",
                        "Archived research diff after temporary cleanup",
                    )
            },
            |frame, report| {
                let report_frame = common::frame_from_capture(&report.captures[0]);
                let report_full = Region::full(report_frame.cols(), report_frame.rows());
                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(
                    &report_frame,
                    "[Research] Architecture review [protocol]: reported",
                    &report_full,
                );
                assertion::assert_text_in_region(&report_frame, "report captured;", &report_full);
                assertion::assert_text_in_region(
                    &report_frame,
                    "temporary edits discarded; verified",
                    &report_full,
                );
                assertion::assert_text_in_region(frame, "Unexpected research write", &full);
                assertion::assert_text_in_region(frame, "Inspect architecture boundaries", &full);
                assertion::assert_text_in_region(frame, "policy.txt", &full);
                assertion::assert_text_in_region(frame, "q/Esc: back", &full);
            },
        )
}

#[test]
fn test_orchestration_campaign_smooth_scroll() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("orchestration_campaign_smooth_scroll")
        .with_git()
        .with_terminal_size(80, 24)
        .setup(seed_scrollable_orchestration_campaign)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Campaign: Managed feature delivery",
                        "Transcript line 59",
                    ]))
                    .capture_labeled("campaign_bottom", "Controller transcript at bottom")
                    .press_key("k")
                    .wait_for_stable_frame(300, 5000)
                    .capture_labeled("campaign_one_line_up", "Controller transcript one line up")
            },
            |_frame, report| {
                assert_eq!(report.captures.len(), 2);

                let bottom_frame = common::frame_from_capture(&report.captures[0]);
                let one_line_up_frame = common::frame_from_capture(&report.captures[1]);
                let bottom_lines = visible_transcript_line_indexes(&bottom_frame);
                let one_line_up_lines = visible_transcript_line_indexes(&one_line_up_frame);
                assert!(!bottom_lines.is_empty(), "expected bottom transcript rows");
                assert!(
                    !one_line_up_lines.is_empty(),
                    "expected scrolled transcript rows"
                );
                assert_eq!(
                    one_line_up_lines
                        .first()
                        .copied()
                        .map(|line_index| line_index + 1),
                    bottom_lines.first().copied()
                );
            },
        )
}

#[test]
fn test_managed_worker_restricted_view() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("managed_worker_restricted_view")
        .with_git()
        .setup(seed_orchestration_campaign)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .press_key("j")
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Managed by controller-0001",
                        "Working...",
                    ]))
                    .press_key("ctrl+c")
                    .wait_for_stable_frame(300, 5000)
                    .capture_labeled(
                        "managed_worker_running",
                        "Managed worker remains active after Ctrl+c",
                    )
                    .press_key("c")
                    .wait_for_stable_frame(300, 5000)
                    .press_key("?")
                    .wait_for_stable_frame(300, 5000)
                    .capture_labeled("managed_worker", "Managed worker restricted actions")
            },
            |frame, report| {
                let running_frame = common::frame_from_capture(&report.captures[0]);
                let running_full = Region::full(running_frame.cols(), running_frame.rows());
                assertion::assert_text_in_region(&running_frame, "Working...", &running_full);
                assertion::assert_text_in_region(
                    &running_frame,
                    "Managed by controller-0001",
                    &running_full,
                );

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Managed by controller-0001", &full);
                assertion::assert_text_in_region(frame, "actions restricted", &full);
                assertion::assert_text_in_region(frame, "d: Show diff", &full);
                assertion::assert_text_in_region(frame, "Detach managed", &full);
                let text = frame.text_in_region(&full);
                assert!(!text.contains("Enter: Reply"));
                assert!(!text.contains("p: Create or refresh"));
                assert!(!text.contains("Review comments"));
            },
        )
}

#[test]
fn test_managed_worker_review_open_action() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("managed_worker_review_open_action")
        .env("TMUX", "/tmp/tmux-agentty-test/default,1,0")
        .with_git()
        .setup(seed_review_ready_managed_worker)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .press_key("j")
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Managed by controller-0001",
                    ]))
                    .compose(&common::open_help_overlay())
                    .capture_labeled(
                        "managed_worker_review_help",
                        "Review-ready managed worker exposes worktree open",
                    )
                    .press_key("q")
                    .wait_for_stable_frame(300, 5000)
                    .press_key("o")
                    .wait_for_text("Open Managed Worktree", 5000)
                    .capture_labeled(
                        "managed_worker_open_warning",
                        "Managed worktree write-access warning",
                    )
            },
            |frame, report| {
                let help_frame = common::frame_from_capture(&report.captures[0]);
                let help_full = Region::full(help_frame.cols(), help_frame.rows());
                assertion::assert_text_in_region(&help_frame, "o: Open worktree", &help_full);
                let help_text = help_frame.text_in_region(&help_full);
                assert!(!help_text.contains("Enter: Reply"));
                assert!(!help_text.contains("m: Add to merge queue"));

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Managed by controller-0001", &full);
                assertion::assert_text_in_region(frame, "Open Managed Worktree", &full);
                assertion::assert_text_in_region(frame, "This opens a writable", &full);
            },
        )
}

#[test]
fn managed_worker_review_hides_open_outside_tmux() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("managed_worker_review_hides_open_outside_tmux")
        .env("TMUX", "")
        .with_git()
        .setup(seed_review_ready_managed_worker)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .press_key("j")
                    .compose(&common::open_selected_session_view_with_texts(&[
                        "Managed by controller-0001",
                    ]))
                    .compose(&common::open_help_overlay())
                    .capture_labeled(
                        "managed_worker_review_help",
                        "Worktree open is hidden outside tmux",
                    )
                    .press_key("q")
                    .wait_for_stable_frame(300, 5000)
                    .press_key("o")
                    .wait_for_stable_frame(300, 5000)
                    .capture_labeled(
                        "managed_worker_review_after_o",
                        "Worktree open key is ignored outside tmux",
                    )
            },
            |frame, report| {
                let help_frame = common::frame_from_capture(&report.captures[0]);
                assertion::assert_not_visible(&help_frame, "o: Open worktree");

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Managed by controller-0001", &full);
                assertion::assert_not_visible(frame, "Open Managed Worktree");
            },
        )
}

#[test]
fn managed_worker_archived_diff() -> E2eResult {
    // Arrange, Act, Assert
    FeatureTest::new("managed_worker_archived_diff")
        .with_git()
        .setup(seed_archived_managed_worker)
        .run(
            |scenario| {
                scenario
                    .compose(&common::wait_for_agentty_startup())
                    .compose(&common::switch_to_tab("Sessions"))
                    .press_key("j")
                    .press_key("Enter")
                    .wait_for_text("Managed by controller-0001", 5000)
                    .press_key("d")
                    .wait_for_text("Archived worker result", 5000)
                    .capture_labeled(
                        "archived_worker_diff",
                        "Archived managed worker diff after cleanup",
                    )
                    .press_key("q")
                    .press_key("q")
                    .press_key("k")
                    .press_key("Enter")
                    .wait_for_text("Campaign complete", 5000)
                    .capture_labeled("completed_campaign", "Completed campaign archive")
            },
            |frame, report| {
                let diff_frame = common::frame_from_capture(&report.captures[0]);
                let diff_full = Region::full(diff_frame.cols(), diff_frame.rows());
                assertion::assert_text_in_region(&diff_frame, "Archived worker result", &diff_full);
                assertion::assert_text_in_region(&diff_frame, "worker.txt", &diff_full);
                assertion::assert_text_in_region(&diff_frame, "q/Esc: back", &diff_full);

                let full = Region::full(frame.cols(), frame.rows());
                assertion::assert_text_in_region(frame, "Phase: Done", &full);
                assertion::assert_text_in_region(frame, "Campaign complete", &full);
            },
        )
}