pushkin-core 0.2.0

Core envelope, manifest, pipeline, and waiver types for the pushkin write-gate
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
582
583
584
585
586
587
588
589
590
//! Append-only `SQLite` event log (charter N4, N7). Every gate decision is an
//! event from Phase 1 onward; append-only is enforced in the schema itself
//! via triggers, not by convention. Timestamps: UTC ISO-8601 (one convention,
//! this table, documented here).

use rusqlite::Connection;
use std::path::Path;
use thiserror::Error;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use uuid::Uuid;

use crate::envelope::CheckResult;

pub const SCHEMA_VERSION: u32 = 2;

/// Rule id recorded when a shim fails open on malformed input
/// (review directive: drift must be visible, never silent).
pub const FAILOPEN_RULE: &str = "pushkin.failopen.malformed_input";

/// Rule id recorded when the escalation ladder reaches its attempt cap
/// (addendum §7/§8: escalation is an event, not just prose).
pub const ESCALATION_RULE: &str = "pushkin.escalation";

/// Versioned, append-only migration steps shipped in the binary (AGENTS.md
/// `SQLite` rules). Index = schema version - 1. Never edit a shipped step;
/// add a new one.
const MIGRATIONS: &[&str] = &[
    "
    CREATE TABLE IF NOT EXISTS schema_meta (
        version INTEGER NOT NULL
    );
    CREATE TABLE IF NOT EXISTS events (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        session TEXT NOT NULL,
        seq INTEGER NOT NULL,
        ts TEXT NOT NULL, -- UTC ISO-8601 (RFC 3339)
        decision TEXT NOT NULL,
        rule TEXT,
        file TEXT,
        payload TEXT NOT NULL,
        UNIQUE (session, seq)
    );
    CREATE TRIGGER IF NOT EXISTS events_no_update
        BEFORE UPDATE ON events
        BEGIN SELECT RAISE(ABORT, 'events are append-only'); END;
    CREATE TRIGGER IF NOT EXISTS events_no_delete
        BEFORE DELETE ON events
        BEGIN SELECT RAISE(ABORT, 'events are append-only'); END;
    ",
    // v2 — delivered-slice index (spec §7.3). Deliberately mutable working
    // state, unlike events: compaction clears scopes, horizon math updates
    // counters. No append-only triggers here BY DESIGN.
    "
    CREATE TABLE IF NOT EXISTS delivered_slices (
        session TEXT NOT NULL,
        cwd TEXT NOT NULL,
        slice_key TEXT NOT NULL,
        delivered_at_emission INTEGER NOT NULL,
        PRIMARY KEY (session, cwd, slice_key)
    );
    CREATE TABLE IF NOT EXISTS delivery_counters (
        session TEXT NOT NULL,
        cwd TEXT NOT NULL,
        emissions INTEGER NOT NULL,
        PRIMARY KEY (session, cwd)
    );
    ",
];

#[derive(Debug, Error)]
pub enum EventLogError {
    #[error("event log storage error: {0}")]
    Storage(#[from] rusqlite::Error),
    #[error("event serialization error: {0}")]
    Serialize(#[from] serde_json::Error),
    #[error("timestamp formatting error: {0}")]
    Timestamp(#[from] time::error::Format),
}

/// Session identifier newtype (AGENTS.md: newtypes for domain IDs).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionId(String);

impl SessionId {
    /// Adopts an agent-supplied session identifier (hook payloads carry the
    /// agent's own session id; the ladder must count across process runs).
    #[must_use]
    pub fn from_name(name: &str) -> Self {
        Self(name.to_owned())
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Aggregates surfaced by `pushkin stats` (spec §14).
#[derive(Debug)]
pub struct StatsSummary {
    pub blocks_by_rule: Vec<(String, u64)>,
    pub compression_events: u64,
    pub compression_saved_chars: u64,
    pub nudge_arms: Vec<(String, u64)>,
    pub failopen_events: u64,
}

/// One telemetry emission: the rule tag it files under and its payload.
#[derive(Debug)]
pub struct Telemetry<'a> {
    pub rule: &'a str,
    pub payload: String,
}

/// The event envelope of spec §8.3 (`event` field of the result JSON).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GateEvent {
    pub session: String,
    pub seq: u64,
    pub ts: String,
}

pub struct EventLog {
    conn: Connection,
}

impl EventLog {
    /// Opens (creating if needed) the event log and applies pending
    /// migration steps in order, idempotently.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on any `SQLite` failure.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, EventLogError> {
        let conn = Connection::open(path)?;
        apply_migrations(&conn)?;
        Ok(Self { conn })
    }

    /// Starts a new session (fresh UUID; seq restarts at 1 within it).
    ///
    /// # Errors
    /// Currently infallible in practice; `Result` for API stability.
    pub fn begin_session(&self) -> Result<SessionId, EventLogError> {
        Ok(SessionId(Uuid::new_v4().to_string()))
    }

    /// Appends one gate decision as an event and returns its envelope.
    ///
    /// # Errors
    /// Returns `EventLogError` on storage or serialization failure.
    pub fn append(
        &self,
        session: &SessionId,
        result: &CheckResult,
    ) -> Result<GateEvent, EventLogError> {
        let rule = result.violations.first().map(|v| v.rule.clone());
        let file = result.violations.first().map(|v| v.file.clone());
        self.append_row(
            session,
            result.decision.as_str(),
            rule,
            file,
            serde_json::to_string(result)?,
        )
    }

    /// Records a fail-open occurrence (review directive: visible, queryable).
    ///
    /// # Errors
    /// Returns `EventLogError` on storage failure.
    pub fn append_failopen(
        &self,
        session: &SessionId,
        detail: &str,
    ) -> Result<GateEvent, EventLogError> {
        let payload = serde_json::json!({ "failopen": true, "detail": detail }).to_string();
        self.append_row(
            session,
            "allow",
            Some(FAILOPEN_RULE.to_owned()),
            None,
            payload,
        )
    }

    /// Records a ladder escalation (attempt cap reached — addendum §7/§8).
    ///
    /// # Errors
    /// Returns `EventLogError` on storage failure.
    pub fn append_escalation(
        &self,
        session: &SessionId,
        detail: &str,
    ) -> Result<GateEvent, EventLogError> {
        let payload = serde_json::json!({ "escalation": true, "detail": detail }).to_string();
        self.append_row(
            session,
            "block",
            Some(ESCALATION_RULE.to_owned()),
            None,
            payload,
        )
    }

    /// Records a non-decision telemetry event (spec §14: delivery, nudge
    /// arms, and compression tiers ride the same stream as gate
    /// decisions, distinguished by `decision = "telemetry"`).
    ///
    /// # Errors
    /// Returns `EventLogError` on storage failure.
    pub fn append_telemetry(
        &self,
        session: &SessionId,
        telemetry: Telemetry<'_>,
    ) -> Result<GateEvent, EventLogError> {
        self.append_row(
            session,
            "telemetry",
            Some(telemetry.rule.to_owned()),
            None,
            telemetry.payload,
        )
    }

    /// Session-scoped rule-hit counter — the Phase 2 escalation-ladder
    /// substrate (attempt N derives from this).
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn attempts(
        &self,
        session: &SessionId,
        rule: &str,
        file: &str,
    ) -> Result<u64, EventLogError> {
        let count: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events
             WHERE session = ?1 AND rule IN (?2, ?3) AND file = ?4",
            (
                session.as_str(),
                rule,
                crate::legacy::legacy_rule_id(rule).as_ref(),
                file,
            ),
            |row| row.get(0),
        )?;
        Ok(count)
    }

    /// Has an agent write to `file` ever been denied under `rule`? The
    /// evidence half of the amended charter option B: a recorded deny plus a
    /// staged change means the write happened through a surface `PreToolUse`
    /// never saw.
    ///
    /// Deliberately CROSS-SESSION, unlike [`Self::attempts`]. The ladder
    /// counts within one agent conversation; this answers "did any agent get
    /// told no about this path", and the shell running `git commit` is never
    /// the session that was denied.
    ///
    /// `since` is the commit time of the last commit that TOUCHED `file`,
    /// not `HEAD`'s. That difference is the whole design: the question is
    /// not "when was the deny" but "is the denied change still
    /// uncommitted", and only a per-path boundary answers it. Once a human
    /// commits the file, the boundary moves past the deny and later edits
    /// pass — resolution is committing the change, which is what a human
    /// owning the edit actually does.
    ///
    /// Comparison is on WHOLE SECONDS, because a git commit time resolves
    /// to the second while `ts` carries microseconds. `substr(x, 1, 19)` is
    /// exactly `YYYY-MM-DDTHH:MM:SS` — the whole second and nothing after
    /// it — so both sides normalize to the one shape they share, however
    /// each spells what follows: the `time` crate writes a fraction only
    /// when it is nonzero and renders UTC as a literal `Z`, while git may
    /// render the offset as `Z` or as `+00:00`.
    ///
    /// The earlier 20-character form is what made this fragile. At that
    /// length the deciding character was whichever of `.` (0x2e), `Z`
    /// (0x5a), or `+` (0x2b) the two formatters happened to emit, so the
    /// verdict rode a formatting accident rather than time. Executed
    /// evidence: with git rendering `Z`, a deny in the same second as the
    /// commit compared as OLDER and was silently dropped.
    ///
    /// Both sides must therefore be UTC before they arrive: comparing a
    /// local-time `since` against a UTC `ts` would be wrong by the offset.
    /// `last_commit_touching` pins `TZ=UTC` on its `git log` for exactly
    /// this reason, and that pin is load-bearing here.
    ///
    /// `>=` counts a deny in the same second as the boundary commit. That
    /// direction is deliberate: git's second resolution cannot tell before
    /// from after within a second, and the tie must fall toward blocking,
    /// because a false block is visible and recoverable (commit the file,
    /// or `--no-verify`) while a false pass silently defeats the gate. A
    /// deny in a strictly earlier second is resolved history and does not
    /// count.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn denied_since(&self, rule: &str, file: &str, since: &str) -> Result<bool, EventLogError> {
        let count: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events
             WHERE decision = 'block' AND rule IN (?1, ?2)
               AND file = ?3 AND substr(ts, 1, 19) >= substr(?4, 1, 19)",
            (
                rule,
                crate::legacy::legacy_rule_id(rule).as_ref(),
                file,
                since,
            ),
            |row| row.get(0),
        )?;
        Ok(count > 0)
    }

    /// Count of block decisions in a session (any rule, any file).
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn block_count(&self, session: &SessionId) -> Result<u64, EventLogError> {
        let count: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events WHERE session = ?1 AND decision = 'block'",
            [session.as_str()],
            |row| row.get(0),
        )?;
        Ok(count)
    }

    /// Count of events in a session filed under `rule` (telemetry included).
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn rule_count(&self, session: &SessionId, rule: &str) -> Result<u64, EventLogError> {
        let count: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events WHERE session = ?1 AND rule IN (?2, ?3)",
            (
                session.as_str(),
                rule,
                crate::legacy::legacy_rule_id(rule).as_ref(),
            ),
            |row| row.get(0),
        )?;
        Ok(count)
    }

    /// Count of fail-open events in a session.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn failopen_count(&self, session: &SessionId) -> Result<u64, EventLogError> {
        let count: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events WHERE session = ?1 AND rule IN (?2, ?3)",
            (
                session.as_str(),
                FAILOPEN_RULE,
                crate::legacy::legacy_rule_id(FAILOPEN_RULE).as_ref(),
            ),
            |row| row.get(0),
        )?;
        Ok(count)
    }

    /// Aggregates for `pushkin stats` (spec §14): blocks by rule,
    /// compression savings, nudge arms, fail-opens — across all sessions
    /// in this repo's log. Historical rows keep their legacy-prefixed
    /// ids (append-only); aggregation normalizes so the mixed population
    /// groups as one rule (remediation pass 3, PART B2).
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn stats(&self) -> Result<StatsSummary, EventLogError> {
        let mut blocks = self.conn.prepare(
            "SELECT rule, COUNT(*) FROM events
             WHERE decision = 'block' AND rule IS NOT NULL
             GROUP BY rule ORDER BY COUNT(*) DESC, rule",
        )?;
        let raw_blocks = blocks
            .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
            .collect::<Result<Vec<(String, u64)>, _>>()?;
        let blocks_by_rule = group_modern(raw_blocks);
        let (compression_events, compression_saved_chars): (u64, u64) = self.conn.query_row(
            "SELECT COUNT(*), COALESCE(SUM(json_extract(payload, '$.saved_chars')), 0)
             FROM events WHERE rule IN (?1, ?2)",
            (
                "pushkin.compression",
                crate::legacy::legacy_rule_id("pushkin.compression").as_ref(),
            ),
            |row| Ok((row.get(0)?, row.get(1)?)),
        )?;
        let mut arms = self.conn.prepare(
            "SELECT COALESCE(json_extract(payload, '$.arm'), 'unknown'), COUNT(*)
             FROM events WHERE rule IN (?1, ?2) GROUP BY 1 ORDER BY 1",
        )?;
        let nudge_arms = arms
            .query_map(
                (
                    "pushkin.nudge",
                    crate::legacy::legacy_rule_id("pushkin.nudge").as_ref(),
                ),
                |row| Ok((row.get(0)?, row.get(1)?)),
            )?
            .collect::<Result<Vec<(String, u64)>, _>>()?;
        let failopen_events: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events WHERE rule IN (?1, ?2)",
            (
                FAILOPEN_RULE,
                crate::legacy::legacy_rule_id(FAILOPEN_RULE).as_ref(),
            ),
            |row| row.get(0),
        )?;
        Ok(StatsSummary {
            blocks_by_rule,
            compression_events,
            compression_saved_chars,
            nudge_arms,
            failopen_events,
        })
    }

    /// Decision counts for the compact statusline segment: (checks,
    /// denials) — real gate decisions only, escalation marker rows
    /// excluded so three denials read as three, not four.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn decision_counts(&self) -> Result<(u64, u64), EventLogError> {
        let row = self.conn.query_row(
            "SELECT
               COUNT(*) FILTER (WHERE decision IN ('allow', 'block')),
               COUNT(*) FILTER (WHERE decision = 'block')
             FROM events WHERE rule IS NULL OR rule NOT IN (?1, ?2)",
            (
                ESCALATION_RULE,
                crate::legacy::legacy_rule_id(ESCALATION_RULE).as_ref(),
            ),
            |row| Ok((row.get(0)?, row.get(1)?)),
        )?;
        Ok(row)
    }

    /// Whether the most recent session has hit the escalation ladder cap.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn latest_session_escalated(&self) -> Result<bool, EventLogError> {
        let escalated: u64 = self.conn.query_row(
            "SELECT COUNT(*) FROM events
             WHERE rule IN (?1, ?2) AND session =
               (SELECT session FROM events ORDER BY id DESC LIMIT 1)",
            (
                ESCALATION_RULE,
                crate::legacy::legacy_rule_id(ESCALATION_RULE).as_ref(),
            ),
            |row| row.get(0),
        )?;
        Ok(escalated > 0)
    }

    /// Mean-time-to-compliance (integration doc §8): across sessions that
    /// recovered (a block followed by an allow), the average number of
    /// attempts — denials before the allow, plus the complying write.
    /// `None` when no session has recovered yet.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn mean_attempts_to_compliance(&self) -> Result<Option<f64>, EventLogError> {
        let mut statement = self.conn.prepare(
            "SELECT session, decision FROM events
             WHERE decision IN ('allow', 'block')
               AND (rule IS NULL OR rule NOT IN (?1, ?2))
             ORDER BY session, seq",
        )?;
        let rows = statement
            .query_map(
                (
                    ESCALATION_RULE,
                    crate::legacy::legacy_rule_id(ESCALATION_RULE).as_ref(),
                ),
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
            )?
            .collect::<Result<Vec<_>, _>>()?;

        let mut recoveries: Vec<u64> = Vec::new();
        let mut current_session = String::new();
        let mut open_blocks: u64 = 0;
        for (session, decision) in rows {
            if session != current_session {
                current_session = session;
                open_blocks = 0;
            }
            match decision.as_str() {
                "block" => open_blocks += 1,
                "allow" if open_blocks > 0 => {
                    recoveries.push(open_blocks + 1);
                    open_blocks = 0;
                }
                _ => {}
            }
        }
        if recoveries.is_empty() {
            return Ok(None);
        }
        #[allow(clippy::cast_precision_loss)]
        let mean = recoveries.iter().sum::<u64>() as f64 / recoveries.len() as f64;
        Ok(Some(mean))
    }

    /// Current schema version.
    ///
    /// # Errors
    /// Returns `EventLogError::Storage` on query failure.
    pub fn schema_version(&self) -> Result<u32, EventLogError> {
        let version: u32 = self
            .conn
            .query_row("SELECT version FROM schema_meta", [], |row| row.get(0))?;
        Ok(version)
    }

    fn append_row(
        &self,
        session: &SessionId,
        decision: &str,
        rule: Option<String>,
        file: Option<String>,
        payload: String,
    ) -> Result<GateEvent, EventLogError> {
        let next_seq: u64 = self.conn.query_row(
            "SELECT COALESCE(MAX(seq), 0) + 1 FROM events WHERE session = ?1",
            [session.as_str()],
            |row| row.get(0),
        )?;
        let ts = OffsetDateTime::now_utc().format(&Rfc3339)?;
        self.conn.execute(
            "INSERT INTO events (session, seq, ts, decision, rule, file, payload)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            (
                session.as_str(),
                next_seq,
                &ts,
                decision,
                rule,
                file,
                payload,
            ),
        )?;
        Ok(GateEvent {
            session: session.as_str().to_owned(),
            seq: next_seq,
            ts,
        })
    }
}

/// Folds mixed-spelling rule rows into their modern ids (legacy
/// legacy-prefixed rows keep their stored ids; presentation groups them),
/// preserving the count-desc, rule-asc order of the source query.
fn group_modern(rows: Vec<(String, u64)>) -> Vec<(String, u64)> {
    let mut merged: std::collections::BTreeMap<String, u64> = std::collections::BTreeMap::new();
    for (rule, count) in rows {
        *merged
            .entry(crate::legacy::modern_rule_id(&rule).into_owned())
            .or_insert(0) += count;
    }
    let mut grouped: Vec<(String, u64)> = merged.into_iter().collect();
    grouped.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
    grouped
}

pub(crate) fn apply_migrations(conn: &Connection) -> Result<(), EventLogError> {
    let current: u32 = conn
        .query_row("SELECT version FROM schema_meta", [], |row| row.get(0))
        .unwrap_or(0);
    for (index, step) in MIGRATIONS.iter().enumerate() {
        let step_version = u32::try_from(index).unwrap_or(u32::MAX).saturating_add(1);
        if step_version > current {
            conn.execute_batch(step)?;
        }
    }
    if current == 0 {
        conn.execute(
            "INSERT INTO schema_meta (version) VALUES (?1)",
            [SCHEMA_VERSION],
        )?;
    } else if current < SCHEMA_VERSION {
        // Pre-existing DBs must record the upgrade, or every later open
        // re-applies the tail steps and the version reads stale forever.
        conn.execute("UPDATE schema_meta SET version = ?1", [SCHEMA_VERSION])?;
    }
    Ok(())
}