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
//! Application-owned lifecycle for registered storage reads.
//!
//! Persistent reads register against the target session before consuming content. Registration
//! failures reject the read; they never authorize an unregistered fallback.

use bcode_session::storage_admission::{StorageAdmissionRegistry, StorageReadAdmission};
use bcode_session_models::SessionId;
use std::io;
use std::path::PathBuf;

/// An operation registered durably before content access, retained through access persistence.
pub struct RegisteredStorageRead {
    registry: StorageAdmissionRegistry,
    participant: SessionId,
    admission: StorageReadAdmission,
}

impl RegisteredStorageRead {
    /// Acquire session-scoped admission before content access.
    ///
    /// In-memory sessions require no persistent admission. Persistent registration failures return
    /// an error before content can be consumed, without poisoning unrelated sessions.
    ///
    /// # Errors
    /// Returns a retryable storage error when registration cannot be acquired.
    pub async fn for_session(
        state: &super::ServerState,
        session_id: SessionId,
    ) -> Result<Option<Self>, bcode_session::SessionError> {
        let Some(root) = state.sessions.session_store_root() else {
            return Ok(None);
        };
        // Register before lookup: existence can change, and metadata failure is not proof that
        // this operation cannot consume persistent content.
        Self::begin(root, session_id).await.map(Some).map_err(|_| {
            bcode_session::db::SessionDbError::Io(io::Error::new(
                io::ErrorKind::WouldBlock,
                "session storage admission unavailable; retry after coordination recovers",
            ))
            .into()
        })
    }

    /// Persist access before completing admission. Failure leaves a durable dirty participant.
    pub async fn finish_history(self, state: &super::ServerState, session_id: SessionId) {
        self.finish_consumption(
            state,
            session_id,
            bcode_session::storage_access::StorageAccessKind::History,
        )
        .await;
    }

    /// Persist the category's access timestamp before retiring a successful read participant.
    pub async fn finish_consumption(
        self,
        state: &super::ServerState,
        session_id: SessionId,
        kind: bcode_session::storage_access::StorageAccessKind,
    ) {
        if state
            .sessions
            .record_storage_access(session_id, kind)
            .await
            .is_ok()
        {
            if self.complete().await.is_err() {
                state.fail_storage_tracking();
                tracing::warn!("storage read participant retirement deferred");
            }
        } else {
            state.fail_storage_tracking();
            tracing::warn!("storage access tracking failed; participant remains dirty");
        }
    }

    /// Retire an ordinary failed read after all its content work has finished.
    ///
    /// No content was returned to the caller, so access tracking is unnecessary. Cancellation
    /// must not call this: dropping admission continues to preserve dirty crash evidence.
    ///
    /// # Errors
    /// Returns the original read error unchanged. Retirement failures preserve evidence and
    /// are logged rather than replacing the operation's error.
    pub async fn finish_failed<T, E>(
        admission: &mut Option<Self>,
        result: Result<T, E>,
    ) -> Result<T, E> {
        if result.is_err()
            && let Some(admission) = admission.take()
            && admission.complete().await.is_err()
        {
            tracing::warn!("failed storage read participant retirement deferred");
        }
        result
    }

    /// Register one operation without running blocking lock/sync calls on the async executor.
    ///
    /// # Errors
    /// Returns contention or registry IO/compatibility failures. No content may have been exposed
    /// under this admission if it fails; the caller decides whether to defer optional maintenance.
    pub async fn begin(root: PathBuf, session_id: SessionId) -> io::Result<Self> {
        tokio::task::spawn_blocking(move || {
            let registry = StorageAdmissionRegistry::open_session(&root, session_id)?;
            let participant = SessionId::new();
            let admission = registry.admit_read(participant)?;
            Ok(Self {
                registry,
                participant,
                admission,
            })
        })
        .await
        .map_err(|_| io::Error::other("storage read registration task failed"))?
    }

    /// Finish after durable access tracking succeeds; retire while shared admission is still held.
    ///
    /// A failed/cancelled content operation should drop this guard, retaining dirty evidence.
    /// Other active readers do not prevent completion or cause clean participant accumulation.
    ///
    /// # Errors
    /// Returns tracking-state, sync, lock contention, or retirement failures.
    pub async fn complete(self) -> io::Result<()> {
        tokio::task::spawn_blocking(move || {
            self.registry
                .complete_read(self.participant, self.admission)
        })
        .await
        .map_err(|_| io::Error::other("storage read completion task failed"))?
    }
}

/// Register before startup services can consume session content.
/// Registration failure disables optional maintenance without preventing unrelated startup.
pub(super) async fn register_startup(state: &super::ServerState, root: PathBuf) {
    let fallback_root = root.clone();
    let queue = bcode_metrics::startup::phase("storage_admission.blocking_queue_wait");
    let registered = tokio::task::spawn_blocking(move || {
        queue.finish();
        let registry = bcode_metrics::startup::measure("storage_admission.registry_open", || {
            StorageAdmissionRegistry::open(&root)
        })?;
        if bcode_metrics::startup::measure("storage_admission.retire_completed_daemons", || {
            registry.retire_completed_daemons(4096)
        })
        .is_err()
        {
            tracing::debug!(
                "completed storage registrations could not be retired; preserving registry"
            );
        }
        bcode_metrics::startup::measure("storage_admission.register_daemon", || {
            registry.register_daemon(SessionId::new())
        })
    })
    .await;
    if let Ok(Ok(registration)) = registered {
        *state
            .storage_daemon_registration
            .lock()
            .expect("storage registration lock") = Some(registration);
    } else {
        block_unregistered_reads(state, fallback_root).await;
        tracing::warn!("storage daemon registration unavailable; maintenance is disabled");
    }
}

/// Disable maintenance before allowing reads without daemon or operation registration.
///
/// Failure to persist the blocker is not a compatibility proof: the global dispatch gate must
/// remain closed until an independent fence covers these readers.
pub(super) async fn block_unregistered_reads(state: &super::ServerState, root: PathBuf) -> bool {
    state.fail_storage_tracking();
    let blocked = tokio::task::spawn_blocking(move || {
        StorageAdmissionRegistry::open(&root)?.block_maintenance_for_fallback()
    })
    .await;
    if matches!(blocked, Ok(Ok(()))) {
        tracing::warn!("unregistered storage reads durably disabled maintenance");
        true
    } else {
        tracing::warn!("storage fallback fence unavailable; automatic scheduling remains disabled");
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn repeated_missing_artifact_reads_retire_admission_without_changing_history() {
        let root = tempfile::tempdir().expect("root");
        let sessions = bcode_session::SessionManager::persistent(root.path()).expect("manager");
        let session = sessions
            .create_session(None, root.path().to_path_buf())
            .await
            .expect("session");
        let before = sessions.session_history(session.id).await.expect("history");
        let state = crate::tests::test_server_state(sessions);
        let registry =
            StorageAdmissionRegistry::open_session(root.path(), session.id).expect("registry");
        for _ in 0..32 {
            assert!(
                crate::read_session_artifact_range(
                    &state,
                    session.id,
                    "missing",
                    "recording",
                    0,
                    64,
                )
                .await
                .is_err()
            );
            drop(
                registry
                    .admit_maintenance(1)
                    .expect("only the gate remains"),
            );
        }
        assert_eq!(
            state
                .sessions
                .session_history(session.id)
                .await
                .expect("history"),
            before
        );
        drop(state);
    }

    #[tokio::test]
    async fn failed_history_lookup_retires_but_dropped_read_preserves_evidence() {
        let root = tempfile::tempdir().expect("root");
        let sessions = bcode_session::SessionManager::persistent(root.path()).expect("manager");
        let state = crate::tests::test_server_state(sessions);
        let id = SessionId::new();
        for _ in 0..16 {
            assert!(
                crate::session_operations::complete_history(
                    &state,
                    bcode_session_models::ClientId::new(),
                    id,
                )
                .await
                .is_err()
            );
            let registry =
                StorageAdmissionRegistry::open_session(root.path(), id).expect("registry");
            drop(
                registry
                    .admit_maintenance(1)
                    .expect("failed lookup retired"),
            );
        }
        drop(
            RegisteredStorageRead::begin(root.path().to_path_buf(), id)
                .await
                .expect("read"),
        );
        let registry = StorageAdmissionRegistry::open_session(root.path(), id).expect("registry");
        assert!(registry.admit_maintenance(16).is_err());
    }

    #[tokio::test]
    async fn maintenance_contention_rejects_read_until_authority_is_released() {
        let root = tempfile::tempdir().expect("root");
        let sessions = bcode_session::SessionManager::persistent(root.path()).expect("manager");
        let session = sessions
            .create_session(None, root.path().to_path_buf())
            .await
            .expect("create");
        let expected = sessions.session_history(session.id).await.expect("history");
        sessions
            .release_session_ownership(session.id)
            .await
            .expect("release");
        let before = bcode_session::storage_access::observe_session_access(root.path(), session.id)
            .expect("access");
        let registry =
            StorageAdmissionRegistry::open_session(root.path(), session.id).expect("registry");
        let maintenance = registry.admit_maintenance(16).expect("maintenance");
        let state = crate::tests::test_server_state(sessions);
        let client = bcode_session_models::ClientId::new();
        assert!(
            crate::session_operations::complete_history(&state, client, session.id)
                .await
                .is_err()
        );
        assert_eq!(
            bcode_session::storage_access::observe_session_access(root.path(), session.id)
                .expect("access unchanged"),
            before
        );
        drop(maintenance);
        assert_eq!(
            crate::session_operations::complete_history(&state, client, session.id)
                .await
                .expect("retry after release"),
            expected
        );
        assert!(
            !state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        drop(state);
    }

    #[tokio::test]
    async fn double_failure_rejects_history_and_retry_preserves_content() {
        let root = tempfile::tempdir().expect("root");
        let sessions = bcode_session::SessionManager::persistent(root.path()).expect("manager");
        let session = sessions
            .create_session(None, root.path().to_path_buf())
            .await
            .expect("create");
        sessions
            .append_event(
                session.id,
                bcode_session_models::SessionEventKind::SystemMessage {
                    text: "history must remain readable after coordination recovers".into(),
                },
            )
            .await
            .expect("append");
        let expected = sessions.session_history(session.id).await.expect("history");
        sessions
            .release_session_ownership(session.id)
            .await
            .expect("release");
        let before = bcode_session::storage_access::observe_session_access(root.path(), session.id)
            .expect("access");
        let obstruction = root.path().join("storage-admission-sessions-v1");
        std::fs::write(&obstruction, b"unavailable registry").expect("obstruct");
        let state = crate::tests::test_server_state(sessions);
        let client = bcode_session_models::ClientId::new();
        assert!(
            crate::session_operations::complete_history(&state, client, session.id)
                .await
                .is_err()
        );
        assert_eq!(
            bcode_session::storage_access::observe_session_access(root.path(), session.id)
                .expect("unchanged access"),
            before
        );
        assert_eq!(
            std::fs::read(&obstruction).expect("preserved"),
            b"unavailable registry"
        );
        std::fs::remove_file(&obstruction).expect("remove test obstruction");
        assert_eq!(
            crate::session_operations::complete_history(&state, client, session.id)
                .await
                .expect("retry"),
            expected
        );
        assert!(
            !state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        drop(state);
    }

    #[tokio::test]
    async fn missing_session_still_registers_before_storage_lookup() {
        let root = tempfile::tempdir().expect("root");
        let sessions = bcode_session::SessionManager::persistent(root.path()).expect("manager");
        let state = crate::tests::test_server_state(sessions);
        let id = SessionId::new();
        let read = RegisteredStorageRead::for_session(&state, id)
            .await
            .expect("admission available")
            .expect("admitted");
        let registry = StorageAdmissionRegistry::open_session(root.path(), id).expect("registry");
        assert!(registry.admit_maintenance(16).is_err());
        read.complete().await.expect("complete empty lookup");
        drop(state);
        drop(registry.admit_maintenance(16).expect("released"));
        assert!(!root.path().join(id.to_string()).exists());
    }

    #[tokio::test]
    async fn startup_registration_blocks_foreign_maintenance_until_shutdown() {
        let root = tempfile::tempdir().expect("root");
        let state = crate::tests::test_server_state(bcode_session::SessionManager::default());
        register_startup(&state, root.path().to_path_buf()).await;
        let registry = StorageAdmissionRegistry::open(root.path()).expect("registry");
        assert!(registry.admit_maintenance(16).is_err());
        assert!(
            !state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        state.request_shutdown();
        drop(state);
        drop(registry.admit_maintenance(16).expect("clean shutdown"));
    }

    #[tokio::test]
    async fn unregistered_startup_persists_blocker_across_restart() {
        let root = tempfile::tempdir().expect("root");
        let state = crate::tests::test_server_state(bcode_session::SessionManager::default());
        block_unregistered_reads(&state, root.path().to_path_buf()).await;
        assert!(
            state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        state.request_shutdown();
        drop(state);
        let registry = StorageAdmissionRegistry::open(root.path()).expect("restart");
        assert!(registry.admit_maintenance(16).is_err());
    }

    #[tokio::test]
    async fn unavailable_fallback_keeps_local_tracking_failed() {
        let root = tempfile::NamedTempFile::new().expect("not a directory");
        let state = crate::tests::test_server_state(bcode_session::SessionManager::default());
        register_startup(&state, root.path().to_path_buf()).await;
        assert!(
            state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        drop(state);
        assert_eq!(std::fs::metadata(root.path()).expect("preserved").len(), 0);
    }

    #[tokio::test]
    async fn healthy_shutdown_cleans_only_after_last_state_owner_releases() {
        let root = tempfile::tempdir().expect("root");
        let registry = StorageAdmissionRegistry::open(root.path()).expect("registry");
        let state = std::sync::Arc::new(crate::tests::test_server_state(
            bcode_session::SessionManager::default(),
        ));
        *state.storage_daemon_registration.lock().expect("lock") =
            Some(registry.register_daemon(SessionId::new()).expect("daemon"));
        let outstanding = std::sync::Arc::clone(&state);
        state.request_shutdown();
        drop(state);
        assert!(registry.admit_maintenance(16).is_err());
        drop(outstanding);
        drop(
            registry
                .admit_maintenance(16)
                .expect("clean after final release"),
        );
        assert_eq!(registry.retire_completed_daemons(16).expect("retire"), 1);
    }

    #[tokio::test]
    async fn shutdown_with_outstanding_operation_token_leaves_dirty_record() {
        let root = tempfile::tempdir().expect("root");
        let registry = StorageAdmissionRegistry::open(root.path()).expect("registry");
        let state = crate::tests::test_server_state(bcode_session::SessionManager::default());
        let daemon = registry.register_daemon(SessionId::new()).expect("daemon");
        let token = daemon.acknowledgement().expect("token");
        *state.storage_daemon_registration.lock().expect("lock") = Some(daemon);
        state.request_shutdown();
        drop(state);
        assert!(token.check().is_err());
        drop(token);
        assert!(registry.admit_maintenance(16).is_err());
    }

    #[tokio::test]
    async fn tracking_failure_invalidates_live_acknowledgement_and_survives_restart() {
        let root = tempfile::tempdir().expect("root");
        let registry = StorageAdmissionRegistry::open(root.path()).expect("registry");
        let registration = registry
            .register_daemon(SessionId::new())
            .expect("registration");
        let state = crate::tests::test_server_state(bcode_session::SessionManager::default());
        *state.storage_daemon_registration.lock().expect("lock") = Some(registration);
        {
            let mut held = state.storage_daemon_registration.lock().expect("lock");
            drop(
                registry
                    .admit_acknowledged(16, held.as_mut().expect("registered"))
                    .expect("healthy acknowledgement"),
            );
        }
        state.fail_storage_tracking();
        assert!(
            state
                .storage_tracking_failed
                .load(std::sync::atomic::Ordering::SeqCst)
        );
        {
            let mut held = state.storage_daemon_registration.lock().expect("lock");
            assert!(!held.as_ref().expect("registered").healthy());
            assert!(
                registry
                    .admit_acknowledged(16, held.as_mut().expect("registered"))
                    .is_err()
            );
            drop(held);
        }
        drop(state);
        drop(registry);
        let reopened = StorageAdmissionRegistry::open(root.path()).expect("restart");
        assert!(reopened.admit_maintenance(16).is_err());
    }

    #[tokio::test]
    async fn registered_read_lifecycle_retires_success_but_preserves_abandonment() {
        let root = tempfile::tempdir().expect("root");
        let id = SessionId::new();
        let read = RegisteredStorageRead::begin(root.path().to_path_buf(), id)
            .await
            .expect("registered");
        let registry = StorageAdmissionRegistry::open_session(root.path(), id).expect("registry");
        assert!(registry.admit_maintenance(10).is_err());
        read.complete().await.expect("complete");
        drop(registry.admit_maintenance(1).expect("empty clean registry"));
        drop(
            RegisteredStorageRead::begin(root.path().to_path_buf(), id)
                .await
                .expect("abandoned"),
        );
        assert!(registry.admit_maintenance(10).is_err());
    }
}