deepstrike-core 0.2.70

Cross-language agent runtime kernel — pure computation, zero I/O
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
471
472
473
474
475
476
477
//! Binding-safe façade for the canonical durable transition protocol.
//!
//! [`CanonicalKernel`] is the public pair that hosts need: the transaction decides whether an
//! envelope is accepted, while the driver decides what the accepted input means. Keeping the pair
//! behind one handle makes two invalid call sequences unavailable to bindings:
//!
//! * a host cannot call the semantic planner without first preparing a durable record;
//! * a host cannot commit the transaction without also advancing the driver's committed fold.
//!
//! There is intentionally no `step` method. Production callers must prepare, CAS-append the exact
//! core-produced record bytes, then commit. Checkpoint restore mutates the handle in place so a
//! binding object keeps its identity across a CAS rebuild.

use super::checkpoint::{
    CheckpointCandidate, KernelCheckpoint, LogicalKernelState, LogicalStateProjection,
};
use super::config::ConfigDefaults;
use super::driver::{CanonicalOperationDriver, PlannedStep};
use super::effect::{Digest, KernelEffect};
use super::envelope::{
    OperationLifecycle, WireEnvelope, WireRejection, WireRejectionKind, decode_envelope_json,
};
use super::fault::{
    KernelFault, KernelFaultCode, KernelPreparation, PrepareToken, RejectedTransition,
};
use super::record::{KernelRecord, RecordPreparation};
use super::restore::{RestoreCost, restore_operation};
use super::terminal::KernelTerminal;
use super::transaction::{
    CheckpointBoundary, CommittedTransition, DurableHead, InMemoryRecordIndex, KernelTransaction,
    TailUsage,
};

type CanonicalTransaction = KernelTransaction<PlannedStep, InMemoryRecordIndex>;

enum DriverRestorePoint {
    Fresh,
    Logical(Box<LogicalKernelState>),
}

/// One canonical operation driven exclusively through the durable transition protocol.
///
/// The type is deliberately not `Clone`: duplicating a live candidate would make two handles able
/// to commit the same prepare token. Rebuild instead uses [`Self::restore`] or
/// [`Self::restore_bytes`], both of which replace this handle's internals in place.
pub struct CanonicalKernel {
    defaults: ConfigDefaults,
    transaction: CanonicalTransaction,
    driver: CanonicalOperationDriver,
    before_candidate: Option<DriverRestorePoint>,
}

impl std::fmt::Debug for CanonicalKernel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CanonicalKernel")
            .field("operation_id", &self.transaction.operation_id())
            .field("head", &self.transaction.head())
            .field("lifecycle", &self.transaction.lifecycle())
            .field("has_candidate", &self.transaction.has_candidate())
            .finish()
    }
}

impl Default for CanonicalKernel {
    fn default() -> Self {
        Self::new(ConfigDefaults::default())
    }
}

impl CanonicalKernel {
    /// Construct an empty operation under explicit compile-time defaults/bootstrap ceilings.
    pub fn new(defaults: ConfigDefaults) -> Self {
        Self {
            transaction: KernelTransaction::new(defaults.clone(), InMemoryRecordIndex::new()),
            driver: CanonicalOperationDriver::new(),
            defaults,
            before_candidate: None,
        }
    }

    /// Strict JSON boundary for dynamic-language bindings.
    ///
    /// Decode failures use the same closed `Rejected` arm as typed policy/lifecycle failures; a
    /// malformed payload never escapes as one language's parser exception.
    pub fn prepare_json(&mut self, input_json: &str) -> RecordPreparation<PlannedStep> {
        match decode_envelope_json(input_json, &self.defaults.bootstrap_limits) {
            Ok(envelope) => self.prepare(&envelope),
            Err(rejection) => KernelPreparation::Rejected(RejectedTransition {
                fault: rejection_fault(rejection),
            }),
        }
    }

    /// Plan one typed canonical envelope and stage its core-produced record.
    pub fn prepare(&mut self, envelope: &WireEnvelope) -> RecordPreparation<PlannedStep> {
        // Preserve an existing candidate. `KernelTransaction::prepare` will return the structured
        // transaction-conflict rejection before invoking this closure.
        if self.before_candidate.is_some() {
            let Self {
                transaction,
                driver,
                ..
            } = self;
            return transaction.prepare(envelope, |context| driver.plan(context));
        }

        let restore_point = match self.capture_driver_restore_point() {
            Ok(restore_point) => restore_point,
            Err(fault) => {
                return KernelPreparation::Rejected(RejectedTransition { fault });
            }
        };
        let preparation = {
            let Self {
                transaction,
                driver,
                ..
            } = self;
            transaction.prepare(envelope, |context| driver.plan(context))
        };

        if matches!(preparation, KernelPreparation::Prepared(_)) {
            self.before_candidate = Some(restore_point);
            return preparation;
        }

        // A planner can advance the semantic engine before a later record/tail screen rejects the
        // preparation. The transaction promises zero mutation for this arm, so restore the driver
        // to make the promise true for the pair as well.
        if let Err(fault) = self.restore_driver(restore_point) {
            return KernelPreparation::Rejected(RejectedTransition { fault });
        }
        preparation
    }

    /// Complete a transition after the host durably appended the candidate record.
    pub fn commit(
        &mut self,
        token: &PrepareToken,
        appended_head: &Digest,
    ) -> Result<CommittedTransition<PlannedStep>, KernelFault> {
        let committed = self.transaction.commit(token, appended_head)?;
        let step_seq = committed.step_seq;
        self.before_candidate = None;
        self.driver.note_committed(step_seq)?;
        Ok(committed)
    }

    /// Discard a candidate that did not reach the journal and restore the semantic driver.
    pub fn abort(&mut self, token: &PrepareToken) -> Result<KernelRecord, KernelFault> {
        let record = self.transaction.abort(token)?;
        let restore_point = self.before_candidate.take().ok_or_else(|| {
            KernelFault::new(
                KernelFaultCode::TransactionConflict,
                "the transaction aborted a candidate but the canonical driver has no restore point",
            )
        })?;
        self.restore_driver(restore_point)?;
        Ok(record)
    }

    /// Rebuild this exact handle from a verified checkpoint plus records above it.
    ///
    /// `checkpoint = None` means the records are the whole retained journal and the fold starts at
    /// genesis. With a checkpoint, `records` are strictly above `through_step_seq`.
    pub fn restore(
        &mut self,
        checkpoint: Option<&KernelCheckpoint>,
        records: &[KernelRecord],
    ) -> Result<RestoreCost, KernelFault> {
        let restored = restore_operation(
            checkpoint,
            records,
            self.defaults.clone(),
            InMemoryRecordIndex::from_records(records),
        )?;
        let cost = restored.cost;
        self.transaction = restored.transaction;
        self.driver = restored.driver;
        self.before_candidate = None;
        Ok(cost)
    }

    /// Binding boundary for native checkpoint/record byte containers.
    pub fn restore_bytes(
        &mut self,
        checkpoint_bytes: Option<&[u8]>,
        record_bytes: &[Vec<u8>],
    ) -> Result<RestoreCost, KernelFault> {
        let checkpoint = checkpoint_bytes
            .map(KernelCheckpoint::from_checkpoint_bytes)
            .transpose()
            .map_err(|error| error.fault())?;
        let records = record_bytes
            .iter()
            .map(|bytes| {
                KernelRecord::from_record_bytes(bytes)
                    .map_err(|error| KernelFault::new(error.code(), error.message().to_string()))
            })
            .collect::<Result<Vec<_>, _>>()?;
        self.restore(checkpoint.as_ref(), &records)
    }

    /// Produce a full-state checkpoint candidate over the current durable head.
    pub fn checkpoint_candidate(&self) -> Result<CheckpointCandidate, KernelFault> {
        self.transaction
            .checkpoint_candidate(self.driver.project_logical_state())
    }

    /// Produce the incremental checkpoint form over a previously captured logical base.
    pub fn checkpoint_rebase(
        &self,
        base: &KernelCheckpoint,
    ) -> Result<CheckpointCandidate, KernelFault> {
        self.transaction
            .checkpoint_rebase(&base.boundary(), base.logical_state().clone())
    }

    /// Close the retention boundary after the host durably acknowledged a checkpoint install.
    pub fn note_checkpoint_acked(
        &mut self,
        boundary: &CheckpointBoundary,
    ) -> Result<TailUsage, KernelFault> {
        self.transaction.note_checkpoint_acked(boundary)
    }

    pub fn head(&self) -> Option<DurableHead> {
        self.transaction.head()
    }

    pub fn lifecycle(&self) -> OperationLifecycle {
        self.transaction.lifecycle()
    }

    pub fn pending_effects(&self) -> impl Iterator<Item = &KernelEffect> {
        self.transaction.pending_effects()
    }

    /// [`Self::pending_effects`] in publication order — the order a host consumes a
    /// multi-effect step in (see [`KernelTransaction::pending_effects_in_order`]).
    pub fn pending_effects_in_order(&self) -> Vec<&KernelEffect> {
        self.transaction.pending_effects_in_order()
    }

    /// The single host-facing current-action projection.  The transaction owns publication
    /// ordering; this method only delegates the ordered view to the pure projection module.
    pub fn projection(
        &self,
    ) -> Result<super::projection::KernelProjection, super::projection::ProjectionError> {
        super::projection::project_pending_action(
            self.transaction.terminal(),
            self.transaction.pending_effects_in_order(),
        )
    }

    pub fn terminal(&self) -> Option<&KernelTerminal> {
        self.transaction.terminal()
    }

    /// Return the live kernel-issued attempt for a child task, including after checkpoint restore.
    pub fn attempt_id(&self, task_id: &str) -> Option<super::scalar::AttemptId> {
        self.driver.attempt_id(task_id).cloned()
    }

    /// Current scheduler turn, restored from the canonical journal/checkpoint state.
    pub fn turn(&self) -> u32 {
        self.driver.engine().map_or(0, |engine| engine.turn)
    }

    /// Recovery replay budget in bytes, when the operation has been configured.
    pub fn recovery_content_bytes(&self) -> Option<usize> {
        self.driver.engine().map(|engine| {
            let tokens = engine
                .ctx
                .config
                .recovery_content_tokens(engine.ctx.max_tokens);
            engine.ctx.engine.token_budget_to_bytes(tokens)
        })
    }

    /// Task-state references that context pressure must keep resident.
    pub fn preserved_refs(&self) -> Vec<String> {
        self.driver
            .engine()
            .map(|engine| engine.ctx.partitions.task_state.preserved_refs.clone())
            .unwrap_or_default()
    }

    /// Count text using the configured canonical context token engine.
    pub fn count_tokens(&self, text: &str) -> Option<u32> {
        self.driver
            .engine()
            .map(|engine| engine.ctx.engine.count(text))
    }

    /// Cumulative kernel-owned child spawn count for this operation.
    pub fn local_subagents_spawned(&self) -> u32 {
        self.driver
            .engine()
            .map_or(0, |engine| engine.local_subagents_spawned())
    }

    /// Messages added to the canonical operation history.
    pub fn new_messages(&self) -> Vec<crate::types::message::CoreMessage> {
        self.driver
            .engine()
            .map(|engine| engine.drain_new_messages())
            .unwrap_or_default()
    }

    pub fn poison(&self) -> Option<&KernelFault> {
        self.transaction.poison().or_else(|| self.driver.poison())
    }

    fn capture_driver_restore_point(&self) -> Result<DriverRestorePoint, KernelFault> {
        if self.transaction.config().is_none() {
            return Ok(DriverRestorePoint::Fresh);
        }
        let LogicalStateProjection {
            root_kind,
            focus,
            syscall,
            scheduler,
            context_vm,
        } = self.driver.project_logical_state();
        let transition = self
            .transaction
            .transition_state_for_restore(root_kind, focus)?;
        Ok(DriverRestorePoint::Logical(Box::new(LogicalKernelState {
            transition,
            syscall,
            scheduler,
            context_vm,
        })))
    }

    fn restore_driver(&mut self, restore_point: DriverRestorePoint) -> Result<(), KernelFault> {
        self.driver = match restore_point {
            DriverRestorePoint::Fresh => CanonicalOperationDriver::new(),
            DriverRestorePoint::Logical(state) => CanonicalOperationDriver::restore_logical_state(
                &state.transition.resolved_config,
                &state,
            )?,
        };
        Ok(())
    }
}

fn rejection_fault(rejection: WireRejection) -> KernelFault {
    let code = match rejection.kind {
        WireRejectionKind::PolicyViolation => KernelFaultCode::InvalidConfig,
        _ => KernelFaultCode::MalformedEnvelope,
    };
    KernelFault::new(code, rejection.message)
}

#[cfg(test)]
mod tests {
    use serde_json::Value;

    use super::CanonicalKernel;
    use crate::runtime::kernel::wire::{
        KernelFaultCode, KernelPreparation, PrepareToken, WireEnvelope,
    };

    fn golden_agent_root() -> Value {
        serde_json::from_str(include_str!(
            "../../../../../../tests/fixtures/kernel-wire/golden_lifecycle_agent_root.json"
        ))
        .expect("golden fixture")
    }

    fn commit_input(kernel: &mut CanonicalKernel, input: &str) {
        let prepared = kernel.prepare_json(input);
        let KernelPreparation::Prepared(prepared) = prepared else {
            panic!("expected prepared transition");
        };
        kernel
            .commit(&prepared.token, prepared.record.record_digest())
            .expect("commit");
    }

    #[test]
    fn canonical_kernel_produces_the_shared_genesis_record() {
        let fixture = golden_agent_root();
        let mut kernel = CanonicalKernel::default();
        let preparation = kernel.prepare_json(&fixture["links"][0]["envelope"].to_string());
        let KernelPreparation::Prepared(prepared) = preparation else {
            panic!("golden envelope must prepare");
        };

        assert_eq!(
            prepared.record.record_digest().as_str(),
            fixture["genesis_digest"].as_str().unwrap()
        );
        assert_eq!(
            std::str::from_utf8(prepared.record.record_bytes().as_slice()).unwrap(),
            serde_json::to_string(&fixture["links"][0]["record"]).unwrap()
        );
    }

    #[test]
    fn abort_restores_the_driver_before_the_next_prepare() {
        let fixture = golden_agent_root();
        let mut kernel = CanonicalKernel::default();
        commit_input(&mut kernel, &fixture["links"][0]["envelope"].to_string());

        let start = fixture["links"][1]["envelope"].clone();
        let first = kernel.prepare_json(&start.to_string());
        let KernelPreparation::Prepared(first) = first else {
            panic!("start must prepare");
        };
        let first_digest = first.record.record_digest().clone();
        kernel.abort(&first.token).expect("abort before append");

        let second = kernel.prepare_json(&start.to_string());
        let KernelPreparation::Prepared(second) = second else {
            panic!("the same input must prepare after abort");
        };
        assert_eq!(second.record.record_digest(), &first_digest);
    }

    #[test]
    fn malformed_and_unknown_envelopes_are_structured_rejections() {
        let mut kernel = CanonicalKernel::default();
        let malformed = kernel.prepare_json("{");
        assert_eq!(
            malformed.fault().map(|fault| fault.code),
            Some(KernelFaultCode::MalformedEnvelope)
        );

        let fixture = golden_agent_root();
        let mut unknown = fixture["links"][0]["envelope"].clone();
        unknown
            .as_object_mut()
            .unwrap()
            .insert("session_id".to_string(), Value::String("host-only".into()));
        let rejected = kernel.prepare_json(&unknown.to_string());
        assert_eq!(
            rejected.fault().map(|fault| fault.code),
            Some(KernelFaultCode::MalformedEnvelope)
        );
    }

    #[test]
    fn restore_replaces_the_same_typed_handle() {
        let fixture = golden_agent_root();
        let mut kernel = CanonicalKernel::default();
        commit_input(&mut kernel, &fixture["links"][0]["envelope"].to_string());
        let checkpoint = kernel
            .checkpoint_candidate()
            .expect("configured operation checkpoints")
            .decode()
            .expect("checkpoint verifies");

        let start: WireEnvelope =
            serde_json::from_value(fixture["links"][1]["envelope"].clone()).unwrap();
        let KernelPreparation::Prepared(prepared) = kernel.prepare(&start) else {
            panic!("start prepares");
        };
        let post_checkpoint_record = prepared.record.clone();
        let expected_head = prepared.record.record_digest().clone();
        kernel
            .commit(&prepared.token, prepared.record.record_digest())
            .unwrap();

        let handle_address = std::ptr::addr_of!(kernel);
        kernel
            .restore(Some(&checkpoint), &[post_checkpoint_record])
            .expect("restore");
        assert_eq!(std::ptr::addr_of!(kernel), handle_address);
        assert_eq!(kernel.head().unwrap().digest, expected_head);

        let no_candidate = PrepareToken::new("no-candidate").unwrap();
        assert!(kernel.abort(&no_candidate).is_err());
    }
}