clawft-kernel 0.8.3

WeftOS kernel: boot sequence, process table, service registry, and health subsystem
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
//! Artifact streams over machine-mesh deliveries (ADR-106 section 5.4).
//!
//! The daemon holds the [`ArtifactExchange`] but not the mesh connections:
//! the machine mesh service owns those and hands the daemon `deliver` frames
//! and takes its `send`s. This module carries the piece protocol across that
//! seam. A [`TunnelStream`] is a [`MeshStream`] whose bytes travel as
//! `mesh.artifact.tunnel` messages addressed to a peer node:
//!
//! - **Dial** ([`TunnelDialer`], the production [`PeerDialer`]): opens a
//!   session id and speaks the fetcher side.
//! - **Serve** (inbound): the first `req` frame of an unknown session starts
//!   [`ArtifactExchange::serve_as`] with a [`ServePeer`] built from the
//!   service-stamped origin. Only a verified `node` peer is served; the
//!   redistribution policy still decides what.
//!
//! Replies are accepted only from the peer that was dialled and only while
//! that peer is a verified node, so an unadmitted sender cannot inject
//! frames into a fetch. Frames larger than [`CHUNK`] are split and
//! reassembled (a mesh-local line is capped at 1 MiB).

use std::net::SocketAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use dashmap::DashMap;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;

use crate::error::KernelResult;
use crate::ipc::{KernelMessage, MessagePayload, MessageTarget};
use crate::mesh::{MeshError, MeshStream};
use crate::mesh_admit::PeerClass;
use crate::mesh_artifact::ArtifactExchange;
use crate::mesh_artifact_types::MAX_ARTIFACT_FRAME;
use crate::mesh_delivery::PeerCtx;
use crate::mesh_swarm_fetch::PeerDialer;
use crate::mesh_swarm_state::ServePeer;
use crate::workload_pkg::codec::hex_encode;

/// Topic of tunnel frames.
pub const TOPIC_TUNNEL: &str = "mesh.artifact.tunnel";
/// Raw bytes per tunnel message.
pub const CHUNK: usize = 192 * 1024;
/// Most serve sessions from one peer.
pub const MAX_SERVE_PER_PEER: usize = 8;
/// Most serve sessions in all.
pub const MAX_SERVE_TOTAL: usize = 64;
/// Most dialled sessions open at once.
pub const MAX_DIAL_TOTAL: usize = 64;
/// Frames queued per session before the sender is dropped.
const SESSION_QUEUE: usize = 128;

/// Sends one kernel message to a peer node through the machine mesh. The
/// daemon implements it with its service link; tests with an in-memory net.
#[async_trait]
pub trait PeerSender: Send + Sync + 'static {
    /// Deliver `msg` to `node_id`.
    async fn send_to_node(&self, node_id: &str, msg: KernelMessage) -> KernelResult<()>;
}

/// Which side of the session a frame was sent by.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Dir {
    /// The fetcher (the dialler) to the holder.
    Req,
    /// The holder back to the fetcher.
    Rsp,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct TunnelFrame {
    v: u32,
    sid: String,
    dir: Dir,
    /// Chunk index within one stream frame.
    #[serde(default)]
    part: u32,
    /// Last chunk of the frame.
    #[serde(default)]
    last: bool,
    /// The stream was closed by the sender.
    #[serde(default)]
    close: bool,
    /// Chunk bytes, hex.
    #[serde(default)]
    data: String,
}

/// Counters, all monotonic.
#[derive(Debug, Default)]
pub struct TunnelCounters {
    /// Frames refused because the peer is not a verified node.
    pub refused_unverified: AtomicU64,
    /// Frames refused for a limit or a malformed shape.
    pub refused_other: AtomicU64,
    /// Serve sessions started.
    pub served_sessions: AtomicU64,
    /// Sessions dropped because their queue was full (a slow consumer).
    pub dropped_slow: AtomicU64,
}

struct Session {
    tx: mpsc::Sender<Vec<u8>>,
    partial: Mutex<Vec<u8>>,
    next_part: Mutex<u32>,
}

/// Both ends of the artifact tunnel for one node.
pub struct ArtifactTunnel {
    exchange: Arc<ArtifactExchange>,
    sender: Arc<dyn PeerSender>,
    /// Sessions this node serves, by (peer, sid).
    serving: DashMap<(String, String), Arc<Session>>,
    /// Sessions this node dialled, by (peer, sid).
    dialled: DashMap<(String, String), Arc<Session>>,
    /// Counters.
    pub counters: TunnelCounters,
}

/// Whether `from` may be served licensed content or sent licence floods:
/// admission verified it (the service-stamped `AdmittedPeer`) and its class
/// is `node`. A verified leaf is not: leaves never get licensed bytes or
/// grant floods. Every serve, checkout and flood decision goes through this.
pub fn licensed_peer(from: &PeerCtx) -> bool {
    from.node_verified && from.class == PeerClass::Node
}

/// Lower- or upper-case hex to bytes; `None` on odd length or a bad digit.
fn hex_decode(s: &str) -> Option<Vec<u8>> {
    if !s.len().is_multiple_of(2) {
        return None;
    }
    s.as_bytes()
        .chunks(2)
        .map(|p| {
            let hi = (p[0] as char).to_digit(16)?;
            let lo = (p[1] as char).to_digit(16)?;
            Some((hi * 16 + lo) as u8)
        })
        .collect()
}

fn new_sid() -> String {
    let mut b = [0u8; 8];
    rand::rngs::OsRng.fill_bytes(&mut b);
    hex_encode(&b)
}

impl ArtifactTunnel {
    /// A tunnel serving `exchange` and sending through `sender`.
    pub fn new(exchange: Arc<ArtifactExchange>, sender: Arc<dyn PeerSender>) -> Arc<Self> {
        Arc::new(Self {
            exchange,
            sender,
            serving: DashMap::new(),
            dialled: DashMap::new(),
            counters: TunnelCounters::default(),
        })
    }

    /// The production [`PeerDialer`] over this tunnel.
    pub fn dialer(self: &Arc<Self>) -> Arc<dyn PeerDialer> {
        Arc::new(TunnelDialer { tunnel: self.clone() })
    }

    /// Serve sessions open now.
    pub fn serving_sessions(&self) -> usize {
        self.serving.len()
    }

    /// Handle one inbound `mesh.artifact.tunnel` delivery. `from` is built
    /// from the service-stamped origin; anything but a verified node is
    /// dropped and counted.
    pub async fn on_message(self: &Arc<Self>, from: &PeerCtx, msg: KernelMessage) {
        if !licensed_peer(from) {
            self.counters.refused_unverified.fetch_add(1, Ordering::Relaxed);
            return;
        }
        let MessagePayload::Json(v) = msg.payload else {
            self.counters.refused_other.fetch_add(1, Ordering::Relaxed);
            return;
        };
        let Ok(f) = serde_json::from_value::<TunnelFrame>(v) else {
            self.counters.refused_other.fetch_add(1, Ordering::Relaxed);
            return;
        };
        if f.v != 1 || f.sid.is_empty() || f.sid.len() > 32 || f.data.len() > CHUNK * 2 {
            self.counters.refused_other.fetch_add(1, Ordering::Relaxed);
            return;
        }
        let key = (from.peer_id.clone(), f.sid.clone());
        if f.close {
            // Dropping the registry's handle closes the session's receiver,
            // which ends the serve loop or fails the fetch.
            match f.dir {
                Dir::Req => self.serving.remove(&key),
                Dir::Rsp => self.dialled.remove(&key),
            };
            return;
        }
        match f.dir {
            Dir::Req => self.inbound_req(key, f).await,
            Dir::Rsp => {
                if let Some(s) = self.dialled.get(&key).map(|s| s.clone()) {
                    self.feed(&key, Dir::Rsp, &s, &f);
                } else {
                    self.counters.refused_other.fetch_add(1, Ordering::Relaxed);
                }
            }
        }
    }

    async fn inbound_req(self: &Arc<Self>, key: (String, String), f: TunnelFrame) {
        let existing = self.serving.get(&key).map(|s| s.clone());
        let session = match existing {
            Some(s) => s,
            None => {
                let per_peer = self.serving.iter().filter(|e| e.key().0 == key.0).count();
                if per_peer >= MAX_SERVE_PER_PEER || self.serving.len() >= MAX_SERVE_TOTAL {
                    self.counters.refused_other.fetch_add(1, Ordering::Relaxed);
                    return;
                }
                let (tx, rx) = mpsc::channel(SESSION_QUEUE);
                let s = Arc::new(Session {
                    tx,
                    partial: Mutex::default(),
                    next_part: Mutex::default(),
                });
                self.serving.insert(key.clone(), s.clone());
                self.counters.served_sessions.fetch_add(1, Ordering::Relaxed);
                let me = self.clone();
                let (peer, sid) = key.clone();
                tokio::spawn(async move {
                    let mut stream = TunnelStream::new(me.clone(), peer.clone(), sid.clone(), Dir::Rsp, rx);
                    let who = ServePeer::verified(peer.clone());
                    let _ = me.exchange.serve_as(&mut stream, &who).await;
                    me.serving.remove(&(peer, sid));
                });
                s
            }
        };
        self.feed(&key, Dir::Req, &session, &f);
    }

    /// Reassemble `f` into whole stream frames and queue them for the session.
    /// Never waits: this runs on the daemon's single delivery worker, so a
    /// session whose queue is full is dropped (counted) instead of stalling
    /// every other delivery behind it.
    fn feed(&self, key: &(String, String), dir: Dir, s: &Session, f: &TunnelFrame) {
        let Some(bytes) = hex_decode(&f.data) else { return };
        let whole = {
            let mut part = s.next_part.lock().unwrap_or_else(|p| p.into_inner());
            let mut buf = s.partial.lock().unwrap_or_else(|p| p.into_inner());
            if f.part != *part || buf.len() + bytes.len() > MAX_ARTIFACT_FRAME + 64 {
                buf.clear();
                *part = 0;
                return;
            }
            buf.extend_from_slice(&bytes);
            if f.last {
                *part = 0;
                Some(std::mem::take(&mut *buf))
            } else {
                *part += 1;
                None
            }
        };
        if let Some(w) = whole
            && s.tx.try_send(w).is_err()
        {
            self.counters.dropped_slow.fetch_add(1, Ordering::Relaxed);
            match dir {
                Dir::Req => self.serving.remove(key),
                Dir::Rsp => self.dialled.remove(key),
            };
        }
    }
}

struct TunnelDialer {
    tunnel: Arc<ArtifactTunnel>,
}

#[async_trait]
impl PeerDialer for TunnelDialer {
    async fn dial(&self, peer_id: &str) -> Result<Box<dyn MeshStream>, MeshError> {
        let t = &self.tunnel;
        if t.dialled.len() >= MAX_DIAL_TOTAL {
            return Err(MeshError::Transport("too many artifact sessions open".into()));
        }
        let sid = new_sid();
        let (tx, rx) = mpsc::channel(SESSION_QUEUE);
        let s = Arc::new(Session {
            tx,
            partial: Mutex::default(),
            next_part: Mutex::default(),
        });
        t.dialled.insert((peer_id.to_owned(), sid.clone()), s);
        Ok(Box::new(TunnelStream::new(t.clone(), peer_id.to_owned(), sid, Dir::Req, rx)))
    }
}

/// One artifact session as a [`MeshStream`].
pub struct TunnelStream {
    tunnel: Arc<ArtifactTunnel>,
    peer: String,
    sid: String,
    /// The direction this end sends in.
    out: Dir,
    rx: mpsc::Receiver<Vec<u8>>,
    closed: bool,
}

impl TunnelStream {
    fn new(
        tunnel: Arc<ArtifactTunnel>,
        peer: String,
        sid: String,
        out: Dir,
        rx: mpsc::Receiver<Vec<u8>>,
    ) -> Self {
        Self { tunnel, peer, sid, out, rx, closed: false }
    }

    fn registry_remove(&self) {
        let key = (self.peer.clone(), self.sid.clone());
        match self.out {
            Dir::Req => self.tunnel.dialled.remove(&key),
            Dir::Rsp => self.tunnel.serving.remove(&key),
        };
    }

    async fn emit(&self, f: TunnelFrame) -> Result<(), MeshError> {
        let value = serde_json::to_value(&f).map_err(|e| MeshError::Io(e.to_string()))?;
        let msg = KernelMessage::new(
            0,
            MessageTarget::Topic(TOPIC_TUNNEL.into()),
            MessagePayload::Json(value),
        );
        self.tunnel
            .sender
            .send_to_node(&self.peer, msg)
            .await
            .map_err(|e| MeshError::Transport(e.to_string()))
    }
}

#[async_trait]
impl MeshStream for TunnelStream {
    async fn send(&mut self, data: &[u8]) -> Result<(), MeshError> {
        if self.closed {
            return Err(MeshError::ConnectionClosed);
        }
        let n = data.chunks(CHUNK).count().max(1);
        for (i, chunk) in data.chunks(CHUNK).chain(data.is_empty().then_some(&[][..])).enumerate() {
            self.emit(TunnelFrame {
                v: 1,
                sid: self.sid.clone(),
                dir: self.out,
                part: i as u32,
                last: i + 1 == n,
                close: false,
                data: hex_encode(chunk),
            })
            .await?;
        }
        Ok(())
    }

    async fn recv(&mut self) -> Result<Vec<u8>, MeshError> {
        self.rx.recv().await.ok_or(MeshError::ConnectionClosed)
    }

    async fn close(&mut self) -> Result<(), MeshError> {
        if std::mem::replace(&mut self.closed, true) {
            return Ok(());
        }
        self.registry_remove();
        let _ = self
            .emit(TunnelFrame {
                v: 1,
                sid: self.sid.clone(),
                dir: self.out,
                part: 0,
                last: true,
                close: true,
                data: String::new(),
            })
            .await;
        Ok(())
    }

    fn remote_addr(&self) -> Option<SocketAddr> {
        None
    }
}

impl Drop for TunnelStream {
    fn drop(&mut self) {
        if !self.closed {
            self.registry_remove();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;
    use crate::artifact_store::ArtifactStore;
    use crate::mesh_artifact::ExchangeConfig;

    struct Null;

    #[async_trait]
    impl PeerSender for Null {
        async fn send_to_node(&self, _: &str, _: KernelMessage) -> KernelResult<()> {
            Ok(())
        }
    }

    fn node_ctx(id: &str) -> PeerCtx {
        PeerCtx {
            peer_id: id.into(),
            node_verified: true,
            class: PeerClass::Node,
            remote_static: None,
            src_scope: None,
        }
    }

    fn rsp(sid: &str, part: u32) -> KernelMessage {
        KernelMessage::new(
            0,
            MessageTarget::Topic(TOPIC_TUNNEL.into()),
            MessagePayload::Json(serde_json::json!({
                "v": 1, "sid": sid, "dir": "rsp", "part": part, "last": true, "data": "00"
            })),
        )
    }

    #[tokio::test]
    async fn a_session_that_stops_reading_is_dropped_and_never_blocks_the_delivery_worker() {
        let ex = Arc::new(
            ArtifactExchange::new("x", Arc::new(ArtifactStore::new_memory()), ExchangeConfig::default())
                .unwrap(),
        );
        let t = ArtifactTunnel::new(ex, Arc::new(Null));
        // Dial a peer and never read: find the session id the tunnel made.
        let _stream = t.dialer().dial("peer-a").await.unwrap();
        let sid = t.dialled.iter().next().map(|e| e.key().1.clone()).unwrap();
        // Far more whole frames than the queue holds, delivered inline: every
        // call must return at once (a plain await on a full queue would hang
        // this test, and the daemon's only delivery worker with it).
        let all = tokio::time::timeout(Duration::from_secs(5), async {
            for _ in 0..(SESSION_QUEUE + 10) {
                t.on_message(&node_ctx("peer-a"), rsp(&sid, 0)).await;
            }
        })
        .await;
        assert!(all.is_ok(), "delivery blocked on a slow session");
        assert_eq!(t.counters.dropped_slow.load(Ordering::Relaxed), 1);
        assert!(t.dialled.is_empty(), "the slow session was dropped");
    }
}