ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! On-disk cache, backed by redb, and review-state persistence
//! ([`reviews`], kept outside the cache). Both keep each account's apart
//! ([`account`]).
//!
//! The database carries a schema version. When it doesn't match
//! [`SCHEMA_VERSION`] the file is discarded and recreated; there are no
//! migrations. If the database can't be opened (another ghtui process holds
//! the lock, the disk is read-only, ...) the store runs disabled: reads miss
//! and writes are dropped, so the app keeps working without a cache.
//!
//! Cached answers (REST responses and queries) can be fetched again, so
//! they're written behind the reply: kept in memory, where reads see them,
//! and committed together in the background without waiting on the disk
//! ([`Db::flush`]). The rest is committed, durably, before returning.

use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use redb::{Database, Durability, ReadableDatabase, TableDefinition, TableHandle};
use serde::{Deserialize, Serialize, de::DeserializeOwned};

/// Bump whenever the meaning or encoding of any table changes.
pub const SCHEMA_VERSION: u64 = 17;

/// Where the pages visited lately are kept.
pub const VISITS: &str = "visits";
/// Where the tabs open when ghtui last quit are kept.
pub const TABS: &str = "tabs";

const META: TableDefinition<&str, u64> = TableDefinition::new("meta");
/// REST responses keyed by request path, stored with their ETag.
const HTTP: TableDefinition<&str, &[u8]> = TableDefinition::new(HTTP_NAME);
const HTTP_NAME: &str = "http";
/// Decoded GraphQL results keyed by a caller-chosen key.
const QUERIES: TableDefinition<&str, &[u8]> = TableDefinition::new(QUERIES_NAME);
const QUERIES_NAME: &str = "queries";
/// Logins keyed by `<host>/<token fingerprint>`.
const ACCOUNTS: TableDefinition<&str, &[u8]> = TableDefinition::new("accounts");
pub mod account;
pub mod queue;
pub mod reviews;

pub use account::Account;
pub use reviews::Reviews;

const VERSION_KEY: &str = "schema_version";

#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    #[error("cache database error: {0}")]
    Db(#[from] redb::Error),
    #[error("cache encoding error: {0}")]
    Encoding(#[from] serde_json::Error),
    #[error("cache file error: {0}")]
    Io(#[from] std::io::Error),
}

macro_rules! from_redb {
    ($($ty:ty),*) => {$(
        impl From<$ty> for StoreError {
            fn from(err: $ty) -> Self {
                StoreError::Db(err.into())
            }
        }
    )*};
}
from_redb!(
    redb::DatabaseError,
    redb::TransactionError,
    redb::TableError,
    redb::StorageError,
    redb::CommitError
);

/// A cached REST response.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpEntry {
    pub etag: String,
    /// The `Last-Modified` header, sent back as `If-Modified-Since`.
    #[serde(default)]
    pub last_modified: Option<String>,
    /// The `Link` header: where a list's next page is.
    #[serde(default)]
    pub link: Option<String>,
    pub body: String,
    /// How many seconds GitHub asks to wait before asking again
    /// (`X-Poll-Interval`), from the last answer: not kept.
    #[serde(skip)]
    pub poll_interval: Option<u64>,
}

/// A cached value with the time it was stored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cached<T> {
    pub value: T,
    /// Unix seconds.
    pub fetched_at: u64,
}

#[derive(Serialize, Deserialize)]
struct QueryEntry {
    fetched_at: u64,
    value: serde_json::Value,
}

/// What we remember about a PR between sessions.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewState {
    /// Head SHA at the time of my last submitted review.
    #[serde(default)]
    pub last_reviewed_head: Option<String>,
    /// Content hashes of hunks I've marked reviewed.
    #[serde(default)]
    pub reviewed_hunks: Vec<String>,
    /// Comments written but not yet submitted to GitHub.
    #[serde(default)]
    pub pending: Vec<DraftComment>,
}

#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DraftSide {
    Left,
    #[default]
    Right,
}

/// An unsubmitted review comment, anchored by file line numbers.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DraftComment {
    /// Local identifier, unique within the PR.
    pub id: u64,
    pub path: String,
    pub body: String,
    pub side: DraftSide,
    /// `None` for a file-level comment.
    pub line: Option<u32>,
    pub start_line: Option<u32>,
    pub start_side: Option<DraftSide>,
    /// Head commit the comment was written against.
    pub commit: String,
    /// GitHub's reason for rejecting it on the last submit.
    #[serde(default)]
    pub error: Option<String>,
}

impl ReviewState {
    /// An ID not used by any pending comment.
    pub fn next_draft_id(&self) -> u64 {
        self.pending.iter().map(|d| d.id).max().map_or(1, |m| m + 1)
    }
}

/// Cheap to clone; all clones share one database.
#[derive(Clone)]
pub struct Store {
    db: Option<Arc<Db>>,
    /// Where the database is, to say how big it is.
    path: Option<Arc<std::path::PathBuf>>,
    /// Whose entries this store reads and writes.
    account: Account,
}

impl std::fmt::Debug for Store {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Store")
            .field("enabled", &self.db.is_some())
            .finish()
    }
}

impl Store {
    /// Opens (or creates) the cache at `path`. Never fails: problems are
    /// logged and yield a disabled store.
    pub fn open(path: &Path) -> Self {
        match open_versioned(path) {
            Ok(database) => Self {
                db: Some(Arc::new(Db {
                    database,
                    later: Mutex::default(),
                })),
                path: Some(Arc::new(path.to_owned())),
                account: Account::default(),
            },
            Err(err) => {
                tracing::warn!(path = %path.display(), %err, "cache disabled");
                Self::disabled()
            }
        }
    }

    /// A store that never hits and drops all writes.
    pub fn disabled() -> Self {
        Self {
            db: None,
            path: None,
            account: Account::default(),
        }
    }

    /// This store, keeping to the account on `host` whose token has
    /// `fingerprint`: the login last seen with it, if any.
    #[must_use]
    pub fn for_account(mut self, host: &str, fingerprint: &str) -> Self {
        self.account = Account::new(host, fingerprint, None);
        let login = self
            .account
            .token_key()
            .and_then(|key| self.read_json::<String>(ACCOUNTS, &key));
        if let Some(login) = login {
            self.account.sign_in(&login);
        }
        self
    }

    pub fn account(&self) -> &Account {
        &self.account
    }

    /// GitHub says the token is `login`'s: its entries from now on, and
    /// from the start next time.
    pub fn signed_in(&self, login: &str) {
        if self.account.sign_in(login)
            && let Some(key) = self.account.token_key()
        {
            tracing::info!(login, "caching for this account");
            self.write_json(ACCOUNTS, &key, login);
        }
    }

    fn scoped(&self, key: &str) -> String {
        format!("{}{key}", self.account.scope())
    }

    pub fn is_enabled(&self) -> bool {
        self.db.is_some()
    }

    /// How big the database is on disk, in bytes.
    pub fn size(&self) -> Option<u64> {
        let path = self.path.as_ref()?;
        std::fs::metadata(path.as_path()).ok().map(|m| m.len())
    }

    pub fn http_get(&self, key: &str) -> Option<HttpEntry> {
        self.read_json(HTTP, &self.scoped(key))
    }

    pub fn http_put(&self, key: &str, entry: &HttpEntry) {
        self.write_later(HTTP_NAME, &self.scoped(key), entry);
    }

    pub fn query_get<T: DeserializeOwned>(&self, key: &str) -> Option<Cached<T>> {
        let entry: QueryEntry = self.read_json(QUERIES, &self.scoped(key))?;
        match serde_json::from_value(entry.value) {
            Ok(value) => Some(Cached {
                value,
                fetched_at: entry.fetched_at,
            }),
            Err(err) => {
                tracing::debug!(key, %err, "cached query no longer decodes; ignoring");
                None
            }
        }
    }

    pub fn query_put<T: Serialize>(&self, key: &str, value: &T) {
        let value = match serde_json::to_value(value) {
            Ok(value) => value,
            Err(err) => {
                tracing::warn!(key, %err, "cannot encode query result for cache");
                return;
            }
        };
        let entry = QueryEntry {
            fetched_at: now(),
            value,
        };
        self.write_later(QUERIES_NAME, &self.scoped(key), &entry);
    }

    /// Commits what's written behind, durably, and waits for it: before
    /// exiting.
    pub fn sync(&self) {
        if let Some(db) = &self.db {
            db.sync();
        }
    }

    fn read_json<T: DeserializeOwned>(
        &self,
        table: TableDefinition<&str, &[u8]>,
        key: &str,
    ) -> Option<T> {
        let db = self.db.as_ref()?;
        let result = (|| -> Result<Option<T>, StoreError> {
            if let Some(bytes) = db.later().rows.get(&(table.name(), key.to_owned())) {
                return Ok(Some(serde_json::from_slice(bytes)?));
            }
            let txn = db.database.begin_read()?;
            let table = match txn.open_table(table) {
                Ok(table) => table,
                Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None),
                Err(err) => return Err(err.into()),
            };
            let Some(bytes) = table.get(key)? else {
                return Ok(None);
            };
            Ok(Some(serde_json::from_slice(bytes.value())?))
        })();
        result.unwrap_or_else(|err| {
            tracing::warn!(key, %err, "cache read failed");
            None
        })
    }

    fn write_json<T: Serialize + ?Sized>(
        &self,
        table: TableDefinition<&str, &[u8]>,
        key: &str,
        value: &T,
    ) {
        let Some(db) = self.db.as_ref() else { return };
        let result = (|| -> Result<(), StoreError> {
            let bytes = serde_json::to_vec(value)?;
            let txn = db.database.begin_write()?;
            {
                let mut table = txn.open_table(table)?;
                table.insert(key, bytes.as_slice())?;
            }
            txn.commit()?;
            Ok(())
        })();
        if let Err(err) = result {
            tracing::warn!(key, %err, "cache write failed");
        }
    }

    /// Writes `value` behind ([`Db::flush`]): reads see it at once.
    fn write_later<T: Serialize + ?Sized>(&self, table: &'static str, key: &str, value: &T) {
        let Some(db) = &self.db else { return };
        let bytes = match serde_json::to_vec(value) {
            Ok(bytes) => bytes,
            Err(err) => return tracing::warn!(key, %err, "cannot encode for the cache"),
        };
        let start = {
            let mut later = db.later();
            later.rows.insert((table, key.to_owned()), bytes.into());
            !std::mem::replace(&mut later.flushing, true)
        };
        if start {
            let db = db.clone();
            match tokio::runtime::Handle::try_current() {
                Ok(runtime) => drop(runtime.spawn_blocking(move || db.flush())),
                Err(_) => db.flush(),
            }
        }
    }
}

/// A row: its table's name and its key.
type Row = (&'static str, String);

/// The database, and the rows written behind.
struct Db {
    database: Database,
    later: Mutex<Later>,
}

#[derive(Default)]
struct Later {
    /// Rows not yet committed, by table name and key: the newest value.
    rows: HashMap<Row, Arc<[u8]>>,
    /// Whether a [`Db::flush`] is running.
    flushing: bool,
    /// When a commit last reached the disk.
    durable: Option<Instant>,
}

/// How long rows written behind may go without reaching the disk: commits
/// in between aren't synced, and a crash loses them.
const SYNC_EVERY: Duration = Duration::from_secs(30);

impl Db {
    fn later(&self) -> MutexGuard<'_, Later> {
        self.later.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Commits the rows written behind, together, until none are left:
    /// those written meanwhile go in the next commit. Synced only every
    /// [`SYNC_EVERY`].
    fn flush(&self) {
        loop {
            let (rows, due) = {
                let mut later = self.later();
                if later.rows.is_empty() {
                    later.flushing = false;
                    return;
                }
                let rows: Vec<_> = later
                    .rows
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                let due = later.durable.is_none_or(|at| at.elapsed() >= SYNC_EVERY);
                (rows, due)
            };
            self.commit(
                &rows,
                if due {
                    Durability::Immediate
                } else {
                    Durability::None
                },
            );
            let mut later = self.later();
            if due {
                later.durable = Some(Instant::now());
            }
            // Committed (or failed, and said so): unless written again since.
            for (row, bytes) in rows {
                if later
                    .rows
                    .get(&row)
                    .is_some_and(|now| Arc::ptr_eq(now, &bytes))
                {
                    later.rows.remove(&row);
                }
            }
        }
    }

    /// Commits whatever is written behind, durably.
    fn sync(&self) {
        let rows: Vec<_> = self.later().rows.drain().collect();
        self.commit(&rows, Durability::Immediate);
    }

    fn commit(&self, rows: &[(Row, Arc<[u8]>)], durability: Durability) {
        let result = (|| -> Result<(), StoreError> {
            let mut txn = self.database.begin_write()?;
            txn.set_durability(durability)
                .map_err(|err| StoreError::Db(err.into()))?;
            for ((table, key), bytes) in rows {
                let mut table = txn.open_table(TableDefinition::<&str, &[u8]>::new(table))?;
                table.insert(key.as_str(), &**bytes)?;
            }
            txn.commit()?;
            Ok(())
        })();
        if let Err(err) = result {
            tracing::warn!(rows = rows.len(), %err, "cache write failed");
        }
    }
}

impl Drop for Db {
    fn drop(&mut self) {
        self.sync();
    }
}

fn open_versioned(path: &Path) -> Result<Database, StoreError> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if path.exists() {
        match Database::create(path) {
            Ok(db) if stored_version(&db)? == Some(SCHEMA_VERSION) => return Ok(db),
            Ok(db) => {
                tracing::info!("cache schema changed; discarding {}", path.display());
                drop(db);
            }
            // Someone else has it open: don't delete their file.
            Err(err @ redb::DatabaseError::DatabaseAlreadyOpen) => return Err(err.into()),
            Err(err) => tracing::warn!(%err, "cache unreadable; discarding {}", path.display()),
        }
        std::fs::remove_file(path)?;
    }
    let db = Database::create(path)?;
    let txn = db.begin_write()?;
    {
        let mut meta = txn.open_table(META)?;
        meta.insert(VERSION_KEY, SCHEMA_VERSION)?;
    }
    txn.commit()?;
    Ok(db)
}

fn stored_version(db: &Database) -> Result<Option<u64>, StoreError> {
    let txn = db.begin_read()?;
    let meta = match txn.open_table(META) {
        Ok(meta) => meta,
        Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None),
        Err(err) => return Err(err.into()),
    };
    Ok(meta.get(VERSION_KEY)?.map(|v| v.value()))
}

pub fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn round_trips_queries_and_http() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&dir.path().join("cache.redb"));
        assert!(store.is_enabled());

        store.query_put("prs", &vec![1, 2, 3]);
        let cached: Cached<Vec<u32>> = store.query_get("prs").unwrap();
        assert_eq!(cached.value, vec![1, 2, 3]);

        let entry = HttpEntry {
            etag: "W/\"abc\"".into(),
            last_modified: None,
            link: None,
            body: "{}".into(),
            poll_interval: None,
        };
        store.http_put("/user", &entry);
        assert_eq!(store.http_get("/user"), Some(entry));
        assert_eq!(store.http_get("/missing"), None);
    }

    #[test]
    fn persists_across_reopen() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.redb");
        Store::open(&path).query_put("k", &7);
        let store = Store::open(&path);
        assert_eq!(store.query_get::<i32>("k").unwrap().value, 7);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn written_behind_is_read_at_once() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::open(&dir.path().join("cache.redb"));
        for i in 0..100 {
            store.query_put("k", &i);
            assert_eq!(store.query_get::<i32>("k").unwrap().value, i);
        }
        store.sync();
        assert_eq!(store.query_get::<i32>("k").unwrap().value, 99);
    }

    #[test]
    fn discards_cache_on_version_mismatch() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.redb");
        {
            let store = Store::open(&path);
            store.query_put("k", &1);
            let db = &store.db.as_ref().unwrap().database;
            let txn = db.begin_write().unwrap();
            txn.open_table(META)
                .unwrap()
                .insert(VERSION_KEY, SCHEMA_VERSION + 1)
                .unwrap();
            txn.commit().unwrap();
        }
        let store = Store::open(&path);
        assert!(store.is_enabled());
        assert!(store.query_get::<i32>("k").is_none());
    }

    #[test]
    fn discards_corrupt_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.redb");
        std::fs::write(&path, b"definitely not a redb file").unwrap();
        let store = Store::open(&path);
        assert!(store.is_enabled());
        store.query_put("k", &1);
        assert_eq!(store.query_get::<i32>("k").unwrap().value, 1);
    }

    #[test]
    fn second_open_degrades_to_disabled() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.redb");
        let first = Store::open(&path);
        first.query_put("k", &1);
        let second = Store::open(&path);
        assert!(!second.is_enabled());
        assert!(second.query_get::<i32>("k").is_none());
        // The first holder's data survives.
        assert_eq!(first.query_get::<i32>("k").unwrap().value, 1);
    }

    #[test]
    fn keeps_accounts_apart() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.redb");
        {
            let alice = Store::open(&path).for_account("github.com", "aaaa");
            alice.query_put("tabs", &"alice's");
            alice.signed_in("alice");
            // What came before GitHub said whose the token is stays apart.
            assert!(alice.query_get::<String>("tabs").is_none());
            alice.query_put("tabs", &"alice's");
            let bob = alice.clone().for_account("github.com", "bbbb");
            assert!(bob.query_get::<String>("tabs").is_none());
            bob.signed_in("bob");
            bob.query_put("tabs", &"bob's");
            let ghe = alice.for_account("ghe.example.com", "aaaa");
            ghe.signed_in("alice");
            assert!(ghe.query_get::<String>("tabs").is_none());
        }
        // Next time, each token finds its account's at once.
        let alice = Store::open(&path).for_account("github.com", "aaaa");
        assert_eq!(alice.query_get::<String>("tabs").unwrap().value, "alice's");
        assert_eq!(alice.account().scope(), "github.com/alice/");
        let bob = alice.for_account("github.com", "bbbb");
        assert_eq!(bob.query_get::<String>("tabs").unwrap().value, "bob's");
    }

    #[test]
    fn disabled_store_is_inert() {
        let store = Store::disabled();
        store.query_put("k", &1);
        assert!(store.query_get::<i32>("k").is_none());
    }
}