thirdshift 0.15.0

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! Exercise the resolved-reader interface with retained files and native identities.
use std::os::unix::ffi::OsStrExt;

use super::*;
use crate::logs::reading::Framing;

#[test]
fn disappeared_dispatched_session_retains_its_captured_writer() {
    let dir = tempfile::tempdir().unwrap();
    let folder = dir.path().join("acme/widgets/commands/issue");
    let session = folder.join("30-20261019T110000-0400~removed-implement.logs.jsonl");

    let mut dispatch = record(30);
    dispatch["session_log"] = json!(session);
    let mut parent = record(7);
    parent["dispatched"] = json!([dispatch]);
    let record_path = write(
        dir.path(),
        "7-20261019T110000-0400~removed.command.json",
        &parent,
    );
    fs::write(&session, b"first\nunfinished").unwrap();

    let reader = exact(dir.path(), &session);
    let first = reader.read(0, Framing::Lines, 100).unwrap();
    assert_eq!(first.bytes, b"first\n");
    assert!(!first.done);

    parent["dispatched"] = json!([]);
    fs::write(&record_path, serde_json::to_vec(&parent).unwrap()).unwrap();

    let pending = reader.read(first.offset, Framing::Lines, 100).unwrap();
    assert!(
        !pending.done,
        "removing the observing record cannot prove captured writer exit"
    );
    assert!(pending.bytes.is_empty());
    assert_eq!(pending.offset, first.offset);
}

fn config(root: &Path) -> crate::config::UserConfig {
    let mut config = crate::config::UserConfig::parse("", &root.join("config.toml"), root).unwrap();
    config.logs_dir = root.to_owned();
    config
}

fn find(root: &Path, path: &Path) -> Result<catalog::Log> {
    catalog::find(
        &config(root),
        &catalog::id(path.strip_prefix(root).unwrap()),
    )
}

fn exact(root: &Path, path: &Path) -> Reader {
    find(root, path).unwrap().reader
}

#[test]
fn inventory_reads_unowned_files_by_path_bytes_and_refuses_excluded_storage() {
    let dir = tempfile::tempdir().unwrap();
    let commands = dir.path().join("acme/widgets/commands");
    let mut unowned = vec![
        commands.join("issue/notes.log"),
        commands.join("issue/draft.logs.jsonl"),
    ];
    // APFS refuses names that are not UTF-8, so only other filesystems can hold one.
    if cfg!(not(target_os = "macos")) {
        let unnamed = commands.join(std::ffi::OsStr::from_bytes(b"kind-\xff"));
        unowned.push(unnamed.join("notes.log"));
    }
    for path in unowned {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, b"unowned\n").unwrap();
        let batch = exact(dir.path(), &path)
            .read(0, Framing::Lines, 100)
            .unwrap();
        assert_eq!(batch.bytes, b"unowned\n", "{}", path.display());
        assert!(batch.done, "{}", path.display());
    }
    let sessions = dir.path().join("acme/widgets/sessions");
    fs::create_dir_all(&sessions).unwrap();
    for path in [
        sessions.join("7-20261019T110000-0400-implement.logs.jsonl"),
        commands.join("issue/7-20261019T110000-0400-implement.jsonl"),
        commands.join("issue/7-20261019T110000-0400-implement.spans.jsonl"),
        commands.join("issue/7-20261019T110000-0400.notification.txt"),
        commands.join("issue/.log"),
        commands.join("loose.log"),
    ] {
        fs::write(&path, b"excluded\n").unwrap();
        assert!(find(dir.path(), &path).is_err(), "{}", path.display());
    }
}

#[test]
fn removing_or_symlinking_an_enumerated_entry_ends_inventory_reads() {
    for replaced in [
        "acme",
        "acme/widgets",
        "acme/widgets/commands/issue",
        "acme/widgets/commands/issue/7-20261019T110000-0400~gone.log",
    ] {
        let dir = tempfile::tempdir().unwrap();
        let elsewhere = tempfile::tempdir().unwrap();
        let record_path = write(
            dir.path(),
            "7-20261019T110000-0400~gone.command.json",
            &record(7),
        );
        let path = record_path.with_file_name("7-20261019T110000-0400~gone.log");
        fs::write(&path, b"kept\n").unwrap();
        let reader = exact(dir.path(), &path);
        let snapshot = Snapshot::load(dir.path(), now()).unwrap();
        let selected = snapshot.resolve("widgets#7").unwrap().reader(path.clone());
        assert_eq!(
            reader.read(0, Framing::Lines, 100).unwrap().bytes,
            b"kept\n"
        );
        let entry = dir.path().join(replaced);
        let moved = elsewhere.path().join("moved");
        fs::rename(&entry, &moved).unwrap();
        std::os::unix::fs::symlink(&moved, &entry).unwrap();
        assert_eq!(fs::read(&path).unwrap(), b"kept\n", "{replaced}");
        let error = reader.read(0, Framing::Lines, 100).unwrap_err();
        assert!(
            error.to_string().contains("not in the current inventory"),
            "{replaced}: {error:#}"
        );
        assert!(find(dir.path(), &path).is_err(), "{replaced}");
        // Command selection never required inventory layout.
        let unconstrained = selected.read(0, Framing::Lines, 100).unwrap();
        assert_eq!(unconstrained.bytes, b"kept\n", "{replaced}");
        assert!(!unconstrained.done, "{replaced}");
    }
}

#[test]
fn removing_a_finished_listed_file_ends_its_inventory_reads() {
    let dir = tempfile::tempdir().unwrap();
    let path = write(dir.path(), "notes.log", &Value::Null);
    let reader = exact(dir.path(), &path);
    assert!(reader.read(0, Framing::Raw, 100).unwrap().done);
    fs::remove_file(&path).unwrap();
    let error = reader.read(0, Framing::Raw, 100).unwrap_err();
    assert!(
        error.to_string().contains("not in the current inventory"),
        "{error:#}"
    );
}

#[test]
fn configured_root_and_commands_aliases_stay_readable() {
    let dir = tempfile::tempdir().unwrap();
    let stored = dir.path().join("stored-commands");
    fs::create_dir_all(stored.join("issue")).unwrap();
    fs::create_dir_all(dir.path().join("real/acme/widgets")).unwrap();
    let commands = dir.path().join("real/acme/widgets/commands");
    std::os::unix::fs::symlink(&stored, &commands).unwrap();
    let root = dir.path().join("logs");
    std::os::unix::fs::symlink(dir.path().join("real"), &root).unwrap();
    let path = root.join("acme/widgets/commands/issue/notes.log");
    fs::write(&path, b"through aliases\n").unwrap();
    let reader = exact(&root, &path);
    assert_eq!(
        reader.read(0, Framing::Lines, 100).unwrap().bytes,
        b"through aliases\n"
    );
    // Listing follows a retargeted `commands` alias, so reading does too.
    let moved = dir.path().join("moved-commands");
    fs::rename(&stored, &moved).unwrap();
    fs::remove_file(&commands).unwrap();
    std::os::unix::fs::symlink(&moved, &commands).unwrap();
    let batch = reader.read(0, Framing::Lines, 100).unwrap();
    assert_eq!(batch.bytes, b"through aliases\n");
    assert!(batch.done);
}

#[test]
fn a_selected_read_ignores_an_unrelated_malformed_subtree() {
    let dir = tempfile::tempdir().unwrap();
    let record_path = write(
        dir.path(),
        "7-20261019T110000-0400~local.command.json",
        &record(7),
    );
    let path = record_path.with_file_name("7-20261019T110000-0400~local.log");
    fs::write(&path, b"first\n").unwrap();
    let reader = exact(dir.path(), &path);
    let first = reader.read(0, Framing::Lines, 100).unwrap();
    assert!(!first.done);
    fs::create_dir_all(dir.path().join("acme/other")).unwrap();
    fs::write(dir.path().join("acme/other/commands"), b"not a directory").unwrap();
    assert!(
        catalog::load(&config(dir.path()), None).is_err(),
        "whole-store loading fails on this fixture"
    );
    fs::write(&path, b"first\nsecond\n").unwrap();
    let next = reader.read(first.offset, Framing::Lines, 100).unwrap();
    assert_eq!(next.bytes, b"second\n");
    assert!(!next.done);
}

#[test]
fn raw_and_line_framing_keep_byte_offsets_and_hold_live_unfinished_lines() {
    let dir = tempfile::tempdir().unwrap();
    let record_path = write(
        dir.path(),
        "7-20261019T110000-0400~framing.command.json",
        &record(7),
    );
    let path = record_path.with_file_name("7-20261019T110000-0400~framing.log");
    fs::write(&path, b"caf\xc3\xa9\n\xfftail").unwrap();
    let reader = exact(dir.path(), &path);
    let raw = reader.read(0, Framing::Raw, 4).unwrap();
    assert_eq!(raw.bytes, b"caf\xc3");
    assert_eq!(raw.offset, 4);
    assert!(!raw.done);
    let raw = reader.read(raw.offset, Framing::Raw, 100).unwrap();
    assert_eq!(raw.bytes, b"\xa9\n\xfftail");
    assert_eq!(raw.offset, 11);
    let line = reader.read(0, Framing::Lines, 1).unwrap();
    assert_eq!(line.bytes, "café\n".as_bytes());
    assert_eq!(line.offset, 6);
    let pending = reader.read(line.offset, Framing::Lines, 100).unwrap();
    assert!(pending.bytes.is_empty());
    assert_eq!(pending.offset, 6);
    assert!(!pending.done);
    assert!(reader.read(12, Framing::Raw, 100).is_err());
    fs::write(&path, "short").unwrap();
    assert!(
        reader
            .read(6, Framing::Lines, 100)
            .unwrap_err()
            .to_string()
            .contains("truncated")
    );
}

#[test]
fn completed_files_drain_whole_lines_across_batches_including_the_final_tail() {
    let dir = tempfile::tempdir().unwrap();
    let path = write(
        dir.path(),
        "7-20261019T110000-0400~retained.log",
        &Value::Null,
    );
    fs::write(&path, b"first\nsecond\nfinal").unwrap();
    let reader = exact(dir.path(), &path);
    let first = reader.read(0, Framing::Lines, 2).unwrap();
    assert_eq!(first.bytes, b"first\n");
    assert_eq!(first.offset, 6);
    assert!(!first.done);
    let second = reader.read(first.offset, Framing::Lines, 7).unwrap();
    assert_eq!(second.bytes, b"second\n");
    assert_eq!(second.offset, 13);
    assert!(!second.done);
    let last = reader.read(second.offset, Framing::Lines, 5).unwrap();
    assert_eq!(last.bytes, b"final");
    assert_eq!(last.offset, 18);
    assert!(last.done);
    let end = reader.read(last.offset, Framing::Lines, 100).unwrap();
    assert!(end.bytes.is_empty() && end.done);
}

#[test]
fn captured_writer_ignores_outcome_and_new_commands_but_rejects_a_reused_pid() {
    let dir = tempfile::tempdir().unwrap();
    let mut fixture = record(7);
    fixture["outcome"] = "ready for review".into();
    let record_path = write(
        dir.path(),
        "7-20261019T110000-0400~owner.command.json",
        &fixture,
    );
    let path = record_path.with_file_name("7-20261019T110000-0400~owner.log");
    fs::write(&path, b"tail").unwrap();
    let reader = exact(dir.path(), &path);
    let snapshot = Snapshot::load(dir.path(), now()).unwrap();
    let selected = snapshot.resolve("widgets#7").unwrap().reader(path.clone());
    assert!(!reader.read(0, Framing::Raw, 100).unwrap().done);
    fixture["process_start"] = "reused PID".into();
    write(
        dir.path(),
        "7-20261019T110000-0400~owner.command.json",
        &fixture,
    );
    write(
        dir.path(),
        "7-20261019T110000-0400~another.command.json",
        &record(7),
    );
    assert!(!reader.read(0, Framing::Lines, 100).unwrap().done);
    assert!(!selected.read(0, Framing::Raw, 100).unwrap().done);
    let mismatched = exact(dir.path(), &path)
        .read(0, Framing::Lines, 100)
        .unwrap();
    assert_eq!(mismatched.bytes, b"tail");
    assert!(mismatched.done);
    fixture["process_start"] = "".into();
    write(
        dir.path(),
        "7-20261019T110000-0400~owner.command.json",
        &fixture,
    );
    assert!(
        exact(dir.path(), &path)
            .read(0, Framing::Raw, 100)
            .unwrap()
            .done
    );
    fs::remove_file(&path).unwrap();
    assert!(reader.read(0, Framing::Raw, 100).is_err());
}

#[test]
fn exact_session_tracks_its_current_reference_while_command_selection_keeps_outer_lifetime() {
    let dir = tempfile::tempdir().unwrap();
    let folder = dir.path().join("acme/widgets/commands/issue");
    let original = folder.join("7-20261019T110000-0400~session-implement.jsonl");
    let converted = crate::logs::session::path(&original);
    let mut fixture = record(7);
    fixture["session_log"] = json!(original);
    fixture["outcome"] = "ready for review".into();
    let record_path = write(
        dir.path(),
        "7-20261019T110000-0400~session.command.json",
        &fixture,
    );
    fs::write(&converted, b"finished event\nlast event").unwrap();
    let reader = exact(dir.path(), &converted);
    let snapshot = Snapshot::load(dir.path(), now()).unwrap();
    let selected = snapshot
        .resolve("widgets#7")
        .unwrap()
        .reader(converted.clone());
    assert_eq!(
        reader.read(0, Framing::Lines, 100).unwrap().bytes,
        b"finished event\n"
    );
    // Losing a record cannot prove that its captured writer has stopped.
    fs::remove_file(&record_path).unwrap();
    assert!(!reader.read(0, Framing::Lines, 100).unwrap().done);
    fixture["session_log"] =
        json!(folder.join("7-20261019T110000-0400~session-implement-resume.logs.jsonl"));
    fs::write(&record_path, serde_json::to_vec(&fixture).unwrap()).unwrap();
    let final_batch = reader.read(15, Framing::Lines, 100).unwrap();
    assert_eq!(final_batch.bytes, b"last event");
    assert!(final_batch.done);
    assert!(!selected.read(0, Framing::Raw, 100).unwrap().done);
    fs::write(&record_path, b"unreadable record").unwrap();
    assert!(reader.read(0, Framing::Lines, 100).is_err());
}

#[test]
fn exact_child_session_uses_its_own_writer_and_dispatched_sessions_use_the_pass() {
    let dir = tempfile::tempdir().unwrap();
    let mut parent = record(7);
    parent["process_start"] = "gone".into();
    let parent_path = write(
        dir.path(),
        "7-20261019T110000-0400~tree.command.json",
        &parent,
    );
    let log = parent_path.with_file_name("7-20261019T110000-0400~tree.log");
    fs::write(&log, b"outer tail").unwrap();
    let session = parent_path.with_file_name("21-20261019T110000-0400~tree-implement.logs.jsonl");
    let mut child = record(21);
    child["parent_record"] = json!(parent_path);
    child["session_log"] = json!(session);
    write(
        dir.path(),
        "7-20261019T110000-0400~tree.issue-21.child.json",
        &child,
    );
    fs::write(&session, b"child tail").unwrap();
    assert!(
        !exact(dir.path(), &session)
            .read(0, Framing::Raw, 100)
            .unwrap()
            .done
    );
    assert!(
        exact(dir.path(), &log)
            .read(0, Framing::Raw, 100)
            .unwrap()
            .done
    );
    let snapshot = Snapshot::load(dir.path(), now()).unwrap();
    assert!(
        snapshot
            .resolve("widgets#21")
            .unwrap()
            .reader(session.clone())
            .read(0, Framing::Raw, 100)
            .unwrap()
            .done
    );

    let dispatch_session =
        parent_path.with_file_name("30-20261019T110000-0400~tree-implement.logs.jsonl");
    let mut dispatch = record(30);
    dispatch["session_log"] = json!(dispatch_session);
    parent = record(7);
    parent["dispatched"] = json!([dispatch]);
    fs::write(&parent_path, serde_json::to_vec(&parent).unwrap()).unwrap();
    fs::write(&dispatch_session, b"dispatch tail").unwrap();
    let reader = exact(dir.path(), &dispatch_session);
    assert!(!reader.read(0, Framing::Lines, 100).unwrap().done);
    parent["dispatched"][0]["session_log"] = Value::Null;
    fs::write(&parent_path, serde_json::to_vec(&parent).unwrap()).unwrap();
    assert!(reader.read(0, Framing::Lines, 100).unwrap().done);
}