loong-kernel 0.1.2-alpha.1

Internal support crate for Loong: kernel primitives and governance core
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
#![allow(clippy::expect_used, clippy::panic)]

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Mutex;

use async_trait::async_trait;
use serde_json::json;

use crate::connector::{ConnectorExtensionAdapter, CoreConnectorAdapter};
use crate::contracts::{
    Capability, ConnectorCommand, ConnectorOutcome, ExecutionRoute, HarnessKind, HarnessOutcome,
    HarnessRequest,
};
use crate::errors::{ConnectorError, PolicyError};
use crate::harness::HarnessAdapter;
use crate::memory::{
    CoreMemoryAdapter, MemoryCoreOutcome, MemoryCoreRequest, MemoryExtensionAdapter,
    MemoryExtensionOutcome, MemoryExtensionRequest,
};
use crate::pack::VerticalPackManifest;
use crate::policy_ext::{PolicyExtension, PolicyExtensionContext};
use crate::runtime::{
    CoreRuntimeAdapter, RuntimeCoreOutcome, RuntimeCoreRequest, RuntimeExtensionAdapter,
    RuntimeExtensionOutcome, RuntimeExtensionRequest,
};
use crate::tool::{
    CoreToolAdapter, ToolCoreOutcome, ToolCoreRequest, ToolExtensionAdapter, ToolExtensionOutcome,
    ToolExtensionRequest,
};

pub struct MockEmbeddedPiHarness {
    pub seen_tasks: Mutex<Vec<String>>,
}
pub struct MockCrmConnector;
pub struct MockCoreConnector;
pub struct MockCoreConnectorGrpc;
pub struct MockPanickingCoreConnector;
pub struct MockConnectorExtension;
pub struct MockPanickingConnectorExtension;
pub struct MockAcpHarness;
pub struct MockCoreRuntime;
pub struct MockCoreRuntimeFallback;
pub struct MockRuntimeExtension;
pub struct MockCoreTool;
pub struct MockToolExtension;
pub struct MockCoreMemory;
pub struct MockMemoryExtension;
pub struct NoNetworkEgressPolicyExtension;
pub const TEST_CAPABILITY_VARIANTS: [Capability; 13] = [
    Capability::InvokeTool,
    Capability::InvokeConnector,
    Capability::MemoryRead,
    Capability::MemoryWrite,
    Capability::FilesystemRead,
    Capability::FilesystemWrite,
    Capability::NetworkEgress,
    Capability::ObserveTelemetry,
    Capability::ControlRead,
    Capability::ControlWrite,
    Capability::ControlApprovals,
    Capability::ControlPairing,
    Capability::ControlAcp,
];
pub const TEST_CAPABILITY_VARIANT_COUNT: u8 = TEST_CAPABILITY_VARIANTS.len() as u8;
#[derive(Debug, Clone, Copy)]
pub enum ToolGateMode {
    Deny,
}
#[derive(Debug)]
pub struct ToolGatePolicyExtension {
    pub gated_tool: String,
    pub mode: ToolGateMode,
}
impl ToolGatePolicyExtension {
    pub fn new(gated_tool: &str, mode: ToolGateMode) -> Self {
        Self {
            gated_tool: gated_tool.to_owned(),
            mode,
        }
    }
}

#[async_trait]
impl HarnessAdapter for MockEmbeddedPiHarness {
    fn name(&self) -> &str {
        "pi-local"
    }
    fn kind(&self) -> HarnessKind {
        HarnessKind::EmbeddedPi
    }
    async fn execute(
        &self,
        request: HarnessRequest,
    ) -> Result<HarnessOutcome, crate::HarnessError> {
        self.seen_tasks
            .lock()
            .expect("mutex poisoned")
            .push(request.task_id.clone());
        Ok(HarnessOutcome {
            status: "ok".to_owned(),
            output: json!({"adapter":"pi-local","task_id":request.task_id,"objective":request.objective}),
        })
    }
}
#[async_trait]
impl CoreConnectorAdapter for MockCrmConnector {
    fn name(&self) -> &str {
        "crm"
    }
    async fn invoke_core(
        &self,
        command: ConnectorCommand,
    ) -> Result<ConnectorOutcome, ConnectorError> {
        Ok(ConnectorOutcome {
            status: "ok".to_owned(),
            payload: json!({"operation":command.operation,"echo":command.payload}),
        })
    }
}
#[async_trait]
impl CoreConnectorAdapter for MockCoreConnector {
    fn name(&self) -> &str {
        "http-core"
    }
    async fn invoke_core(
        &self,
        command: ConnectorCommand,
    ) -> Result<ConnectorOutcome, ConnectorError> {
        Ok(ConnectorOutcome {
            status: "ok".to_owned(),
            payload: json!({"tier":"core","adapter":"http-core","connector":command.connector_name,"operation":command.operation,"payload":command.payload}),
        })
    }
}
#[async_trait]
impl CoreConnectorAdapter for MockCoreConnectorGrpc {
    fn name(&self) -> &str {
        "grpc-core"
    }
    async fn invoke_core(
        &self,
        command: ConnectorCommand,
    ) -> Result<ConnectorOutcome, ConnectorError> {
        Ok(ConnectorOutcome {
            status: "ok".to_owned(),
            payload: json!({"tier":"core","adapter":"grpc-core","connector":command.connector_name,"operation":command.operation}),
        })
    }
}
#[async_trait]
impl CoreConnectorAdapter for MockPanickingCoreConnector {
    fn name(&self) -> &str {
        "panic-core"
    }
    async fn invoke_core(
        &self,
        _command: ConnectorCommand,
    ) -> Result<ConnectorOutcome, ConnectorError> {
        panic!("simulated connector core panic");
    }
}
#[async_trait]
impl ConnectorExtensionAdapter for MockConnectorExtension {
    fn name(&self) -> &str {
        "shielded-bridge"
    }
    async fn invoke_extension(
        &self,
        command: ConnectorCommand,
        core: &(dyn CoreConnectorAdapter + Sync),
    ) -> Result<ConnectorOutcome, ConnectorError> {
        let core_probe = core
            .invoke_core(ConnectorCommand {
                connector_name: command.connector_name.clone(),
                operation: "probe".to_owned(),
                required_capabilities: BTreeSet::new(),
                payload: json!({"mode":"probe"}),
            })
            .await?;
        Ok(ConnectorOutcome {
            status: "ok".to_owned(),
            payload: json!({"tier":"extension","extension":"shielded-bridge","operation":command.operation,"core_probe":core_probe.payload,"payload":command.payload}),
        })
    }
}
#[async_trait]
impl ConnectorExtensionAdapter for MockPanickingConnectorExtension {
    fn name(&self) -> &str {
        "panic-extension"
    }
    async fn invoke_extension(
        &self,
        _command: ConnectorCommand,
        _core: &(dyn CoreConnectorAdapter + Sync),
    ) -> Result<ConnectorOutcome, ConnectorError> {
        panic!("simulated connector extension panic");
    }
}
#[async_trait]
impl HarnessAdapter for MockAcpHarness {
    fn name(&self) -> &str {
        "acp-gateway"
    }
    fn kind(&self) -> HarnessKind {
        HarnessKind::Acp
    }
    async fn execute(
        &self,
        request: HarnessRequest,
    ) -> Result<HarnessOutcome, crate::HarnessError> {
        Ok(HarnessOutcome {
            status: "ok".to_owned(),
            output: json!({"adapter":"acp-gateway","task_id":request.task_id}),
        })
    }
}
#[async_trait]
impl CoreRuntimeAdapter for MockCoreRuntime {
    fn name(&self) -> &str {
        "native-core"
    }
    async fn execute_core(
        &self,
        request: RuntimeCoreRequest,
    ) -> Result<RuntimeCoreOutcome, crate::RuntimePlaneError> {
        Ok(RuntimeCoreOutcome {
            status: "ok".to_owned(),
            payload: json!({"adapter":"native-core","action":request.action,"payload":request.payload}),
        })
    }
}
#[async_trait]
impl CoreRuntimeAdapter for MockCoreRuntimeFallback {
    fn name(&self) -> &str {
        "fallback-core"
    }
    async fn execute_core(
        &self,
        request: RuntimeCoreRequest,
    ) -> Result<RuntimeCoreOutcome, crate::RuntimePlaneError> {
        Ok(RuntimeCoreOutcome {
            status: "ok".to_owned(),
            payload: json!({"adapter":"fallback-core","action":request.action}),
        })
    }
}
#[async_trait]
impl RuntimeExtensionAdapter for MockRuntimeExtension {
    fn name(&self) -> &str {
        "acp-bridge"
    }
    async fn execute_extension(
        &self,
        request: RuntimeExtensionRequest,
        core: &(dyn CoreRuntimeAdapter + Sync),
    ) -> Result<RuntimeExtensionOutcome, crate::RuntimePlaneError> {
        let core_probe = core
            .execute_core(RuntimeCoreRequest {
                action: "probe".to_owned(),
                payload: json!({}),
            })
            .await?;
        Ok(RuntimeExtensionOutcome {
            status: "ok".to_owned(),
            payload: json!({"extension":"acp-bridge","action":request.action,"core_probe":core_probe.payload,"payload":request.payload}),
        })
    }
}
#[async_trait]
impl CoreToolAdapter for MockCoreTool {
    fn name(&self) -> &str {
        "core-tools"
    }

    async fn execute_core_tool(
        &self,
        request: ToolCoreRequest,
    ) -> Result<ToolCoreOutcome, crate::ToolPlaneError> {
        Ok(ToolCoreOutcome {
            status: "ok".to_owned(),
            payload: json!({"tool":request.tool_name,"payload":request.payload}),
        })
    }
}
#[async_trait]
impl ToolExtensionAdapter for MockToolExtension {
    fn name(&self) -> &str {
        "sql-analytics"
    }
    async fn execute_tool_extension(
        &self,
        request: ToolExtensionRequest,
        core: &(dyn CoreToolAdapter + Sync),
    ) -> Result<ToolExtensionOutcome, crate::ToolPlaneError> {
        let core_probe = core
            .execute_core_tool(ToolCoreRequest {
                tool_name: "schema_probe".to_owned(),
                payload: json!({}),
            })
            .await?;
        Ok(ToolExtensionOutcome {
            status: "ok".to_owned(),
            payload: json!({"extension":"sql-analytics","action":request.extension_action,"core_probe":core_probe.payload}),
        })
    }
}
#[async_trait]
impl CoreMemoryAdapter for MockCoreMemory {
    fn name(&self) -> &str {
        "kv-core"
    }
    async fn execute_core_memory(
        &self,
        request: MemoryCoreRequest,
    ) -> Result<MemoryCoreOutcome, crate::MemoryPlaneError> {
        Ok(MemoryCoreOutcome {
            status: "ok".to_owned(),
            payload: json!({"operation":request.operation,"payload":request.payload}),
        })
    }
}
#[async_trait]
impl MemoryExtensionAdapter for MockMemoryExtension {
    fn name(&self) -> &str {
        "vector-index"
    }
    async fn execute_memory_extension(
        &self,
        request: MemoryExtensionRequest,
        core: &(dyn CoreMemoryAdapter + Sync),
    ) -> Result<MemoryExtensionOutcome, crate::MemoryPlaneError> {
        let core_probe = core
            .execute_core_memory(MemoryCoreRequest {
                operation: "read".to_owned(),
                payload: json!({"key":"seed"}),
            })
            .await?;
        Ok(MemoryExtensionOutcome {
            status: "ok".to_owned(),
            payload: json!({"extension":"vector-index","operation":request.operation,"core_probe":core_probe.payload}),
        })
    }
}
impl PolicyExtension for NoNetworkEgressPolicyExtension {
    fn name(&self) -> &str {
        "no-network-egress"
    }
    fn authorize_extension(&self, context: &PolicyExtensionContext<'_>) -> Result<(), PolicyError> {
        if context
            .required_capabilities
            .contains(&Capability::NetworkEgress)
        {
            return Err(PolicyError::ExtensionDenied {
                extension: self.name().to_owned(),
                reason: "network egress is blocked for this environment".to_owned(),
            });
        }
        Ok(())
    }
}
impl PolicyExtension for ToolGatePolicyExtension {
    fn name(&self) -> &str {
        "tool-gate"
    }
    fn authorize_extension(&self, context: &PolicyExtensionContext<'_>) -> Result<(), PolicyError> {
        let Some(params) = context.request_parameters else {
            return Ok(());
        };
        let tool_name = params
            .get("tool_name")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        if tool_name != self.gated_tool {
            return Ok(());
        }
        match self.mode {
            ToolGateMode::Deny => Err(PolicyError::ToolCallDenied {
                tool_name: tool_name.to_owned(),
                reason: "blocked by deterministic policy rule".to_owned(),
            }),
        }
    }
}
pub fn sample_pack() -> VerticalPackManifest {
    VerticalPackManifest {
        pack_id: "sales-intel".to_owned(),
        domain: "sales".to_owned(),
        version: "0.1.0".to_owned(),
        default_route: ExecutionRoute {
            harness_kind: HarnessKind::EmbeddedPi,
            adapter: Some("pi-local".to_owned()),
        },
        allowed_connectors: BTreeSet::from(["crm".to_owned()]),
        granted_capabilities: BTreeSet::from([
            Capability::InvokeTool,
            Capability::InvokeConnector,
            Capability::MemoryRead,
        ]),
        metadata: BTreeMap::from([("owner".to_owned(), "revenue-team".to_owned())]),
    }
}
pub fn acp_pack_without_explicit_adapter() -> VerticalPackManifest {
    VerticalPackManifest {
        pack_id: "code-review".to_owned(),
        domain: "engineering".to_owned(),
        version: "0.1.0".to_owned(),
        default_route: ExecutionRoute {
            harness_kind: HarnessKind::Acp,
            adapter: None,
        },
        allowed_connectors: BTreeSet::new(),
        granted_capabilities: BTreeSet::from([Capability::InvokeTool]),
        metadata: BTreeMap::new(),
    }
}
pub fn capability_from_bit(bit: u8) -> Capability {
    let bit_index = usize::from(bit);
    TEST_CAPABILITY_VARIANTS
        .get(bit_index)
        .copied()
        .expect("test capability bit should be in range")
}
pub fn capability_set_from_mask(mask: u16) -> BTreeSet<Capability> {
    let mut capabilities = BTreeSet::new();
    for (bit_index, capability) in TEST_CAPABILITY_VARIANTS.iter().copied().enumerate() {
        let bit_mask = 1_u16 << bit_index;
        let is_enabled = (mask & bit_mask) != 0;
        if is_enabled {
            capabilities.insert(capability);
        }
    }
    capabilities
}