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
//! Concrete DLL custody for an owned ordinary binding. No discovery or CLI bridge.
//!
//! This is a trusted Rust assembly boundary, not a resource/permission grant.
//! Profiles must not expose DLLs until artifact freezing and capability checks
//! also exist. Callback scopes belong in the actual FFI worker, not its waiter.
use super::ordinary::{BackendError, BackendResult};
use rpi_extensions::{
ExtensionSession, PluginCleanupReceipt, PluginCleanupStatus, PluginDiagnostics,
PluginLoadCustody,
};
use rpi_harness::session::jsonl::JsonlSessionStorage;
use std::{
path::PathBuf,
sync::{Arc, Mutex, MutexGuard},
};
use tokio::sync::{watch, Mutex as AsyncMutex};
struct State {
claimed: bool,
attempted: bool,
loading: bool,
unknown: bool,
closing: bool,
calls: usize,
session: Option<ExtensionSession>,
failed: Option<PluginLoadCustody>,
receipts: Vec<PluginCleanupReceipt>,
storage: Option<JsonlSessionStorage>,
catalog: Option<super::HostCatalog>,
}
/// Host ownership of one explicit DLL batch, including failed registration.
/// Debug never reveals artifact paths, plugin state or Session data.
/// Harness hooks must retain only a Weak reference; each actual worker takes
/// a call scope, and shutdown drops the session before waiting on its receipts.
pub struct NativeExtensionCustody {
state: Mutex<State>,
changed: watch::Sender<()>,
close_gate: AsyncMutex<()>,
}
impl std::fmt::Debug for NativeExtensionCustody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("NativeExtensionCustody { .. }")
}
}
impl NativeExtensionCustody {
pub fn new() -> Arc<Self> {
let (changed, _) = watch::channel(());
Arc::new(Self {
state: Mutex::new(State {
claimed: false,
attempted: false,
loading: false,
unknown: false,
closing: false,
calls: 0,
session: None,
failed: None,
receipts: Vec::new(),
storage: None,
catalog: None,
}),
changed,
close_gate: AsyncMutex::new(()),
})
}
fn state(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(|error| error.into_inner())
}
/// Invoke only from owned assembly, never readonly validation. The caller
/// supplies trusted frozen/staged files and must validate capabilities before
/// publishing hooks. No ActionBridge, scanner or ambient registry is used.
/// Losing this waiter does not discard a partially loaded batch.
pub async fn load_owned(
self: &Arc<Self>,
files: Vec<PathBuf>,
diagnostics: Arc<dyn PluginDiagnostics>,
artifact_guard: Option<Arc<dyn Send + Sync>>,
) -> BackendResult<()> {
self.load_owned_checked(files, diagnostics, artifact_guard, |_, _| true)
.await
}
/// Exact per-plugin data-only check, before SDK batch merge/publication.
/// Rejection and validator panic retain the current and all preceding
/// plugins in the same supervised cleanup path, never partial success.
pub async fn load_owned_checked(
self: &Arc<Self>,
files: Vec<PathBuf>,
diagnostics: Arc<dyn PluginDiagnostics>,
artifact_guard: Option<Arc<dyn Send + Sync>>,
validate: impl FnMut(usize, &rpi_extensions::RegistrationInventory) -> bool + Send + 'static,
) -> BackendResult<()> {
{
let mut state = self.state();
if !state.claimed || state.attempted || state.closing {
return Err(BackendError::Binding("DLL custody is not reusable"));
}
state.attempted = true;
state.loading = true;
}
let owner = self.clone();
let loading = LoadingLease(owner.clone());
tokio::task::spawn_blocking(move || {
let _loading = loading;
let loaded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
rpi_extensions::load_explicit_session_owned_checked(
&files,
diagnostics,
artifact_guard,
validate,
)
}));
let result = {
let mut state = owner.state();
state.loading = false;
match loaded {
Ok(Ok(session)) => {
state.receipts = session.cleanup_receipts();
state.session = Some(session);
Ok(())
}
Ok(Err(failure)) => {
state.receipts = failure.custody.cleanup_receipts();
state.failed = Some(failure.custody);
Err(BackendError::Binding("explicit DLL assembly failed"))
}
Err(_) => {
// A panic may have dropped a loaded handle into async
// cleanup without handing us its receipt. Never guess.
state.unknown = true;
Err(BackendError::TaskStopped)
}
}
};
owner.changed.send_replace(());
result
})
.await
.map_err(|_| BackendError::TaskStopped)?
}
/// This value MUST move into the actual callback worker. It holds a session
/// keepalive until the callback and all plugin-owned frees have returned.
pub fn call(self: &Arc<Self>) -> BackendResult<NativeExtensionCall> {
let mut state = self.state();
if state.closing || state.loading || state.unknown {
return Err(BackendError::Closed);
}
let session = state.session.clone().ok_or(BackendError::Closed)?;
state.calls = state.calls.checked_add(1).ok_or(BackendError::Faulted)?;
Ok(NativeExtensionCall {
session,
_lease: CallLease(self.clone()),
})
}
pub(super) fn claim(&self, catalog: super::HostCatalog) -> BackendResult<()> {
let mut state = self.state();
if state.claimed || state.attempted || state.closing {
return Err(BackendError::Binding("DLL custody is not reusable"));
}
state.claimed = true;
state.catalog = Some(catalog);
Ok(())
}
pub(super) fn retain_storage(&self, storage: JsonlSessionStorage) {
self.state().storage = Some(storage);
}
/// Seal callbacks, wait for physical FFI scopes/loading, then relinquish all
/// session/custody keepalives and await final SDK/OS/context cleanup. This
/// operation is called by the supervised owner, never an unowned IPC waiter.
pub(super) async fn close(self: &Arc<Self>) -> BackendResult<()> {
let _close = self.close_gate.lock().await;
let mut changed = self.changed.subscribe();
let (session, failed, receipts, unknown) = loop {
let ready = {
let mut state = self.state();
state.closing = true;
if state.loading || state.calls != 0 {
None
} else {
Some((
state.session.take(),
state.failed.take(),
state.receipts.clone(),
state.unknown,
))
}
};
if let Some(ready) = ready {
break ready;
}
let _ = changed.changed().await;
};
// Last keepalive Drop queues cleanup. Receipts own no code/context and
// therefore cannot themselves keep the DLL loaded indefinitely.
drop(session);
drop(failed);
let mut retained = unknown;
for receipt in receipts {
retained |= receipt.wait().await != PluginCleanupStatus::Released;
}
if retained {
Err(BackendError::ExtensionUnsettled(self.clone()))
} else {
Ok(())
}
}
/// Failed-start rollback uses this only AFTER close proved DLL settlement.
/// The clone keeps the real writer alive across errors/panics in assembly.
pub(super) async fn release_storage(&self) {
let storage = self.state().storage.take();
if let Some(storage) = storage {
storage.drain().await;
storage.release_writer_lease();
}
}
}
impl Drop for NativeExtensionCustody {
fn drop(&mut self) {
let state = self
.state
.get_mut()
.unwrap_or_else(|error| error.into_inner());
let unproven = state.unknown
|| state.loading
|| state.calls != 0
|| state
.receipts
.iter()
.any(|receipt| receipt.status() != PluginCleanupStatus::Released);
if unproven {
// Last-owner Drop is not a settlement operation. If explicit
// shutdown failed/was omitted, keep both the singleton exclusion
// and any real writer for process lifetime rather than let a new
// Host owner race code which may still be alive. Normal supervised
// close reaches Released first and never takes this fail-closed path.
if let Some(storage) = state.storage.take() {
std::mem::forget(storage);
}
if let Some(catalog) = state.catalog.take() {
std::mem::forget(catalog);
}
}
}
}
/// A scoped session view. Field order drops the session BEFORE publishing that
/// the callback finished; close must not observe zero with a keepalive in flight.
pub struct NativeExtensionCall {
session: ExtensionSession,
_lease: CallLease,
}
impl NativeExtensionCall {
pub fn session(&self) -> &ExtensionSession {
&self.session
}
}
struct LoadingLease(Arc<NativeExtensionCustody>);
impl Drop for LoadingLease {
fn drop(&mut self) {
let mut state = self.0.state();
if state.loading {
// A queued worker can be destroyed without running during runtime
// shutdown. Do not leave close waiting for a nonexistent reporter.
state.loading = false;
state.unknown = true;
self.0.changed.send_replace(());
}
}
}
struct CallLease(Arc<NativeExtensionCustody>);
impl Drop for CallLease {
fn drop(&mut self) {
let mut state = self.0.state();
state.calls -= 1;
self.0.changed.send_replace(());
}
}