claude-pool 0.4.0

Slot pool orchestration library for Claude CLI
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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Git worktree isolation for parallel slots.
//!
//! When multiple slots operate on the same repository, they need
//! isolated working directories to avoid stepping on each other's
//! git state. This module manages git worktree creation and cleanup.

use std::path::{Path, PathBuf};

use crate::error::{Error, Result};
use crate::types::{SlotId, TaskId};

/// Manages git worktrees for pool slots.
///
/// When dropped, the manager attempts best-effort cleanup of all worktrees
/// under its base directory. This ensures stale worktrees are removed even
/// if the pool panics or exits without calling [`cleanup_all`][Self::cleanup_all].
#[derive(Debug)]
pub struct WorktreeManager {
    /// Root directory for worktrees (e.g. `/tmp/claude-pool/worktrees`).
    base_dir: PathBuf,
    /// Source repository path.
    repo_dir: PathBuf,
    /// Slot IDs currently tracked (for cleanup on drop).
    tracked_slots: std::sync::Mutex<Vec<SlotId>>,
    /// Chain task IDs currently tracked (for cleanup on drop).
    tracked_chains: std::sync::Mutex<Vec<TaskId>>,
}

impl WorktreeManager {
    /// Create a new worktree manager.
    ///
    /// - `repo_dir`: The source repository to create worktrees from.
    /// - `base_dir`: Directory where worktrees will be created. If `None`,
    ///   uses a temp directory under the system temp dir.
    pub fn new(repo_dir: impl Into<PathBuf>, base_dir: Option<PathBuf>) -> Self {
        let repo_dir = repo_dir.into();
        let base_dir =
            base_dir.unwrap_or_else(|| std::env::temp_dir().join("claude-pool").join("worktrees"));
        Self {
            base_dir,
            repo_dir,
            tracked_slots: std::sync::Mutex::new(Vec::new()),
            tracked_chains: std::sync::Mutex::new(Vec::new()),
        }
    }

    /// Create a worktree manager after verifying the repo directory is a git repository.
    ///
    /// Returns an error if `repo_dir` is not inside a git working tree.
    pub async fn new_validated(
        repo_dir: impl Into<PathBuf>,
        base_dir: Option<PathBuf>,
    ) -> Result<Self> {
        let repo_dir = repo_dir.into();
        let output = tokio::process::Command::new("git")
            .args(["rev-parse", "--is-inside-work-tree"])
            .current_dir(&repo_dir)
            .output()
            .await
            .map_err(|e| {
                Error::Store(format!(
                    "failed to check git repo at {}: {e}",
                    repo_dir.display()
                ))
            })?;

        if !output.status.success() {
            return Err(Error::Store(format!(
                "worktree isolation requires a git repository, but {} is not inside a git work tree",
                repo_dir.display()
            )));
        }

        Ok(Self::new(repo_dir, base_dir))
    }

    /// Track a slot ID for cleanup on drop.
    fn track_slot(&self, slot_id: &SlotId) {
        if let Ok(mut slots) = self.tracked_slots.lock()
            && !slots.iter().any(|s| s.0 == slot_id.0)
        {
            slots.push(slot_id.clone());
        }
    }

    /// Untrack a slot ID (after explicit removal).
    fn untrack_slot(&self, slot_id: &SlotId) {
        if let Ok(mut slots) = self.tracked_slots.lock() {
            slots.retain(|s| s.0 != slot_id.0);
        }
    }

    /// Track a chain task ID for cleanup on drop.
    fn track_chain(&self, task_id: &TaskId) {
        if let Ok(mut chains) = self.tracked_chains.lock()
            && !chains.iter().any(|t| t.0 == task_id.0)
        {
            chains.push(task_id.clone());
        }
    }

    /// Untrack a chain task ID (after explicit removal).
    fn untrack_chain(&self, task_id: &TaskId) {
        if let Ok(mut chains) = self.tracked_chains.lock() {
            chains.retain(|t| t.0 != task_id.0);
        }
    }

    /// Create a worktree for a slot.
    ///
    /// Creates a git worktree at `{base_dir}/{slot_id}` branched from
    /// the current HEAD.
    pub async fn create(&self, slot_id: &SlotId) -> Result<PathBuf> {
        let worktree_path = self.base_dir.join(&slot_id.0);

        // Ensure base directory exists.
        tokio::fs::create_dir_all(&self.base_dir)
            .await
            .map_err(|e| Error::Store(format!("failed to create worktree base dir: {e}")))?;

        // Remove existing worktree if it exists (stale from previous run).
        if worktree_path.exists() {
            self.remove(slot_id).await?;
        }

        let branch_name = format!("claude-pool/{}", slot_id.0);
        let output = tokio::process::Command::new("git")
            .args([
                "worktree",
                "add",
                "-b",
                &branch_name,
                worktree_path.to_str().unwrap_or_default(),
                "HEAD",
            ])
            .current_dir(&self.repo_dir)
            .output()
            .await
            .map_err(|e| Error::Store(format!("failed to create git worktree: {e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::Store(format!("git worktree add failed: {stderr}")));
        }

        self.track_slot(slot_id);

        tracing::info!(
            slot_id = %slot_id.0,
            path = %worktree_path.display(),
            "created git worktree"
        );

        Ok(worktree_path)
    }

    /// Remove a slot's worktree and its branch.
    pub async fn remove(&self, slot_id: &SlotId) -> Result<()> {
        let worktree_path = self.base_dir.join(&slot_id.0);

        if worktree_path.exists() {
            let output = tokio::process::Command::new("git")
                .args([
                    "worktree",
                    "remove",
                    "--force",
                    worktree_path.to_str().unwrap_or_default(),
                ])
                .current_dir(&self.repo_dir)
                .output()
                .await
                .map_err(|e| Error::Store(format!("failed to remove git worktree: {e}")))?;

            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                tracing::warn!(
                    slot_id = %slot_id.0,
                    error = %stderr,
                    "failed to remove worktree, cleaning up manually"
                );
                // Fall back to manual removal.
                let _ = tokio::fs::remove_dir_all(&worktree_path).await;
            }
        }

        // Clean up the branch.
        let branch_name = format!("claude-pool/{}", slot_id.0);
        let _ = tokio::process::Command::new("git")
            .args(["branch", "-D", &branch_name])
            .current_dir(&self.repo_dir)
            .output()
            .await;

        self.untrack_slot(slot_id);

        tracing::debug!(
            slot_id = %slot_id.0,
            "removed git worktree"
        );

        Ok(())
    }

    /// Remove all worktrees managed by this pool.
    pub async fn cleanup_all(&self, slot_ids: &[SlotId]) -> Result<()> {
        for id in slot_ids {
            self.remove(id).await?;
        }

        // Prune stale worktree references.
        let _ = tokio::process::Command::new("git")
            .args(["worktree", "prune"])
            .current_dir(&self.repo_dir)
            .output()
            .await;

        Ok(())
    }

    /// Get the worktree path for a slot (may not exist yet).
    pub fn worktree_path(&self, slot_id: &SlotId) -> PathBuf {
        self.base_dir.join(&slot_id.0)
    }

    /// Get the base directory for all worktrees.
    pub fn base_dir(&self) -> &Path {
        &self.base_dir
    }

    /// Get the source repository directory.
    pub fn repo_dir(&self) -> &Path {
        &self.repo_dir
    }

    /// Create a worktree for a chain execution.
    ///
    /// Creates a git worktree at `{base_dir}/chains/{task_id}` branched from
    /// the current HEAD.
    pub async fn create_for_chain(&self, task_id: &TaskId) -> Result<PathBuf> {
        let worktree_path = self.chain_worktree_path(task_id);

        // Ensure chains directory exists.
        let chains_dir = self.base_dir.join("chains");
        tokio::fs::create_dir_all(&chains_dir)
            .await
            .map_err(|e| Error::Store(format!("failed to create chains dir: {e}")))?;

        // Remove existing worktree if it exists (stale from previous run).
        if worktree_path.exists() {
            self.remove_chain(task_id).await?;
        }

        let branch_name = format!("claude-pool/chain/{}", task_id.0);
        let output = tokio::process::Command::new("git")
            .args([
                "worktree",
                "add",
                "-b",
                &branch_name,
                worktree_path.to_str().unwrap_or_default(),
                "HEAD",
            ])
            .current_dir(&self.repo_dir)
            .output()
            .await
            .map_err(|e| Error::Store(format!("failed to create chain worktree: {e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::Store(format!(
                "git worktree add failed for chain: {stderr}"
            )));
        }

        self.track_chain(task_id);

        tracing::info!(
            task_id = %task_id.0,
            path = %worktree_path.display(),
            "created chain worktree"
        );

        Ok(worktree_path)
    }

    /// Remove a chain's worktree and its branch.
    pub async fn remove_chain(&self, task_id: &TaskId) -> Result<()> {
        let worktree_path = self.chain_worktree_path(task_id);

        if worktree_path.exists() {
            let output = tokio::process::Command::new("git")
                .args([
                    "worktree",
                    "remove",
                    "--force",
                    worktree_path.to_str().unwrap_or_default(),
                ])
                .current_dir(&self.repo_dir)
                .output()
                .await
                .map_err(|e| Error::Store(format!("failed to remove chain worktree: {e}")))?;

            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                tracing::warn!(
                    task_id = %task_id.0,
                    error = %stderr,
                    "failed to remove chain worktree, cleaning up manually"
                );
                let _ = tokio::fs::remove_dir_all(&worktree_path).await;
            }
        }

        // Clean up the branch.
        let branch_name = format!("claude-pool/chain/{}", task_id.0);
        let _ = tokio::process::Command::new("git")
            .args(["branch", "-D", &branch_name])
            .current_dir(&self.repo_dir)
            .output()
            .await;

        self.untrack_chain(task_id);

        tracing::debug!(
            task_id = %task_id.0,
            "removed chain worktree"
        );

        Ok(())
    }

    /// Get the worktree path for a chain (may not exist yet).
    pub fn chain_worktree_path(&self, task_id: &TaskId) -> PathBuf {
        self.base_dir.join("chains").join(&task_id.0)
    }

    /// Create a full clone for a chain execution using `git clone --local --shared`.
    ///
    /// Creates a clone at `{base_dir}/clones/{task_id}` with no shared .git directory.
    pub async fn create_clone_for_chain(&self, task_id: &TaskId) -> Result<PathBuf> {
        let clone_path = self.clone_path(task_id);

        let clones_dir = self.base_dir.join("clones");
        tokio::fs::create_dir_all(&clones_dir)
            .await
            .map_err(|e| Error::Store(format!("failed to create clones dir: {e}")))?;

        if clone_path.exists() {
            self.remove_clone(task_id).await?;
        }

        let output = tokio::process::Command::new("git")
            .args([
                "clone",
                "--local",
                "--shared",
                self.repo_dir.to_str().unwrap_or_default(),
                clone_path.to_str().unwrap_or_default(),
            ])
            .output()
            .await
            .map_err(|e| Error::Store(format!("failed to create chain clone: {e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::Store(format!(
                "git clone failed for chain: {stderr}"
            )));
        }

        // Preserve the GitHub remote URL from the source repo so that
        // tools like `gh pr create` work inside the clone (#152).
        let remote_output = tokio::process::Command::new("git")
            .args(["remote", "get-url", "origin"])
            .current_dir(&self.repo_dir)
            .output()
            .await
            .map_err(|e| Error::Store(format!("failed to get origin URL: {e}")))?;

        if remote_output.status.success() {
            let url = String::from_utf8_lossy(&remote_output.stdout)
                .trim()
                .to_string();
            // Only override if the source has a non-local remote (i.e. GitHub URL).
            if !url.is_empty() && !url.starts_with('/') {
                let set_output = tokio::process::Command::new("git")
                    .args(["remote", "set-url", "origin", &url])
                    .current_dir(&clone_path)
                    .output()
                    .await
                    .map_err(|e| Error::Store(format!("failed to set origin URL in clone: {e}")))?;

                if !set_output.status.success() {
                    let stderr = String::from_utf8_lossy(&set_output.stderr);
                    tracing::warn!(
                        task_id = %task_id.0,
                        error = %stderr,
                        "failed to set origin URL in clone"
                    );
                }
            }
        }

        tracing::info!(
            task_id = %task_id.0,
            path = %clone_path.display(),
            "created chain clone"
        );

        Ok(clone_path)
    }

    /// Remove a chain's clone directory.
    pub async fn remove_clone(&self, task_id: &TaskId) -> Result<()> {
        let clone_path = self.clone_path(task_id);

        if clone_path.exists() {
            tokio::fs::remove_dir_all(&clone_path).await.map_err(|e| {
                Error::Store(format!(
                    "failed to remove chain clone at {}: {e}",
                    clone_path.display()
                ))
            })?;
        }

        tracing::debug!(task_id = %task_id.0, "removed chain clone");
        Ok(())
    }

    /// Get the clone path for a chain (may not exist yet).
    pub fn clone_path(&self, task_id: &TaskId) -> PathBuf {
        self.base_dir.join("clones").join(&task_id.0)
    }
}

impl Drop for WorktreeManager {
    fn drop(&mut self) {
        let slots: Vec<SlotId> = self
            .tracked_slots
            .lock()
            .map(|s| s.clone())
            .unwrap_or_default();
        let chains: Vec<TaskId> = self
            .tracked_chains
            .lock()
            .map(|c| c.clone())
            .unwrap_or_default();

        if slots.is_empty() && chains.is_empty() {
            return;
        }

        tracing::info!(
            slots = slots.len(),
            chains = chains.len(),
            "cleaning up worktrees on drop"
        );

        // Best-effort synchronous cleanup using blocking git commands.
        for slot_id in &slots {
            let worktree_path = self.base_dir.join(&slot_id.0);
            if worktree_path.exists() {
                let _ = std::process::Command::new("git")
                    .args([
                        "worktree",
                        "remove",
                        "--force",
                        worktree_path.to_str().unwrap_or_default(),
                    ])
                    .current_dir(&self.repo_dir)
                    .output();

                // Fall back to manual removal if git worktree remove fails.
                if worktree_path.exists() {
                    let _ = std::fs::remove_dir_all(&worktree_path);
                }
            }
            let branch_name = format!("claude-pool/{}", slot_id.0);
            let _ = std::process::Command::new("git")
                .args(["branch", "-D", &branch_name])
                .current_dir(&self.repo_dir)
                .output();
        }

        for task_id in &chains {
            let worktree_path = self.base_dir.join("chains").join(&task_id.0);
            if worktree_path.exists() {
                let _ = std::process::Command::new("git")
                    .args([
                        "worktree",
                        "remove",
                        "--force",
                        worktree_path.to_str().unwrap_or_default(),
                    ])
                    .current_dir(&self.repo_dir)
                    .output();

                if worktree_path.exists() {
                    let _ = std::fs::remove_dir_all(&worktree_path);
                }
            }
            let branch_name = format!("claude-pool/chain/{}", task_id.0);
            let _ = std::process::Command::new("git")
                .args(["branch", "-D", &branch_name])
                .current_dir(&self.repo_dir)
                .output();
        }

        // Prune stale worktree references.
        let _ = std::process::Command::new("git")
            .args(["worktree", "prune"])
            .current_dir(&self.repo_dir)
            .output();
    }
}

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

    /// Initialize a git repo with a dummy user config and an initial commit.
    /// This works in CI where no global git identity is configured.
    fn init_test_repo(path: &std::path::Path) {
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(path)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "--allow-empty", "-m", "init"])
            .current_dir(path)
            .output()
            .unwrap();
    }

    #[tokio::test]
    async fn new_validated_rejects_non_repo() {
        let tmpdir = tempfile::tempdir().unwrap();
        let result = WorktreeManager::new_validated(tmpdir.path(), None).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not inside a git work tree"),
            "expected git work tree error, got: {err}"
        );
    }

    #[tokio::test]
    async fn new_validated_accepts_git_repo() {
        let tmpdir = tempfile::tempdir().unwrap();
        // Only needs git init, no commit required for validation.
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(tmpdir.path())
            .output()
            .unwrap();
        let mgr = WorktreeManager::new_validated(tmpdir.path(), None).await;
        assert!(mgr.is_ok());
    }

    #[test]
    fn worktree_path_construction() {
        let mgr = WorktreeManager::new("/repo", Some(PathBuf::from("/tmp/wt")));
        let id = SlotId("slot-0".into());
        assert_eq!(mgr.worktree_path(&id), PathBuf::from("/tmp/wt/slot-0"));
    }

    #[test]
    fn default_base_dir() {
        let mgr = WorktreeManager::new("/repo", None);
        let expected = std::env::temp_dir().join("claude-pool").join("worktrees");
        assert_eq!(mgr.base_dir(), expected);
    }

    #[tokio::test]
    async fn clone_preserves_non_local_remote() {
        // Set up a source repo with a non-local origin.
        let src = tempfile::tempdir().unwrap();
        init_test_repo(src.path());
        std::process::Command::new("git")
            .args(["remote", "add", "origin", "git@github.com:user/repo.git"])
            .current_dir(src.path())
            .output()
            .unwrap();

        let base = tempfile::tempdir().unwrap();
        let mgr = WorktreeManager::new(src.path(), Some(base.path().to_path_buf()));
        let task_id = TaskId("chain-test-remote".into());
        let clone_path = mgr.create_clone_for_chain(&task_id).await.unwrap();

        // Verify the clone's origin points to GitHub, not the local path.
        let output = std::process::Command::new("git")
            .args(["remote", "get-url", "origin"])
            .current_dir(&clone_path)
            .output()
            .unwrap();
        let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
        assert_eq!(url, "git@github.com:user/repo.git");

        mgr.remove_clone(&task_id).await.unwrap();
    }

    #[test]
    fn chain_worktree_path_construction() {
        let mgr = WorktreeManager::new("/repo", Some(PathBuf::from("/tmp/wt")));
        let task_id = TaskId("chain-abc123".into());
        assert_eq!(
            mgr.chain_worktree_path(&task_id),
            PathBuf::from("/tmp/wt/chains/chain-abc123")
        );
    }

    #[tokio::test]
    async fn drop_cleans_up_slot_worktrees() {
        let src = tempfile::tempdir().unwrap();
        init_test_repo(src.path());

        let base = tempfile::tempdir().unwrap();
        let slot_id = SlotId("drop-test-slot".into());
        let worktree_path;

        {
            let mgr = WorktreeManager::new(src.path(), Some(base.path().to_path_buf()));
            worktree_path = mgr.create(&slot_id).await.unwrap();
            assert!(worktree_path.exists(), "worktree should exist after create");
            // mgr is dropped here, triggering cleanup
        }

        assert!(
            !worktree_path.exists(),
            "worktree should be cleaned up after drop"
        );
    }

    #[tokio::test]
    async fn drop_cleans_up_chain_worktrees() {
        let src = tempfile::tempdir().unwrap();
        init_test_repo(src.path());

        let base = tempfile::tempdir().unwrap();
        let task_id = TaskId("drop-test-chain".into());
        let worktree_path;

        {
            let mgr = WorktreeManager::new(src.path(), Some(base.path().to_path_buf()));
            worktree_path = mgr.create_for_chain(&task_id).await.unwrap();
            assert!(
                worktree_path.exists(),
                "chain worktree should exist after create"
            );
            // mgr is dropped here
        }

        assert!(
            !worktree_path.exists(),
            "chain worktree should be cleaned up after drop"
        );
    }

    #[tokio::test]
    async fn explicit_remove_prevents_double_cleanup() {
        let src = tempfile::tempdir().unwrap();
        init_test_repo(src.path());

        let base = tempfile::tempdir().unwrap();
        let slot_id = SlotId("explicit-remove-test".into());

        let mgr = WorktreeManager::new(src.path(), Some(base.path().to_path_buf()));
        let _path = mgr.create(&slot_id).await.unwrap();
        mgr.remove(&slot_id).await.unwrap();

        // After explicit remove, tracked_slots should be empty.
        let tracked = mgr.tracked_slots.lock().unwrap();
        assert!(
            tracked.is_empty(),
            "slot should be untracked after explicit remove"
        );
    }

    #[test]
    fn tracking_is_idempotent() {
        let mgr = WorktreeManager::new("/repo", Some(PathBuf::from("/tmp/wt")));
        let id = SlotId("dup-test".into());
        mgr.track_slot(&id);
        mgr.track_slot(&id);
        let tracked = mgr.tracked_slots.lock().unwrap();
        assert_eq!(tracked.len(), 1, "duplicate tracking should be prevented");
    }
}