netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! Tor client implementation using arti
pub mod actions;

pub use actions::TorClientProtocol;

use anyhow::{Context, Result};
use arti_client::{TorClient as ArtiClient, TorClientConfig};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::{mpsc, Mutex};
use tracing::{error, info, trace, warn};

#[cfg(unix)]
use serde::Serialize;
#[cfg(unix)]
use tor_netdir::{NetDir, Relay};

use crate::client::llm_budget::call_llm_for_client;
use crate::client::tor::actions::{TOR_CLIENT_CONNECTED_EVENT, TOR_CLIENT_DATA_RECEIVED_EVENT};
use crate::llm::ollama_client::OllamaClient;
use crate::llm::ClientLlmResult;
use crate::protocol::Event;
use crate::state::app_state::AppState;
use crate::state::{ClientId, ClientStatus};

/// Connection state for LLM processing
#[derive(Debug, Clone, PartialEq)]
enum ConnectionState {
    Idle,
    Processing,
    Accumulating,
}

/// Per-client data for LLM handling
struct ClientData {
    state: ConnectionState,
    queued_data: Vec<u8>,
    memory: String,
}

/// Relay filter criteria for directory queries
#[cfg(unix)]
#[derive(Debug, Clone, Default)]
pub struct RelayFilter {
    pub flags: Option<Vec<String>>,
    pub min_bandwidth: Option<u64>,
    pub nickname_pattern: Option<String>,
    pub limit: Option<usize>,
}

#[cfg(unix)]
impl RelayFilter {
    /// Check if a relay matches this filter
    fn matches(&self, _relay: &Relay<'_>) -> bool {
        // NOTE: tor-netdir's Relay type doesn't expose flag-checking methods in the public API
        // Even with experimental-api, the internal RouterStatus fields are not accessible
        // For now, we match all relays - filtering would require deeper Arti API integration

        // TODO: When Arti exposes flag/nickname access in experimental-api, implement:
        // - Flag filtering (Guard, Exit, Fast, Stable, Running, Valid)
        // - Nickname pattern matching
        // - Bandwidth filtering

        true
    }
}

/// Simplified relay information for LLM
#[cfg(unix)]
#[derive(Debug, Clone, Serialize)]
pub struct RelayInfo {
    pub nickname: String,
    pub fingerprint: String,
    pub flags: Vec<String>,
    pub is_guard: bool,
    pub is_exit: bool,
    pub is_fast: bool,
    pub is_stable: bool,
    pub is_running: bool,
    pub is_valid: bool,
}

#[cfg(unix)]
impl RelayInfo {
    /// Create RelayInfo from a Tor relay
    fn from_relay(relay: &Relay<'_>) -> Self {
        // Get relay identity - rsa_id() is available via public trait
        let fingerprint = format!("{:?}", relay.rsa_id());

        // Use first 8 chars of fingerprint as a proxy for nickname
        // NOTE: tor-netdir's Relay type doesn't expose nickname or flags in public API
        let nickname = fingerprint.chars().take(8).collect::<String>();

        // Return minimal info - flag details not accessible via current Arti API
        // TODO: Update when Arti's experimental-api exposes RouterStatus fields
        Self {
            nickname,
            fingerprint,
            flags: vec![], // Not accessible via public API
            is_guard: false,
            is_exit: false,
            is_fast: false,
            is_stable: false,
            is_running: false,
            is_valid: false,
        }
    }
}

/// Startup parameter that opts a Tor client in to the public Tor network.
///
/// Named as a constant because it appears in four places that must agree: the parameter
/// declaration, the refusal message, this module's check, and the test that pins all of it.
pub const ALLOW_PUBLIC_TOR_NETWORK_PARAM: &str = "allow_public_tor_network";

/// Where a Tor client is allowed to bootstrap from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BootstrapTarget {
    /// A directory the caller named explicitly — normally a local `tor_relay`.
    CustomDirectory(String),
    /// The real Tor directory authorities, on the public internet. Opt-in only.
    PublicNetwork,
}

/// Decide what `connect()` may bootstrap against, and refuse rather than reach the internet.
///
/// # Why this exists
///
/// `arti_client::TorClient::create_bootstrapped()` contacts the real Tor directory authorities
/// **before it ever looks at the requested address** — measured at 14 seconds in an unguarded
/// run. So merely *opening* a Tor client made outbound connections to third parties, whatever
/// the user asked it to connect to. That is surprising for a tool that binds loopback
/// everywhere else, and it made the client impossible to exercise offline: the whole-registry
/// smoke test had to carve out a named exclusion for Tor because running it would have put a
/// smoke test on the public internet.
///
/// # Why refuse rather than make it lazy
///
/// Deferring the bootstrap until a request "genuinely needs the network" buys nothing here:
/// `connect()` is *given* the destination, so the need is immediate and the bootstrap would
/// happen a few milliseconds later anyway — with the failure now surfacing somewhere with
/// worse reporting than `connect()`'s `Err`. Deferral would hide the reach, not prevent it.
/// The property worth having is that the reach is a **decision the caller made**, and the only
/// way to express that is an explicit opt-in.
///
/// # The rule
///
/// * `directory_server` set → the caller named the directory to bootstrap from (normally a
///   local `tor_relay`). That is already an explicit choice, so it is allowed as-is.
/// * `allow_public_tor_network: true` → the caller explicitly asked for the real network.
/// * Neither → `Err`, naming the parameter. **This is the default.**
///
/// Setting both is a contradiction and is refused rather than silently resolved: which one the
/// caller meant is not inferable, and guessing is how a "local test" ends up on the public
/// internet.
pub fn bootstrap_target(
    directory_server: Option<&str>,
    allow_public_tor_network: bool,
) -> Result<BootstrapTarget> {
    match (directory_server, allow_public_tor_network) {
        (Some(dir), false) => Ok(BootstrapTarget::CustomDirectory(dir.to_string())),
        (None, true) => Ok(BootstrapTarget::PublicNetwork),
        (Some(dir), true) => Err(anyhow::anyhow!(
            "Tor client: `directory_server` ({dir}) and `{ALLOW_PUBLIC_TOR_NETWORK_PARAM}` are \
             mutually exclusive — the first bootstraps from the directory you named, the second \
             from the public Tor directory authorities. Pass exactly one."
        )),
        (None, false) => Err(anyhow::anyhow!(
            "Tor client refused to start: bootstrapping contacts the real Tor directory \
             authorities on the public internet, and does so before it looks at the address you \
             asked for — so opening this client would make outbound connections to third \
             parties regardless of the destination. NetGet does not do that by default. Either \
             pass `directory_server` (e.g. \"127.0.0.1:9001\", a local `tor_relay`) to bootstrap \
             locally, or pass `{ALLOW_PUBLIC_TOR_NETWORK_PARAM}: true` to opt in to the public \
             Tor network."
        )),
    }
}

/// The write half of a Tor circuit, once one exists.
type TorWriteHalf = Arc<Mutex<tokio::io::WriteHalf<arti_client::DataStream>>>;

/// Run one directory verb. Needs no circuit — the consensus lives in `AppState`, put there
/// when `create_bootstrapped` returned.
#[cfg(unix)]
async fn run_directory_action(
    name: &str,
    data: &serde_json::Value,
    client_id: ClientId,
    app_state: &Arc<AppState>,
    status_tx: &mpsc::UnboundedSender<String>,
) {
    match name {
        "get_consensus_info" => match TorClient::get_consensus_info(app_state, client_id).await {
            Ok(info) => {
                let _ = status_tx.send(format!(
                    "[TOR] Consensus: {} relays, valid until {}",
                    info["relay_count"], info["valid_until"]
                ));
                trace!("Consensus info: {}", info);
            }
            Err(e) => {
                error!("Failed to get consensus info: {}", e);
                let _ = status_tx.send(format!("[TOR] Error: {}", e));
            }
        },
        "list_relays" | "search_relays" => {
            let limit = data.get("limit").and_then(|v| v.as_u64()).unwrap_or(100) as usize;
            let filter = RelayFilter {
                flags: data.get("flags").and_then(|v| v.as_array()).map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                }),
                nickname_pattern: data
                    .get("nickname")
                    .and_then(|v| v.as_str())
                    .map(String::from),
                limit: Some(limit),
                ..Default::default()
            };
            match TorClient::query_relays(app_state, client_id, filter).await {
                Ok(relays) => {
                    let _ = status_tx.send(format!("[TOR] Found {} relays", relays.len()));
                    for (i, relay) in relays.iter().take(10).enumerate() {
                        trace!(
                            "Relay {}: {} ({})",
                            i + 1,
                            relay.nickname,
                            relay.flags.join(", ")
                        );
                    }
                }
                Err(e) => {
                    error!("Failed to query relays: {}", e);
                    let _ = status_tx.send(format!("[TOR] Error: {}", e));
                }
            }
        }
        other => {
            warn!("Unknown custom action: {}", other);
        }
    }
}

/// Carry out what the model answered with. Returns `true` if it asked to disconnect.
///
/// `write_half` is `None` at `tor_bootstrap_complete`, which fires while `connect()` is still
/// bootstrapping and no circuit exists yet. A `send_tor_data` there is **refused loudly**
/// rather than dropped: the directory verbs are the ones that make sense at that moment, and
/// the model needs to be told which of its answers could not be carried out. Every other
/// caller has a circuit and passes `Some`.
///
/// `pub` so `tests/client/tor/` can pin the no-circuit contract directly: a live test would
/// need a real Tor bootstrap and a circuit, which this repo has no cheap loopback for.
pub async fn apply_actions(
    actions: Vec<serde_json::Value>,
    write_half: Option<&TorWriteHalf>,
    client_id: ClientId,
    app_state: &Arc<AppState>,
    status_tx: &mpsc::UnboundedSender<String>,
) -> bool {
    use crate::llm::actions::client_trait::{Client, ClientActionResult};
    let protocol = crate::client::tor::actions::TorClientProtocol::new();

    for action in actions {
        match protocol.execute_action(action) {
            Ok(ClientActionResult::SendData(bytes)) => {
                let Some(write_half) = write_half else {
                    warn!(
                        "Tor client {} cannot send {} bytes: no circuit yet",
                        client_id,
                        bytes.len()
                    );
                    let _ = status_tx.send(format!(
                        "[TOR] ✖ send_tor_data ignored for client {}: the circuit is not open yet",
                        client_id
                    ));
                    continue;
                };
                match write_half.lock().await.write_all(&bytes).await {
                    Ok(()) => trace!("Tor client {} sent {} bytes", client_id, bytes.len()),
                    Err(e) => error!("Tor client {} write failed: {}", client_id, e),
                }
            }
            Ok(ClientActionResult::Disconnect) => {
                info!("Tor client {} disconnecting", client_id);
                return true;
            }
            #[cfg(unix)]
            Ok(ClientActionResult::Custom { name, data }) => {
                run_directory_action(&name, &data, client_id, app_state, status_tx).await;
            }
            Ok(_) => {}
            Err(e) => {
                warn!("Tor client {} rejected action: {}", client_id, e);
                let _ = status_tx.send(format!(
                    "[TOR] ✖ client {} rejected action: {}",
                    client_id, e
                ));
            }
        }
    }
    false
}

/// Tor client that connects through the Tor network
pub struct TorClient;

impl TorClient {
    /// Connect to a destination through Tor with integrated LLM actions
    pub async fn connect_with_llm_actions(
        remote_addr: String,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        client_id: ClientId,
        startup_params: Option<crate::protocol::StartupParams>,
    ) -> Result<SocketAddr> {
        info!("Tor client {} initializing...", client_id);
        let _ = status_tx.send(format!("[CLIENT] Tor client {} initializing...", client_id));

        // Choose the directory to bootstrap from — and refuse to reach the public Tor network
        // unless the caller asked for it. See `bootstrap_target` for the reasoning.
        let directory_server = match startup_params {
            Some(ref params) => params.get_optional_string("directory_server")?,
            None => None,
        };
        let allow_public = match startup_params {
            Some(ref params) => params
                .get_optional_bool(ALLOW_PUBLIC_TOR_NETWORK_PARAM)?
                .unwrap_or(false),
            None => false,
        };

        let config = match bootstrap_target(directory_server.as_deref(), allow_public)? {
            BootstrapTarget::CustomDirectory(directory_server) => {
                info!(
                    "Tor client {} using custom directory server: {}",
                    client_id, directory_server
                );
                let _ = status_tx.send(format!(
                    "[CLIENT] Tor client {} using custom directory: {}",
                    client_id, directory_server
                ));
                Self::create_custom_config(&directory_server)?
            }
            BootstrapTarget::PublicNetwork => {
                // Loud, because this is the one path that leaves the machine.
                warn!(
                    "Tor client {} is bootstrapping against the PUBLIC Tor directory \
                     authorities ({}=true): this makes outbound connections to third parties \
                     before any traffic is sent to {}",
                    client_id, ALLOW_PUBLIC_TOR_NETWORK_PARAM, remote_addr
                );
                let _ = status_tx.send(format!(
                    "[CLIENT] âš  Tor client {} contacting the PUBLIC Tor directory authorities \
                     (opted in via {})",
                    client_id, ALLOW_PUBLIC_TOR_NETWORK_PARAM
                ));
                TorClientConfig::default()
            }
        };

        let tor_client = ArtiClient::create_bootstrapped(config)
            .await
            .context("Failed to bootstrap Tor client")?;

        info!("Tor client {} bootstrapped successfully", client_id);
        let _ = status_tx.send(format!("[CLIENT] Tor client {} bootstrapped", client_id));

        // Store Tor client for directory queries (requires experimental-api feature)
        #[cfg(unix)]
        app_state
            .set_tor_client(client_id, Arc::new(tor_client.clone()))
            .await;

        // Emit bootstrap complete event with consensus info
        #[cfg(unix)]
        if let Some(instruction) = app_state.get_instruction_for_client(client_id).await {
            let protocol = Arc::new(crate::client::tor::actions::TorClientProtocol::new());

            // Try to get consensus info (may fail if bootstrap just completed)
            match Self::get_consensus_info(&app_state, client_id).await {
                Ok(info) => {
                    use crate::client::tor::actions::TOR_BOOTSTRAP_COMPLETE_EVENT;
                    let event = Event::new(
                        &TOR_BOOTSTRAP_COMPLETE_EVENT,
                        serde_json::json!({
                            "relay_count": info["relay_count"],
                            "valid_after": info["valid_after"],
                        }),
                    );

                    // The answer is carried out, not just logged. At this point there is no
                    // circuit — `connect()` has not run — so `apply_actions` gets `None` and
                    // the directory verbs (`get_consensus_info`, `list_relays`,
                    // `search_relays`) are the ones that can do anything. That is exactly
                    // what this event is for: it reports `relay_count` and `valid_after`, and
                    // its whole purpose is letting the model query the consensus it just
                    // learned about.
                    match call_llm_for_client(
                        &llm_client,
                        &app_state,
                        client_id.to_string(),
                        &instruction,
                        "",
                        Some(&event),
                        protocol.as_ref(),
                        &status_tx,
                    )
                    .await
                    {
                        Ok(ClientLlmResult {
                            actions,
                            memory_updates,
                        }) => {
                            if let Some(mem) = memory_updates {
                                app_state.set_memory_for_client(client_id, mem).await;
                            }
                            apply_actions(actions, None, client_id, &app_state, &status_tx).await;
                        }
                        Err(e) => {
                            warn!("Failed to call LLM for bootstrap event: {}", e);
                        }
                    }
                }
                Err(e) => {
                    trace!(
                        "Could not get consensus info at bootstrap (may not be available yet): {}",
                        e
                    );
                }
            }
        }

        // Parse target address (can be hostname:port or .onion:port)
        let target = remote_addr.clone();

        // Connect through Tor
        let stream = tor_client
            .connect(target.as_str())
            .await
            .context(format!("Failed to connect to {} through Tor", target))?;

        // Get a dummy local address since Tor connections don't have real local addresses
        let local_addr = SocketAddr::from(([127, 0, 0, 1], 0));

        info!(
            "Tor client {} connected to {} through Tor network",
            client_id, remote_addr
        );

        // Update client state
        app_state
            .update_client_status(client_id, ClientStatus::Connected)
            .await;
        let _ = status_tx.send(format!(
            "[CLIENT] Tor client {} connected to {}",
            client_id, remote_addr
        ));
        let _ = status_tx.send("__UPDATE_UI__".to_string());

        // The `tor_connected` LLM call happens inside the read-loop task below, not here.
        // Two reasons, both of which were bugs at this spot: it needs the write half to carry
        // out what the model answers with, and it must not precede the command channel's
        // registration — a manual `*` routing rule parks this call until a human answers, and
        // the dashboard's `[ send ]` has to reach the client for the whole park.

        // Split stream into read/write halves
        let (mut read_half, write_half) = tokio::io::split(stream);
        let write_half_arc = Arc::new(Mutex::new(write_half));

        // Initialize client data
        let client_data = Arc::new(Mutex::new(ClientData {
            state: ConnectionState::Idle,
            queued_data: Vec::new(),
            memory: String::new(),
        }));

        // Spawn read loop
        // Registered with AppState so stop_client can abort this task —
        // dropping a JoinHandle only detaches it in Tokio.
        let task_registrar = app_state.clone();
        // Command channel: lets the dashboard inject actions into this loop
        // via AppState::send_to_client (see client/command_support.rs).
        let mut command_rx =
            crate::client::command_support::register_command_channel(&app_state, client_id).await;

        let task_handle = tokio::spawn(async move {
            // `tor_connected`: ask the model what to do with the circuit, then do it. The
            // answer used to be `Ok(_) => trace!("LLM called successfully")` — the round-trip
            // was paid for and every action the model chose was dropped on the floor, so a
            // client told "send an HTTP GET and analyse the response" sent nothing and then
            // waited forever for a reply to a request it never made.
            if let Some(instruction) = app_state.get_instruction_for_client(client_id).await {
                let protocol = crate::client::tor::actions::TorClientProtocol::new();
                let event = Event::new(
                    &TOR_CLIENT_CONNECTED_EVENT,
                    serde_json::json!({ "target": remote_addr }),
                );
                match call_llm_for_client(
                    &llm_client,
                    &app_state,
                    client_id.to_string(),
                    &instruction,
                    "",
                    Some(&event),
                    &protocol,
                    &status_tx,
                )
                .await
                {
                    Ok(ClientLlmResult {
                        actions,
                        memory_updates,
                    }) => {
                        if let Some(mem) = memory_updates {
                            client_data.lock().await.memory = mem;
                        }
                        if apply_actions(
                            actions,
                            Some(&write_half_arc),
                            client_id,
                            &app_state,
                            &status_tx,
                        )
                        .await
                        {
                            app_state
                                .update_client_status(client_id, ClientStatus::Disconnected)
                                .await;
                            let _ = status_tx.send("__UPDATE_UI__".to_string());
                            app_state.remove_client_handle(client_id).await;
                            return;
                        }
                    }
                    Err(e) => {
                        warn!(
                            "Failed to call LLM for Tor client {} connection: {}",
                            client_id, e
                        );
                    }
                }
            }

            let mut buffer = vec![0u8; 8192];

            loop {
                let read_result = tokio::select! {
                    read = read_half.read(&mut buffer) => read,
                    Some(cmd) = command_rx.recv() => {
                        let disconnect = crate::client::command_support::handle_stream_client_command(
                            &crate::client::tor::actions::TorClientProtocol,
                            &write_half_arc,
                            cmd,
                            client_id,
                            &app_state,
                            &status_tx,
                        )
                        .await;
                        if disconnect {
                            app_state
                                .update_client_status(client_id, ClientStatus::Disconnected)
                                .await;
                            let _ = status_tx.send(format!(
                                "[CLIENT] Tor client {} disconnected (injected action)",
                                client_id
                            ));
                            let _ = status_tx.send("__UPDATE_UI__".to_string());
                            break;
                        }
                        continue;
                    }
                };
                match read_result {
                    Ok(0) => {
                        info!("Tor client {} disconnected", client_id);
                        app_state
                            .update_client_status(client_id, ClientStatus::Disconnected)
                            .await;
                        let _ = status_tx
                            .send(format!("[CLIENT] Tor client {} disconnected", client_id));
                        let _ = status_tx.send("__UPDATE_UI__".to_string());
                        break;
                    }
                    Ok(n) => {
                        let data = buffer[..n].to_vec();
                        trace!("Tor client {} received {} bytes", client_id, n);

                        // Handle data with LLM.
                        //
                        // A `disconnect` used to `break` the `for action in actions` loop and
                        // nothing else, so the model could never actually close a Tor client:
                        // it stopped executing the rest of that one answer and went straight
                        // back to reading. The flag carries the decision out to the read loop.
                        let mut disconnect_requested = false;
                        let mut client_data_lock = client_data.lock().await;

                        match client_data_lock.state {
                            ConnectionState::Idle => {
                                // Process immediately
                                client_data_lock.state = ConnectionState::Processing;
                                drop(client_data_lock);

                                // Call LLM
                                if let Some(instruction) =
                                    app_state.get_instruction_for_client(client_id).await
                                {
                                    let protocol = Arc::new(
                                        crate::client::tor::actions::TorClientProtocol::new(),
                                    );
                                    let event = Event::new(
                                        &TOR_CLIENT_DATA_RECEIVED_EVENT,
                                        crate::client::tor::actions::inbound_event_fields(&data),
                                    );

                                    match call_llm_for_client(
                                        &llm_client,
                                        &app_state,
                                        client_id.to_string(),
                                        &instruction,
                                        &client_data.lock().await.memory,
                                        Some(&event),
                                        protocol.as_ref(),
                                        &status_tx,
                                    )
                                    .await
                                    {
                                        Ok(ClientLlmResult {
                                            actions,
                                            memory_updates,
                                        }) => {
                                            if let Some(mem) = memory_updates {
                                                client_data.lock().await.memory = mem;
                                            }
                                            // Shared with the connected-event and
                                            // bootstrap-complete paths, so the Tor action
                                            // vocabulary is executed in exactly one place.
                                            disconnect_requested = apply_actions(
                                                actions,
                                                Some(&write_half_arc),
                                                client_id,
                                                &app_state,
                                                &status_tx,
                                            )
                                            .await;
                                        }
                                        Err(e) => {
                                            error!("LLM error for Tor client {}: {}", client_id, e);
                                        }
                                    }
                                }

                                // Process queued data if any
                                let mut client_data_lock = client_data.lock().await;
                                if !client_data_lock.queued_data.is_empty() {
                                    client_data_lock.queued_data.clear();
                                }
                                client_data_lock.state = ConnectionState::Idle;
                            }
                            ConnectionState::Processing => {
                                // Queue data
                                client_data_lock.queued_data.extend_from_slice(&data);
                                client_data_lock.state = ConnectionState::Accumulating;
                            }
                            ConnectionState::Accumulating => {
                                // Continue queuing
                                client_data_lock.queued_data.extend_from_slice(&data);
                            }
                        }

                        if disconnect_requested {
                            app_state
                                .update_client_status(client_id, ClientStatus::Disconnected)
                                .await;
                            let _ = status_tx.send(format!(
                                "[CLIENT] Tor client {} disconnected (model asked)",
                                client_id
                            ));
                            let _ = status_tx.send("__UPDATE_UI__".to_string());
                            break;
                        }
                    }
                    Err(e) => {
                        error!("Tor client {} read error: {}", client_id, e);
                        app_state
                            .update_client_status(client_id, ClientStatus::Error(e.to_string()))
                            .await;
                        let _ = status_tx.send("__UPDATE_UI__".to_string());
                        break;
                    }
                }
            }

            // The loop owns the only receiver; dropping the registered handle
            // makes later send_to_client calls fail fast instead of timing out.
            app_state.remove_client_handle(client_id).await;
        });
        task_registrar
            .register_client_task(client_id, task_handle)
            .await;

        Ok(local_addr)
    }

    /// Create custom TorClientConfig pointing to a localhost Tor relay
    ///
    /// This allows testing with a local tor_relay server (using BEGIN_DIR for directory queries)
    /// instead of real Tor network. The directory_server parameter should be in format "127.0.0.1:9001"
    #[cfg(unix)]
    fn create_custom_config(directory_server: &str) -> Result<TorClientConfig> {
        use arti_client::config::dir::FallbackDir;

        // Parse the directory server address
        let addr: SocketAddr = directory_server.parse().context(
            "Invalid directory_server address format. Expected 'IP:port' like '127.0.0.1:9030'",
        )?;

        // WARNING: Arti FallbackDir requires an OR (relay) port, but we only have HTTP directory
        // This configuration will likely fail during bootstrap because Arti expects a working
        // Tor relay at the OR port, not just an HTTP directory server.
        //
        // For now, we configure the OR port to point to the same address as a workaround,
        // but this is a fundamental incompatibility: Arti needs a full Tor relay to bootstrap,
        // not just a directory server.

        let mut fallback = FallbackDir::builder();
        fallback
            .rsa_identity([0x42; 20].into()) // Dummy RSA identity
            .ed_identity([0x99; 32].into()) // Dummy Ed25519 identity
            .orports() // Arti only supports orports(), not dirports()
            .push(addr);

        // Build config with custom fallback
        let mut bld = TorClientConfig::builder();
        bld.tor_network().set_fallback_caches(vec![fallback]);

        // Note: We don't set custom authorities here because fallback_caches is sufficient
        // for bootstrapping. The client will use the fallback to fetch the consensus.

        let config = bld.build().context("Failed to build TorClientConfig")?;

        Ok(config)
    }

    /// Get network directory from Arti (requires experimental-api feature)
    #[cfg(unix)]
    pub async fn get_netdir(
        app_state: &Arc<crate::state::app_state::AppState>,
        client_id: crate::state::ClientId,
    ) -> Result<Arc<NetDir>> {
        let tor_client = app_state
            .get_tor_client(client_id)
            .await
            .context("Tor client not found in app state")?;

        // Access directory manager (requires experimental-api feature)
        let dirmgr = tor_client.dirmgr();

        // Get current network directory
        let netdir = dirmgr
            .netdir(tor_netdir::Timeliness::Timely)
            .context("No network directory available - client may still be bootstrapping")?;

        Ok(netdir)
    }

    /// Query relays from network directory with optional filter
    #[cfg(unix)]
    pub async fn query_relays(
        app_state: &Arc<crate::state::app_state::AppState>,
        client_id: crate::state::ClientId,
        filter: RelayFilter,
    ) -> Result<Vec<RelayInfo>> {
        let netdir = Self::get_netdir(app_state, client_id).await?;

        let limit = filter.limit.unwrap_or(100);
        let mut relays = Vec::new();

        for relay in netdir.relays() {
            if filter.matches(&relay) {
                relays.push(RelayInfo::from_relay(&relay));
                if relays.len() >= limit {
                    break;
                }
            }
        }

        Ok(relays)
    }

    /// Get consensus metadata (relay count, validity times)
    #[cfg(unix)]
    pub async fn get_consensus_info(
        app_state: &Arc<crate::state::app_state::AppState>,
        client_id: crate::state::ClientId,
    ) -> Result<serde_json::Value> {
        let netdir = Self::get_netdir(app_state, client_id).await?;

        let relay_count = netdir.relays().count();
        let lifetime = netdir.lifetime();

        Ok(serde_json::json!({
            "relay_count": relay_count,
            "valid_after": format!("{:?}", lifetime.valid_after()),
            "fresh_until": format!("{:?}", lifetime.fresh_until()),
            "valid_until": format!("{:?}", lifetime.valid_until()),
        }))
    }
}