trusty-mpm 1.8.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
//! Tests for `content::first_use` (#9396): tm installs the content release on
//! first use, once, never over a present lock, and fails closed offline.
//!
//! A child of `bundle_cache_tests` so it reuses that file's in-memory
//! [`FakeSource`]; nothing here touches the network.

use std::sync::atomic::{AtomicUsize, Ordering};

use trusty_common::content::ContentSource;

use super::*;
use crate::content::first_use::resolve_or_fetch_in;

/// A [`ReleaseSource`] that counts the listings and asset reads it serves.
struct Counting<'a> {
    inner: &'a FakeSource,
    listings: AtomicUsize,
    assets: AtomicUsize,
}

impl<'a> Counting<'a> {
    fn new(inner: &'a FakeSource) -> Self {
        Self {
            inner,
            listings: AtomicUsize::new(0),
            assets: AtomicUsize::new(0),
        }
    }

    fn listings(&self) -> usize {
        self.listings.load(Ordering::SeqCst)
    }

    fn assets(&self) -> usize {
        self.assets.load(Ordering::SeqCst)
    }
}

impl ReleaseSource for Counting<'_> {
    fn asset_url(&self, tag: &str, file: &str) -> String {
        self.inner.asset_url(tag, file)
    }

    fn asset(&self, tag: &str, file: &str, max: u64) -> Result<Option<Vec<u8>>, FetchError> {
        self.assets.fetch_add(1, Ordering::SeqCst);
        self.inner.asset(tag, file, max)
    }

    fn content_tags(&self) -> Result<Vec<String>, FetchError> {
        self.listings.fetch_add(1, Ordering::SeqCst);
        self.inner.content_tags()
    }

    fn release(&self, tag: &str) -> Result<Release, FetchError> {
        self.inner.release(tag)
    }
}

/// The installed tag `content` is served from; panics on any other source.
fn installed_tag(content: &trusty_common::content::ResolvedContent) -> String {
    match content.source() {
        ContentSource::Installed { tag, .. } => tag.clone(),
        other => panic!("expected the installed bundle, got {other:?}"),
    }
}

/// Every `*.tar.gz` stored in `cache`.
fn stored_bundles(cache: &Path) -> Vec<String> {
    std::fs::read_dir(cache)
        .expect("read cache")
        .flatten()
        .filter_map(|e| e.file_name().to_str().map(str::to_owned))
        .filter(|name| name.ends_with(".tar.gz"))
        .collect()
}

/// #9396: with no lock, the first resolution fetches the release `tm content
/// update` picks (the newest), pins it and serves it; the second one reads
/// the pin and fetches nothing.
#[test]
fn missing_lock_fetches_the_release_once() {
    let cache = tempfile::tempdir().unwrap();
    let mut fake = FakeSource::default();
    fake.publish(A);
    fake.publish(B);
    let source = Counting::new(&fake);
    for _ in 0..2 {
        let content = resolve_or_fetch_in(cache.path(), DevOverride::Off, |c| {
            install_if_missing(c, &source)
        })
        .expect("fetched, verified and served");
        assert_eq!(installed_tag(&content), B);
    }
    assert_eq!(source.listings(), 1, "one release listing in total");
    assert_eq!(source.assets(), 2, "one bundle and one sidecar in total");
    assert_eq!(pinned(cache.path()).tag(), B);
    resolves_to(cache.path(), B);
}

/// #9396: a present lock never triggers a fetch — a valid one is served, and
/// one that does not parse is an error left untouched for `tm content update`.
#[test]
fn present_lock_never_fetches() {
    let (src, cache) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
    let bytes = bundle(A, 1, A.as_bytes());
    install_from_file(cache.path(), &bundle_file(src.path(), A, &bytes, true)).expect("pin A");
    let mut fake = FakeSource::default();
    fake.publish(B);
    let calls = AtomicUsize::new(0);
    let fetch = |c: &Path| {
        calls.fetch_add(1, Ordering::SeqCst);
        install_if_missing(c, &fake)
    };
    let content = resolve_or_fetch_in(cache.path(), DevOverride::Off, fetch).expect("pinned A");
    assert_eq!(installed_tag(&content), A, "the present pin is served");

    let broken = tempfile::tempdir().unwrap();
    let lock = broken.path().join(LOCK_FILE_NAME);
    std::fs::write(&lock, "not = [valid").unwrap();
    let fetch = |c: &Path| {
        calls.fetch_add(1, Ordering::SeqCst);
        install_if_missing(c, &fake)
    };
    let err = resolve_or_fetch_in(broken.path(), DevOverride::Off, fetch)
        .expect_err("an unreadable lock is an error");
    assert!(!err.is_not_installed(), "{err:?}");
    assert_eq!(std::fs::read_to_string(&lock).unwrap(), "not = [valid");
    assert_eq!(
        calls.load(Ordering::SeqCst),
        0,
        "a present lock never fetches"
    );
}

/// #9396: offline, or with bytes that fail their sidecar, the first use fails
/// closed: no lock, no stored bundle, and the error names `tm content update`
/// and the offline `--from` install.
#[test]
fn missing_lock_offline_fails_closed_naming_update_and_from() {
    let offline = FakeSource {
        offline: true,
        ..FakeSource::default()
    };
    let mut tampered = FakeSource::default();
    let bytes = bundle(A, 1, A.as_bytes());
    tampered.put(A, &bytes);
    tampered
        .assets
        .insert(format!("{A}/{A}.tar.gz.sha256"), sidecar(A, b"other bytes"));
    for source in [&offline, &tampered] {
        let cache = tempfile::tempdir().unwrap();
        let err = resolve_or_fetch_in(cache.path(), DevOverride::Off, |c| {
            install_if_missing(c, source)
        })
        .expect_err("nothing verified to serve");
        assert!(err.is_not_installed(), "{err:?}");
        let msg = err.to_string();
        assert!(msg.contains("run `tm content update`"), "{msg}");
        assert!(msg.contains(INSTALL_HINT), "{msg}");
        assert!(!cache.path().join(LOCK_FILE_NAME).exists(), "no lock");
        assert!(stored_bundles(cache.path()).is_empty(), "no stored bundle");
    }
}

/// #9396: two first uses racing on an empty cache both serve the one pin the
/// first wrote; the second re-checks under the update lock and fetches
/// nothing.
#[test]
fn concurrent_first_use_leaves_one_valid_lock() {
    let cache = tempfile::tempdir().unwrap();
    let mut fake = FakeSource::default();
    fake.publish(B);
    let (entered_tx, entered_rx) = channel();
    let (release_tx, release_rx) = channel();
    fake.gate = Some(Gate {
        file: format!("{B}.tar.gz"),
        entered: entered_tx,
        release: Mutex::new(release_rx),
        timeout: Duration::from_secs(5),
    });
    let source = Counting::new(&fake);
    let first_use = || {
        resolve_or_fetch_in(cache.path(), DevOverride::Off, |c| {
            install_if_missing(c, &source)
        })
        .map(|content| installed_tag(&content))
    };
    std::thread::scope(|s| {
        let first = s.spawn(first_use);
        // The first is mid-fetch, holding the update lock, with no lock file
        // written yet: the second resolves "not installed" and must wait.
        let reached = entered_rx.recv_timeout(Duration::from_secs(10));
        let second = s.spawn(first_use);
        std::thread::sleep(Duration::from_millis(200));
        let _ = release_tx.send(());
        let first = first.join().expect("join");
        let second = second.join().expect("join");
        reached.expect("the first use reached its fetch");
        assert_eq!(first.expect("first use"), B);
        assert_eq!(second.expect("second use"), B);
    });
    assert_eq!(source.listings(), 1, "only one first use fetched");
    assert_eq!(pinned(cache.path()).tag(), B);
    resolves_to(cache.path(), B);
}

/// A first-use fetch that fails as an unreachable network does.
fn unreachable_fetch(_: &Path) -> Result<Option<UpdateOutcome>, CacheError> {
    Err(CacheError::Network {
        url: "https://api.github.com".to_owned(),
        reason: "network is unreachable".to_owned(),
        tag: None,
        fallback: Fallback::None,
    })
}

/// #9396: after a failed fetch, a composition within the window does not
/// fetch again, and says why; after the window it does. The memo never
/// serves content: a bundle installed meanwhile resolves at once.
#[test]
fn a_failed_fetch_is_not_retried_within_the_window() {
    use crate::content::first_use::{FailureMemo, resolve_or_fetch_with};
    let memo = FailureMemo::new(Duration::from_secs(60));
    let cache = tempfile::tempdir().unwrap();
    let t0 = std::time::Instant::now();
    let calls = AtomicUsize::new(0);
    let failing = |c: &Path| {
        calls.fetch_add(1, Ordering::SeqCst);
        unreachable_fetch(c)
    };
    let first = resolve_or_fetch_with(cache.path(), DevOverride::Off, failing, &memo, || t0)
        .expect_err("unreachable");
    assert!(
        first.to_string().contains("network is unreachable"),
        "{first}"
    );
    let again = resolve_or_fetch_with(
        cache.path(),
        DevOverride::Off,
        |_: &Path| -> Result<Option<UpdateOutcome>, CacheError> {
            panic!("retried inside the window")
        },
        &memo,
        || t0 + Duration::from_secs(30),
    )
    .expect_err("still not installed");
    assert!(again.is_not_installed(), "{again:?}");
    let msg = again.to_string();
    assert!(msg.contains("network is unreachable"), "{msg}");
    assert!(msg.contains("did not retry"), "{msg}");
    assert!(msg.contains("tm content update"), "{msg}");

    let later = t0 + Duration::from_secs(61);
    resolve_or_fetch_with(cache.path(), DevOverride::Off, failing, &memo, || later)
        .expect_err("still unreachable");
    assert_eq!(calls.load(Ordering::SeqCst), 2, "retried after the window");

    let src = tempfile::tempdir().unwrap();
    let bytes = bundle(A, 1, A.as_bytes());
    install_from_file(cache.path(), &bundle_file(src.path(), A, &bytes, true)).expect("pin A");
    let content = resolve_or_fetch_with(
        cache.path(),
        DevOverride::Off,
        |_: &Path| -> Result<Option<UpdateOutcome>, CacheError> { panic!("a lock is present") },
        &memo,
        || later,
    )
    .expect("the installed bundle serves inside the window");
    assert_eq!(installed_tag(&content), A);
}

/// #9396: a fetch that takes 120 s to fail is memoized from when it failed,
/// so a composition 10 s after the failure does not fetch again.
#[test]
fn a_slow_failed_fetch_is_memoized_from_when_it_failed() {
    use crate::content::first_use::{FailureMemo, resolve_or_fetch_with};
    let memo = FailureMemo::new(Duration::from_secs(60));
    let cache = tempfile::tempdir().unwrap();
    let t0 = std::time::Instant::now();
    let clock = Mutex::new(t0);
    let now = || *clock.lock().unwrap();
    let slow = |c: &Path| {
        *clock.lock().unwrap() += Duration::from_secs(120);
        unreachable_fetch(c)
    };
    resolve_or_fetch_with(cache.path(), DevOverride::Off, slow, &memo, now)
        .expect_err("unreachable");
    *clock.lock().unwrap() = t0 + Duration::from_secs(130);
    let never = |_: &Path| -> Result<Option<UpdateOutcome>, CacheError> {
        panic!("fetched again 10 s after a failure")
    };
    let again = resolve_or_fetch_with(cache.path(), DevOverride::Off, never, &memo, now)
        .expect_err("memoized");
    assert!(again.to_string().contains("did not retry"), "{again}");
}

/// #9396: callers that queue behind a failing fetch read its failure from
/// the memo, so an outage costs one fetch, not one per caller.
#[test]
fn callers_queued_behind_a_failing_fetch_do_not_fetch_again() {
    use crate::content::first_use::{FailureMemo, resolve_or_fetch_with};
    const CALLERS: usize = 8;
    let memo = FailureMemo::new(Duration::from_secs(60));
    let cache = tempfile::tempdir().unwrap();
    let fetches = AtomicUsize::new(0);
    let start = std::sync::Barrier::new(CALLERS);
    let failing = |c: &Path| {
        fetches.fetch_add(1, Ordering::SeqCst);
        std::thread::sleep(Duration::from_millis(200));
        unreachable_fetch(c)
    };
    std::thread::scope(|s| {
        for _ in 0..CALLERS {
            s.spawn(|| {
                start.wait();
                let err = resolve_or_fetch_with(
                    cache.path(),
                    DevOverride::Off,
                    failing,
                    &memo,
                    std::time::Instant::now,
                )
                .expect_err("unreachable");
                assert!(err.to_string().contains("network is unreachable"), "{err}");
            });
        }
    });
    assert_eq!(fetches.load(Ordering::SeqCst), 1, "one fetch per outage");
}

/// #9396: a fetch run from a task on a one-worker runtime hands the worker
/// off, so another task still runs while the fetch blocks. Without that the
/// releasing task starves until the fetch gives up.
#[test]
fn a_fetch_inside_a_runtime_leaves_the_worker_free() {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(1)
        .enable_all()
        .build()
        .expect("runtime");
    let cache = tempfile::tempdir().unwrap();
    let path = cache.path().to_path_buf();
    let (entered_tx, entered_rx) = channel::<()>();
    let (release_tx, release_rx) = channel::<()>();
    let released = runtime.block_on(async move {
        let fetching = tokio::spawn(async move {
            let mut released = false;
            let _ = resolve_or_fetch_in(&path, DevOverride::Off, |c| {
                let _ = entered_tx.send(());
                released = release_rx.recv_timeout(Duration::from_secs(5)).is_ok();
                unreachable_fetch(c)
            });
            released
        });
        // The fetch now blocks the only worker; this task needs one to run.
        entered_rx
            .recv_timeout(Duration::from_secs(10))
            .expect("the fetch started");
        tokio::spawn(async move {
            let _ = release_tx.send(());
        });
        fetching.await.expect("fetch task")
    });
    assert!(
        released,
        "the releasing task never ran while the fetch blocked"
    );
}

/// Lays out a tm-managed workspace `<home>/trusty-mpm-projects/<owner>/<repo>`
/// holding `.git`; a `trusty-tools` repo also gets a stale checkout's layout
/// (every class directory, a `[workspace]` manifest, no `BASE-AGENT.md`).
fn managed_workspace(home: &Path, owner: &str, repo: &str) -> std::path::PathBuf {
    let root = home.join("trusty-mpm-projects").join(owner).join(repo);
    std::fs::create_dir_all(root.join(".git")).expect(".git");
    if repo == "trusty-tools" {
        for (_, rel) in trusty_common::content::DEV_CLASS_SOURCES {
            std::fs::create_dir_all(root.join(rel)).expect("class dir");
        }
        std::fs::write(root.join("Cargo.toml"), "[workspace]\nmembers = []\n").expect("manifest");
        std::fs::write(root.join("content/agents/engineer.md"), "e\n").expect("agent");
    } else {
        std::fs::write(root.join("Cargo.toml"), "[package]\nname = \"w\"\n").expect("manifest");
    }
    root
}

/// #9396: from a tm-managed workspace that is not trusty-tools, an empty
/// cache is `NotInstalled` naming the remedy (the first-use fetch failing
/// leaves the same answer), and once a bundle is installed into the cache
/// the very next resolution serves it — no restart, no re-init. A stale
/// trusty-tools base clone serves its own tree, and its error names the
/// clone and `git pull`, never `tm content update`.
#[test]
fn a_managed_workspace_resolves_the_cache_once_a_bundle_lands() {
    use crate::content::first_use::FETCH_OVERRIDE;
    use crate::core::content_source::{Fetch, resolve_for_in};
    use trusty_agents_common::agent_content::{AgentRoster, REMEDY};

    let home = tempfile::tempdir().unwrap();
    let cache = home.path().join(".trusty-mpm/content");
    std::fs::create_dir_all(&cache).unwrap();
    let project = managed_workspace(home.path(), "acme", "widget");
    let stale = managed_workspace(home.path(), "bobmatnyc", "trusty-tools");

    let err =
        resolve_for_in(Some(&project), None, Some(&cache), Fetch::Never).expect_err("empty cache");
    assert!(err.is_not_installed(), "{err:?}");
    assert!(err.to_string().contains(REMEDY), "{err}");
    FETCH_OVERRIDE.with(|f| f.set(Some(unreachable_fetch)));
    let fetched = resolve_for_in(Some(&project), None, Some(&cache), Fetch::OnFirstUse);
    FETCH_OVERRIDE.with(|f| f.set(None));
    let err = fetched.expect_err("the fetch failed");
    assert!(err.is_not_installed(), "{err:?}");
    assert!(err.to_string().contains("tm content update"), "{err}");

    let src = tempfile::tempdir().unwrap();
    let bytes = bundle(A, 1, A.as_bytes());
    install_from_file(&cache, &bundle_file(src.path(), A, &bytes, true)).expect("pin A");
    for fetch in [Fetch::Never, Fetch::OnFirstUse] {
        let content = resolve_for_in(Some(&project), None, Some(&cache), fetch)
            .expect("the installed bundle serves");
        assert_eq!(installed_tag(&content), A);
    }

    let content = resolve_for_in(Some(&stale), None, Some(&cache), Fetch::Never)
        .expect("the clone serves its working tree");
    let msg = AgentRoster::load(&content)
        .expect_err("no BASE-AGENT.md in the stale clone")
        .to_string();
    assert!(msg.contains(&stale.display().to_string()), "{msg}");
    assert!(msg.contains("git pull"), "{msg}");
    assert!(!msg.contains("tm content update"), "{msg}");
}