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
//! In-process nodes for control-plane tests: real signed packages, real
//! signed facts, real native adapters under `WorkloadGate`, and the real
//! wire (envelopes + artifact frames over in-memory streams). Every node
//! gets its own in-memory chain (never the operator's chain.rvf).

use std::path::{Path, PathBuf};
use std::sync::Arc;

use clawft_types::placement::{AttrValue, Capability, CapabilityId, NodeFacts, Provenance};
use ed25519_dalek::SigningKey;
use serde_json::Value;

use crate::artifact_store::ArtifactStore;
use crate::chain::ChainManager;
use crate::mesh_artifact::{ArtifactExchange, ExchangeConfig};
use crate::node_facts_advert::sign_node_facts;
use crate::node_registry::node_id_from_pubkey;
use crate::workload_governance::{NetworkPolicy, WorkloadGate, WorkloadPermitRule};
use crate::workload_pkg::codec::hex_encode;
use crate::workload_pkg::{
    CogPackInput, KeyOrigin, PackageSource, TrustAnchors, key_id_for, pack_cog, sign_envelope,
    write_manifest,
};
use crate::workload_runtime::native::host_arch;
use crate::workload_runtime::container_cmd::CmdOutput;
use crate::workload_runtime::{
    CommandRunner, ContainerRuntime, ContainerRuntimeConfig, Engine, NativeConfig, NativeRuntime,
    RuntimeError, WorkloadHost,
};

use super::host_service::WorkloadHostService;
use super::plane::PlacementControlPlane;
use super::transport::CtlConnector;

/// The package signer (operator key) every node pins.
pub fn signer() -> SigningKey {
    SigningKey::from_bytes(&[42; 32])
}

pub fn anchors() -> TrustAnchors {
    let pk = signer().verifying_key().to_bytes();
    let mut a = TrustAnchors::default();
    a.push_signer(&key_id_for(&pk), &hex_encode(&pk), KeyOrigin::Operator)
        .unwrap();
    a
}

/// This host's arch (the arch the test package ships and nodes advertise).
pub fn arch() -> &'static str {
    host_arch().expect("tests run on a supported arch")
}

/// A signed cog package whose binary (for this host's arch) is `script`.
pub fn package(root: &Path, id: &str, script: &str, arches: &[&str]) -> PathBuf {
    package_from(root, id, script, arches, None)
}

/// [`package`] with a release URL (one naming `cognitum` gives the package
/// Cognitum origin, ADR-106).
pub fn package_from(root: &Path, id: &str, script: &str, arches: &[&str], release_url: Option<&str>) -> PathBuf {
    let per: Vec<(&str, &str)> = arches.iter().map(|a| (*a, script)).collect();
    package_bins(root, id, &per, release_url)
}

/// [`package_from`] with its own bytes per arch.
pub fn package_bins(root: &Path, id: &str, bins_in: &[(&str, &str)], release_url: Option<&str>) -> PathBuf {
    let same = bins_in.windows(2).all(|w| w[0].1 == w[1].1);
    let src = root.join("src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(
        src.join("cog.toml"),
        format!("[cog]\nid = \"{id}\"\nname = \"T\"\nversion = \"0.1.0\"\n"),
    )
    .unwrap();
    let mut bins = Vec::new();
    for (a, script) in bins_in {
        let p = root.join(format!("bin-{a}"));
        std::fs::write(&p, script).unwrap();
        bins.push((a.to_string(), p));
    }
    // With the same bytes on every arch one provenance covers them all;
    // different bytes are packed without one (and not redistributable).
    if let Some((_, first)) = bins.first().filter(|_| same) {
        crate::workload_pkg::write_source_build_provenance(&src, first).unwrap();
    }
    let input = CogPackInput {
        cog_dir: src,
        binaries: bins,
        source: PackageSource {
            repo: Some("cogs-fork".into()),
            commit: Some("0000000".into()),
            release_url: release_url.map(str::to_owned),
        },
        cognitum_record: None,
        redistributable: same,
        provenance: None,
        allow_no_provenance: !same,
    };
    let pkg = root.join("pkg");
    let mut env = pack_cog(&input, &pkg).unwrap();
    let k = signer();
    sign_envelope(&mut env, &k, &key_id_for(&k.verifying_key().to_bytes())).unwrap();
    write_manifest(&pkg, &env).unwrap();
    pkg
}

pub fn cap(id: &str) -> Capability {
    Capability::new(CapabilityId::new(id).unwrap(), Provenance::Probed)
}

pub fn list(items: &[&str]) -> AttrValue {
    AttrValue::List(items.iter().map(|s| AttrValue::from(*s)).collect())
}

/// Facts of a Linux ARM-class board (a Pi 5).
pub fn board_caps(class: &str) -> Vec<Capability> {
    vec![
        cap(&format!("cpu.arch.{}", arch())),
        cap("os.linux"),
        cap(&format!("node.class.{class}")),
        cap("runtime.native").with_attr("arches_native", list(&[arch()])),
        cap("mem.system").with_attr("free", 4i64 << 30),
    ]
}

/// Facts of the dev Mac (as the macOS probe reports it with OrbStack).
pub fn mac_caps() -> Vec<Capability> {
    vec![
        cap(&format!("cpu.arch.{}", arch())),
        cap("os.macos"),
        cap("node.class.dev-mac"),
        cap("runtime.native").with_attr("arches_native", list(&[arch()])),
        cap("runtime.container.docker")
            .with_attr("arches_native", list(&[arch()]))
            .with_attr("arches_emulated", list(&["armv7", "x86_64"])),
        cap("mem.unified").with_attr("free", 64i64 << 30),
    ]
}

pub fn gate(chain: &Arc<ChainManager>) -> Arc<WorkloadGate> {
    let mut permit = WorkloadPermitRule::new("test-cog", ["workload.*"], ["cog"]);
    permit.max_network = NetworkPolicy::Egress;
    Arc::new(
        WorkloadGate::exempt(0.95, false, "test")
            .with_permit(permit)
            .unwrap()
            .with_chain(chain.clone()),
    )
}

pub fn exchange(id: &str, chain: &Arc<ChainManager>) -> Arc<ArtifactExchange> {
    let mut ex = ArtifactExchange::new(
        id,
        Arc::new(ArtifactStore::new_memory()),
        ExchangeConfig::default(),
    )
    .unwrap();
    ex.set_chain_manager(chain.clone());
    Arc::new(ex)
}

/// A container engine that is not there (tests never run a real engine):
/// every CLI call fails, so a dispatched container variant is refused by
/// the adapter itself.
pub struct NoEngine;

#[async_trait::async_trait]
impl CommandRunner for NoEngine {
    async fn run(
        &self,
        program: &str,
        _: &[String],
        _: std::time::Duration,
        _: usize,
    ) -> Result<CmdOutput, RuntimeError> {
        Err(RuntimeError::Backend(format!("{program}: no engine in tests")))
    }
}

/// One node's `workload-host`.
pub struct HostNode {
    pub svc: Arc<WorkloadHostService>,
    pub chain: Arc<ChainManager>,
    pub id: String,
    pub _tmp: tempfile::TempDir,
}

/// A node with `caps`, a native adapter (`scripts` allows interpreted
/// payloads), accepting requests from `controller`.
pub fn host_node(
    seed: u8,
    caps: Vec<Capability>,
    scripts: bool,
    controller: &SigningKey,
) -> HostNode {
    host_node_with(seed, caps, scripts, controller, gate)
}

/// [`host_node`] with its own governance (`make_gate` gets the node's chain).
pub fn host_node_with(
    seed: u8,
    caps: Vec<Capability>,
    scripts: bool,
    controller: &SigningKey,
    make_gate: impl FnOnce(&Arc<ChainManager>) -> Arc<WorkloadGate>,
) -> HostNode {
    let container = caps
        .iter()
        .any(|c| c.id.as_str().starts_with("runtime.container"));
    host_node_routes(seed, caps, scripts, controller, make_gate, container, None, None)
}

/// [`host_node`] serving only its native adapter, whatever its facts say.
pub fn host_node_native_only(
    seed: u8,
    caps: Vec<Capability>,
    scripts: bool,
    controller: &SigningKey,
) -> HostNode {
    host_node_routes(seed, caps, scripts, controller, gate, false, None, None)
}

/// [`host_node_native_only`] with the ingest bridge wired in.
pub fn host_node_ingest(
    seed: u8,
    caps: Vec<Capability>,
    controller: &SigningKey,
    ingest: crate::cog_ingest::IngestHooks,
) -> HostNode {
    host_node_routes(seed, caps, true, controller, gate, false, Some(ingest), None)
}

/// [`host_node`] with the opt-in controller lease.
pub fn host_node_leased(
    seed: u8,
    caps: Vec<Capability>,
    controller: &SigningKey,
    lease: std::time::Duration,
) -> HostNode {
    host_node_routes(seed, caps, true, controller, gate, false, None, Some(lease))
}

#[allow(clippy::too_many_arguments)]
fn host_node_routes(
    seed: u8,
    caps: Vec<Capability>,
    scripts: bool,
    controller: &SigningKey,
    make_gate: impl FnOnce(&Arc<ChainManager>) -> Arc<WorkloadGate>,
    container: bool,
    ingest: Option<crate::cog_ingest::IngestHooks>,
    lease: Option<std::time::Duration>,
) -> HostNode {
    let key = SigningKey::from_bytes(&[seed; 32]);
    let id = node_id_from_pubkey(&key.verifying_key().to_bytes());
    let chain = Arc::new(ChainManager::new(0, 1000));
    let tmp = tempfile::tempdir().unwrap();
    let gate = make_gate(&chain);
    let native = NativeRuntime::new(NativeConfig {
        root: tmp.path().join("instances"),
        run_as: None,
        allow_interpreted: scripts,
    });
    let host = Arc::new(
        WorkloadHost::new(
            Arc::new(native),
            gate.clone(),
            id.clone(),
            crate::workload_governance::NodeTrustTier::Paired,
        )
        .with_chain(chain.clone()),
    );
    let now = chrono::Utc::now().timestamp() as u64;
    let mut facts = NodeFacts::new(id.clone(), now, 600, 1);
    facts.capabilities = caps.clone();
    let signed = sign_node_facts(&facts, &key).unwrap();
    let mut svc = WorkloadHostService::new(key, exchange(&id, &chain), anchors(), gate.clone())
        .with_route("native", host);
    if container {
        let cfg = ContainerRuntimeConfig::new(
            Engine::Docker,
            format!("debian@sha256:{}", "0".repeat(64)),
            tmp.path().join("containers"),
        );
        let rt = ContainerRuntime::new(cfg, Arc::new(NoEngine));
        let h = WorkloadHost::new(
            Arc::new(rt),
            gate,
            id.clone(),
            crate::workload_governance::NodeTrustTier::Paired,
        )
        .with_chain(chain.clone());
        svc = svc.with_route("container", Arc::new(h));
    }
    if let Some(h) = ingest {
        svc = svc.with_ingest(h);
    }
    if let Some(l) = lease {
        svc = svc.with_lease(l);
    }
    let svc = svc
        .with_controllers(vec![controller.verifying_key().to_bytes()])
        .with_chain(chain.clone());
    svc.set_facts(signed);
    HostNode {
        svc: Arc::new(svc),
        chain,
        id,
        _tmp: tmp,
    }
}

/// The controller with its own chain and gate.
pub fn controller(
    key: &SigningKey,
    connector: Arc<dyn CtlConnector>,
) -> (PlacementControlPlane, Arc<ChainManager>) {
    let chain = Arc::new(ChainManager::new(0, 1000));
    let id = node_id_from_pubkey(&key.verifying_key().to_bytes());
    let plane = PlacementControlPlane::new(
        key.clone(),
        gate(&chain),
        chain.clone(),
        exchange(&id, &chain),
        anchors(),
        connector,
    );
    (plane, chain)
}

/// Payloads of chain events of `kind`.
pub fn events(chain: &ChainManager, kind: &str) -> Vec<(String, Value)> {
    chain
        .tail(chain.len())
        .into_iter()
        .filter(|e| e.kind == kind)
        .map(|e| (e.source, e.payload.unwrap_or(Value::Null)))
        .collect()
}

/// A connector whose nodes can be killed and revived: a dead address
/// refuses every connection, as a powered-off node does.
pub struct Killable {
    inner: super::transport::MeshConnector,
    dead: std::sync::RwLock<std::collections::HashSet<String>>,
}

impl Killable {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            inner: super::transport::MeshConnector::new(false),
            dead: Default::default(),
        })
    }

    /// Serve `svc` in process at `mem://<name>`.
    pub fn serve(&self, name: &str, svc: Arc<WorkloadHostService>) -> String {
        self.inner.register_local(name, svc)
    }

    pub fn kill(&self, addr: &str) {
        self.dead.write().unwrap().insert(addr.to_string());
    }

    pub fn revive(&self, addr: &str) {
        self.dead.write().unwrap().remove(addr);
    }
}

#[async_trait::async_trait]
impl CtlConnector for Killable {
    async fn connect(
        &self,
        addr: &str,
    ) -> Result<Box<dyn crate::mesh::MeshStream>, crate::mesh::MeshError> {
        if self.dead.read().unwrap().contains(addr) {
            return Err(crate::mesh::MeshError::PeerNotConnected(addr.to_string()));
        }
        self.inner.connect(addr).await
    }
}

/// A placement order for `pkg` in `mode`, preferring `prefer` when given.
pub fn mode_order(
    pkg: &Path,
    mode: crate::workload_runtime::RunMode,
    prefer: Option<&str>,
) -> super::plane_place::PlaceOrder {
    super::plane_place::PlaceOrder {
        package_dir: pkg.to_path_buf(),
        config: super::host_service::CtlConfig {
            mode,
            args: vec![],
            csi_port: 15040,
        },
        pin: None,
        prefer: prefer.map(|p| vec![p.to_string()]).unwrap_or_default(),
        avoid: vec![],
        allow_emulated: false,
        start: true,
        dry_run: false,
        project_id: None,
    }
}