trusty-common 0.67.5

Shared utilities and provider-agnostic streaming chat (ChatProvider, OllamaProvider, OpenRouter, tool-use) for trusty-* projects
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
//! The protocol handshake's client half, against fake daemons (#9288).
//!
//! Why: the real daemon only ever answers its own version, so the refusal arms
//! — a newer or older daemon, a broken answer, a daemon that predates the
//! handshake — need a socket that answers what the test chooses.
//! What: each test serves an [`RpcRouter`] on a temp socket and drives the
//! shared client against it.
//! Test: this file.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use serde_json::{Value, json};
use tokio::sync::oneshot;

use super::*;
use crate::uds::server::{RpcError, RpcRouter, RpcServeOptions, serve_until};

const BUDGET: Duration = Duration::from_secs(10);

/// A router served on a temp socket; dropping it stops the accept loop.
struct Fake {
    socket: PathBuf,
    _dir: tempfile::TempDir,
    stop: Option<oneshot::Sender<()>>,
}

impl Drop for Fake {
    fn drop(&mut self) {
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(());
        }
    }
}

fn serve(router: RpcRouter) -> Fake {
    let dir = tempfile::tempdir().expect("tempdir");
    let socket = dir.path().join("fake-memory.sock");
    let listener = crate::uds::bind_hardened(&socket).expect("bind the fake daemon");
    let (stop, shutdown) = oneshot::channel::<()>();
    tokio::spawn(async move {
        serve_until(
            &listener,
            Arc::new(router),
            RpcServeOptions::default(),
            async {
                let _ = shutdown.await;
            },
        )
        .await;
    });
    Fake {
        socket,
        _dir: dir,
        stop: Some(stop),
    }
}

/// A router whose `memory.protocol` answers `body`.
fn answering(body: Value) -> RpcRouter {
    RpcRouter::new().typed::<Value, Value, _, _>(METHOD_PROTOCOL, move |_| {
        let body = body.clone();
        async move { Ok(body) }
    })
}

/// A router whose `memory.protocol` refuses with `error`.
fn refusing(error: RpcError) -> RpcRouter {
    RpcRouter::new().typed::<Value, Value, _, _>(METHOD_PROTOCOL, move |_| {
        let error = error.clone();
        async move { Err(error) }
    })
}

/// Why (#9288): a daemon in range must be callable, and the info it reported
/// must reach the caller unchanged.
/// Test: itself.
#[tokio::test]
async fn protocol_check_accepts_a_daemon_in_the_supported_range() {
    let version = *SUPPORTED_MEMORY_PROTOCOLS.end();
    let fake = serve(answering(
        json!({ "protocol_version": version, "daemon_version": "9.9.9" }),
    ));

    let verdict = check_memory_protocol_at(&fake.socket, BUDGET)
        .await
        .expect("an in-range daemon is callable");

    assert_eq!(
        verdict,
        MemoryProtocol::Supported(MemoryProtocolInfo::new(version, Some("9.9.9".to_string())))
    );
}

/// Why (#9288): an old client meeting a newer daemon — or a new client meeting
/// an older one — must get the named error, not a parse failure later.
/// Test: itself.
#[tokio::test]
async fn protocol_check_refuses_an_out_of_range_daemon_with_a_named_error() {
    let below = SUPPORTED_MEMORY_PROTOCOLS.start().saturating_sub(1);
    let above = SUPPORTED_MEMORY_PROTOCOLS.end() + 1;
    for reported in [below, above] {
        let fake = serve(answering(
            json!({ "protocol_version": reported, "daemon_version": "2.0.0" }),
        ));

        let err = check_memory_protocol_at(&fake.socket, BUDGET)
            .await
            .expect_err("an out-of-range daemon is refused");

        match &err {
            MemoryProtocolError::Unsupported {
                daemon,
                daemon_version,
                min,
                max,
                ..
            } => {
                assert_eq!(*daemon, reported);
                assert_eq!(daemon_version, "2.0.0");
                assert_eq!(*min, *SUPPORTED_MEMORY_PROTOCOLS.start());
                assert_eq!(*max, *SUPPORTED_MEMORY_PROTOCOLS.end());
            }
            other => panic!("protocol {reported}: expected Unsupported, got {other:?}"),
        }
        assert!(
            err.to_string()
                .starts_with("unsupported trusty-memory protocol"),
            "the refusal names itself: {err}"
        );
    }
}

/// Why (#9288): a daemon that predates the handshake answers method-not-found.
/// That one answer reads as `PreHandshake` — callable during a rolling upgrade,
/// and distinct from `Supported` so no caller mistakes it for a checked daemon.
/// Test: itself.
#[tokio::test]
async fn protocol_check_reads_a_pre_handshake_daemon_as_pre_handshake() {
    let fake = serve(
        RpcRouter::new().typed::<Value, Value, _, _>("memory.health", |_| async {
            Ok(json!({ "status": "ok" }))
        }),
    );

    let verdict = check_memory_protocol_at(&fake.socket, BUDGET)
        .await
        .expect("a pre-handshake daemon is callable by rule");

    assert_eq!(verdict, MemoryProtocol::PreHandshake);
}

/// Why (#9288, Fail-Open Check): a version query that fails must never fall
/// through to a callable verdict. Only method-not-found reads as
/// pre-handshake; every other refusal, an unreadable body, and a dead socket
/// are errors.
/// Test: itself.
#[tokio::test]
async fn protocol_check_fails_closed_when_the_query_fails() {
    let refusals = [
        RpcError::internal("handshake handler failed"),
        RpcError::new(CODE_NOT_FOUND, "not found"),
        RpcError::invalid_params("bad params"),
    ];
    for error in refusals {
        let code = error.code;
        let fake = serve(refusing(error));
        let err = check_memory_protocol_at(&fake.socket, BUDGET)
            .await
            .expect_err("a refused query is not callable");
        assert!(
            matches!(err, MemoryProtocolError::HandshakeFailed { .. }),
            "code {code}: expected HandshakeFailed, got {err:?}"
        );
    }

    let bodies = [
        json!({ "protocol_version": "1" }),
        json!({ "protocol_version": -1 }),
        json!({}),
        json!("ok"),
    ];
    for body in bodies {
        let fake = serve(answering(body.clone()));
        let err = check_memory_protocol_at(&fake.socket, BUDGET)
            .await
            .expect_err("an unreadable answer is not callable");
        assert!(
            matches!(err, MemoryProtocolError::MalformedHandshake { .. }),
            "body {body}: expected MalformedHandshake, got {err:?}"
        );
    }

    let dir = tempfile::tempdir().expect("tempdir");
    let err = check_memory_protocol_at(&dir.path().join("absent.sock"), BUDGET)
        .await
        .expect_err("a dead socket is not callable");
    assert!(
        matches!(err, MemoryProtocolError::HandshakeFailed { .. }),
        "dead socket: expected HandshakeFailed, got {err:?}"
    );
}

/// Why (#9288): a refusal must not be remembered, or a daemon restarted into
/// the installed release stays refused; a callable verdict is reused inside
/// the recheck interval.
/// Test: itself.
#[tokio::test]
async fn an_unsupported_verdict_is_not_cached() {
    let reported = Arc::new(AtomicU64::new(SUPPORTED_MEMORY_PROTOCOLS.end() + 1));
    let answer = Arc::clone(&reported);
    let fake = serve(
        RpcRouter::new().typed::<Value, Value, _, _>(METHOD_PROTOCOL, move |_| {
            let version = answer.load(Ordering::SeqCst);
            async move { Ok(json!({ "protocol_version": version })) }
        }),
    );

    let first = ensure_memory_protocol_at(&fake.socket, BUDGET).await;
    assert!(
        matches!(first, Err(MemoryProtocolError::Unsupported { .. })),
        "{first:?}"
    );

    reported.store(*SUPPORTED_MEMORY_PROTOCOLS.end(), Ordering::SeqCst);
    let healed = ensure_memory_protocol_at(&fake.socket, BUDGET).await;
    assert!(
        matches!(healed, Ok(MemoryProtocol::Supported(_))),
        "the refusal was cached: {healed:?}"
    );

    reported.store(SUPPORTED_MEMORY_PROTOCOLS.end() + 1, Ordering::SeqCst);
    let reused = ensure_memory_protocol_at(&fake.socket, BUDGET).await;
    assert!(
        matches!(reused, Ok(MemoryProtocol::Supported(_))),
        "a callable verdict is reused inside the interval: {reused:?}"
    );
}

/// Why (#9288): `MemoryRpcError` used to drop the error object's `data`.
/// Test: itself.
#[tokio::test]
async fn memory_rpc_error_keeps_the_data_member() {
    let detail = json!({ "palace": "p", "retryable": false });
    let fake = serve(refusing(
        RpcError::internal("refused with detail").with_data(detail.clone()),
    ));

    let err = call_memory_tool_at(&fake.socket, METHOD_PROTOCOL, json!({}))
        .await
        .expect_err("the fake refuses");
    let typed = err
        .downcast_ref::<MemoryRpcError>()
        .expect("a typed refusal");

    assert_eq!(typed.data.as_ref(), Some(&detail));
}

/// Why (#9288): a daemon that predates the handshake speaks protocol 1, so it
/// is callable only while this client supports protocol 1.
/// Test: itself.
#[test]
fn a_pre_handshake_daemon_is_refused_once_protocol_1_is_unsupported() {
    let socket = Path::new("/tmp/pre-handshake.sock");

    assert_eq!(
        pre_handshake_verdict(socket, &(1..=2)).expect("protocol 1 is supported"),
        MemoryProtocol::PreHandshake
    );
    let err = pre_handshake_verdict(socket, &(2..=3)).expect_err("protocol 1 is dropped");
    assert!(
        matches!(
            err,
            MemoryProtocolError::Unsupported {
                daemon: 1,
                min: 2,
                max: 3,
                ..
            }
        ),
        "{err:?}"
    );
}

/// Why (#9288, supervisor ruling): a daemon that predates the handshake is
/// called, and the operator is told to restart it — once per process, not on
/// every call of a long-lived client.
/// What: two such daemons on two sockets (so the verdict cache cannot hide the
/// second check); both are called, and the process logs exactly one warning
/// that names the daemon version and the restart, and asks `memory.health`
/// for a version at most once.
/// Test: itself.
#[tokio::test]
async fn a_pre_handshake_daemon_is_called_and_warned_about_once_per_process() {
    use tracing_subscriber::layer::SubscriberExt as _;

    // The current-thread runtime runs this body on one thread, so a
    // thread-default subscriber sees every event `ensure_memory_protocol_at`
    // emits.
    let warns = WarnLog::default();
    let logged = Arc::clone(&warns.0);
    let _log = tracing::subscriber::set_default(tracing_subscriber::registry().with(warns));

    let health_calls = Arc::new(AtomicU64::new(0));
    let mut fakes = Vec::new();
    let mut status_calls = Vec::new();
    for _ in 0..2 {
        let health = Arc::clone(&health_calls);
        let status = Arc::new(AtomicU64::new(0));
        let counted = Arc::clone(&status);
        let router = RpcRouter::new()
            .typed::<Value, Value, _, _>("memory.health", move |_| {
                health.fetch_add(1, Ordering::SeqCst);
                async { Ok(json!({ "status": "ok", "version": "0.29.0" })) }
            })
            .typed::<Value, Value, _, _>("memory.status", move |_| {
                counted.fetch_add(1, Ordering::SeqCst);
                async { Ok(json!({ "ok": true })) }
            });
        fakes.push(serve(router));
        status_calls.push(status);
    }

    for fake in &fakes {
        let verdict = ensure_memory_protocol_at(&fake.socket, BUDGET)
            .await
            .expect("a pre-handshake daemon is callable");
        assert_eq!(verdict, MemoryProtocol::PreHandshake);
        call_memory_tool_at(&fake.socket, "memory.status", json!({}))
            .await
            .expect("the call proceeds");
    }

    for status in &status_calls {
        assert_eq!(status.load(Ordering::SeqCst), 1, "each daemon is called");
    }
    assert_eq!(
        PRE_HANDSHAKE_WARNINGS.load(Ordering::SeqCst),
        1,
        "exactly one warning per process"
    );
    assert!(health_calls.load(Ordering::SeqCst) <= 1);
    let logged = logged.lock().expect("warn log");
    assert_eq!(logged.len(), 1, "one logged warning: {logged:?}");
    assert!(
        logged[0].contains("0.29.0") && logged[0].contains("Restart the trusty-memory daemon"),
        "the warning names the daemon version and the restart: {logged:?}"
    );
}

/// Collects the WARN events `memory_rpc` logs, for the once-per-process test.
#[derive(Clone, Default)]
struct WarnLog(Arc<std::sync::Mutex<Vec<String>>>);

impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for WarnLog {
    fn on_event(&self, event: &tracing::Event<'_>, _: tracing_subscriber::layer::Context<'_, S>) {
        let meta = event.metadata();
        if *meta.level() != tracing::Level::WARN || meta.target() != "trusty_common::memory_rpc" {
            return;
        }
        let mut text = String::new();
        event.record(&mut |_: &tracing::field::Field, v: &dyn std::fmt::Debug| {
            text.push_str(&format!("{v:?} "));
        });
        self.0.lock().expect("warn log").push(text);
    }
}

/// A router answering `memory.protocol` with `version` and counting every
/// `memory.status` that reaches its handler.
fn counting_status(version: u64, hits: Arc<AtomicU64>) -> RpcRouter {
    answering(json!({ "protocol_version": version })).typed::<Value, Value, _, _>(
        "memory.status",
        move |_| {
            hits.fetch_add(1, Ordering::SeqCst);
            async { Ok(json!({ "ok": true })) }
        },
    )
}

/// Why (#9538): every caller of the shared client must refuse an unsupported
/// daemon, not only the ones that remembered a wrapper.
/// What: a daemon on protocol 99 answers `memory.status`; the checked call
/// returns a downcastable `MemoryProtocolError::Unsupported`, the method never
/// reaches the handler, and the context still names the method. The raw call
/// is the only way past it.
/// Test: itself.
#[tokio::test]
async fn a_memory_call_is_refused_when_the_daemon_speaks_an_unsupported_protocol() {
    let hits = Arc::new(AtomicU64::new(0));
    let fake = serve(counting_status(99, Arc::clone(&hits)));

    for err in [
        call_memory_tool_at(&fake.socket, "memory.status", json!({}))
            .await
            .expect_err("protocol 99 is refused"),
        call_memory_tool_at_with_timeout(&fake.socket, "memory.status", json!({}), BUDGET)
            .await
            .expect_err("protocol 99 is refused"),
    ] {
        assert!(
            matches!(
                err.downcast_ref::<MemoryProtocolError>(),
                Some(MemoryProtocolError::Unsupported { daemon: 99, .. })
            ),
            "{err:?}"
        );
        assert!(format!("{err:#}").contains("call memory.status"), "{err:#}");
    }
    assert_eq!(hits.load(Ordering::SeqCst), 0, "the call must not be sent");

    call_memory_tool_at_raw_with_timeout(&fake.socket, "memory.status", json!({}), BUDGET)
        .await
        .expect("the raw call skips the check");
    assert_eq!(hits.load(Ordering::SeqCst), 1);
}

/// Why (#9538): the door must not break a supported daemon, and the handshake
/// must not recurse into the door.
/// What: a supported daemon is called through the checked path; asking for
/// `memory.protocol` itself answers the body even when it reports protocol 99.
/// Test: itself.
#[tokio::test]
async fn a_supported_daemon_is_called_and_the_handshake_method_is_not_checked() {
    let hits = Arc::new(AtomicU64::new(0));
    let supported = *SUPPORTED_MEMORY_PROTOCOLS.end();
    let fake = serve(counting_status(supported, Arc::clone(&hits)));
    call_memory_tool_at(&fake.socket, "memory.status", json!({}))
        .await
        .expect("a supported daemon is called");
    assert_eq!(hits.load(Ordering::SeqCst), 1);

    let future = serve(answering(json!({ "protocol_version": 99 })));
    let body = call_memory_tool_at(&future.socket, METHOD_PROTOCOL, json!({}))
        .await
        .expect("the handshake method itself is never checked");
    assert_eq!(body["protocol_version"], 99);
}