thoughts-tool 0.12.0

Flexible thought management using filesystem mounts for git repositories
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
#![expect(clippy::expect_used, reason = "Tests should panic on failure")]
//! Integration tests for git sync JSONL smart-merge during rebase conflicts.
//! Run with: `THOUGHTS_INTEGRATION_TESTS=1 just test-integration`
//!
//! Note: Divergence-state tests are unit tests in src/git/sync.rs because they need
//! access to the crate-private `check_divergence()` method. This integration test
//! exercises the full sync flow through the public `GitSync::sync()` API.

mod support;

use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use tempfile::TempDir;

use thoughts_tool::git::sync::GitSync;

fn should_run_integration_tests() -> bool {
    std::env::var("THOUGHTS_INTEGRATION_TESTS").ok().as_deref() == Some("1")
}

fn ensure_remote_head_points_to_main(remote: &TempDir) {
    let bare_repo = git2::Repository::open(remote.path()).expect("open bare remote");
    bare_repo
        .set_head("refs/heads/main")
        .expect("set bare HEAD to main");
}

/// Test: JSONL smart-merge during rebase conflict.
/// Creates divergent commits on a `tool_logs` JSONL file, runs `sync()`, and asserts
/// the merged file contains entries from both sides with correct collision semantics.
#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_jsonl_smart_merge_on_conflict() {
    if !should_run_integration_tests() {
        eprintln!("skipping integration test; set THOUGHTS_INTEGRATION_TESTS=1");
        return;
    }

    // 1. Create bare remote
    let remote = TempDir::new().unwrap();
    support::git_ok(remote.path(), &["init", "--bare"]);

    // 2. Create local repo
    let local = TempDir::new().unwrap();
    support::git_ok(local.path(), &["init"]);

    // 3. Create branch/logs/tool_logs_base.jsonl with base entry
    let logs_dir = local.path().join("branch/logs");
    fs::create_dir_all(&logs_dir).unwrap();
    let jsonl_path = logs_dir.join("tool_logs_base.jsonl");
    fs::write(
        &jsonl_path,
        r#"{"call_id":"base","started_at":"2025-01-01T10:00:00Z","tool":"init"}"#,
    )
    .unwrap();
    fs::write(local.path().join("readme.txt"), "readme").unwrap();

    // 4. Initial commit and push
    support::git_ok(local.path(), &["add", "."]);
    support::git_ok(
        local.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "initial",
        ],
    );
    // Normalize branch name (git init may create master or main depending on config)
    support::git_ok(local.path(), &["branch", "-M", "main"]);
    support::git_ok(
        local.path(),
        &["remote", "add", "origin", remote.path().to_str().unwrap()],
    );
    support::git_ok(local.path(), &["push", "-u", "origin", "main"]);

    // Fix bare remote HEAD to point to main (otherwise clone gets detached HEAD)
    // Bare repos initialized with `git init --bare` keep HEAD at refs/heads/master
    // and don't update it on push. Real Git servers set HEAD appropriately.
    ensure_remote_head_points_to_main(&remote);

    // 5. Clone to second location (simulating another client)
    let other = TempDir::new().unwrap();
    support::git_ok(
        other.path(),
        &["clone", remote.path().to_str().unwrap(), "."],
    );

    // 6. In other clone: REPLACE base line with remote_modified AND add remote_only entry
    // (This creates a conflict on the base line, triggering the smart JSONL merge)
    let other_jsonl = other.path().join("branch/logs/tool_logs_base.jsonl");
    // Replace original "init" with "remote_modified" for the base entry
    let new_content = r#"{"call_id":"base","started_at":"2025-01-01T10:00:00Z","tool":"remote_modified"}
{"call_id":"remote_only","started_at":"2025-01-01T11:00:00Z","tool":"remote"}"#;
    fs::write(&other_jsonl, new_content).unwrap();
    support::git_ok(other.path(), &["add", "."]);
    support::git_ok(
        other.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "remote change",
        ],
    );
    support::git_ok(other.path(), &["push"]);

    // 7. Back in original local: REPLACE base line with local_override AND add local_only entry
    // (do NOT commit yet — sync() will stage and commit)
    // Both sides now modify the base line, creating a conflict that triggers smart merge
    let new_local_content = r#"{"call_id":"base","started_at":"2025-01-01T10:00:00Z","tool":"local_override"}
{"call_id":"local_only","started_at":"2025-01-01T12:00:00Z","tool":"local"}"#;
    fs::write(&jsonl_path, new_local_content).unwrap();

    // 8. Run sync - this should trigger rebase with smart-merge
    let sync = GitSync::new(local.path(), None).unwrap();
    sync.sync("test").await.unwrap();

    // 9. Verify merged content
    let merged = fs::read_to_string(&jsonl_path).unwrap();

    // Remote-only entry present
    assert!(
        merged.contains("remote_only"),
        "should contain remote_only entry"
    );
    // Local-only entry present
    assert!(
        merged.contains("local_only"),
        "should contain local_only entry"
    );
    // Local wins on collision — "local_override" not "remote_modified"
    assert!(
        merged.contains("local_override"),
        "should contain local_override (local wins)"
    );
    assert!(
        !merged.contains(r#""tool":"remote_modified""#),
        "should NOT contain remote_modified (local wins on collision)"
    );

    // Verify chronological ordering by started_at
    let lines: Vec<&str> = merged.lines().collect();
    assert!(
        lines.len() >= 3,
        "should have at least 3 lines, got {}",
        lines.len()
    );
    // base (10:00) < remote_only (11:00) < local_only (12:00)
    assert!(lines[0].contains("base"), "first line should be base entry");
    assert!(
        lines[1].contains("remote_only"),
        "second line should be remote_only"
    );
    assert!(
        lines[2].contains("local_only"),
        "third line should be local_only"
    );
}

#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_remote_behind_with_local_uncommitted_creates_one_final_commit() {
    if !should_run_integration_tests() {
        eprintln!("skipping integration test; set THOUGHTS_INTEGRATION_TESTS=1");
        return;
    }

    let remote = TempDir::new().unwrap();
    support::git_ok(remote.path(), &["init", "--bare"]);

    let local = TempDir::new().unwrap();
    support::git_ok(local.path(), &["init"]);
    fs::write(local.path().join("base.txt"), "base\n").unwrap();
    support::git_ok(local.path(), &["add", "."]);
    support::git_ok(
        local.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "initial",
        ],
    );
    support::git_ok(local.path(), &["branch", "-M", "main"]);
    support::git_ok(
        local.path(),
        &["remote", "add", "origin", remote.path().to_str().unwrap()],
    );
    support::git_ok(local.path(), &["push", "-u", "origin", "main"]);
    ensure_remote_head_points_to_main(&remote);

    let other = TempDir::new().unwrap();
    support::git_ok(
        other.path(),
        &["clone", remote.path().to_str().unwrap(), "."],
    );
    fs::write(other.path().join("remote.txt"), "remote\n").unwrap();
    support::git_ok(other.path(), &["add", "."]);
    support::git_ok(
        other.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "remote change",
        ],
    );
    let remote_parent = support::git_stdout(other.path(), &["rev-parse", "HEAD"]);
    support::git_ok(other.path(), &["push"]);

    fs::write(local.path().join("local.txt"), "local\n").unwrap();

    let sync = GitSync::new(local.path(), None).unwrap();
    sync.sync("test-mount").await.unwrap();

    let head_message = support::git_stdout(local.path(), &["log", "-1", "--pretty=%s"]);
    assert_eq!(head_message, "Auto-sync thoughts for test-mount");

    let parent = support::git_stdout(local.path(), &["rev-parse", "HEAD^"]);
    assert_eq!(parent, remote_parent);

    let auto_sync_count: i32 = support::git_stdout(
        local.path(),
        &[
            "rev-list",
            "--count",
            "--grep=^Auto-sync thoughts for test-mount$",
            "HEAD",
        ],
    )
    .parse()
    .unwrap();
    assert_eq!(auto_sync_count, 1);
}

#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_remote_tool_log_only_behind_fast_forwards_without_auto_sync_commit() {
    if !should_run_integration_tests() {
        eprintln!("skipping integration test; set THOUGHTS_INTEGRATION_TESTS=1");
        return;
    }

    let remote = TempDir::new().unwrap();
    support::git_ok(remote.path(), &["init", "--bare"]);

    let local = TempDir::new().unwrap();
    support::git_ok(local.path(), &["init"]);
    let logs_dir = local.path().join("branch/logs");
    fs::create_dir_all(&logs_dir).unwrap();
    let jsonl_path = logs_dir.join("tool_logs_base.jsonl");
    fs::write(
        &jsonl_path,
        r#"{"call_id":"base","started_at":"2025-01-01T10:00:00Z","tool":"init"}"#,
    )
    .unwrap();
    support::git_ok(local.path(), &["add", "."]);
    support::git_ok(
        local.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "initial",
        ],
    );
    support::git_ok(local.path(), &["branch", "-M", "main"]);
    support::git_ok(
        local.path(),
        &["remote", "add", "origin", remote.path().to_str().unwrap()],
    );
    support::git_ok(local.path(), &["push", "-u", "origin", "main"]);
    ensure_remote_head_points_to_main(&remote);

    let other = TempDir::new().unwrap();
    support::git_ok(
        other.path(),
        &["clone", remote.path().to_str().unwrap(), "."],
    );
    fs::write(
        other.path().join("branch/logs/tool_logs_base.jsonl"),
        "{\"call_id\":\"base\",\"started_at\":\"2025-01-01T10:00:00Z\",\"tool\":\"init\"}\n{\"call_id\":\"remote_only\",\"started_at\":\"2025-01-01T11:00:00Z\",\"tool\":\"remote\"}",
    )
    .unwrap();
    support::git_ok(other.path(), &["add", "."]);
    support::git_ok(
        other.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "remote tool log",
        ],
    );
    let remote_head = support::git_stdout(other.path(), &["rev-parse", "HEAD"]);
    support::git_ok(other.path(), &["push"]);

    let sync = GitSync::new(local.path(), None).unwrap();
    sync.sync("test-mount").await.unwrap();

    let local_head = support::git_stdout(local.path(), &["rev-parse", "HEAD"]);
    assert_eq!(local_head, remote_head);

    let auto_sync_count: i32 = support::git_stdout(
        local.path(),
        &[
            "rev-list",
            "--count",
            "--grep=^Auto-sync thoughts for test-mount$",
            "HEAD",
        ],
    )
    .parse()
    .unwrap();
    assert_eq!(auto_sync_count, 0);

    let merged = fs::read_to_string(&jsonl_path).unwrap();
    assert!(merged.contains("remote_only"));
}

#[ignore = "integration test - run with: just test-integration"]
#[tokio::test]
async fn sync_retries_once_after_race_like_push_rejection() {
    if !should_run_integration_tests() {
        eprintln!("skipping integration test; set THOUGHTS_INTEGRATION_TESTS=1");
        return;
    }

    let remote = TempDir::new().unwrap();
    support::git_ok(remote.path(), &["init", "--bare"]);

    let local = TempDir::new().unwrap();
    support::git_ok(local.path(), &["init"]);
    fs::write(local.path().join("base.txt"), "base\n").unwrap();
    support::git_ok(local.path(), &["add", "."]);
    support::git_ok(
        local.path(),
        &[
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-m",
            "initial",
        ],
    );
    support::git_ok(local.path(), &["branch", "-M", "main"]);
    support::git_ok(
        local.path(),
        &["remote", "add", "origin", remote.path().to_str().unwrap()],
    );
    support::git_ok(local.path(), &["push", "-u", "origin", "main"]);
    ensure_remote_head_points_to_main(&remote);

    let hook_path = remote.path().join("hooks/pre-receive");
    let marker_path = remote.path().join("hooks/race_once_marker");
    fs::write(
        &hook_path,
        format!(
            "#!/bin/sh\nMARKER='{}'\nif [ ! -f \"$MARKER\" ]; then\n  touch \"$MARKER\"\n  echo 'remote: ! [rejected] HEAD -> main (fetch first)' >&2\n  echo 'remote: error: failed to push some refs' >&2\n  exit 1\nfi\nexit 0\n",
            marker_path.display()
        ),
    )
    .unwrap();
    #[cfg(unix)]
    {
        let mut perms = fs::metadata(&hook_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&hook_path, perms).unwrap();
    }

    fs::write(local.path().join("retry.txt"), "retry\n").unwrap();

    let sync = GitSync::new(local.path(), None).unwrap();
    sync.sync("test-mount").await.unwrap();

    assert!(
        marker_path.exists(),
        "first push attempt should have been rejected"
    );

    let local_head = support::git_stdout(local.path(), &["rev-parse", "HEAD"]);
    let remote_head = support::git_stdout(remote.path(), &["rev-parse", "refs/heads/main"]);
    assert_eq!(local_head, remote_head);
}