a3s-code-core 8.0.3

A3S Code Core - Embeddable AI agent library with tool execution
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use std::collections::{BTreeMap, VecDeque};
use std::fmt;
use std::sync::{Arc, Mutex};

use tokio::time::Instant;

use crate::cognitive_context::CognitiveContextSession;
use crate::commands::SlashCommand;
use crate::context::ContextProvider;
use crate::hooks::HookBinding;
use crate::skills::Skill;
use crate::subagent::AgentDefinition;
use crate::tools::Tool;

use super::{
    CapabilityEffect, CapabilityId, CapabilityKind, CapabilityProjectionError,
    CapabilityReadinessPlan, CapabilitySet, CapabilityValue, CodeCatalogGeneration, FlowBinding,
    KnowledgeSurfaceBinding, McpBinding, ScopeClosePolicy, Sha256Digest, UiBinding,
    UseGenerationLeaseProvider,
};

/// Immutable pairing of one identity set with exactly one typed runtime value
/// for every descriptor.
#[derive(Debug)]
pub struct CapabilityProjection {
    set: Arc<CapabilitySet>,
    readiness: Arc<CapabilityReadinessPlan>,
    values: BTreeMap<CapabilityId, CapabilityValue>,
}

impl CapabilityProjection {
    pub fn new(
        set: Arc<CapabilitySet>,
        values: impl IntoIterator<Item = (CapabilityId, CapabilityValue)>,
    ) -> Result<Arc<Self>, CapabilityProjectionError> {
        let readiness = Arc::new(CapabilityReadinessPlan::from_set(&set)?);
        Self::with_readiness(set, readiness, values)
    }

    pub(super) fn with_readiness(
        set: Arc<CapabilitySet>,
        readiness: Arc<CapabilityReadinessPlan>,
        values: impl IntoIterator<Item = (CapabilityId, CapabilityValue)>,
    ) -> Result<Arc<Self>, CapabilityProjectionError> {
        if !readiness.matches(&set) {
            return Err(CapabilityProjectionError::ReadinessPlanMismatch {
                expected_generation: set.generation().get(),
                actual_generation: readiness.generation().get(),
                digest_mismatch: readiness.digest() != set.digest(),
            });
        }
        let mut canonical = BTreeMap::new();
        for (id, value) in values {
            if canonical.insert(id.clone(), value).is_some() {
                return Err(CapabilityProjectionError::DuplicateValue {
                    capability: id.to_string(),
                });
            }
        }

        for (id, descriptor) in set.iter() {
            let Some(value) = canonical.get(id) else {
                return Err(CapabilityProjectionError::MissingValue {
                    capability: id.to_string(),
                });
            };
            if value.kind() != descriptor.id().kind() {
                return Err(CapabilityProjectionError::KindMismatch {
                    capability: id.to_string(),
                    descriptor_kind: descriptor.id().kind(),
                    value_kind: value.kind(),
                });
            }
            if let Some(actual) = value.public_name() {
                if actual != descriptor.public_name() {
                    return Err(CapabilityProjectionError::PublicNameMismatch {
                        capability: id.to_string(),
                        expected: descriptor.public_name().to_owned(),
                        actual: actual.to_owned(),
                    });
                }
            }
            if let CapabilityValue::Ui(binding) = value {
                if descriptor.surface_digest() != binding.surface_digest() {
                    return Err(CapabilityProjectionError::SurfaceDigestMismatch {
                        capability: id.to_string(),
                        expected: descriptor.surface_digest().to_string(),
                        actual: binding.surface_digest().to_string(),
                    });
                }
                for dependency in descriptor.dependencies() {
                    if !matches!(
                        dependency.kind(),
                        CapabilityKind::Tool
                            | CapabilityKind::Skill
                            | CapabilityKind::Mcp
                            | CapabilityKind::Flow
                    ) {
                        return Err(CapabilityProjectionError::UnsupportedUiDependencyKind {
                            capability: id.to_string(),
                            dependency: dependency.to_string(),
                            dependency_kind: dependency.kind(),
                        });
                    }
                }
            }
            if let CapabilityValue::KnowledgeSurface(binding) = value {
                if descriptor.surface_digest() != binding.surface_digest() {
                    return Err(CapabilityProjectionError::SurfaceDigestMismatch {
                        capability: id.to_string(),
                        expected: descriptor.surface_digest().to_string(),
                        actual: binding.surface_digest().to_string(),
                    });
                }
            }
        }
        for id in canonical.keys() {
            if !set.contains(id) {
                return Err(CapabilityProjectionError::UnexpectedValue {
                    capability: id.to_string(),
                });
            }
        }

        Ok(Arc::new(Self {
            set,
            readiness,
            values: canonical,
        }))
    }

    pub fn set(&self) -> &CapabilitySet {
        &self.set
    }

    pub(crate) fn set_arc(&self) -> &Arc<CapabilitySet> {
        &self.set
    }

    pub fn readiness_plan(&self) -> &CapabilityReadinessPlan {
        &self.readiness
    }

    pub fn len(&self) -> usize {
        self.values.len()
    }

    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    pub fn contains(&self, id: &CapabilityId) -> bool {
        self.values.contains_key(id)
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&CapabilityId, &CapabilityValue)> {
        self.values.iter()
    }

    pub fn tool(&self, id: &CapabilityId) -> Option<&dyn Tool> {
        match self.values.get(id) {
            Some(CapabilityValue::Tool(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn skill(&self, id: &CapabilityId) -> Option<&Skill> {
        match self.values.get(id) {
            Some(CapabilityValue::Skill(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn agent(&self, id: &CapabilityId) -> Option<&AgentDefinition> {
        match self.values.get(id) {
            Some(CapabilityValue::Agent(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn command(&self, id: &CapabilityId) -> Option<&dyn SlashCommand> {
        match self.values.get(id) {
            Some(CapabilityValue::Command(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn hook(&self, id: &CapabilityId) -> Option<&HookBinding> {
        match self.values.get(id) {
            Some(CapabilityValue::Hook(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn mcp(&self, id: &CapabilityId) -> Option<&McpBinding> {
        match self.values.get(id) {
            Some(CapabilityValue::Mcp(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn flow(&self, id: &CapabilityId) -> Option<&FlowBinding> {
        match self.values.get(id) {
            Some(CapabilityValue::Flow(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn knowledge(&self, id: &CapabilityId) -> Option<&CognitiveContextSession> {
        match self.values.get(id) {
            Some(CapabilityValue::Knowledge(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn knowledge_surface(&self, id: &CapabilityId) -> Option<&KnowledgeSurfaceBinding> {
        match self.values.get(id) {
            Some(CapabilityValue::KnowledgeSurface(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn ui(&self, id: &CapabilityId) -> Option<&UiBinding> {
        match self.values.get(id) {
            Some(CapabilityValue::Ui(value)) => Some(value.as_ref()),
            _ => None,
        }
    }

    pub fn context(&self, id: &CapabilityId) -> Option<&dyn ContextProvider> {
        match self.values.get(id) {
            Some(CapabilityValue::Context(value)) => Some(value.as_ref()),
            _ => None,
        }
    }
}

/// Exact local generation and identity digest used by catalog CAS publication.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CapabilityCatalogStamp {
    generation: CodeCatalogGeneration,
    digest: Sha256Digest,
}

impl CapabilityCatalogStamp {
    fn from_projection(projection: &CapabilityProjection) -> Self {
        Self {
            generation: projection.set().generation(),
            digest: projection.set().digest().clone(),
        }
    }

    pub const fn generation(&self) -> CodeCatalogGeneration {
        self.generation
    }

    pub fn digest(&self) -> &Sha256Digest {
        &self.digest
    }
}

/// Successful all-or-nothing projection publication evidence.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CapabilityCommitReceipt {
    previous: CapabilityCatalogStamp,
    committed: CapabilityCatalogStamp,
}

impl CapabilityCommitReceipt {
    pub fn previous(&self) -> &CapabilityCatalogStamp {
        &self.previous
    }

    pub fn committed(&self) -> &CapabilityCatalogStamp {
        &self.committed
    }
}

#[derive(Clone, Copy)]
enum CleanupReason {
    Rollback,
    Retired,
}

struct CleanupBatch {
    reason: CleanupReason,
    effects: Vec<Box<dyn CapabilityEffect>>,
}

#[derive(Default)]
struct CleanupQueue {
    batches: Mutex<VecDeque<CleanupBatch>>,
}

impl CleanupQueue {
    fn enqueue(&self, reason: CleanupReason, effects: Vec<Box<dyn CapabilityEffect>>) {
        if effects.is_empty() {
            return;
        }
        self.batches
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .push_back(CleanupBatch { reason, effects });
    }

    fn take_all(&self) -> VecDeque<CleanupBatch> {
        std::mem::take(
            &mut *self
                .batches
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner),
        )
    }

    fn len(&self) -> usize {
        self.batches
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .len()
    }
}

struct PublishedGeneration {
    projection: Arc<CapabilityProjection>,
    stamp: CapabilityCatalogStamp,
    use_lease_provider: Option<Arc<dyn UseGenerationLeaseProvider>>,
    effects: Mutex<Vec<Box<dyn CapabilityEffect>>>,
    cleanup: Arc<CleanupQueue>,
}

impl PublishedGeneration {
    fn new(
        projection: Arc<CapabilityProjection>,
        use_lease_provider: Option<Arc<dyn UseGenerationLeaseProvider>>,
        effects: Vec<Box<dyn CapabilityEffect>>,
        cleanup: Arc<CleanupQueue>,
    ) -> Self {
        let stamp = CapabilityCatalogStamp::from_projection(&projection);
        Self {
            projection,
            stamp,
            use_lease_provider,
            effects: Mutex::new(effects),
            cleanup,
        }
    }
}

impl Drop for PublishedGeneration {
    fn drop(&mut self) {
        let effects = std::mem::take(
            self.effects
                .get_mut()
                .unwrap_or_else(std::sync::PoisonError::into_inner),
        );
        self.cleanup.enqueue(CleanupReason::Retired, effects);
    }
}

struct CatalogState {
    current: Arc<PublishedGeneration>,
}

pub(super) struct CatalogInner {
    state: Mutex<CatalogState>,
    cleanup: Arc<CleanupQueue>,
}

impl CatalogInner {
    pub(super) fn current_stamp(&self) -> CapabilityCatalogStamp {
        self.state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .current
            .stamp
            .clone()
    }

    pub(super) fn enqueue_rollback(&self, effects: Vec<Box<dyn CapabilityEffect>>) {
        self.cleanup.enqueue(CleanupReason::Rollback, effects);
    }

    pub(super) fn publish(
        &self,
        base: &CapabilityCatalogStamp,
        projection: Arc<CapabilityProjection>,
        use_lease_provider: Option<Arc<dyn UseGenerationLeaseProvider>>,
        effects: Vec<Box<dyn CapabilityEffect>>,
    ) -> Result<CapabilityCommitReceipt, CapabilityProjectionError> {
        let committed = CapabilityCatalogStamp::from_projection(&projection);
        let old = {
            let mut state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let actual = &state.current.stamp;
            if actual != base {
                let error = CapabilityProjectionError::CommitConflict {
                    expected_generation: base.generation().get(),
                    expected_digest: base.digest().to_string(),
                    actual_generation: actual.generation().get(),
                    actual_digest: actual.digest().to_string(),
                };
                drop(state);
                self.enqueue_rollback(effects);
                return Err(error);
            }
            let published = Arc::new(PublishedGeneration::new(
                projection,
                use_lease_provider,
                effects,
                Arc::clone(&self.cleanup),
            ));
            std::mem::replace(&mut state.current, published)
        };
        let previous = old.stamp.clone();
        drop(old);
        Ok(CapabilityCommitReceipt {
            previous,
            committed,
        })
    }
}

/// Session-local immutable capability publication catalog.
///
/// Readers only clone one `Arc` under a short mutex and then resolve through a
/// pinned [`CapabilityProjectionLease`]. Writers use an exact generation and
/// digest compare-and-swap; a losing writer cannot mutate the current value.
pub struct CapabilityCatalog {
    pub(super) inner: Arc<CatalogInner>,
}

impl CapabilityCatalog {
    pub fn new(initial: Arc<CapabilityProjection>) -> Self {
        let cleanup = Arc::new(CleanupQueue::default());
        let current = Arc::new(PublishedGeneration::new(
            initial,
            None,
            Vec::new(),
            Arc::clone(&cleanup),
        ));
        Self {
            inner: Arc::new(CatalogInner {
                state: Mutex::new(CatalogState { current }),
                cleanup,
            }),
        }
    }

    pub fn current_stamp(&self) -> CapabilityCatalogStamp {
        self.inner.current_stamp()
    }

    pub fn pin(&self) -> CapabilityProjectionLease {
        let generation = Arc::clone(
            &self
                .inner
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .current,
        );
        CapabilityProjectionLease { generation }
    }

    pub fn pending_cleanup_batches(&self) -> usize {
        self.inner.cleanup.len()
    }

    pub(crate) fn retire_current_effects(&self) {
        let generation = Arc::clone(
            &self
                .inner
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .current,
        );
        let effects = std::mem::take(
            &mut *generation
                .effects
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner),
        );
        self.inner.cleanup.enqueue(CleanupReason::Retired, effects);
    }

    pub async fn drain_cleanup(&self) -> CapabilityCleanupReport {
        self.drain_cleanup_with_policy(ScopeClosePolicy::default())
            .await
    }

    pub async fn drain_cleanup_with_policy(
        &self,
        policy: ScopeClosePolicy,
    ) -> CapabilityCleanupReport {
        let mut batches = self.inner.cleanup.take_all();
        let deadline = Instant::now() + policy.timeout();
        let mut report = CapabilityCleanupReport::default();

        while let Some(mut batch) = batches.pop_front() {
            match batch.reason {
                CleanupReason::Rollback => report.rollback_batches += 1,
                CleanupReason::Retired => report.retired_batches += 1,
            }
            while let Some(effect) = batch.effects.pop() {
                if Instant::now() >= deadline {
                    report.effects_timed_out += 1 + batch.effects.len();
                    break;
                }
                match tokio::time::timeout_at(deadline, effect.close()).await {
                    Ok(Ok(())) => report.effects_closed += 1,
                    Ok(Err(_)) => report.effects_failed += 1,
                    Err(_) => report.effects_timed_out += 1,
                }
            }
        }
        report
    }
}

impl fmt::Debug for CapabilityCatalog {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CapabilityCatalog")
            .field("current", &self.current_stamp())
            .field("pending_cleanup_batches", &self.pending_cleanup_batches())
            .finish()
    }
}

/// Non-clone reader lease retaining one exact projected generation.
///
/// The contained values are exposed by borrow, so ordinary execution cannot
/// accidentally switch to the catalog's latest generation. When the last
/// lease and catalog pointer to a retired generation disappear, Rust `Arc`
/// ownership moves its effects to the asynchronous cleanup queue.
#[must_use = "a projection lease pins one exact catalog generation"]
pub struct CapabilityProjectionLease {
    generation: Arc<PublishedGeneration>,
}

impl CapabilityProjectionLease {
    pub fn stamp(&self) -> &CapabilityCatalogStamp {
        &self.generation.stamp
    }

    pub fn projection(&self) -> &CapabilityProjection {
        &self.generation.projection
    }

    pub(super) fn use_lease_provider(&self) -> Option<&Arc<dyn UseGenerationLeaseProvider>> {
        self.generation.use_lease_provider.as_ref()
    }
}

impl fmt::Debug for CapabilityProjectionLease {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CapabilityProjectionLease")
            .field("stamp", self.stamp())
            .finish_non_exhaustive()
    }
}

/// Bounded reverse-teardown result for retired or rolled-back effects.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CapabilityCleanupReport {
    pub rollback_batches: usize,
    pub retired_batches: usize,
    pub effects_closed: usize,
    pub effects_failed: usize,
    pub effects_timed_out: usize,
}

impl CapabilityCleanupReport {
    pub const fn is_clean(&self) -> bool {
        self.effects_failed == 0 && self.effects_timed_out == 0
    }
}