khive-runtime 0.12.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
//! Runtime-owned entity candidate admission. Production snapshots stay empty.

use std::sync::Arc;

use khive_storage::Entity;
use serde_json::Value;

use super::manifest::{digest_to_hex, scoped_digest, ManifestSnapshot, RuntimeFieldScope};
use super::outcome::{FinalizerOutcome, ManifestFault};
use crate::{EventAttribution, NamespaceToken, RuntimeError, RuntimeResult};

#[cfg(test)]
pub(crate) mod fixture;

pub(crate) const EXEMPTION_STAMP: &str = "exempted:content-sha256-manifest-v1";

#[derive(Debug, Clone, Copy)]
pub(crate) enum EntityEntryPoint {
    Create,
    Claim,
    Update,
    Bulk,
    CodeIngest,
    CodeFindingsIngest,
}

impl EntityEntryPoint {
    pub(crate) fn family(self, replacing: bool) -> &'static str {
        match self {
            Self::Update => "entity.update",
            Self::Bulk => "entity.bulk",
            Self::CodeIngest | Self::CodeFindingsIngest if replacing => "entity.update",
            _ => "entity.create",
        }
    }

    pub(crate) fn verb(self) -> &'static str {
        match self {
            Self::Update => "update",
            Self::CodeIngest => "code.ingest",
            Self::CodeFindingsIngest => "code.findings_ingest",
            _ => "create",
        }
    }
}

/// Closed direct-ingest origins; callers cannot supply audit verb strings.
#[derive(Debug, Clone, Copy)]
pub enum EntityCandidateOrigin {
    CodeIngest,
    CodeFindingsIngest,
}

/// An immutable admission snapshot captured before direct-ingest preflight.
/// Clones retain that same snapshot through bounded CAS retries.
#[derive(Debug, Clone)]
pub struct EntityCandidateContext {
    namespace: String,
    snapshot: Arc<ManifestSnapshot>,
    entry: EntityEntryPoint,
    record: String,
}

/// A checked direct-ingest candidate, without a caller-visible runtime stamp.
#[derive(Debug)]
pub struct EntityCandidatePrepared {
    context: EntityCandidateContext,
    candidate: Entity,
}

impl EntityCandidatePrepared {
    pub fn entity(&self) -> &Entity {
        &self.candidate
    }

    pub(crate) fn admit(self, token: &NamespaceToken) -> RuntimeResult<EntityAdmission> {
        if token.namespace().as_str() != self.context.namespace {
            return Err(RuntimeError::InvalidInput(
                "direct-ingest candidate namespace differs from its authorization token".into(),
            ));
        }
        admit(self.context, token, self.candidate)
    }
}

impl EntityCandidateContext {
    pub fn new(namespace: &str, origin: EntityCandidateOrigin) -> Self {
        Self::capture(
            namespace,
            match origin {
                EntityCandidateOrigin::CodeIngest => EntityEntryPoint::CodeIngest,
                EntityCandidateOrigin::CodeFindingsIngest => EntityEntryPoint::CodeFindingsIngest,
            },
        )
    }

    fn capture(namespace: &str, entry: EntityEntryPoint) -> Self {
        #[cfg(test)]
        let snapshot = fixture::snapshot(namespace);
        #[cfg(not(test))]
        let snapshot = ManifestSnapshot::empty();
        Self {
            namespace: namespace.into(),
            snapshot,
            entry,
            record: "entity".into(),
        }
    }

    pub fn check_name_description(&self, value: &str) -> RuntimeResult<()> {
        self.check_value(RuntimeFieldScope::NameDescription, value)
    }

    pub fn check_properties(&self, properties: Option<&Value>) -> RuntimeResult<()> {
        crate::secret_gate::reject_reserved_secret_gate_property(properties)?;
        if let Some(value) = properties {
            let mut values = Vec::new();
            property_strings(value, &mut values);
            for value in values {
                self.check_value(RuntimeFieldScope::JsonProperties, value)?;
            }
        }
        Ok(())
    }

    pub fn check_tags(&self, tags: &[String]) -> RuntimeResult<()> {
        for tag in tags {
            self.check_value(RuntimeFieldScope::Tags, tag)?;
        }
        Ok(())
    }

    /// Consume one clone for the exact final candidate; no database is opened.
    pub fn prepare(self, candidate: Entity) -> RuntimeResult<EntityCandidatePrepared> {
        if candidate.namespace != self.namespace {
            return Err(RuntimeError::InvalidInput(
                "direct-ingest candidate namespace changed after preflight".into(),
            ));
        }
        let matched = scan_candidate(&self, &candidate)?;
        // This direct lane writes structural rows, not attachment state. A
        // committed receipt must not echo an unverified attachment projection.
        if matched.is_some() && candidate.content_ref.is_some() {
            return Err(RuntimeError::InvalidInput(
                "manifest direct-ingest candidates must not carry content_ref; use the attachment-aware entity constructor".into(),
            ));
        }
        Ok(EntityCandidatePrepared {
            context: self,
            candidate,
        })
    }

    fn check_value(&self, scope: RuntimeFieldScope, value: &str) -> RuntimeResult<()> {
        if self.snapshot.lookup(scope, value).is_some() {
            Ok(())
        } else {
            crate::secret_gate::check(value)
        }
    }
}

pub(crate) enum EntityAdmission {
    Legacy(Entity),
    Exempt(PreparedEntityExemption),
}

/// The ordinary patch route retains its historical patch-only scanner when
/// no manifest is active, while a matched candidate uses final-object checks.
pub(crate) struct OrdinaryEntityUpdateContext(EntityCandidateContext);

impl OrdinaryEntityUpdateContext {
    pub(crate) fn capture(token: &NamespaceToken) -> Self {
        Self(EntityCandidateContext::capture(
            token.namespace().as_str(),
            EntityEntryPoint::Update,
        ))
    }

    pub(crate) fn check_patch(
        &self,
        name: Option<&str>,
        description: Option<&str>,
        properties: Option<&Value>,
        tags: Option<&[String]>,
    ) -> RuntimeResult<()> {
        crate::secret_gate::reject_reserved_secret_gate_property(properties)?;
        if let Some(name) = name {
            crate::secret_gate::locate(self.0.check_name_description(name), "entity", "name")?;
        }
        if let Some(description) = description {
            crate::secret_gate::locate(
                self.0.check_name_description(description),
                "entity",
                "description",
            )?;
        }
        if let Some(properties) = properties {
            crate::secret_gate::locate(
                self.0.check_properties(Some(properties)),
                "entity",
                "properties",
            )?;
        }
        if let Some(tags) = tags {
            crate::secret_gate::locate(self.0.check_tags(tags), "entity", "tags")?;
        }
        Ok(())
    }

    pub(crate) fn admit(
        self,
        token: &NamespaceToken,
        candidate: Entity,
    ) -> RuntimeResult<EntityAdmission> {
        if self.0.snapshot.is_empty() {
            crate::secret_gate::reject_reserved_secret_gate_property(
                candidate.properties.as_ref(),
            )?;
            reject_injected_manifest_fault(&candidate.namespace)?;
            Ok(EntityAdmission::Legacy(candidate))
        } else {
            admit(self.0, token, candidate)
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct PreparedEntityExemption {
    pub(super) entity: Entity,
    pub(super) snapshot: Arc<ManifestSnapshot>,
    pub(super) entry: EntityEntryPoint,
    pub(super) attribution: EventAttribution,
    pub(super) digest_sha256: String,
    pub(super) field_scope: &'static str,
    pub(super) overridden_detector: String,
}

impl PreparedEntityExemption {
    /// The finalized, runtime-stamped candidate. Do not pass it back through
    /// caller-input reservation checks; the opaque plan owns its write binding.
    pub(crate) fn entity(&self) -> &Entity {
        &self.entity
    }
}

pub(crate) fn prepare_entity_admission(
    token: &NamespaceToken,
    entry: EntityEntryPoint,
    candidate: Entity,
) -> RuntimeResult<EntityAdmission> {
    prepare_entity_admission_at(token, entry, candidate, "entity")
}

pub(crate) fn prepare_entity_admission_at(
    token: &NamespaceToken,
    entry: EntityEntryPoint,
    candidate: Entity,
    record: &str,
) -> RuntimeResult<EntityAdmission> {
    let mut context = EntityCandidateContext::capture(&candidate.namespace, entry);
    context.record = record.into();
    admit(context, token, candidate)
}

fn admit(
    context: EntityCandidateContext,
    token: &NamespaceToken,
    mut candidate: Entity,
) -> RuntimeResult<EntityAdmission> {
    reject_injected_manifest_fault(&candidate.namespace)?;
    let matched = scan_candidate(&context, &candidate)?;
    let Some((scope, digest_sha256, overridden_detector)) = matched else {
        return Ok(EntityAdmission::Legacy(candidate));
    };
    let properties = candidate
        .properties
        .get_or_insert_with(|| serde_json::json!({}));
    let Some(properties) = properties.as_object_mut() else {
        return Err(RuntimeError::InvalidInput(
            "manifest-exempt entity properties must be an object".into(),
        ));
    };
    properties.insert(
        crate::secret_gate::RESERVED_SECRET_GATE_KEY.into(),
        EXEMPTION_STAMP.into(),
    );
    let record_token = token.with_namespace(
        crate::Namespace::parse(&candidate.namespace)
            .map_err(|_| RuntimeError::InvalidInput("invalid entity namespace".into()))?,
    );
    Ok(EntityAdmission::Exempt(PreparedEntityExemption {
        entity: candidate,
        snapshot: context.snapshot,
        entry: context.entry,
        attribution: EventAttribution::from_token(&record_token),
        digest_sha256,
        field_scope: match scope {
            RuntimeFieldScope::NameDescription => "name-description",
            RuntimeFieldScope::JsonProperties => "json-properties",
            RuntimeFieldScope::Tags => "tags",
            RuntimeFieldScope::RecordContent => "record-content",
            RuntimeFieldScope::CodeSource => "code-source",
        },
        overridden_detector,
    }))
}

fn reject_injected_manifest_fault(namespace: &str) -> RuntimeResult<()> {
    if let Some(fault) = super::faults::consume_manifest_invalid(namespace) {
        observe(namespace, FinalizerOutcome::ManifestInvalid(fault));
        return Err(RuntimeError::InvalidInput(
            "secret-gate manifest invalid".into(),
        ));
    }
    Ok(())
}

type CandidateMatch = (RuntimeFieldScope, String, String);

fn scan_candidate(
    context: &EntityCandidateContext,
    entity: &Entity,
) -> RuntimeResult<Option<CandidateMatch>> {
    crate::secret_gate::reject_reserved_secret_gate_property(entity.properties.as_ref())?;
    if context.snapshot.is_empty() {
        crate::secret_gate::check_at(&entity.name, &context.record, "name")?;
        if let Some(description) = &entity.description {
            crate::secret_gate::check_at(description, &context.record, "description")?;
        }
        if let Some(properties) = &entity.properties {
            crate::secret_gate::check_json_at(properties, &context.record, "properties")?;
        }
        crate::secret_gate::check_tags_at(&entity.tags, &context.record, "tags")?;
        return Ok(None);
    }
    let mut values = vec![(
        RuntimeFieldScope::NameDescription,
        entity.name.as_str(),
        "name",
    )];
    if let Some(description) = &entity.description {
        values.push((
            RuntimeFieldScope::NameDescription,
            description,
            "description",
        ));
    }
    if let Some(properties) = &entity.properties {
        let mut leaves = Vec::new();
        property_strings(properties, &mut leaves);
        values.extend(
            leaves
                .into_iter()
                .map(|value| (RuntimeFieldScope::JsonProperties, value, "properties")),
        );
    }
    values.extend(
        entity
            .tags
            .iter()
            .map(|value| (RuntimeFieldScope::Tags, value.as_str(), "tags")),
    );
    let mut found: Option<CandidateMatch> = None;
    for (scope, value, field) in values {
        if let Some(meta) = context.snapshot.lookup(scope, value) {
            let digest = digest_to_hex(&scoped_digest(scope, value));
            if found.as_ref().is_some_and(|(old_scope, old_digest, _)| {
                *old_scope != scope || *old_digest != digest
            }) {
                observe(
                    &entity.namespace,
                    FinalizerOutcome::ManifestInvalid(ManifestFault::MultipleMatches),
                );
                return Err(RuntimeError::InvalidInput(
                    "secret-gate manifest has multiple candidate matches".into(),
                ));
            }
            found = Some((scope, digest, meta.overridden_detector.clone()));
        } else {
            crate::secret_gate::check_at(value, &context.record, field)?;
        }
    }
    Ok(found)
}

fn property_strings<'a>(value: &'a Value, values: &mut Vec<&'a str>) {
    match value {
        Value::String(value) => values.push(value),
        Value::Array(array) => array
            .iter()
            .for_each(|value| property_strings(value, values)),
        Value::Object(object) => {
            for (key, value) in object {
                values.push(key);
                property_strings(value, values);
            }
        }
        _ => {}
    }
}

pub(super) fn observe(namespace: &str, outcome: FinalizerOutcome) {
    #[cfg(test)]
    fixture::record_outcome(namespace, outcome);
    #[cfg(not(test))]
    let _ = (namespace, outcome);
}