quinjet 0.0.23

A fast, live, keyboard-first Git source-control interface for the terminal
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
use std::ffi::OsString;

use anyhow::{Context, Result, bail};
use serde::Serialize;

use super::{
    CacheLife, PullRequest, Repository, bounded_command_error, bounded_text, cache_read,
    cache_write, parse_tsv_record,
};

/// The renderer wraps every entry to the pane width on each redraw, so this cap
/// is what keeps that work bounded. It is far above any real thread; the entries
/// dropped are the oldest, and the view says so.
const MAX_CONVERSATION_ENTRIES: usize = 500;
const MAX_CONVERSATION_BODY_BYTES: usize = 64 * 1024;
const MAX_CONVERSATION_CONTEXT_BYTES: usize = 8 * 1024;
const CONVERSATION_FIELDS: usize = 8;
const CONVERSATION_PAGE_SIZE: usize = 100;

/// Events GitHub records but never renders in a pull-request conversation.
/// Dropping them in the query keeps the response small and the thread readable.
const HIDDEN_TIMELINE_EVENTS: &str = r#"["subscribed","unsubscribed","mentioned","referenced","milestoned","demilestoned","user_blocked","connected","disconnected","transferred","pinned","unpinned","locked","unlocked","marked_as_duplicate","unmarked_as_duplicate","comment_deleted","deployed","deployment_environment_changed","automatic_base_change_succeeded","automatic_base_change_failed"]"#;

const REVIEW_COMMENT_TSV_JQ: &str = r#".[] | ["review_comment", (.user.login // ""), (.created_at // ""), ((.path // "") + ":" + (((.line // .original_line) // 0)|tostring)), (.body // ""), (.html_url // ""), ((.pull_request_review_id // 0)|tostring), (.diff_hunk // "")] | @tsv"#;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ConversationKind {
    Opened,
    Comment,
    Review,
    ReviewComment,
    Commit,
    ForcePush,
    Merged,
    Closed,
    Reopened,
    Labeled,
    Unlabeled,
    Renamed,
    ReadyForReview,
    ConvertedToDraft,
    ReviewRequested,
    ReviewRequestRemoved,
    Assigned,
    Unassigned,
    CrossReferenced,
    HeadRefDeleted,
    HeadRefRestored,
    BaseRefChanged,
    Other,
}

impl ConversationKind {
    fn parse(value: &str) -> Self {
        match value {
            "comment" => Self::Comment,
            "review" => Self::Review,
            "review_comment" => Self::ReviewComment,
            "commit" => Self::Commit,
            "force_push" => Self::ForcePush,
            "merged" => Self::Merged,
            "closed" => Self::Closed,
            "reopened" => Self::Reopened,
            "labeled" => Self::Labeled,
            "unlabeled" => Self::Unlabeled,
            "renamed" => Self::Renamed,
            "ready_for_review" => Self::ReadyForReview,
            "convert_to_draft" | "converted_to_draft" => Self::ConvertedToDraft,
            "review_requested" => Self::ReviewRequested,
            "review_request_removed" => Self::ReviewRequestRemoved,
            "assigned" => Self::Assigned,
            "unassigned" => Self::Unassigned,
            "cross_referenced" => Self::CrossReferenced,
            "head_ref_deleted" => Self::HeadRefDeleted,
            "head_ref_restored" => Self::HeadRefRestored,
            "base_ref_changed" => Self::BaseRefChanged,
            _ => Self::Other,
        }
    }

    /// Whether the entry carries prose worth rendering under its header.
    pub(crate) const fn has_body(self) -> bool {
        matches!(
            self,
            Self::Opened | Self::Comment | Self::Review | Self::ReviewComment | Self::Commit
        )
    }
}

struct ConversationRecords {
    entries: Vec<ConversationEntry>,
    truncated: bool,
    from_cache: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ConversationEntry {
    pub kind: ConversationKind,
    pub actor: String,
    pub timestamp: String,
    /// Short qualifier for the header: a review verdict, a label name, a
    /// `path:line` anchor, an abbreviated commit, or a renamed title.
    pub detail: String,
    pub body: String,
    pub url: String,
    /// Stable identity for the underlying object: a commit OID, a review id, or
    /// the post-rename title.
    pub reference: String,
    /// Supporting text shown with the entry, currently a review comment's hunk.
    pub context: String,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PullRequestConversation {
    pub entries: Vec<ConversationEntry>,
    pub truncated: bool,
    /// True when nothing had to be transferred: either the thread was already
    /// held for this update stamp, or GitHub confirmed it had not changed.
    pub from_cache: bool,
}

impl PullRequestConversation {
    pub(crate) fn comment_count(&self) -> usize {
        self.entries
            .iter()
            .filter(|entry| {
                matches!(
                    entry.kind,
                    ConversationKind::Comment | ConversationKind::ReviewComment
                )
            })
            .count()
    }
}

impl Repository {
    /// Read the whole pull-request conversation: issue comments, reviews and
    /// their inline comments, pushed commits, force pushes, and the lifecycle
    /// events GitHub shows between them.
    ///
    /// Inline review comments are read from their own endpoint rather than
    /// trusted to the timeline, which only groups them into `line-commented`
    /// entries for some pull requests.
    pub(crate) fn pull_request_conversation(
        &self,
        pull_request: &PullRequest,
    ) -> Result<PullRequestConversation> {
        let stamp = format!(
            "{}\n{}\n{}",
            pull_request.base_repository.url.trim_end_matches('/'),
            pull_request.number,
            pull_request.updated_at
        );
        let identity = format!(
            "{}\n{}",
            pull_request.base_repository.url.trim_end_matches('/'),
            pull_request.number
        );
        let timeline = self.conversation_records(
            &format!("conversation-timeline-v1\n{stamp}"),
            &format!("conversation-timeline-validator-v1\n{identity}"),
            timeline_args(pull_request),
            "unable to load the pull-request timeline",
        )?;
        let comments = self.conversation_records(
            &format!("conversation-comments-v1\n{stamp}"),
            &format!("conversation-comments-validator-v1\n{identity}"),
            review_comment_args(pull_request),
            "unable to load pull-request review comments",
        )?;

        let from_cache = timeline.from_cache && comments.from_cache;
        let mut entries = vec![opened_entry(pull_request)];
        entries.extend(timeline.entries);
        for comment in comments.entries {
            if entries
                .iter()
                .any(|entry| !entry.url.is_empty() && entry.url == comment.url)
            {
                continue;
            }
            entries.push(comment);
        }
        entries.sort_by(|left, right| left.timestamp.cmp(&right.timestamp));

        let overflowing = entries.len() > MAX_CONVERSATION_ENTRIES;
        if overflowing {
            let opened = entries.remove(0);
            let dropped = entries.len() - (MAX_CONVERSATION_ENTRIES - 1);
            drop(entries.drain(..dropped));
            entries.insert(0, opened);
        }
        let truncated = timeline.truncated || comments.truncated || overflowing;
        Ok(PullRequestConversation {
            entries,
            truncated,
            from_cache,
        })
    }

    fn conversation_records(
        &self,
        key: &str,
        validator_key: &str,
        args: Vec<OsString>,
        error_context: &str,
    ) -> Result<ConversationRecords> {
        if let Some(data) = cache_read(key, CacheLife::Immutable) {
            return Ok(ConversationRecords {
                entries: parse_conversation(&data).context(error_context.to_owned())?,
                truncated: false,
                from_cache: true,
            });
        }
        let single_page = validator_args(&args);
        if let Ok(read) = self.validated_gh(validator_key, single_page)
            && read.complete
        {
            let entries = parse_conversation(&read.data).context(error_context.to_owned())?;
            cache_write(key, &read.data);
            return Ok(ConversationRecords {
                entries,
                truncated: false,
                from_cache: read.unchanged,
            });
        }
        let output = self.run_gh(args)?;
        if !output.status.success() && !output.stdout_truncated {
            bail!("{}", bounded_command_error(error_context, &output));
        }
        let mut data = output.stdout;
        if output.stdout_truncated {
            while data.last().is_some_and(|byte| *byte != b'\n') {
                let _ = data.pop();
            }
        }
        let entries = parse_conversation(&data).context(error_context.to_owned())?;
        if !output.stdout_truncated {
            cache_write(key, &data);
        }
        Ok(ConversationRecords {
            entries,
            truncated: output.stdout_truncated,
            from_cache: false,
        })
    }
}

fn validator_args(args: &[OsString]) -> Vec<OsString> {
    args.iter()
        .filter(|arg| arg.as_encoded_bytes() != b"api" && arg.as_encoded_bytes() != b"--paginate")
        .cloned()
        .collect()
}

fn opened_entry(pull_request: &PullRequest) -> ConversationEntry {
    ConversationEntry {
        kind: ConversationKind::Opened,
        actor: pull_request.author.clone(),
        timestamp: pull_request.created_at.clone(),
        detail: format!("{} into {}", pull_request.head_ref, pull_request.base_ref),
        body: pull_request.description.clone(),
        url: pull_request.url.clone(),
        reference: pull_request.head_oid.clone(),
        context: String::new(),
    }
}

fn parse_conversation(output: &[u8]) -> Result<Vec<ConversationEntry>> {
    let mut entries = Vec::new();
    for (index, record) in output.split(|byte| *byte == b'\n').enumerate() {
        if record.is_empty() {
            continue;
        }
        let [
            kind,
            actor,
            timestamp,
            detail,
            body,
            url,
            reference,
            context,
        ] = parse_tsv_record::<CONVERSATION_FIELDS>(record)
            .with_context(|| format!("invalid conversation record {}", index + 1))?;
        entries.push(ConversationEntry {
            kind: ConversationKind::parse(&kind),
            actor,
            timestamp,
            detail,
            body: bounded_text(&body, MAX_CONVERSATION_BODY_BYTES),
            url,
            reference,
            context: bounded_text(&context, MAX_CONVERSATION_CONTEXT_BYTES),
        });
    }
    Ok(entries)
}

fn timeline_args(pull_request: &PullRequest) -> Vec<OsString> {
    api_args(
        format!(
            "repos/{}/issues/{}/timeline?per_page={CONVERSATION_PAGE_SIZE}",
            pull_request.base_repository.name_with_owner, pull_request.number
        ),
        timeline_tsv_jq(),
    )
}

fn review_comment_args(pull_request: &PullRequest) -> Vec<OsString> {
    api_args(
        format!(
            "repos/{}/pulls/{}/comments?per_page={CONVERSATION_PAGE_SIZE}",
            pull_request.base_repository.name_with_owner, pull_request.number
        ),
        REVIEW_COMMENT_TSV_JQ.to_owned(),
    )
}

fn api_args(endpoint: String, jq: String) -> Vec<OsString> {
    vec![
        OsString::from("api"),
        OsString::from("--paginate"),
        OsString::from(endpoint),
        OsString::from("--jq"),
        OsString::from(jq),
    ]
}

/// Flatten every timeline shape into one fixed-width record. GitHub gives each
/// event type its own field names, so the mapping has to be explicit; anything
/// unrecognized still arrives with an actor and a timestamp.
fn timeline_tsv_jq() -> String {
    format!(
        r#".[]
| select(.event as $event | ({HIDDEN_TIMELINE_EVENTS} | index($event)) == null)
| if .event == "commented" then
    ["comment", (.user.login // .actor.login // ""), (.created_at // ""), "", (.body // ""), (.html_url // ""), "", ""]
  elif .event == "reviewed" then
    ["review", (.user.login // ""), (.submitted_at // ""), (.state // ""), (.body // ""), (.html_url // ""), ((.id // 0)|tostring), ""]
  elif .event == "committed" then
    ["commit", (.author.name // .committer.name // ""), (.author.date // .committer.date // ""), ((.sha // "")[0:7]), (.message // ""), (.html_url // ""), (.sha // ""), ""]
  elif .event == "head_ref_force_pushed" then
    ["force_push", (.actor.login // ""), (.created_at // ""), "", "", "", (.commit_id // ""), ""]
  elif .event == "labeled" or .event == "unlabeled" then
    [.event, (.actor.login // ""), (.created_at // ""), (.label.name // ""), "", "", "", ""]
  elif .event == "renamed" then
    ["renamed", (.actor.login // ""), (.created_at // ""), (.rename.from // ""), "", "", (.rename.to // ""), ""]
  elif .event == "cross-referenced" then
    ["cross_referenced", (.actor.login // ""), (.created_at // ""), (((.source.issue.number // 0)|tostring) + " " + (.source.issue.title // "")), "", (.source.issue.html_url // ""), "", ""]
  elif .event == "review_requested" or .event == "review_request_removed" then
    [.event, (.actor.login // ""), (.created_at // ""), (.requested_reviewer.login // .requested_team.name // ""), "", "", "", ""]
  elif .event == "assigned" or .event == "unassigned" then
    [.event, (.actor.login // ""), (.created_at // ""), (.assignee.login // ""), "", "", "", ""]
  elif .event == "line-commented" or .event == "commit-commented" then
    (.comments[]? | ["review_comment", (.user.login // ""), (.created_at // ""), ((.path // "") + ":" + (((.line // .original_line) // 0)|tostring)), (.body // ""), (.html_url // ""), ((.pull_request_review_id // 0)|tostring), (.diff_hunk // "")])
  else
    [(.event // "event"), (.actor.login // .user.login // .author.name // ""), (.created_at // .submitted_at // .author.date // ""), "", "", (.html_url // ""), "", ""]
  end
| @tsv"#
    )
}

#[cfg(test)]
#[expect(
    unused_results,
    reason = "test helpers return values the assertions do not use"
)]
mod tests {
    use super::*;

    #[test]
    fn parses_every_conversation_shape_the_query_can_emit() {
        let output = b"comment\toctocat\t2026-08-01T10:00:00Z\t\tLooks good to me\\nship it\thttps://example.test/c/1\t\t\n\
review\treviewer\t2026-08-01T11:00:00Z\tAPPROVED\t\thttps://example.test/r/1\t99\t\n\
review_comment\treviewer\t2026-08-01T11:00:01Z\tsrc/main.rs:42\tExtract this\thttps://example.test/rc/1\t99\t@@ -1 +1 @@\n\
commit\tAda\t2026-08-01T12:00:00Z\tabc1234\tAdd the thing\thttps://example.test/commit\tabc1234567890\t\n\
force_push\toctocat\t2026-08-01T13:00:00Z\t\t\t\tdeadbeef\t\n\
renamed\toctocat\t2026-08-01T14:00:00Z\tOld title\t\t\tNew title\t\n\
weird_new_event\tsomebody\t2026-08-01T15:00:00Z\t\t\t\t\t\n";

        let entries = parse_conversation(output).unwrap();

        assert_eq!(entries.len(), 7);
        assert_eq!(entries[0].kind, ConversationKind::Comment);
        assert_eq!(entries[0].body, "Looks good to me\nship it");
        assert_eq!(entries[1].kind, ConversationKind::Review);
        assert_eq!(entries[1].detail, "APPROVED");
        assert_eq!(entries[2].kind, ConversationKind::ReviewComment);
        assert_eq!(entries[2].detail, "src/main.rs:42");
        assert_eq!(entries[2].context, "@@ -1 +1 @@");
        assert_eq!(entries[3].kind, ConversationKind::Commit);
        assert_eq!(entries[3].reference, "abc1234567890");
        assert_eq!(entries[4].kind, ConversationKind::ForcePush);
        assert_eq!(entries[4].reference, "deadbeef");
        assert_eq!(entries[5].kind, ConversationKind::Renamed);
        assert_eq!(
            (entries[5].detail.as_str(), entries[5].reference.as_str()),
            ("Old title", "New title")
        );
        assert_eq!(
            entries[6].kind,
            ConversationKind::Other,
            "an event GitHub adds later still renders with its actor and time"
        );
        assert_eq!(entries[6].actor, "somebody");
    }

    #[test]
    fn rejects_records_that_do_not_match_the_query_shape() {
        parse_conversation(b"comment\tonly\ttwo\n").unwrap_err();
    }

    #[test]
    fn queries_are_paginated_and_scoped_to_the_pull_request() {
        let request = super::super::tests::pull_request(
            super::super::tests::repository(
                "acme/widget",
                "https://github.com/acme/widget",
                &["origin"],
            ),
            42,
        );

        let timeline: Vec<String> = timeline_args(&request)
            .iter()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect();
        let comments: Vec<String> = review_comment_args(&request)
            .iter()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect();

        assert_eq!(
            &timeline[..3],
            &[
                "api",
                "--paginate",
                "repos/acme/widget/issues/42/timeline?per_page=100"
            ]
        );
        assert_eq!(
            &comments[..3],
            &[
                "api",
                "--paginate",
                "repos/acme/widget/pulls/42/comments?per_page=100"
            ]
        );
        assert!(timeline[4].contains("head_ref_force_pushed"));
        assert!(timeline[4].contains("line-commented"));
        assert!(comments[4].contains("diff_hunk"));
        assert_eq!(
            validator_args(&timeline_args(&request))
                .iter()
                .map(|arg| arg.to_string_lossy().into_owned())
                .collect::<Vec<_>>(),
            timeline[2..]
        );
    }
}