kindling-service 0.2.0

In-process orchestration layer for kindling memory: capsules, observations, retrieval and pins.
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
//! `KindlingService` — in-process orchestration over the store/provider/filter
//! crates. Ports `KindlingService` from
//! `packages/kindling-core/src/service/kindling-service.ts`.

use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::filter::mask_secrets;
use kindling_provider::{retrieve_at, LocalFtsProvider};
use kindling_store::{SqliteKindlingStore, StoreError, StoreOptions};
use kindling_types::{
    Capsule, CapsuleInput, CapsuleStatus, CapsuleType, Id, Observation, ObservationInput, Pin,
    PinInput, PinTargetType, RetrieveOptions, RetrieveResult, ScopeIds, Summary, SummaryInput,
    Timestamp,
};

use crate::context::{PreCompactContext, ResolvedPin, SessionStartContext};
use crate::error::{ServiceError, ServiceResult};
use crate::validation;

/// Options for [`KindlingService::open_capsule`].
///
/// Mirrors `OpenCapsuleOptions` in `packages/kindling-core/src/capsule/types.ts`.
#[derive(Debug, Clone)]
pub struct OpenCapsuleOptions {
    /// Capsule type (`type` in the TS option object).
    pub kind: CapsuleType,
    /// Human-readable intent/purpose.
    pub intent: String,
    /// Scope dimensions for isolation.
    pub scope_ids: ScopeIds,
    /// Optional pre-generated id; defaults to a fresh UUID.
    pub id: Option<Id>,
}

/// Options for [`KindlingService::close_capsule`].
///
/// Mirrors `CloseCapsuleOptions` in the TS service. A summary is generated and
/// persisted only when `generate_summary` is set AND `summary_content` is
/// present.
#[derive(Debug, Clone, Default)]
pub struct CloseCapsuleOptions {
    /// Whether to generate a closing summary.
    pub generate_summary: bool,
    /// Content for the generated summary.
    pub summary_content: Option<String>,
    /// Summary confidence; defaults to `1.0` (the TS service default).
    pub confidence: Option<f64>,
}

/// Options for [`KindlingService::append_observation`].
///
/// Mirrors `AppendObservationOptions` in the TS service. `validate` defaults to
/// `true`; use [`AppendObservationOptions::default`] for the common case.
#[derive(Debug, Clone)]
pub struct AppendObservationOptions {
    /// Capsule to attach the observation to, if any.
    pub capsule_id: Option<Id>,
    /// Run validation before storing (TS default: `true`).
    pub validate: bool,
}

impl Default for AppendObservationOptions {
    fn default() -> Self {
        Self {
            capsule_id: None,
            validate: true,
        }
    }
}

/// Options for [`KindlingService::pin`].
///
/// Mirrors `CreatePinOptions` in the TS service. `note` becomes the pin's
/// `reason`; `ttl_ms` sets `expires_at = now + ttl_ms`.
#[derive(Debug, Clone)]
pub struct CreatePinOptions {
    /// What kind of entity the pin targets.
    pub target_type: PinTargetType,
    /// Id of the targeted observation or summary.
    pub target_id: Id,
    /// Optional human note (stored as the pin `reason`).
    pub note: Option<String>,
    /// Time-to-live in ms; `None` means the pin never expires.
    pub ttl_ms: Option<i64>,
    /// Scope for the pin; defaults to empty.
    pub scope_ids: Option<ScopeIds>,
}

/// In-process kindling orchestration service.
///
/// Owns the [`SqliteKindlingStore`]. The retrieval provider borrows the store
/// connection, so it is constructed per-retrieve rather than held as a field.
pub struct KindlingService {
    store: SqliteKindlingStore,
}

impl KindlingService {
    /// Build a service over an already-open store.
    pub fn new(store: SqliteKindlingStore) -> Self {
        Self { store }
    }

    /// Open (and initialise if fresh) the database at `path`.
    pub fn open(path: &Path) -> ServiceResult<Self> {
        Ok(Self::new(SqliteKindlingStore::open(path)?))
    }

    /// Open the database at `path` with explicit store options.
    pub fn open_with_options(path: &Path, options: &StoreOptions) -> ServiceResult<Self> {
        Ok(Self::new(SqliteKindlingStore::open_with_options(
            path, options,
        )?))
    }

    /// Open a fresh in-memory store (test/scratch use).
    pub fn open_in_memory() -> ServiceResult<Self> {
        Ok(Self::new(SqliteKindlingStore::open_in_memory()?))
    }

    /// Borrow the underlying store (e.g. for read-only callers).
    pub fn store(&self) -> &SqliteKindlingStore {
        &self.store
    }

    // ===== capsule lifecycle =====

    /// Open a new capsule (`status = open`). For `Session` capsules with a
    /// session scope, rejects opening when one is already open for that
    /// session. Ports `openCapsule` lifecycle.
    pub fn open_capsule(&self, options: OpenCapsuleOptions) -> ServiceResult<Capsule> {
        self.open_capsule_at(options, now_ms())
    }

    /// [`Self::open_capsule`] with an explicit clock for deterministic tests.
    pub fn open_capsule_at(
        &self,
        options: OpenCapsuleOptions,
        now: Timestamp,
    ) -> ServiceResult<Capsule> {
        // Duplicate-open guard (session-scoped only), matching the TS lifecycle.
        if options.kind == CapsuleType::Session {
            if let Some(session_id) = options.scope_ids.session_id.as_deref() {
                if let Some(existing) = self.store.get_open_capsule_for_session(session_id)? {
                    return Err(ServiceError::Conflict(format!(
                        "session {session_id} already has an open capsule ({})",
                        existing.id
                    )));
                }
            }
        }

        let capsule = validation::validate_capsule(
            CapsuleInput {
                id: options.id,
                kind: options.kind,
                intent: options.intent,
                status: Some(CapsuleStatus::Open),
                opened_at: None,
                closed_at: None,
                scope_ids: options.scope_ids,
                observation_ids: None,
                summary_id: None,
            },
            now,
        )
        .map_err(ServiceError::Validation)?;

        self.store.create_capsule(&capsule)?;
        Ok(capsule)
    }

    /// Close a capsule, optionally persisting a generated summary first.
    /// Errors if the capsule is missing ([`ServiceError::NotFound`]) or already
    /// closed ([`ServiceError::AlreadyClosed`]). Ports the service `closeCapsule`.
    pub fn close_capsule(
        &self,
        capsule_id: &str,
        options: CloseCapsuleOptions,
    ) -> ServiceResult<Capsule> {
        self.close_capsule_at(capsule_id, options, now_ms())
    }

    /// [`Self::close_capsule`] with an explicit clock for deterministic tests.
    pub fn close_capsule_at(
        &self,
        capsule_id: &str,
        options: CloseCapsuleOptions,
        now: Timestamp,
    ) -> ServiceResult<Capsule> {
        // Distinguish not-found from already-closed up front (the store
        // collapses both into one error; the TS service reports them
        // separately).
        let mut capsule = match self.store.get_capsule(capsule_id)? {
            None => return Err(ServiceError::NotFound(capsule_id.to_string())),
            Some(capsule) => capsule,
        };
        if capsule.status == CapsuleStatus::Closed {
            return Err(ServiceError::AlreadyClosed(capsule_id.to_string()));
        }

        // Generate + persist the summary before closing, matching TS ordering.
        if options.generate_summary {
            if let Some(content) = options.summary_content {
                let summary = validation::validate_summary(
                    SummaryInput {
                        id: None,
                        capsule_id: capsule_id.to_string(),
                        content,
                        confidence: options.confidence.unwrap_or(1.0),
                        created_at: Some(now),
                        evidence_refs: Vec::new(),
                    },
                    now,
                )
                .map_err(ServiceError::Validation)?;
                self.store.insert_summary(&summary)?;
            }
        }

        // Close in the store. The summary (if any) is linked via
        // summaries.capsule_id, so no summary_id is threaded here.
        match self.store.close_capsule(capsule_id, Some(now), None) {
            Ok(()) => {}
            // Lost a race: someone closed it between our read and this write.
            Err(StoreError::CapsuleNotOpen(_)) => {
                return Err(ServiceError::AlreadyClosed(capsule_id.to_string()))
            }
            Err(err) => return Err(err.into()),
        }

        capsule.status = CapsuleStatus::Closed;
        capsule.closed_at = Some(now);
        Ok(capsule)
    }

    // ===== observations =====

    /// Validate/normalise, secret-mask, store, and (optionally) attach an
    /// observation. Returns the stored observation (with masked content and
    /// any defaulted fields). Ports `appendObservation`, plus the
    /// service-boundary secret masking that has no TS equivalent.
    pub fn append_observation(
        &self,
        input: ObservationInput,
        options: AppendObservationOptions,
    ) -> ServiceResult<Observation> {
        self.append_observation_at(input, options, now_ms())
    }

    /// [`Self::append_observation`] with an explicit clock for deterministic
    /// tests.
    pub fn append_observation_at(
        &self,
        input: ObservationInput,
        options: AppendObservationOptions,
        now: Timestamp,
    ) -> ServiceResult<Observation> {
        let mut observation = if options.validate {
            validation::validate_observation(input, now).map_err(ServiceError::Validation)?
        } else {
            validation::normalize_observation(input, now)
        };

        // Service-boundary secret masking: mask (do NOT truncate — truncation
        // is a hook-layer concern owned by PORT-009) so no consumer can route
        // around secret filtering. Applies on both the validated and the
        // validate:false path.
        observation.content = mask_secrets(&observation.content);

        self.store.insert_observation(&observation)?;

        if let Some(capsule_id) = options.capsule_id.as_deref() {
            self.store
                .attach_observation_to_capsule(capsule_id, &observation.id)?;
        }

        Ok(observation)
    }

    // ===== retrieval =====

    /// Retrieve relevant context, scored as of the current time.
    pub fn retrieve(&self, options: RetrieveOptions) -> ServiceResult<RetrieveResult> {
        self.retrieve_at(options, now_ms())
    }

    /// [`Self::retrieve`] with an explicit clock for deterministic tests.
    pub fn retrieve_at(
        &self,
        options: RetrieveOptions,
        now: Timestamp,
    ) -> ServiceResult<RetrieveResult> {
        let provider = LocalFtsProvider::from_store(&self.store);
        Ok(retrieve_at(&self.store, &provider, &options, now)?)
    }

    // ===== pins =====

    /// Create a pin. `note` → reason; `ttl_ms` → `expires_at = now + ttl_ms`.
    /// Ports `pin`.
    pub fn pin(&self, options: CreatePinOptions) -> ServiceResult<Pin> {
        self.pin_at(options, now_ms())
    }

    /// [`Self::pin`] with an explicit clock for deterministic tests.
    pub fn pin_at(&self, options: CreatePinOptions, now: Timestamp) -> ServiceResult<Pin> {
        let expires_at = options.ttl_ms.map(|ttl| now + ttl);
        let pin = validation::validate_pin(
            PinInput {
                id: None,
                target_type: options.target_type,
                target_id: options.target_id,
                reason: options.note,
                created_at: Some(now),
                expires_at,
                scope_ids: options.scope_ids.unwrap_or_default(),
            },
            now,
        )
        .map_err(ServiceError::Validation)?;

        self.store.insert_pin(&pin)?;
        Ok(pin)
    }

    /// Remove a pin. Errors with [`ServiceError::Store`] if it does not exist.
    /// Ports `unpin`.
    pub fn unpin(&self, pin_id: &str) -> ServiceResult<()> {
        self.store.delete_pin(pin_id)?;
        Ok(())
    }

    // ===== read accessors =====

    /// Redact an observation (content replaced, `redacted` set). Ports `forget`.
    pub fn forget(&self, observation_id: &str) -> ServiceResult<()> {
        self.store.redact_observation(observation_id)?;
        Ok(())
    }

    /// Capsule by id. Ports `getCapsule`.
    pub fn get_capsule(&self, capsule_id: &str) -> ServiceResult<Option<Capsule>> {
        Ok(self.store.get_capsule(capsule_id)?)
    }

    /// Open capsule for a session, if any. Ports `getOpenCapsule`.
    pub fn get_open_capsule(&self, session_id: &str) -> ServiceResult<Option<Capsule>> {
        Ok(self.store.get_open_capsule_for_session(session_id)?)
    }

    /// Observation by id. Ports `getObservation`.
    pub fn get_observation(&self, observation_id: &str) -> ServiceResult<Option<Observation>> {
        Ok(self.store.get_observation_by_id(observation_id)?)
    }

    /// Summary by id. Ports `getSummary`.
    pub fn get_summary(&self, summary_id: &str) -> ServiceResult<Option<Summary>> {
        Ok(self.store.get_summary_by_id(summary_id)?)
    }

    /// Latest summary for a capsule, if any. (Read helper used by callers and
    /// tests; the TS service exposes the same via the store.)
    pub fn get_latest_summary(&self, capsule_id: &str) -> ServiceResult<Option<Summary>> {
        Ok(self.store.get_latest_summary_for_capsule(capsule_id)?)
    }

    /// Active pins for a scope, as of the current time. Ports `listPins`.
    pub fn list_pins(&self, scope: Option<&ScopeIds>) -> ServiceResult<Vec<Pin>> {
        self.list_pins_at(scope, now_ms())
    }

    /// [`Self::list_pins`] with an explicit clock for deterministic tests.
    pub fn list_pins_at(
        &self,
        scope: Option<&ScopeIds>,
        now: Timestamp,
    ) -> ServiceResult<Vec<Pin>> {
        Ok(self.store.list_active_pins(scope, Some(now))?)
    }

    // ===== injection context (hook support) =====

    /// Assemble the structured data for the SessionStart injection, scored as
    /// of the current time.
    pub fn session_start_context(
        &self,
        scope: &ScopeIds,
        max_results: u32,
    ) -> ServiceResult<SessionStartContext> {
        self.session_start_context_at(scope, max_results, now_ms())
    }

    /// [`Self::session_start_context`] with an explicit clock for deterministic
    /// tests (controls active-pin expiry).
    ///
    /// Ports the inline queries in
    /// `plugins/kindling-claude-code/hooks/session-start.js`:
    /// active pins for the scope (resolved to target content) plus the most
    /// recent non-redacted observations for the scope, capped at `max_results`.
    pub fn session_start_context_at(
        &self,
        scope: &ScopeIds,
        max_results: u32,
        now: Timestamp,
    ) -> ServiceResult<SessionStartContext> {
        let pins = self.resolved_active_pins(scope, now)?;
        // The Node hook orders by `ts DESC LIMIT maxResults`, excluding
        // redacted rows — exactly `query_observations` with no time bounds.
        let recent = self
            .store
            .query_observations(Some(scope), None, None, max_results)?;
        Ok(SessionStartContext { pins, recent })
    }

    /// Assemble the structured data for the PreCompact injection, scored as of
    /// the current time.
    pub fn pre_compact_context(&self, scope: &ScopeIds) -> ServiceResult<PreCompactContext> {
        self.pre_compact_context_at(scope, now_ms())
    }

    /// [`Self::pre_compact_context`] with an explicit clock for deterministic
    /// tests (controls active-pin expiry).
    ///
    /// Ports the inline queries in
    /// `plugins/kindling-claude-code/hooks/pre-compact.js`: active pins for the
    /// scope (resolved to target content) plus the single latest summary across
    /// the scope's capsules. An empty-content summary is normalised to `None`
    /// here, matching the Node hook's `latestSummary.content` truthiness gate,
    /// so the server never has to second-guess it.
    pub fn pre_compact_context_at(
        &self,
        scope: &ScopeIds,
        now: Timestamp,
    ) -> ServiceResult<PreCompactContext> {
        let pins = self.resolved_active_pins(scope, now)?;
        let latest_summary = self
            .store
            .latest_summary_for_scope(Some(scope))?
            .filter(|s| !s.content.is_empty());
        Ok(PreCompactContext {
            pins,
            latest_summary,
        })
    }

    /// Active pins for `scope` at `now`, each resolved to its target's content.
    ///
    /// Mirrors the TS `listActivePins` join: `note` is the pin reason, `content`
    /// is the target observation/summary content (redacted observations carry
    /// their `[redacted]` placeholder; missing targets resolve to `None`).
    fn resolved_active_pins(
        &self,
        scope: &ScopeIds,
        now: Timestamp,
    ) -> ServiceResult<Vec<ResolvedPin>> {
        let pins = self.store.list_active_pins(Some(scope), Some(now))?;
        pins.into_iter()
            .map(|pin| {
                let content = match pin.target_type {
                    PinTargetType::Observation => self
                        .store
                        .get_observation_by_id(&pin.target_id)?
                        .map(|o| o.content),
                    PinTargetType::Summary => self
                        .store
                        .get_summary_by_id(&pin.target_id)?
                        .map(|s| s.content),
                };
                Ok(ResolvedPin {
                    note: pin.reason,
                    content,
                })
            })
            .collect()
    }
}

/// Current time in epoch milliseconds.
fn now_ms() -> Timestamp {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system clock before Unix epoch")
        .as_millis() as Timestamp
}