app-kit 0.0.2

Application and durable workflow composition for ai-kit
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
mod workspace_support;

use std::sync::atomic::{AtomicBool, Ordering};
use std::{error::Error, num::NonZeroUsize, path::PathBuf, sync::Arc, time::Duration};

use agents_kit::Agent;
use app_kit::workflow::{
    CheckpointKind, CheckpointSelector, ExecutionOptions, Interaction, Resolution, RestoreOptions,
    ResumePosition, RunSelector, RunSnapshot, RunStatus, WorkflowDefinition, WorkflowId,
    WorkflowRun, WorkflowState,
};
use app_kit::{App, AppConfig, AppError, Shutdown, WorkspaceConfig, WorkspaceId};
use futures_util::FutureExt;
use serde_json::{Map, Value};
use storage_kit::state::config::{LanceStateStoreConfig, StateStoreConfig};
use storage_kit::state::record::{
    StateKey, StateListRequest, StateListResponse, StateRecord, StateRecordSnapshot,
};
use storage_kit::{
    StateStore, StateStoreBackend, StateStoreError, StateTransaction, StateTransactionBackend,
};

struct WorkspaceJourney {
    root: PathBuf,
}

impl WorkspaceJourney {
    fn new() -> Self {
        let stem =
            std::env::temp_dir().join(format!("ai-kit-app-workspace-{}", uuid::Uuid::new_v4()));
        let root = stem.with_extension("workspace");
        std::fs::create_dir_all(&root).expect("workspace fixture root must be created");
        Self { root }
    }

    fn config(&self, state: StateStore) -> AppConfig<StateStore, Agent> {
        let workflow = WorkflowDefinition::parse(WORKFLOW).expect("fixture workflow must parse");
        AppConfig::new(state, Vec::new(), vec![workflow])
    }

    fn history_config(&self, state: StateStore) -> AppConfig<StateStore, Agent> {
        let workflow = WorkflowDefinition::parse(WORKFLOW).expect("fixture workflow must parse");
        let unused = WorkflowDefinition::parse(&WORKFLOW.replace("workspace-flow", "unused-flow"))
            .expect("configured workflow without retained runs must parse");
        AppConfig::new(state, Vec::new(), vec![workflow, unused])
    }

    fn workspace(&self, name: &str) -> WorkspaceConfig {
        WorkspaceConfig {
            name: Some(name.to_owned()),
            root: self.root.clone(),
        }
    }

    async fn shutdown(app: &mut App<StateStore, Agent>) -> Result<(), AppError> {
        app.shutdown(Shutdown::Drain {
            timeout: Duration::from_secs(10),
        })
        .await
    }

    async fn wait(run: &WorkflowRun<StateStore, Agent>) -> Result<RunSnapshot, AppError> {
        let observed = tokio::time::timeout(Duration::from_secs(10), run.wait()).await;
        observed.map_err(|_| AppError::Timeout("workflow observation timed out".to_owned()))?
    }

    async fn workspace_count(&self, app: &App<StateStore, Agent>) -> Result<usize, AppError> {
        let mut count = 0;
        let mut after = None;
        let limit = NonZeroUsize::new(1_000).expect("the page limit must be nonzero");
        loop {
            let page = app.list_workspaces(after, limit).await?;
            let complete = page.len() < limit.get();
            count += page.len();
            after = page.last().map(|workspace| workspace.id.clone());
            if complete {
                return Ok(count);
            }
        }
    }
}

impl Drop for WorkspaceJourney {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.root);
    }
}

const WORKFLOW: &str = r#"
{
  "schema": "app-kit.workflow/v2",
  "id": "workspace-flow",
  "revision": "v1",
  "state": {"answer": null},
  "required": ["answer"],
  "reducers": {"answer": "reject_concurrent"},
  "entry": "ask",
  "nodes": [
    {
      "id": "ask",
      "step": {
        "kind": "input",
        "message": [{"kind": "text", "text": "Continue?"}],
        "schema": {"type": "string"},
        "target": "answer"
      }
    }
  ],
  "routes": {
    "ask": {"mode": "first", "branches": [{"target": {"kind": "end"}}]}
  }
}
"#;

#[tokio::test]
async fn workspace_configuration_is_prevalidated_revision_fenced_and_reopens_by_identity()
-> Result<(), Box<dyn Error + Send + Sync>> {
    WorkspaceJourney::new()
        .reopen_case(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
        .await
}

#[tokio::test]
async fn lance_state_memory_workspace_revision_fencing_and_reopen()
-> Result<(), Box<dyn Error + Send + Sync>> {
    WorkspaceJourney::new()
        .reopen_case(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
        .await
}

#[tokio::test]
async fn lance_state_directory_workspace_revision_fencing_and_reopen()
-> Result<(), Box<dyn Error + Send + Sync>> {
    let fixture = WorkspaceJourney::new();
    let config =
        StateStoreConfig::Lance(LanceStateStoreConfig::directory(fixture.root.join("state")));
    fixture.reopen_case(config).await
}

impl WorkspaceJourney {
    async fn reopen_case(
        &self,
        config: StateStoreConfig,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        let fixture = self;
        let store = StateStore::connect(config).await?;
        let app = App::open(fixture.config(store.clone())).await?;
        let (root, separator) = (fixture.root.display(), std::path::MAIN_SEPARATOR);
        let mut invalid = fixture.workspace("invalid-root");
        invalid.root = PathBuf::from(format!("{root}{separator}{separator}nested"));
        let invalid_root = app.create_workspace(invalid).await;
        assert!(matches!(invalid_root, Err(AppError::InvalidInput(_))));
        assert_eq!(fixture.workspace_count(&app).await?, 0);

        let workspace = app.create_workspace(fixture.workspace("before")).await?;
        let id: WorkspaceId = workspace.id().clone();
        let initial = workspace.snapshot().await?;
        let workflow_id: WorkflowId = "workspace-flow".parse()?;

        workspace
            .replace(initial.revision, fixture.workspace("after"))
            .await?;
        let stale = workspace
            .replace(initial.revision, fixture.workspace("stale"))
            .await;
        assert!(matches!(stale, Err(AppError::Conflict(_))));

        let workflow = workspace.workflow(workflow_id.clone()).await?;
        let run = workflow.start(ExecutionOptions::default()).await?;
        let input = |snapshot: RunSnapshot| match snapshot.interactions.as_slice() {
            [
                Interaction::Input {
                    sequence,
                    generation,
                    ..
                },
            ] => (*sequence, *generation),
            _ => panic!("the retention workflow must request its configured input"),
        };
        let (sequence, generation) = input(WorkspaceJourney::wait(&run).await?);
        let run_id = run.id().clone();
        drop(app);
        assert_eq!(input(run.snapshot().await?), (sequence, generation));
        drop((run, workflow, workspace));

        let mut app = App::open(fixture.config(store)).await?;
        let workspace = app.workspace(id).await?;
        let workflow = workspace.workflow(workflow_id).await?;
        let mut run = workflow
            .open_run(RunSelector::Latest(run_id.clone()))
            .await?;
        assert_eq!(input(run.snapshot().await?), (sequence, generation));
        let oversized = Value::String("x".repeat(1_048_576));
        let rejected = run
            .resolve(sequence, generation, Resolution::Input(oversized))
            .await;
        assert!(matches!(rejected, Err(AppError::InvalidInput(_))));
        assert_eq!(input(run.snapshot().await?), (sequence, generation));

        let competing = workflow
            .open_run(RunSelector::Latest(run_id.clone()))
            .await?;
        let answer = Value::String("retained".to_owned());
        let (left, right) = tokio::join!(
            run.resolve(sequence, generation, Resolution::Input(answer.clone())),
            competing.resolve(sequence, generation, Resolution::Input(answer)),
        );
        let left = left.expect("first exact decision settles or reattaches");
        let right = right.expect("concurrent identical decision replays the same receipt");
        assert_eq!(left.checkpoint, right.checkpoint);
        assert_eq!(left.state, right.state);
        assert_eq!(left.last_action_ordinal, right.last_action_ordinal);
        assert!(left.interactions.is_empty() && right.interactions.is_empty());
        let terminal = WorkspaceJourney::wait(&run).await?;
        assert!(matches!(terminal.status, RunStatus::Succeeded));
        let retained = workflow.list_runs(None, NonZeroUsize::MIN).await?;
        assert_eq!(retained.len(), 1);
        assert_eq!(retained[0].id, run_id);
        assert!(matches!(retained[0].status, RunStatus::Succeeded));
        let action = run.checkpoint(CheckpointSelector::Action(0)).await?;
        assert_eq!(action.state.values()["answer"], "retained");
        tokio::time::timeout(Duration::from_secs(5), run.delete(terminal.checkpoint))
            .await
            .expect("explicit terminal run deletion must converge after reopen")?;
        assert!(
            workflow
                .list_runs(None, NonZeroUsize::MIN)
                .await?
                .is_empty()
        );
        assert!(matches!(run.snapshot().await, Err(AppError::NotFound(_))));
        WorkspaceJourney::shutdown(&mut app).await?;
        Ok(())
    }
}

#[tokio::test]
async fn recovery_rejects_workspace_inventory_above_the_private_bound()
-> Result<(), Box<dyn Error + Send + Sync>> {
    let fixture = WorkspaceJourney::new();
    let store =
        StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory())).await?;
    let app = App::open(fixture.config(store.clone())).await?;
    let workspace = app
        .create_workspace(fixture.workspace("inventory-template"))
        .await?;
    let source_id = workspace.id().to_string();
    let source_key = StateKey::new("app-kit", format!("workspace/{source_id}"))?;
    let template = store
        .load(source_key)
        .await?
        .expect("the public workspace admission must create its durable record")
        .record
        .value;
    drop(workspace);
    drop(app);

    let mut next = 0_usize;
    while next < 10_000 {
        let end = next.saturating_add(256).min(10_000);
        let mut transaction = store.begin_transaction().await?;
        for index in next..end {
            let id = format!("appws_00000000-0000-4000-8000-{index:012x}");
            assert_ne!(
                id, source_id,
                "the deterministic corruption id must be unique"
            );
            let mut value = template.as_ref().clone();
            *value
                .pointer_mut("/id")
                .expect("the public workspace record must contain its identity") =
                Value::String(id.clone());
            transaction
                .put(StateRecord::new(
                    StateKey::new("app-kit", format!("workspace/{id}"))?,
                    value,
                ))
                .await?;
        }
        transaction.commit().await?;
        next = end;
    }

    let error = match App::open(fixture.config(store)).await {
        Ok(_) => panic!("a durable workspace inventory above the private bound must be rejected"),
        Err(error) => error,
    };
    assert!(matches!(
        error,
        AppError::Integration(ref message)
            if message == "durable workspace inventory exceeds the private bound"
    ));
    Ok(())
}

// This controlled lower-boundary timeout proves App error handling. Native
// Lance admission/deadline guarantees are exercised by Storage's own tests.
struct DeletionTimeoutStore {
    inner: StateStore,
    timeout_once: Arc<AtomicBool>,
}

impl StateStoreBackend for DeletionTimeoutStore {
    type Transaction = StateTransaction;

    async fn load(&self, key: StateKey) -> Result<Option<StateRecordSnapshot>, StateStoreError> {
        self.inner.load(key).await
    }

    async fn list(&self, request: StateListRequest) -> Result<StateListResponse, StateStoreError> {
        self.inner.list(request).await
    }

    async fn begin_transaction(&self) -> Result<Self::Transaction, StateStoreError> {
        if self.timeout_once.swap(false, Ordering::SeqCst) {
            return Err(StateStoreError::timed_out("begin_transaction", 50));
        }
        self.inner.begin_transaction().await
    }
}

#[tokio::test]
async fn workspace_delete_is_fenced_bounded_and_removes_only_app_coordination()
-> Result<(), Box<dyn Error + Send + Sync>> {
    let fixture = WorkspaceJourney::new();
    let timeout_once = Arc::new(AtomicBool::new(false));
    let store =
        StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory())).await?;
    let mut app = App::open(AppConfig::<_, Agent>::new(
        DeletionTimeoutStore {
            inner: store,
            timeout_once: Arc::clone(&timeout_once),
        },
        Vec::new(),
        vec![WorkflowDefinition::parse(WORKFLOW)?],
    ))
    .await?;
    let mut workspace = app.create_workspace(fixture.workspace("delete")).await?;
    let id = workspace.id().clone();
    let revision = workspace.snapshot().await?.revision;
    assert!(matches!(
        workspace.delete(revision + 1).await,
        Err(AppError::Conflict(_))
    ));
    timeout_once.store(true, Ordering::SeqCst);
    let exhausted = tokio::time::timeout(Duration::from_secs(5), workspace.delete(revision)).await;
    assert!(matches!(exhausted, Ok(Err(AppError::Timeout(_)))));
    assert!(
        !timeout_once.load(Ordering::SeqCst),
        "deletion observed the selected lower timeout"
    );
    assert_eq!(workspace.snapshot().await?.revision, revision);
    workspace.delete(revision).await?;
    let missing = app.workspace(id).await;
    assert!(matches!(missing, Err(AppError::NotFound(_))));
    app.shutdown(Shutdown::Drain {
        timeout: Duration::from_secs(10),
    })
    .await?;
    Ok(())
}

#[tokio::test]
async fn workspace_replacement_rejects_run_admitted_during_advisory_preflight() {
    workspace_support::AgentRevisionCase::replacement_rechecks_admission_after_preflight().await;
}

#[tokio::test]
async fn workspace_replacement_fences_run_admitted_before_native_snapshot() {
    workspace_support::AgentRevisionCase::replacement_preserves_fence_across_snapshot(
        workspace_support::AdmissionBoundary::BeforeSnapshot,
    )
    .await;
}

#[tokio::test]
async fn workspace_replacement_fences_run_admitted_after_native_snapshot() {
    workspace_support::AgentRevisionCase::replacement_preserves_fence_across_snapshot(
        workspace_support::AdmissionBoundary::AfterSnapshot,
    )
    .await;
}

#[tokio::test]
async fn workspace_mutations_and_run_admission_share_one_durable_fence()
-> Result<(), Box<dyn Error + Send + Sync>> {
    let fixture = WorkspaceJourney::new();
    let mut app = App::open(
        fixture.config(
            StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
                .await
                .expect("caller memory store"),
        ),
    )
    .await?;
    let workspace = app.create_workspace(fixture.workspace("fenced")).await?;
    let competing = app.workspace(workspace.id().clone()).await?;
    let workflow_id: WorkflowId = "workspace-flow".parse()?;
    let workflow = workspace.workflow(workflow_id.clone()).await?;
    let current = workspace.snapshot().await?;

    let (started, replaced) = tokio::join!(
        workflow.start(ExecutionOptions::default()),
        competing.replace(current.revision, fixture.workspace("replaced")),
    );
    match (started, replaced) {
        (Ok(run), Err(AppError::Busy(_) | AppError::Conflict(_))) => {
            let snapshot = WorkspaceJourney::wait(&run).await?;
            run.cancel(snapshot.checkpoint).await?;
        }
        (Err(AppError::Busy(_) | AppError::Conflict(_)), Ok(_)) => {}
        (started, replaced) => {
            panic!(
                "exactly one workspace mutation or run admission must commit; started={}, replaced={}",
                started.is_ok(),
                replaced.is_ok(),
            );
        }
    }

    let mut workspace = app
        .create_workspace(fixture.workspace("delete-fence"))
        .await?;
    let workflow = workspace.workflow(workflow_id).await?;
    let revision = workspace.snapshot().await?.revision;

    let (started, deleted) = tokio::join!(
        workflow.start(ExecutionOptions::default()),
        workspace.delete(revision)
    );
    match (started, deleted) {
        (
            Err(
                AppError::Busy(_)
                | AppError::Conflict(_)
                | AppError::Closed(_)
                | AppError::NotFound(_),
            ),
            Ok(()),
        ) => {}
        (Ok(run), Err(AppError::Busy(_) | AppError::Conflict(_))) => {
            let snapshot = WorkspaceJourney::wait(&run).await?;
            run.cancel(snapshot.checkpoint).await?;
            workspace.delete(revision).await?;
        }
        (Ok(run), Ok(())) => {
            assert!(matches!(run.snapshot().await?.status, RunStatus::Cancelled));
        }
        (started, deleted) => {
            panic!(
                "workspace deletion race returned an invalid outcome; started={}, deleted={}",
                started.is_ok(),
                deleted.is_ok(),
            );
        }
    }

    WorkspaceJourney::shutdown(&mut app).await?;
    Ok(())
}

#[tokio::test]
async fn accepted_workspace_deletion_outlives_its_observer()
-> Result<(), Box<dyn Error + Send + Sync>> {
    let fixture = WorkspaceJourney::new();
    let mut app = App::open(
        fixture.config(
            StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
                .await
                .expect("caller memory store"),
        ),
    )
    .await?;
    let mut workspace = app
        .create_workspace(fixture.workspace("owned-delete"))
        .await?;
    let observer = app.workspace(workspace.id().clone()).await?;
    let mut reattacher = app.workspace(workspace.id().clone()).await?;
    let workflow_id: WorkflowId = "workspace-flow".parse()?;
    let workflow = observer.workflow(workflow_id).await?;
    let run = workflow.start(ExecutionOptions::default()).await?;
    assert!(matches!(
        WorkspaceJourney::wait(&run).await?.interactions.as_slice(),
        [Interaction::Input { .. }]
    ));
    let revision = workspace.snapshot().await?.revision;

    let seal_deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    let mut deletion = Box::pin(workspace.delete(revision));
    let mut reattached = Box::pin(reattacher.delete(revision));
    let sealed = async {
        loop {
            match observer.snapshot().await {
                Ok(_) => {
                    assert!(
                        tokio::time::Instant::now() < seal_deadline,
                        "workspace deletion must publish its seal in time",
                    );
                    tokio::task::yield_now().await;
                }
                Err(AppError::Closed(_)) => break,
                Err(error) => panic!("workspace seal observation failed: {error}"),
            }
        }
    };
    tokio::pin!(sealed);
    tokio::select! {
        biased;
        () = &mut sealed => {}
        result = &mut deletion => {
            result?;
            panic!("workspace deletion completed before its observer was dropped");
        }
        result = &mut reattached => {
            result?;
            panic!("same-fence reattachment completed before the owned deletion");
        }
    }
    drop(deletion);
    reattached.await?;

    let history_workspace = app.workspace(workspace.id().clone()).await?;
    assert!(matches!(
        history_workspace.snapshot().await,
        Err(AppError::NotFound(_))
    ));
    assert_eq!(fixture.workspace_count(&app).await?, 0);
    let retained = history_workspace
        .workflow("workspace-flow".parse()?)
        .await?
        .open_run(RunSelector::Latest(run.id().clone()))
        .await?;
    assert!(matches!(
        retained.snapshot().await?.status,
        RunStatus::Cancelled
    ));
    WorkspaceJourney::shutdown(&mut app).await?;
    Ok(())
}

#[tokio::test]
async fn compensation_preserves_foreign_revision() -> Result<(), Box<dyn std::error::Error>> {
    workspace_support::CompensationCase::preserves_foreign_change("foreign").await
}

#[tokio::test]
async fn agent_revisions_coexist_and_caller_order_selects_new_work() {
    workspace_support::AgentRevisionCase::caller_order_selects_new_work().await;
}

#[tokio::test]
async fn agent_revision_deployment_preserves_prepared_and_restored_bindings() {
    workspace_support::AgentRevisionCase::deployment_preserves_prepared_and_restored_bindings()
        .await;
}

#[tokio::test]
async fn interrupted_agent_revision_extension_preserves_applied_old_bindings() {
    workspace_support::AgentRevisionCase::interrupted_deployment_preserves_applied_bindings().await;
}

#[tokio::test]
async fn missing_agent_revision_rejects_before_claim_or_lower_effects() {
    workspace_support::AgentRevisionCase::missing_pin_rejects_before_claim_or_lower_effects().await;
}

#[tokio::test]
async fn compensation_preserves_equal_configuration_foreign_revision()
-> Result<(), Box<dyn std::error::Error>> {
    workspace_support::CompensationCase::preserves_foreign_change("next").await
}

#[tokio::test]
async fn compensation_does_not_adopt_foreign_prior_configuration()
-> Result<(), Box<dyn std::error::Error>> {
    workspace_support::CompensationCase::preserves_foreign_change("prior").await
}

#[tokio::test]
async fn replacement_does_not_adopt_foreign_matching_write_after_rejection()
-> Result<(), Box<dyn std::error::Error>> {
    workspace_support::CompensationCase::rejects_foreign_forward_receipt().await
}

#[tokio::test]
async fn workspace_operation_panic_settles_exact_observer_and_cleanup() -> Result<(), Box<dyn Error>>
{
    workspace_support::CompensationCase::operation_panic_is_observed().await
}

#[tokio::test]
async fn deleted_workspace_reopens_only_as_retained_history_without_admission() {
    let fixture = WorkspaceJourney::new();
    let store = StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
        .await
        .unwrap();
    let mut app = App::open(fixture.history_config(store.clone()))
        .await
        .unwrap();
    let observed =
        std::panic::AssertUnwindSafe(tokio::time::timeout(Duration::from_secs(10), async {
            let mut workspace = app
                .create_workspace(fixture.workspace("retained-history"))
                .await
                .unwrap();
            let workspace_id = workspace.id().clone();
            let workspace_revision = workspace.snapshot().await.unwrap().revision;
            let workflow = workspace
                .workflow("workspace-flow".parse().unwrap())
                .await
                .unwrap();
            let run = workflow.start(ExecutionOptions::default()).await.unwrap();
            let waiting = WorkspaceJourney::wait(&run).await.unwrap();
            let [
                Interaction::Input {
                    sequence,
                    generation,
                    ..
                },
            ] = waiting.interactions.as_slice()
            else {
                panic!("real input establishes the retained result");
            };
            run.resolve(
                *sequence,
                *generation,
                Resolution::Input(Value::String("retained".into())),
            )
            .await
            .unwrap();
            let terminal = WorkspaceJourney::wait(&run).await.unwrap();
            assert!(matches!(terminal.status, RunStatus::Succeeded));
            assert_eq!(terminal.state.values()["answer"], "retained");
            let scope = format!(
                "{}/workspace-flow/{}/{}",
                workspace_id,
                run.id(),
                terminal.occurrence
            );
            tokio::time::timeout(Duration::from_secs(3), async {
                loop {
                    let head = store
                        .load(StateKey::new("app-kit", format!("run/{scope}")).unwrap())
                        .await
                        .unwrap()
                        .expect("terminal head retained");
                    if head.record.value.pointer("/retention/state")
                        == Some(&Value::String("retained".into()))
                        && store
                            .load(StateKey::new("app-kit", format!("active/{scope}")).unwrap())
                            .await
                            .unwrap()
                            .is_none()
                    {
                        break;
                    }
                    tokio::time::sleep(Duration::from_millis(2)).await;
                }
            })
            .await
            .expect("terminal execution releases capacity before workspace deletion");
            let history = run
                .history(None, NonZeroUsize::new(1000).unwrap())
                .await
                .unwrap();
            assert!(history.next.is_none());
            assert!(history.checkpoints.len() >= 3);
            assert_eq!(
                history
                    .checkpoints
                    .iter()
                    .filter(|row| matches!(row.kind, CheckpointKind::Action))
                    .count(),
                1
            );
            let retained = history
                .checkpoints
                .into_iter()
                .map(|row| {
                    (
                        row.sequence,
                        row.action_ordinal,
                        row.position,
                        row.state.values().clone(),
                        row.outcomes,
                    )
                })
                .collect::<Vec<_>>();
            workspace.delete(workspace_revision).await.unwrap();
            assert_eq!(fixture.workspace_count(&app).await.unwrap(), 0);
            assert!(
                fixture.root.is_dir(),
                "workspace deletion preserves caller files"
            );
            assert_eq!(
                run.snapshot().await.unwrap().checkpoint,
                terminal.checkpoint
            );
            (
                workspace_id,
                workspace_revision,
                run.id().clone(),
                terminal.checkpoint,
                retained,
            )
        }))
        .catch_unwind()
        .await;
    let closed = tokio::time::timeout(
        Duration::from_secs(5),
        app.shutdown(Shutdown::CancelAndDrain {
            timeout: Duration::from_secs(3),
        }),
    )
    .await;
    drop(app);
    let (workspace_id, workspace_revision, run_id, checkpoint, retained) = match observed {
        Ok(result) => result.expect("workspace deletion and retained history setup are bounded"),
        Err(panic) => std::panic::resume_unwind(panic),
    };
    closed.expect("old App joins").expect("old App cleanup");

    let mut fresh = tokio::time::timeout(
        Duration::from_secs(5),
        App::open(fixture.history_config(store.clone())),
    )
    .await
    .expect("fresh App admission is bounded")
    .expect("retained history survives App reopen");
    let mut orphaned_head = None;
    let observed =
        std::panic::AssertUnwindSafe(tokio::time::timeout(Duration::from_secs(10), async {
            assert_eq!(fixture.workspace_count(&fresh).await.unwrap(), 0);
            let missing: WorkspaceId = format!("appws_{}", uuid::Uuid::new_v4())
                .parse()
                .unwrap();
            assert!(matches!(
                fresh.workspace(missing).await,
                Err(AppError::NotFound(_))
            ));
            let workspace = fresh
                .workspace(workspace_id.clone())
                .await
                .expect("retained run heads prove the deleted workspace's history scope");
            assert!(matches!(
                workspace.snapshot().await,
                Err(AppError::NotFound(_) | AppError::Closed(_))
            ));
            assert!(matches!(
                workspace
                    .replace(workspace_revision, fixture.workspace("forbidden-replace"))
                    .await,
                Err(AppError::NotFound(_) | AppError::Closed(_))
            ));
            assert!(
                matches!(
                    workspace.workflow("unused-flow".parse().unwrap()).await,
                    Err(AppError::NotFound(_))
                ),
                "a configured definition without retained runs cannot borrow another workflow's history scope"
            );
            let workflow = workspace
                .workflow("workspace-flow".parse().unwrap())
                .await
                .unwrap();
            let run = workflow.open_run(RunSelector::Latest(run_id)).await.unwrap();
            let snapshot = run.snapshot().await.unwrap();
            assert!(matches!(snapshot.status, RunStatus::Succeeded));
            assert_eq!(snapshot.checkpoint, checkpoint);
            assert_eq!(snapshot.state.values()["answer"], "retained");
            let action = run.checkpoint(CheckpointSelector::Action(0)).await.unwrap();
            assert!(matches!(action.kind, CheckpointKind::Action));
            assert_eq!(action.position.as_deref(), Some("ask"));
            assert_eq!(action.state.values()["answer"], "retained");
            assert_eq!(action.outcomes.len(), 1);
            let history = run
                .history(None, NonZeroUsize::new(1000).unwrap())
                .await
                .unwrap();
            assert!(history.next.is_none());
            assert_eq!(
                history
                    .checkpoints
                    .iter()
                    .filter(|row| matches!(row.kind, CheckpointKind::Action))
                    .count(),
                1
            );
            let actual = history
                .checkpoints
                .into_iter()
                .map(|row| {
                    (
                        row.sequence,
                        row.action_ordinal,
                        row.position,
                        row.state.values().clone(),
                        row.outcomes,
                    )
                })
                .collect::<Vec<_>>();
            assert!(
                actual == retained,
                "deleted workspace keeps exact immutable run history"
            );
            assert!(matches!(
                workflow.start(ExecutionOptions::default()).await,
                Err(AppError::NotFound(_) | AppError::Closed(_))
            ));
            let initial =
                WorkflowState::new(Map::from_iter([("answer".into(), Value::Null)])).unwrap();
            assert!(matches!(
                workflow.prepare(initial).await,
                Err(AppError::NotFound(_) | AppError::Closed(_))
            ));
            assert!(matches!(
                workflow
                    .restore(RestoreOptions {
                        source: RunSelector::Latest(run.id().clone()),
                        checkpoint: CheckpointSelector::Action(0),
                        overrides: WorkflowState::default(),
                        position: ResumePosition::AfterCompleted,
                        fallback: None,
                    })
                    .await,
                Err(AppError::NotFound(_) | AppError::Closed(_))
            ));
            let listed = workflow.list_runs(None, NonZeroUsize::MIN).await.unwrap();
            assert_eq!(listed.len(), 1);
            assert_eq!(&listed[0].id, run.id());
            assert!(matches!(listed[0].status, RunStatus::Succeeded));
            assert_eq!(run.snapshot().await.unwrap().checkpoint, checkpoint);
            assert_eq!(fixture.workspace_count(&fresh).await.unwrap(), 0);
            assert!(
                store
                    .load(StateKey::new("app-kit", format!("workspace/{workspace_id}")).unwrap())
                    .await
                    .unwrap()
                    .is_none(),
                "read-only attachment and rejected admission never recreate workspace coordination"
            );
            let unchanged = run
                .history(None, NonZeroUsize::new(1000).unwrap())
                .await
                .unwrap();
            assert!(unchanged.next.is_none());
            assert!(
                unchanged
                    .checkpoints
                    .into_iter()
                    .map(|row| (
                        row.sequence,
                        row.action_ordinal,
                        row.position,
                        row.state.values().clone(),
                        row.outcomes,
                    ))
                    .collect::<Vec<_>>()
                    == retained,
                "history reads and rejected start, prepare and restore produce no new action or state effects"
            );

            // An orphaned active head is not authority to recreate a history
            // scope. Restore the exact original bytes outside the timed body.
            let scope = format!("{}/workspace-flow/{}/{}", workspace_id, run.id(), snapshot.occurrence);
            let head = store
                .load(StateKey::new("app-kit", format!("run/{scope}")).unwrap())
                .await
                .unwrap()
                .unwrap()
                .record;
            let marker = StateKey::new("app-kit", format!("active/{scope}")).unwrap();
            assert!(store.load(marker.clone()).await.unwrap().is_none());
            assert_eq!(
                head.value.pointer("/retention/state"),
                Some(&Value::String("retained".into()))
            );
            orphaned_head = Some((head.clone(), marker.clone()));
            let mut active = head;
            Arc::make_mut(&mut active.value)["retention"] = serde_json::json!({"state":"active"});
            let mut transaction = store.begin_transaction().await.unwrap();
            transaction
                .put(StateRecord::new(marker, serde_json::json!({"epoch":active.value["epoch"]})))
                .await
                .unwrap();
            transaction.put(active).await.unwrap();
            transaction.commit().await.unwrap();
            assert!(
                matches!(
                    fresh.workspace(workspace_id).await,
                    Err(AppError::NotFound(_) | AppError::Closed(_) | AppError::Integration(_))
                ),
                "missing coordination plus an active head cannot grant a history-only scope"
            );
        }))
        .catch_unwind()
        .await;
    let repaired = match orphaned_head {
        Some((head, marker)) => Some(
            tokio::time::timeout(Duration::from_secs(5), async {
                let mut transaction = store.begin_transaction().await?;
                transaction.put(head).await?;
                transaction.delete(marker).await?;
                transaction.commit().await
            })
            .await,
        ),
        None => None,
    };
    let closed = tokio::time::timeout(
        Duration::from_secs(5),
        fresh.shutdown(Shutdown::CancelAndDrain {
            timeout: Duration::from_secs(3),
        }),
    )
    .await;
    drop(fresh);
    if let Some(repaired) = repaired {
        repaired
            .expect("fault restoration is bounded")
            .expect("exact head restored before cleanup completes");
    }
    match observed {
        Ok(result) => result.expect("history-only scope observations remain bounded"),
        Err(panic) => std::panic::resume_unwind(panic),
    }
    closed.expect("fresh App joins").expect("fresh App cleanup");
}

#[tokio::test]
async fn interrupted_agent_deployment_rejects_pending_and_unknown_required_old_pairs() {
    workspace_support::AgentRevisionCase::interrupted_deployment_rejects_unproven_bindings().await;
}

#[tokio::test]
async fn prepared_parent_pins_transitive_agent_children_across_revision_deployment() {
    workspace_support::AgentRevisionCase::transitive_children_preserve_agent_bindings().await;
}