bcode_server 0.0.1-alpha.0

Local bcode daemon runtime
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
use bcode_metrics::{MetricLabels, MetricsRegistry};
use bcode_plugin::PluginInvocationCancelHandle;
use bcode_session::SessionOwnershipGuard;
pub use bcode_session_models::RuntimeWorkObservedStage as ObservedWorkStage;
use bcode_session_models::{RuntimeWorkKind, RuntimeWorkStatus, SessionId, WorkId};
use bcode_session_models::{RuntimeWorkObservation, RuntimeWorkSnapshot};
use std::collections::BTreeMap;
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::TurnCancelState;

/// Server-owned cancellation handle for active runtime work.
#[derive(Debug, Clone)]
pub enum CancellationHandle {
    /// Cancel a whole session/model turn.
    SessionTurn(Arc<TurnCancelState>),
    /// Cancel an in-flight plugin invocation.
    PluginInvocation(PluginInvocationCancelHandle),
    /// Cancel a typed application service operation.
    ServiceCall(bcode_plugin_sdk::ServiceCancellation),
    /// Cancel a Ralph autonomous runner.
    RalphRun {
        /// Store containing the run record.
        store: bcode_ralph::RalphStateStore,
        /// Run ID to mark cancellation-requested.
        run_id: String,
    },
    /// Cancel a server-hosted durable workflow run.
    WorkflowRun(crate::WorkflowRunCancellationHandle),
    /// Server-hosted workflow node whose active model turn is routed by a child handle.
    WorkflowNode(Arc<TurnCancelState>),
    /// Test/no-op cancellation hook.
    #[cfg(test)]
    Test(Arc<std::sync::atomic::AtomicUsize>),
    /// Test cancellation hook that never returns.
    #[cfg(test)]
    TestBlocked(Arc<tokio::sync::Notify>),
    /// Test cancellation hook that reports cleanup failure.
    #[cfg(test)]
    TestFailed(Arc<std::sync::atomic::AtomicUsize>),
}

impl CancellationHandle {
    /// Request cancellation through the underlying handle.
    pub async fn cancel(&self) -> Result<(), String> {
        match self {
            Self::SessionTurn(cancel_state) | Self::WorkflowNode(cancel_state) => {
                cancel_state.cancel().await;
                Ok(())
            }
            Self::ServiceCall(cancellation) => {
                cancellation.cancel();
                Ok(())
            }
            Self::PluginInvocation(cancel) => {
                cancel.cancel();
                Ok(())
            }
            Self::RalphRun { store, run_id } => store
                .request_run_cancel(run_id)
                .map_err(|error| error.to_string()),
            Self::WorkflowRun(cancellation) => cancellation.cancel().await,
            #[cfg(test)]
            Self::Test(count) => {
                count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Ok(())
            }
            #[cfg(test)]
            Self::TestBlocked(blocked) => {
                blocked.notified().await;
                Ok(())
            }
            #[cfg(test)]
            Self::TestFailed(count) => {
                count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Err("test cleanup failed".to_string())
            }
        }
    }

    /// Runtime work with a handle is cancellable.
    pub const fn is_cancellable(&self) -> bool {
        match self {
            Self::SessionTurn(_)
            | Self::ServiceCall(_)
            | Self::PluginInvocation(_)
            | Self::RalphRun { .. }
            | Self::WorkflowRun(_)
            | Self::WorkflowNode(_) => true,
            #[cfg(test)]
            Self::Test(_) | Self::TestBlocked(_) | Self::TestFailed(_) => true,
        }
    }
}

/// Specification for starting runtime work.
#[derive(Debug, Clone)]
pub struct RuntimeWorkSpec {
    pub work_id: WorkId,
    pub kind: RuntimeWorkKind,
    pub label: String,
    pub tool_call_id: Option<String>,
    pub plugin_id: Option<String>,
    pub service_interface: Option<String>,
    pub operation: Option<String>,
    pub parent_work_id: Option<WorkId>,
    pub cancellation: CancellationHandle,
    pub ownership: Option<SessionOwnershipGuard>,
}

impl RuntimeWorkSpec {
    #[allow(clippy::missing_const_for_fn)]
    pub fn new(
        work_id: WorkId,
        kind: RuntimeWorkKind,
        label: String,
        cancellation: CancellationHandle,
    ) -> Self {
        Self {
            work_id,
            kind,
            label,
            tool_call_id: None,
            plugin_id: None,
            service_interface: None,
            operation: None,
            parent_work_id: None,
            cancellation,
            ownership: None,
        }
    }

    pub fn with_tool_call_id(mut self, tool_call_id: Option<String>) -> Self {
        self.tool_call_id = tool_call_id;
        self
    }

    pub fn with_parent_work_id(mut self, parent_work_id: Option<WorkId>) -> Self {
        self.parent_work_id = parent_work_id;
        self
    }
}

/// Capability bound to one registration and one observed stage revision.
#[derive(Debug, Clone)]
pub struct WorkObservationToken {
    session_id: SessionId,
    work_id: WorkId,
    owner: Arc<()>,
    revision: Arc<()>,
}

/// Bounded local observation, not execution authority or durable history.
#[derive(Debug)]
pub struct WorkObservation {
    pub work_id: WorkId,
    pub stage: Option<ObservedWorkStage>,
    pub elapsed: Option<std::time::Duration>,
    pub cancellation_requested: bool,
}

/// Live pagination is best effort; removed/replaced entries require a fresh scan.
#[derive(Debug)]
pub struct WorkObservationPage {
    pub entries: Vec<WorkObservation>,
    pub next_after: Option<WorkId>,
}

#[derive(Debug, Clone)]
struct ActiveRuntimeWork {
    observation_owner: Arc<()>,
    observation_revision: Arc<()>,
    stage: Option<(ObservedWorkStage, std::time::Instant)>,
    spec: RuntimeWorkSpec,
    cancelled: bool,
    shared_execution: Option<std::sync::Weak<bcode_session::SharedExecutionSessionObservation>>,
}

/// Active runtime-work identity returned by recursive cancellation routing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CancelledRuntimeWork {
    pub session_id: SessionId,
    pub work_id: WorkId,
}

/// Central server registry and cancellation router for active runtime work.
#[derive(Debug, Default)]
pub struct RuntimeWorkManager {
    active: Mutex<BTreeMap<(SessionId, WorkId), ActiveRuntimeWork>>,
    metrics: MetricsRegistry,
}

impl RuntimeWorkManager {
    /// Capture only an existing local registration; never admit missing work.
    pub(super) async fn observation_token(
        &self,
        session_id: SessionId,
        work_id: &WorkId,
    ) -> Option<WorkObservationToken> {
        let active = self.active.lock().await;
        let work = active.get(&(session_id, work_id.clone()))?;
        let result = Some(WorkObservationToken {
            session_id,
            work_id: work_id.clone(),
            owner: Arc::clone(&work.observation_owner),
            revision: Arc::clone(&work.observation_revision),
        });
        drop(active);
        result
    }

    /// Advance the observation only while both owner and revision still match.
    /// Returned token supersedes the old token, fencing delayed stage updates.
    pub(super) async fn observe_stage(
        &self,
        token: &WorkObservationToken,
        stage: ObservedWorkStage,
    ) -> Option<WorkObservationToken> {
        let mut active = self.active.lock().await;
        let work = active.get_mut(&(token.session_id, token.work_id.clone()))?;
        if !Arc::ptr_eq(&token.owner, &work.observation_owner)
            || !Arc::ptr_eq(&token.revision, &work.observation_revision)
        {
            return None;
        }
        work.observation_revision = Arc::new(());
        work.stage = Some((stage, std::time::Instant::now()));
        let result = Some(WorkObservationToken {
            session_id: token.session_id,
            work_id: token.work_id.clone(),
            owner: Arc::clone(&work.observation_owner),
            revision: Arc::clone(&work.observation_revision),
        });
        drop(active);
        result
    }

    /// Observe at most 128 entries through an ordered per-session range.
    /// No labels, arguments, durable rows or foreign owners are inspected.
    pub(super) async fn observation_page(
        &self,
        session_id: SessionId,
        after: Option<&WorkId>,
        limit: usize,
    ) -> WorkObservationPage {
        use std::ops::Bound::{Excluded, Included, Unbounded};
        let limit = limit.clamp(1, 128);
        let active = self.active.lock().await;
        let lower = after.map_or_else(
            || Included((session_id, WorkId::new(""))),
            |id| Excluded((session_id, id.clone())),
        );
        let mut rows = active
            .range((lower, Unbounded))
            .take_while(|((session, _), _)| *session == session_id);
        let now = std::time::Instant::now();
        let entries: Vec<_> = rows
            .by_ref()
            .take(limit)
            .map(|((_, id), work)| WorkObservation {
                work_id: id.clone(),
                stage: work.stage.map(|(stage, _)| stage),
                elapsed: work
                    .stage
                    .map(|(_, started)| now.saturating_duration_since(started)),
                cancellation_requested: work.cancelled,
            })
            .collect();
        let next_after = rows
            .next()
            .and_then(|_| entries.last().map(|entry| entry.work_id.clone()));
        drop(active);
        WorkObservationPage {
            entries,
            next_after,
        }
    }

    /// Create a runtime work manager with aggregate cleanup diagnostics.
    #[must_use]
    pub fn with_metrics(metrics: MetricsRegistry) -> Self {
        Self {
            active: Mutex::default(),
            metrics,
        }
    }

    /// Register active work and return whether it should be advertised as cancellable.
    ///
    /// The boolean tuple is `(cancellable, newly_registered)`. Re-registering an existing exact
    /// identity replaces its cancellation handle without creating another durable start event.
    pub async fn start(&self, session_id: SessionId, spec: RuntimeWorkSpec) -> (bool, bool) {
        let cancellable = spec.cancellation.is_cancellable();
        let mut active = self.active.lock().await;
        let inherited_cancellation = spec.parent_work_id.as_ref().is_some_and(|parent| {
            active
                .get(&(session_id, parent.clone()))
                .is_some_and(|work| work.cancelled)
        });
        let prior_cancellation = active
            .get(&(session_id, spec.work_id.clone()))
            .is_some_and(|work| work.cancelled);
        let cancelled = inherited_cancellation || prior_cancellation;
        let cancellation = spec.cancellation.clone();
        let replaced = active.insert(
            (session_id, spec.work_id.clone()),
            ActiveRuntimeWork {
                observation_owner: Arc::new(()),
                observation_revision: Arc::new(()),
                stage: None,
                spec,
                cancelled,
                shared_execution: None,
            },
        );
        drop(active);
        if cancelled {
            if let CancellationHandle::ServiceCall(token) = &cancellation {
                token.cancel();
            } else {
                tokio::spawn(async move {
                    let _ = cancellation.cancel().await;
                });
            }
        }
        (cancellable, replaced.is_none())
    }

    /// Bind a held sequential permit to an exact active workflow dispatch.
    pub async fn bind_shared_execution(
        &self,
        session_id: SessionId,
        work_id: &WorkId,
        permit: &bcode_session::SharedExecutionSessionPermit,
    ) -> bool {
        if permit.session_id() != session_id {
            return false;
        }
        let mut active = self.active.lock().await;
        let Some(work) = active.get_mut(&(session_id, work_id.clone())) else {
            return false;
        };
        if work.spec.kind != RuntimeWorkKind::WorkflowNode {
            return false;
        }
        work.shared_execution = Some(permit.observation());
        drop(active);
        true
    }

    /// Verify exact shared-parent provenance while both dispatch and permit remain active.
    pub async fn observes_shared_execution(
        &self,
        session_id: SessionId,
        link: &bcode_workflow_store::WorkflowExecutionSessionLink,
    ) -> bool {
        if link.session_id != session_id.to_string() {
            return false;
        }
        let identity = bcode_workflow_store::dispatch_identity(
            &link.run_id,
            &link.node_id,
            &link.activation_id,
            link.attempt,
        );
        let active = self.active.lock().await;
        let Some(work) = active.get(&(session_id, WorkId::new(identity))) else {
            return false;
        };
        let verified = work.spec.kind == RuntimeWorkKind::WorkflowNode
            && work.spec.parent_work_id == Some(WorkId::new(format!("workflow:{}", link.run_id)))
            && work
                .shared_execution
                .as_ref()
                .and_then(std::sync::Weak::upgrade)
                .is_some_and(|observation| {
                    let Some(proof) = observation.provenance() else {
                        return false;
                    };
                    proof.context_mode
                        == bcode_session_models::ExecutionSessionContextMode::SharedSequential
                        && proof.parent_session_id == session_id
                        && proof.run_id == link.run_id
                        && proof.node_id == link.node_id
                        && proof.activation_id.as_deref() == Some(link.activation_id.as_str())
                        && proof.attempt == link.attempt
                        && proof.workspace_snapshot.as_deref()
                            == Some(link.workspace_snapshot.as_str())
                });
        drop(active);
        verified
    }

    /// Replace the cancellation handle for existing active work.
    pub async fn replace_cancellation(
        &self,
        session_id: SessionId,
        work_id: &WorkId,
        cancellation: CancellationHandle,
    ) -> bool {
        let mut active = self.active.lock().await;
        let replaced = if let Some(work) = active.get_mut(&(session_id, work_id.clone())) {
            work.spec.cancellation = cancellation;
            true
        } else {
            false
        };
        drop(active);
        replaced
    }

    /// Cancel active work by exact ID across sessions and return every work item newly signalled.
    pub async fn cancel_global_with_children(&self, work_id: &WorkId) -> Vec<CancelledRuntimeWork> {
        let session_ids = self
            .active
            .lock()
            .await
            .keys()
            .filter(|(_, active_work_id)| active_work_id == work_id)
            .map(|(session_id, _)| *session_id)
            .collect::<Vec<_>>();
        let mut cancelled = Vec::new();
        for session_id in session_ids {
            cancelled.extend(
                self.cancel_with_children(session_id, work_id)
                    .await
                    .into_iter()
                    .map(|work_id| CancelledRuntimeWork {
                        session_id,
                        work_id,
                    }),
            );
        }
        cancelled
    }

    /// Cancel active work by exact ID and return every work item newly signalled.
    pub async fn cancel_with_children(
        &self,
        session_id: SessionId,
        work_id: &WorkId,
    ) -> Vec<WorkId> {
        let mut active = self.active.lock().await;
        if !active.contains_key(&(session_id, work_id.clone())) {
            return Vec::new();
        }
        let mut pending = vec![work_id.clone()];
        let mut cancellations = Vec::new();
        let mut cancelled_work_ids = Vec::new();
        while let Some(next_work_id) = pending.pop() {
            let key = (session_id, next_work_id.clone());
            let Some(work) = active.get_mut(&key) else {
                continue;
            };
            if work.cancelled {
                continue;
            }
            work.cancelled = true;
            cancelled_work_ids.push(next_work_id.clone());
            cancellations.push((
                next_work_id.clone(),
                work.spec.kind,
                work.spec.cancellation.clone(),
            ));
            let children = active
                .iter()
                .filter(|((child_session_id, _), child)| {
                    *child_session_id == session_id
                        && child.spec.parent_work_id.as_ref() == Some(&next_work_id)
                })
                .map(|((_, child_work_id), _)| child_work_id.clone())
                .collect::<Vec<_>>();
            pending.extend(children);
        }
        drop(active);
        for (cleanup_work_id, kind, cancellation) in cancellations {
            let metrics = self.metrics.clone();
            tokio::spawn(async move {
                let started_at = std::time::Instant::now();
                let result = cancellation.cancel().await;
                let outcome = if result.is_ok() {
                    "completed"
                } else {
                    "failed"
                };
                let mut labels = MetricLabels::new();
                labels.insert(
                    "work_kind".to_owned(),
                    runtime_work_kind_label(kind).to_owned(),
                );
                labels.insert("outcome".to_owned(), outcome.to_owned());
                metrics.add_counter_with_labels("runtime_work.cleanup_total", 1, labels.clone());
                metrics.record_histogram_with_labels(
                    "runtime_work.cleanup_duration_ms",
                    u64::try_from(started_at.elapsed().as_millis()).unwrap_or(u64::MAX),
                    labels,
                );
                match result {
                    Ok(()) => tracing::debug!(
                        target: "bcode_server::cleanup",
                        %session_id,
                        work_id = %cleanup_work_id,
                        "detached runtime cleanup completed"
                    ),
                    Err(error) => tracing::warn!(
                        target: "bcode_server::cleanup",
                        %session_id,
                        work_id = %cleanup_work_id,
                        %error,
                        "detached runtime cleanup failed"
                    ),
                }
            });
        }
        cancelled_work_ids
    }

    /// Return active work identifiers for a session.
    pub async fn active_ids_for_session(
        &self,
        session_id: SessionId,
    ) -> std::collections::BTreeSet<WorkId> {
        self.active
            .lock()
            .await
            .keys()
            .filter_map(|(active_session_id, work_id)| {
                (*active_session_id == session_id).then_some(work_id.clone())
            })
            .collect()
    }

    /// Retire only the registration generation captured by this token.
    ///
    /// Stage revisions do not affect retirement authority: an older-stage token
    /// may retire its same owner, but can never remove a replacement owner.
    /// Missing or stale generations return `None` without changing live work.
    pub(super) async fn take_observed(
        &self,
        token: &WorkObservationToken,
    ) -> Option<RuntimeWorkSpec> {
        let mut active = self.active.lock().await;
        let key = (token.session_id, token.work_id.clone());
        let matches_owner = active
            .get(&key)
            .is_some_and(|work| Arc::ptr_eq(&token.owner, &work.observation_owner));
        let owned = if matches_owner {
            active.remove(&key).map(|work| work.spec)
        } else {
            None
        };
        drop(active);
        owned
    }

    /// Remove active work and return the owned registration, if present.
    pub async fn take(&self, session_id: SessionId, work_id: &WorkId) -> Option<RuntimeWorkSpec> {
        self.active
            .lock()
            .await
            .remove(&(session_id, work_id.clone()))
            .map(|work| work.spec)
    }

    /// Remove active work and return whether this manager owned it.
    pub async fn finish(&self, session_id: SessionId, work_id: &WorkId) -> bool {
        self.take(session_id, work_id).await.is_some()
    }

    /// Return the number of active work registrations across all sessions.
    pub async fn active_count(&self) -> usize {
        self.active.lock().await.len()
    }

    /// Return active work snapshots across every session, tagged with their owning session.
    pub async fn active_all(&self) -> Vec<(SessionId, RuntimeWorkSnapshot)> {
        let active = self.active.lock().await;
        let now = std::time::Instant::now();
        let snapshots = active
            .iter()
            .map(|((session_id, work_id), work)| {
                (
                    *session_id,
                    RuntimeWorkSnapshot {
                        work_id: work_id.clone(),
                        kind: work.spec.kind,
                        label: work.spec.label.clone(),
                        tool_call_id: work.spec.tool_call_id.clone(),
                        status: if work.cancelled {
                            RuntimeWorkStatus::Cancelling
                        } else {
                            RuntimeWorkStatus::Running
                        },
                        observation: observed_work_at(work, now),
                        cancellable: work.spec.cancellation.is_cancellable(),
                    },
                )
            })
            .collect();
        drop(active);
        snapshots
    }

    /// Return active work snapshots for a session.
    /// Count observed local work within one session key range, bounded by `budget`.
    /// The completeness flag is false when more work exists than the observation budget.
    pub async fn activity_count_for_session(
        &self,
        session_id: SessionId,
        budget: u32,
    ) -> (u32, bool) {
        let active = self.active.lock().await;
        let result = {
            let mut rows = active
                .range((session_id, WorkId::new(""))..)
                .take_while(|((id, _), _)| *id == session_id);
            let mut count = 0;
            while count < budget && rows.next().is_some() {
                count += 1;
            }
            (count, rows.next().is_none())
        };
        drop(active);
        result
    }

    pub async fn active_for_session(&self, session_id: SessionId) -> Vec<RuntimeWorkSnapshot> {
        let active = self.active.lock().await;
        let now = std::time::Instant::now();
        let snapshots = active
            .iter()
            .filter(|((active_session_id, _), _)| *active_session_id == session_id)
            .map(|((_, work_id), work)| RuntimeWorkSnapshot {
                work_id: work_id.clone(),
                kind: work.spec.kind,
                label: work.spec.label.clone(),
                tool_call_id: work.spec.tool_call_id.clone(),
                status: if work.cancelled {
                    RuntimeWorkStatus::Cancelling
                } else {
                    RuntimeWorkStatus::Running
                },
                observation: observed_work_at(work, now),
                cancellable: work.spec.cancellation.is_cancellable(),
            })
            .collect();
        drop(active);
        snapshots
    }
}

fn observed_work_at(
    work: &ActiveRuntimeWork,
    now: std::time::Instant,
) -> Option<RuntimeWorkObservation> {
    work.stage.map(|(stage, started)| RuntimeWorkObservation {
        stage,
        elapsed_ms: u64::try_from(now.saturating_duration_since(started).as_millis())
            .unwrap_or(u64::MAX),
    })
}

const fn runtime_work_kind_label(kind: RuntimeWorkKind) -> &'static str {
    match kind {
        RuntimeWorkKind::ModelTurn => "model_turn",
        RuntimeWorkKind::Tool => "tool",
        RuntimeWorkKind::PluginInvocation => "plugin_invocation",
        RuntimeWorkKind::EventDelivery => "event_delivery",
        RuntimeWorkKind::Workflow => "workflow",
        RuntimeWorkKind::WorkflowNode => "workflow_node",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    async fn start_observation_fixture(
        manager: &RuntimeWorkManager,
        session: SessionId,
        id: &WorkId,
    ) {
        manager
            .start(
                session,
                RuntimeWorkSpec::new(
                    id.clone(),
                    RuntimeWorkKind::PluginInvocation,
                    "not diagnostic content".into(),
                    CancellationHandle::Test(Arc::new(AtomicUsize::new(0))),
                ),
            )
            .await;
    }

    #[tokio::test]
    async fn observation_stages_survive_cancellation_until_retirement() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        let id = WorkId::new("observed");
        assert!(manager.observation_token(session, &id).await.is_none());
        start_observation_fixture(&manager, session, &id).await;
        let initial = manager.observation_page(session, None, 1).await;
        assert_eq!(initial.entries[0].stage, None);
        assert_eq!(initial.entries[0].elapsed, None);
        let mut token = manager.observation_token(session, &id).await.unwrap();
        for stage in [
            ObservedWorkStage::Dispatch,
            ObservedWorkStage::ReceiptWait,
            ObservedWorkStage::Cleanup,
        ] {
            token = manager.observe_stage(&token, stage).await.unwrap();
        }
        manager.cancel_with_children(session, &id).await;
        let cancelled = manager.observation_page(session, None, 1).await;
        assert_eq!(cancelled.entries[0].stage, Some(ObservedWorkStage::Cleanup));
        assert!(cancelled.entries[0].elapsed.is_some());
        assert!(cancelled.entries[0].cancellation_requested);
        let session_snapshot = manager.active_for_session(session).await;
        let all_snapshot = manager.active_all().await;
        assert_eq!(
            session_snapshot[0].observation.as_ref().unwrap().stage,
            ObservedWorkStage::Cleanup
        );
        assert_eq!(
            all_snapshot[0].1.observation.as_ref().unwrap().stage,
            ObservedWorkStage::Cleanup
        );
        assert_eq!(session_snapshot[0].status, RuntimeWorkStatus::Cancelling);
        token = manager
            .observe_stage(&token, ObservedWorkStage::Retirement)
            .await
            .unwrap();
        assert!(manager.finish(session, &id).await);
        assert!(
            manager
                .observe_stage(&token, ObservedWorkStage::Dispatch)
                .await
                .is_none()
        );
        assert!(
            manager
                .observation_page(session, None, 1)
                .await
                .entries
                .is_empty()
        );
    }

    #[tokio::test]
    async fn observation_tokens_fence_stale_revisions_and_replaced_owners() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        let id = WorkId::new("replaced");
        start_observation_fixture(&manager, session, &id).await;
        let old = manager.observation_token(session, &id).await.unwrap();
        let current = manager
            .observe_stage(&old, ObservedWorkStage::Cleanup)
            .await
            .unwrap();
        assert!(
            manager
                .observe_stage(&old, ObservedWorkStage::Dispatch)
                .await
                .is_none()
        );
        start_observation_fixture(&manager, session, &id).await;
        assert!(
            manager
                .observe_stage(&current, ObservedWorkStage::Retirement)
                .await
                .is_none()
        );
        assert_eq!(
            manager.observation_page(session, None, 1).await.entries[0].stage,
            None
        );
    }

    #[tokio::test]
    async fn observed_retirement_rejects_replaced_owner_without_removing_replacement() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        let id = WorkId::new("retirement-generation");
        start_observation_fixture(&manager, session, &id).await;
        let original = manager.observation_token(session, &id).await.unwrap();
        start_observation_fixture(&manager, session, &id).await;
        let replacement = manager.observation_token(session, &id).await.unwrap();
        assert!(manager.take_observed(&original).await.is_none());
        assert_eq!(manager.active_for_session(session).await.len(), 1);
        assert!(manager.take_observed(&replacement).await.is_some());
        assert!(manager.take_observed(&original).await.is_none());
        assert!(manager.take_observed(&replacement).await.is_none());
    }

    #[tokio::test]
    async fn observed_retirement_accepts_same_owner_with_expired_stage_revision() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        let id = WorkId::new("retirement-revision");
        start_observation_fixture(&manager, session, &id).await;
        let original = manager.observation_token(session, &id).await.unwrap();
        let current = manager
            .observe_stage(&original, ObservedWorkStage::Retirement)
            .await
            .unwrap();
        assert!(
            manager
                .observe_stage(&original, ObservedWorkStage::Dispatch)
                .await
                .is_none()
        );
        let retired = manager.take_observed(&original).await.unwrap();
        assert_eq!(retired.work_id, id);
        assert!(manager.take_observed(&current).await.is_none());
        assert!(manager.active_for_session(session).await.is_empty());
    }

    #[tokio::test]
    async fn observation_pages_are_bounded_session_scoped_and_exclusive() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        for index in 0..130 {
            start_observation_fixture(&manager, session, &WorkId::new(format!("work-{index:03}")))
                .await;
        }
        start_observation_fixture(&manager, SessionId::new(), &WorkId::new("foreign")).await;
        let page = manager.observation_page(session, None, usize::MAX).await;
        assert_eq!(page.entries.len(), 128);
        let cursor = page.next_after.unwrap();
        let tail = manager.observation_page(session, Some(&cursor), 128).await;
        assert_eq!(tail.entries.len(), 2);
        assert!(tail.entries.iter().all(|entry| entry.work_id > cursor));
        assert!(tail.next_after.is_none());
        assert_eq!(
            manager
                .observation_page(session, None, 0)
                .await
                .entries
                .len(),
            1
        );
    }

    #[tokio::test]
    async fn typed_service_cancellation_remains_routable_until_settlement() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let parent = WorkId::new("model-parent");
        let child = WorkId::new("auxiliary-child");
        let cancellation = bcode_plugin_sdk::ServiceCancellation::default();
        manager
            .start(
                session_id,
                RuntimeWorkSpec::new(
                    parent.clone(),
                    RuntimeWorkKind::ModelTurn,
                    "parent".into(),
                    CancellationHandle::Test(Arc::new(AtomicUsize::new(0))),
                ),
            )
            .await;
        manager
            .start(
                session_id,
                RuntimeWorkSpec::new(
                    child.clone(),
                    RuntimeWorkKind::PluginInvocation,
                    "auxiliary".into(),
                    CancellationHandle::ServiceCall(cancellation.clone()),
                )
                .with_parent_work_id(Some(parent.clone())),
            )
            .await;
        assert!(!cancellation.is_cancelled());
        let cancelled = manager.cancel_with_children(session_id, &parent).await;
        assert!(cancelled.contains(&child));
        tokio::time::timeout(std::time::Duration::from_secs(1), async {
            while !cancellation.is_cancelled() {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("detached cancellation delivery");
        assert!(cancellation.is_cancelled());
        assert_eq!(manager.active_count().await, 2);
        assert!(manager.finish(session_id, &child).await);
        assert!(manager.finish(session_id, &parent).await);
        assert_eq!(manager.active_count().await, 0);
    }

    #[tokio::test]
    async fn active_ids_are_removed_exactly_once_on_finish() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let work_id = WorkId::new("model-turn");
        manager
            .start(
                session_id,
                RuntimeWorkSpec::new(
                    work_id.clone(),
                    RuntimeWorkKind::ModelTurn,
                    "model turn".to_owned(),
                    CancellationHandle::Test(Arc::new(AtomicUsize::new(0))),
                ),
            )
            .await;

        assert!(
            manager
                .active_ids_for_session(session_id)
                .await
                .contains(&work_id)
        );
        assert!(manager.finish(session_id, &work_id).await);
        assert!(!manager.finish(session_id, &work_id).await);
        assert!(manager.active_ids_for_session(session_id).await.is_empty());
    }

    async fn wait_for_count(count: &AtomicUsize, expected: usize) {
        tokio::time::timeout(std::time::Duration::from_secs(1), async {
            while count.load(Ordering::SeqCst) != expected {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("detached cancellation should be dispatched");
    }

    fn test_spec(work_id: &str, count: Arc<AtomicUsize>) -> RuntimeWorkSpec {
        RuntimeWorkSpec::new(
            WorkId::new(work_id),
            RuntimeWorkKind::Tool,
            work_id.to_string(),
            CancellationHandle::Test(count),
        )
    }

    #[tokio::test]
    async fn child_registration_inherits_parent_cancellation() {
        let manager = RuntimeWorkManager::default();
        let session = SessionId::new();
        let count = Arc::new(AtomicUsize::new(0));
        manager
            .start(session, test_spec("parent", Arc::clone(&count)))
            .await;
        manager
            .cancel_with_children(session, &WorkId::new("parent"))
            .await;
        wait_for_count(&count, 1).await;
        manager
            .start(
                session,
                test_spec("child", Arc::clone(&count))
                    .with_parent_work_id(Some(WorkId::new("parent"))),
            )
            .await;
        wait_for_count(&count, 2).await;
        manager
            .start(session, test_spec("child", Arc::clone(&count)))
            .await;
        wait_for_count(&count, 3).await;
    }

    #[tokio::test]
    async fn global_cancellation_finds_exact_work_across_sessions() {
        let manager = RuntimeWorkManager::default();
        let first_session = SessionId::new();
        let second_session = SessionId::new();
        let count = Arc::new(AtomicUsize::new(0));
        let work_id = WorkId::new("shared-work");
        manager
            .start(first_session, test_spec("shared-work", Arc::clone(&count)))
            .await;
        manager
            .start(second_session, test_spec("other-work", Arc::clone(&count)))
            .await;
        let cancelled = manager.cancel_global_with_children(&work_id).await;
        assert_eq!(cancelled.len(), 1);
        assert_eq!(cancelled[0].session_id, first_session);
        assert_eq!(cancelled[0].work_id, work_id);
        wait_for_count(&count, 1).await;
    }

    #[tokio::test]
    async fn shared_execution_proof_is_exact_and_release_bound() {
        let sessions = bcode_session::SessionManager::default();
        let parent = sessions
            .create_session(None, std::env::temp_dir())
            .await
            .unwrap();
        let provenance = bcode_session_models::ExecutionSessionProvenance {
            version: bcode_session_models::EXECUTION_SESSION_PROVENANCE_VERSION,
            owner: "bcode.workflow".into(),
            run_id: "run".into(),
            node_id: "node".into(),
            activation_id: Some("activation".into()),
            attempt: 1,
            parent_session_id: parent.id,
            context_mode: bcode_session_models::ExecutionSessionContextMode::SharedSequential,
            workspace_snapshot: Some("workspace".into()),
            parent_generation: None,
        };
        let permit = sessions
            .admit_shared_execution_session(parent.id, &provenance)
            .await
            .unwrap();
        let link = bcode_workflow_store::WorkflowExecutionSessionLink::new(
            "run".into(),
            "node".into(),
            "activation".into(),
            1,
            parent.id.to_string(),
            "workspace".into(),
            1,
        );
        let manager = RuntimeWorkManager::default();
        let work_id = WorkId::new(bcode_workflow_store::dispatch_identity(
            "run",
            "node",
            "activation",
            1,
        ));
        manager
            .start(
                parent.id,
                RuntimeWorkSpec::new(
                    work_id.clone(),
                    RuntimeWorkKind::WorkflowNode,
                    "node".into(),
                    CancellationHandle::Test(Arc::new(AtomicUsize::new(0))),
                )
                .with_parent_work_id(Some(WorkId::new("workflow:run"))),
            )
            .await;
        assert!(!manager.observes_shared_execution(parent.id, &link).await);
        assert!(
            manager
                .bind_shared_execution(parent.id, &work_id, &permit)
                .await
        );
        assert!(manager.observes_shared_execution(parent.id, &link).await);
        let mut spoofed = link.clone();
        spoofed.attempt = 2;
        assert!(!manager.observes_shared_execution(parent.id, &spoofed).await);
        spoofed = link.clone();
        spoofed.workspace_snapshot = "other".into();
        assert!(!manager.observes_shared_execution(parent.id, &spoofed).await);
        assert!(
            !manager
                .observes_shared_execution(SessionId::new(), &link)
                .await
        );
        let retained_observation = permit.observation().upgrade().unwrap();
        drop(permit);
        assert!(retained_observation.provenance().is_none());
        assert!(!manager.observes_shared_execution(parent.id, &link).await);
        assert!(
            sessions
                .session_summary_non_owning(parent.id)
                .await
                .unwrap()
                .execution
                .is_none()
        );
    }

    #[tokio::test]
    async fn start_cancel_finish_tracks_active_work() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let count = Arc::new(AtomicUsize::new(0));
        let work_id = WorkId::new("work");

        assert_eq!(
            manager
                .start(session_id, test_spec("work", Arc::clone(&count)))
                .await,
            (true, true)
        );
        assert_eq!(manager.active_for_session(session_id).await.len(), 1);
        assert!(
            !manager
                .cancel_with_children(session_id, &work_id)
                .await
                .is_empty()
        );
        wait_for_count(&count, 1).await;
        assert_eq!(
            manager.active_for_session(session_id).await[0].status,
            RuntimeWorkStatus::Cancelling
        );
        manager.finish(session_id, &work_id).await;
        assert!(manager.active_for_session(session_id).await.is_empty());
    }

    #[tokio::test]
    async fn duplicate_cancel_only_signals_once() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let count = Arc::new(AtomicUsize::new(0));
        let work_id = WorkId::new("work");

        manager
            .start(session_id, test_spec("work", Arc::clone(&count)))
            .await;
        assert!(
            !manager
                .cancel_with_children(session_id, &work_id)
                .await
                .is_empty()
        );
        assert!(
            manager
                .cancel_with_children(session_id, &work_id)
                .await
                .is_empty()
        );
        wait_for_count(&count, 1).await;
    }

    #[tokio::test]
    async fn replace_cancellation_changes_signal_target() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let first = Arc::new(AtomicUsize::new(0));
        let second = Arc::new(AtomicUsize::new(0));
        let work_id = WorkId::new("work");

        manager
            .start(session_id, test_spec("work", Arc::clone(&first)))
            .await;
        assert!(
            manager
                .replace_cancellation(
                    session_id,
                    &work_id,
                    CancellationHandle::Test(Arc::clone(&second))
                )
                .await
        );
        assert!(
            !manager
                .cancel_with_children(session_id, &work_id)
                .await
                .is_empty()
        );
        assert_eq!(first.load(Ordering::SeqCst), 0);
        wait_for_count(&second, 1).await;
    }
    #[tokio::test]
    async fn cancelling_parent_cancels_child() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let parent_count = Arc::new(AtomicUsize::new(0));
        let child_count = Arc::new(AtomicUsize::new(0));
        let parent_id = WorkId::new("parent");
        let child_id = WorkId::new("child");

        manager
            .start(session_id, test_spec("parent", Arc::clone(&parent_count)))
            .await;
        manager
            .start(
                session_id,
                test_spec("child", Arc::clone(&child_count))
                    .with_parent_work_id(Some(parent_id.clone())),
            )
            .await;

        assert!(
            !manager
                .cancel_with_children(session_id, &parent_id)
                .await
                .is_empty()
        );
        wait_for_count(&parent_count, 1).await;
        wait_for_count(&child_count, 1).await;
        assert_eq!(
            manager
                .active_for_session(session_id)
                .await
                .into_iter()
                .find(|work| work.work_id == child_id)
                .expect("child remains active until finished")
                .status,
            RuntimeWorkStatus::Cancelling
        );
    }
    #[tokio::test]
    async fn non_returning_cleanup_cannot_delay_local_cancellation() {
        let manager = RuntimeWorkManager::default();
        let session_id = SessionId::new();
        let work_id = WorkId::new("blocked");
        manager
            .start(
                session_id,
                RuntimeWorkSpec::new(
                    work_id.clone(),
                    RuntimeWorkKind::Tool,
                    "blocked cleanup".to_string(),
                    CancellationHandle::TestBlocked(Arc::new(tokio::sync::Notify::new())),
                ),
            )
            .await;

        let cancelled = tokio::time::timeout(
            std::time::Duration::from_millis(100),
            manager.cancel_with_children(session_id, &work_id),
        )
        .await
        .expect("local cancellation must not await cleanup");

        assert_eq!(cancelled, vec![work_id]);
        assert_eq!(
            manager.active_for_session(session_id).await[0].status,
            RuntimeWorkStatus::Cancelling
        );
    }
    #[tokio::test]
    async fn cleanup_failure_is_diagnostic_only_after_local_cancellation() {
        let metrics = MetricsRegistry::default();
        let manager = RuntimeWorkManager::with_metrics(metrics.clone());
        let session_id = SessionId::new();
        let work_id = WorkId::new("failed-cleanup");
        let attempts = Arc::new(AtomicUsize::new(0));
        manager
            .start(
                session_id,
                RuntimeWorkSpec::new(
                    work_id.clone(),
                    RuntimeWorkKind::Tool,
                    "failed cleanup".to_string(),
                    CancellationHandle::TestFailed(Arc::clone(&attempts)),
                ),
            )
            .await;

        let cancelled = manager.cancel_with_children(session_id, &work_id).await;
        assert_eq!(cancelled, vec![work_id]);
        assert_eq!(
            manager.active_for_session(session_id).await[0].status,
            RuntimeWorkStatus::Cancelling
        );
        wait_for_count(&attempts, 1).await;
        tokio::time::timeout(std::time::Duration::from_secs(1), async {
            while metrics
                .snapshot()
                .counters
                .get("runtime_work.cleanup_total")
                .copied()
                != Some(1)
            {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("cleanup diagnostic should be recorded");
        let report = metrics.report();
        assert!(report.events.iter().any(|event| {
            event.name == "runtime_work.cleanup_total"
                && event.labels.get("work_kind").map(String::as_str) == Some("tool")
                && event.labels.get("outcome").map(String::as_str) == Some("failed")
                && !event.labels.contains_key("work_id")
        }));
        assert!(
            metrics
                .snapshot()
                .histograms
                .contains_key("runtime_work.cleanup_duration_ms")
        );
        assert_eq!(
            manager.active_for_session(session_id).await[0].status,
            RuntimeWorkStatus::Cancelling,
            "cleanup failure must not reverse local cancellation"
        );
    }
}