eidetic-engine 0.15.2

Durable, local-first, explainable memory for coding agents.
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
//! Deterministic core for speculative context-pack pre-assembly.
//!
//! This module is intentionally integration-light: the CLI/steward wiring can
//! call into these pure pieces once the L2 pack-cache producer path is clear.

use std::collections::VecDeque;

use serde::Serialize;

pub const SPEC_PACK_SCHEMA_V1: &str = "ee.spec_pack.v1";
pub const DEFAULT_SPEC_PACK_TTL_SECONDS: u64 = 30;
pub const DEFAULT_SPEC_PACK_CONCURRENCY: usize = 4;
pub const DEFAULT_SPEC_PACK_RING_CAPACITY: usize = 64;
pub const DEFAULT_SPEC_PACK_RING_TOKEN_CAPACITY: u64 = 64 * 4_000;
pub const DEFAULT_SPEC_PACK_TOP_K: usize = 4;

pub const SPEC_PACK_RING_EMPTY_CODE: &str = "ring_empty";
pub const SPEC_PACK_QOS_BACK_PRESSURE_CODE: &str = "qos_back_pressure";
pub const SPEC_PACK_CACHE_ADMISSION_DENIED_CODE: &str = "cache_admission_denied";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SpecPackConfig {
    pub ttl_seconds: u64,
    pub per_workspace_concurrency: usize,
    pub ring_capacity: usize,
    pub ring_token_capacity: u64,
    pub top_k: usize,
}

impl Default for SpecPackConfig {
    fn default() -> Self {
        Self {
            ttl_seconds: DEFAULT_SPEC_PACK_TTL_SECONDS,
            per_workspace_concurrency: DEFAULT_SPEC_PACK_CONCURRENCY,
            ring_capacity: DEFAULT_SPEC_PACK_RING_CAPACITY,
            ring_token_capacity: DEFAULT_SPEC_PACK_RING_TOKEN_CAPACITY,
            top_k: DEFAULT_SPEC_PACK_TOP_K,
        }
    }
}

impl SpecPackConfig {
    #[must_use]
    pub const fn new(
        ttl_seconds: u64,
        per_workspace_concurrency: usize,
        ring_capacity: usize,
        ring_token_capacity: u64,
        top_k: usize,
    ) -> Self {
        Self {
            ttl_seconds,
            per_workspace_concurrency,
            ring_capacity,
            ring_token_capacity,
            top_k,
        }
    }

    #[must_use]
    pub fn effective_ttl_seconds(&self) -> u64 {
        self.ttl_seconds.min(3_600)
    }

    #[must_use]
    pub fn effective_concurrency(&self) -> usize {
        self.per_workspace_concurrency.max(1)
    }

    #[must_use]
    pub fn effective_ring_capacity(&self) -> usize {
        self.ring_capacity.max(1)
    }

    #[must_use]
    pub fn effective_top_k(&self) -> usize {
        self.top_k.max(1)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecentQueryShape {
    pub workspace_id: String,
    pub query: String,
    pub bead_id: Option<String>,
    pub max_tokens: u32,
    pub profile: String,
    pub agent_name_when_bias_active: Option<String>,
}

impl RecentQueryShape {
    #[must_use]
    pub fn new(
        workspace_id: impl Into<String>,
        query: impl Into<String>,
        bead_id: Option<impl Into<String>>,
        max_tokens: u32,
        profile: impl Into<String>,
        agent_name_when_bias_active: Option<impl Into<String>>,
    ) -> Self {
        Self {
            workspace_id: workspace_id.into(),
            query: query.into(),
            bead_id: bead_id.map(Into::into),
            max_tokens,
            profile: profile.into(),
            agent_name_when_bias_active: agent_name_when_bias_active.map(Into::into),
        }
    }

    #[must_use]
    pub fn workspace_id_hash(&self) -> String {
        blake3_hex_hash([("workspace_id", self.workspace_id.as_str())])
    }

    #[must_use]
    pub fn query_shape_hash(&self) -> String {
        let max_tokens = self.max_tokens.to_string();
        blake3_hex_hash([
            ("workspace_id", self.workspace_id.as_str()),
            ("query", self.query.as_str()),
            ("bead_id", self.bead_id.as_deref().unwrap_or("")),
            ("max_tokens", max_tokens.as_str()),
            ("profile", self.profile.as_str()),
            (
                "agent_name_when_bias_active",
                self.agent_name_when_bias_active.as_deref().unwrap_or(""),
            ),
        ])
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecentQueryObservation {
    pub shape: RecentQueryShape,
    pub tokens_used: u64,
    pub observed_at_ms: u64,
}

impl RecentQueryObservation {
    #[must_use]
    pub const fn new(shape: RecentQueryShape, tokens_used: u64, observed_at_ms: u64) -> Self {
        Self {
            shape,
            tokens_used,
            observed_at_ms,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecentQueryRing {
    max_entries: usize,
    max_tokens: u64,
    total_tokens: u64,
    entries: VecDeque<RecentQueryObservation>,
}

impl RecentQueryRing {
    #[must_use]
    pub fn new(max_entries: usize, max_tokens: u64) -> Self {
        Self {
            max_entries: max_entries.max(1),
            max_tokens,
            total_tokens: 0,
            entries: VecDeque::new(),
        }
    }

    pub fn record(&mut self, observation: RecentQueryObservation) {
        let query_hash = observation.shape.query_shape_hash();
        if let Some(index) = self
            .entries
            .iter()
            .position(|entry| entry.shape.query_shape_hash() == query_hash)
            && let Some(previous) = self.entries.remove(index)
        {
            self.total_tokens = self.total_tokens.saturating_sub(previous.tokens_used);
        }

        self.total_tokens = self.total_tokens.saturating_add(observation.tokens_used);
        self.entries.push_back(observation);
        self.evict_to_limits();
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    #[must_use]
    pub const fn total_tokens(&self) -> u64 {
        self.total_tokens
    }

    #[must_use]
    pub fn entries_newest_first(&self) -> Vec<RecentQueryObservation> {
        self.entries.iter().rev().cloned().collect()
    }

    fn evict_to_limits(&mut self) {
        while self.entries.len() > self.max_entries {
            self.pop_oldest();
        }
        while self.entries.len() > 1 && self.total_tokens > self.max_tokens {
            self.pop_oldest();
        }
    }

    fn pop_oldest(&mut self) {
        if let Some(removed) = self.entries.pop_front() {
            self.total_tokens = self.total_tokens.saturating_sub(removed.tokens_used);
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SpecPackCacheFreshness {
    Fresh,
    Missing,
    Stale,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SpecPackCandidate {
    pub observation: RecentQueryObservation,
    pub cache_freshness: SpecPackCacheFreshness,
}

impl SpecPackCandidate {
    #[must_use]
    pub const fn new(
        observation: RecentQueryObservation,
        cache_freshness: SpecPackCacheFreshness,
    ) -> Self {
        Self {
            observation,
            cache_freshness,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SpecPackSelectedCandidate {
    pub workspace_id_hash: String,
    pub query_shape_hash: String,
    pub observed_at_ms: u64,
    pub tokens_used: u64,
    pub cache_freshness: SpecPackCacheFreshness,
}

#[must_use]
pub fn select_spec_pack_candidates(
    candidates: impl IntoIterator<Item = SpecPackCandidate>,
    top_k: usize,
) -> Vec<SpecPackSelectedCandidate> {
    let mut selected = candidates
        .into_iter()
        .filter(|candidate| candidate.cache_freshness != SpecPackCacheFreshness::Fresh)
        .map(|candidate| {
            let query_shape_hash = candidate.observation.shape.query_shape_hash();
            SpecPackSelectedCandidate {
                workspace_id_hash: candidate.observation.shape.workspace_id_hash(),
                query_shape_hash,
                observed_at_ms: candidate.observation.observed_at_ms,
                tokens_used: candidate.observation.tokens_used,
                cache_freshness: candidate.cache_freshness,
            }
        })
        .collect::<Vec<_>>();
    selected.sort_by(|left, right| {
        right
            .observed_at_ms
            .cmp(&left.observed_at_ms)
            .then_with(|| right.tokens_used.cmp(&left.tokens_used))
            .then_with(|| left.query_shape_hash.cmp(&right.query_shape_hash))
    });
    selected.truncate(top_k.max(1));
    selected
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SpecPackAdmissionVerdict {
    Admitted,
    DeniedForegroundRequestIdActive,
    DeniedReadPoolForegroundPinHeld,
    DeniedQosForegroundPressure,
    DeniedPerWorkspaceConcurrencyCap,
}

impl SpecPackAdmissionVerdict {
    #[must_use]
    pub const fn degraded_code(self) -> Option<&'static str> {
        match self {
            Self::Admitted => None,
            Self::DeniedForegroundRequestIdActive
            | Self::DeniedReadPoolForegroundPinHeld
            | Self::DeniedQosForegroundPressure => Some(SPEC_PACK_QOS_BACK_PRESSURE_CODE),
            Self::DeniedPerWorkspaceConcurrencyCap => Some(SPEC_PACK_CACHE_ADMISSION_DENIED_CODE),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SpecPackQosSnapshot {
    pub foreground_request_id_active: bool,
    pub read_pool_foreground_pin_held: bool,
    pub qos_foreground_active: bool,
    pub active_speculations_for_workspace: usize,
}

impl SpecPackQosSnapshot {
    #[must_use]
    pub const fn idle() -> Self {
        Self {
            foreground_request_id_active: false,
            read_pool_foreground_pin_held: false,
            qos_foreground_active: false,
            active_speculations_for_workspace: 0,
        }
    }

    #[must_use]
    pub fn snapshot_hash(&self) -> String {
        let active = self.active_speculations_for_workspace.to_string();
        blake3_hex_hash([
            (
                "foreground_request_id_active",
                bool_str(self.foreground_request_id_active),
            ),
            (
                "read_pool_foreground_pin_held",
                bool_str(self.read_pool_foreground_pin_held),
            ),
            (
                "qos_foreground_active",
                bool_str(self.qos_foreground_active),
            ),
            ("active_speculations_for_workspace", active.as_str()),
        ])
    }
}

#[must_use]
pub fn admit_spec_pack_candidate(
    config: &SpecPackConfig,
    qos: SpecPackQosSnapshot,
) -> SpecPackAdmissionVerdict {
    if qos.foreground_request_id_active {
        return SpecPackAdmissionVerdict::DeniedForegroundRequestIdActive;
    }
    if qos.read_pool_foreground_pin_held {
        return SpecPackAdmissionVerdict::DeniedReadPoolForegroundPinHeld;
    }
    if qos.qos_foreground_active {
        return SpecPackAdmissionVerdict::DeniedQosForegroundPressure;
    }
    if qos.active_speculations_for_workspace >= config.effective_concurrency() {
        return SpecPackAdmissionVerdict::DeniedPerWorkspaceConcurrencyCap;
    }
    SpecPackAdmissionVerdict::Admitted
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SpecPackPhase {
    Admission,
    Prepare,
    Run,
    Abort,
    Store,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SpecPackAbortReason {
    ForegroundRequestArrived,
    Shutdown,
    MemoryPressure,
    TtlExpiredBeforeStore,
    SelectionNoLongerTopK,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SpecPackTelemetryEvent {
    pub schema: &'static str,
    pub side_effect_free: bool,
    pub phase: SpecPackPhase,
    pub ts: String,
    pub workspace_id_hash: String,
    pub query_shape_hash: String,
    pub qos_lane_snapshot_hash: String,
    pub admission_verdict: SpecPackAdmissionVerdict,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub surface_pack_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl_seconds: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokens_used: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub elapsed_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub abort_reason: Option<SpecPackAbortReason>,
    pub degraded_codes: Vec<&'static str>,
}

impl SpecPackTelemetryEvent {
    #[must_use]
    pub fn admission(
        ts: impl Into<String>,
        shape: &RecentQueryShape,
        qos: SpecPackQosSnapshot,
        verdict: SpecPackAdmissionVerdict,
    ) -> Self {
        Self::new(ts, SpecPackPhase::Admission, shape, qos, verdict)
    }

    #[must_use]
    pub fn for_phase(
        ts: impl Into<String>,
        phase: SpecPackPhase,
        shape: &RecentQueryShape,
        qos: SpecPackQosSnapshot,
        verdict: SpecPackAdmissionVerdict,
    ) -> Self {
        Self::new(ts, phase, shape, qos, verdict)
    }

    #[must_use]
    pub fn with_surface_pack_id(mut self, surface_pack_id: impl Into<String>) -> Self {
        self.surface_pack_id = Some(surface_pack_id.into());
        self
    }

    #[must_use]
    pub fn with_ttl_seconds(mut self, ttl_seconds: u64) -> Self {
        self.ttl_seconds = Some(ttl_seconds.min(3_600));
        self
    }

    #[must_use]
    pub const fn with_tokens_used(mut self, tokens_used: u64) -> Self {
        self.tokens_used = Some(tokens_used);
        self
    }

    #[must_use]
    pub const fn with_elapsed_ms(mut self, elapsed_ms: u64) -> Self {
        self.elapsed_ms = Some(elapsed_ms);
        self
    }

    #[must_use]
    pub const fn with_abort_reason(mut self, abort_reason: SpecPackAbortReason) -> Self {
        self.abort_reason = Some(abort_reason);
        self
    }

    fn new(
        ts: impl Into<String>,
        phase: SpecPackPhase,
        shape: &RecentQueryShape,
        qos: SpecPackQosSnapshot,
        verdict: SpecPackAdmissionVerdict,
    ) -> Self {
        let degraded_codes = verdict.degraded_code().into_iter().collect();
        Self {
            schema: SPEC_PACK_SCHEMA_V1,
            side_effect_free: true,
            phase,
            ts: ts.into(),
            workspace_id_hash: shape.workspace_id_hash(),
            query_shape_hash: shape.query_shape_hash(),
            qos_lane_snapshot_hash: qos.snapshot_hash(),
            admission_verdict: verdict,
            surface_pack_id: None,
            ttl_seconds: None,
            tokens_used: None,
            elapsed_ms: None,
            abort_reason: None,
            degraded_codes,
        }
    }
}

fn bool_str(value: bool) -> &'static str {
    if value { "true" } else { "false" }
}

fn blake3_hex_hash<'a>(fields: impl IntoIterator<Item = (&'a str, &'a str)>) -> String {
    let mut hasher = blake3::Hasher::new();
    for (label, value) in fields {
        hash_field(&mut hasher, label, value);
    }
    format!("blake3:{}", hasher.finalize().to_hex())
}

fn hash_field(hasher: &mut blake3::Hasher, label: &str, value: &str) {
    hasher.update(label.as_bytes());
    hasher.update(&[0]);
    let value_len = u64::try_from(value.len()).unwrap_or(u64::MAX);
    hasher.update(&value_len.to_be_bytes());
    hasher.update(value.as_bytes());
    hasher.update(&[0xff]);
}