a3s-code-core 5.3.2

A3S Code Core - Embeddable AI agent library with tool execution
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
//! Long-lived language server process lifecycle.

use super::client::LspClient;
use super::router::ServerRequestRouter;
use crate::code_intelligence::language_profile::LanguageServerCommand;
use crate::tools::process::{configure_process_group, ProcessGroupGuard};
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex as StdMutex};
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::process::{Child, Command};
use tokio::sync::{mpsc, watch};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

const STDERR_LIMIT_BYTES: usize = 64 * 1024;
const STDERR_SETTLEMENT_TIMEOUT: Duration = Duration::from_secs(1);

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum LspProcessState {
    Running,
    Exited { code: Option<i32>, forced: bool },
    Failed { message: String },
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum LspProcessError {
    #[error("failed to start language server {program:?}: {source}")]
    Spawn {
        program: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("language server process did not expose piped {stream}")]
    MissingPipe { stream: &'static str },

    #[error("language server did not report an exit within {duration:?}")]
    ShutdownIncomplete { duration: Duration },
}

enum ProcessControl {
    ForceKill,
}

/// Handle to one process and its protocol client.
pub(crate) struct LspProcess {
    client: LspClient,
    control: mpsc::UnboundedSender<ProcessControl>,
    state: watch::Receiver<LspProcessState>,
    stderr: Arc<StdMutex<BoundedStderr>>,
    shutdown_started: AtomicBool,
}

impl std::fmt::Debug for LspProcess {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LspProcess")
            .field("state", &*self.state.borrow())
            .field("client_closed", &self.client.is_closed())
            .finish()
    }
}

impl LspProcess {
    pub(crate) fn spawn(
        command: &LanguageServerCommand,
        working_directory: &Path,
        router: ServerRequestRouter,
    ) -> Result<Self, LspProcessError> {
        let mut process = Command::new(&command.program);
        process
            .args(&command.args)
            .envs(&command.env)
            .current_dir(working_directory)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true);
        configure_process_group(&mut process);

        let mut child = process.spawn().map_err(|source| LspProcessError::Spawn {
            program: command.program.clone(),
            source,
        })?;
        let process_group = ProcessGroupGuard::for_child(&child);
        let stdout = child
            .stdout
            .take()
            .ok_or(LspProcessError::MissingPipe { stream: "stdout" })?;
        let stdin = child
            .stdin
            .take()
            .ok_or(LspProcessError::MissingPipe { stream: "stdin" })?;
        let stderr = child
            .stderr
            .take()
            .ok_or(LspProcessError::MissingPipe { stream: "stderr" })?;

        let client = LspClient::start_split(stdout, stdin, router);
        let protocol_closed = client.shutdown_token();
        let stderr_buffer = Arc::new(StdMutex::new(BoundedStderr::new(STDERR_LIMIT_BYTES)));
        let stderr_task = tokio::spawn(read_stderr(stderr, Arc::clone(&stderr_buffer)));
        let (control, control_rx) = mpsc::unbounded_channel();
        let (state_tx, state) = watch::channel(LspProcessState::Running);
        tokio::spawn(monitor_process(
            child,
            process_group,
            client.clone(),
            protocol_closed,
            control_rx,
            state_tx,
            stderr_task,
        ));

        Ok(Self {
            client,
            control,
            state,
            stderr: stderr_buffer,
            shutdown_started: AtomicBool::new(false),
        })
    }

    pub(crate) fn client(&self) -> LspClient {
        self.client.clone()
    }

    pub(crate) fn state(&self) -> LspProcessState {
        self.state.borrow().clone()
    }

    pub(crate) fn subscribe_state(&self) -> watch::Receiver<LspProcessState> {
        self.state.clone()
    }

    pub(crate) fn stderr_snapshot(&self) -> String {
        self.stderr
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .render()
    }

    /// Request a graceful protocol shutdown, then kill the process group if
    /// the server does not exit within the supplied bounds.
    pub(crate) async fn shutdown(
        &self,
        request_timeout: Duration,
        exit_timeout: Duration,
    ) -> Result<LspProcessState, LspProcessError> {
        if !self.shutdown_started.swap(true, Ordering::AcqRel)
            && matches!(self.state(), LspProcessState::Running)
        {
            let _ = self
                .client
                .request("shutdown", None, CancellationToken::new(), request_timeout)
                .await;
            // A saturated writer queue must not make host shutdown
            // unbounded after the graceful request has already timed out.
            let _ = tokio::time::timeout(request_timeout, self.client.notify("exit", None)).await;
        }

        if let Some(state) = wait_for_terminal_state(self.state.clone(), exit_timeout).await {
            return Ok(state);
        }

        let _ = self.control.send(ProcessControl::ForceKill);
        wait_for_terminal_state(self.state.clone(), exit_timeout)
            .await
            .ok_or(LspProcessError::ShutdownIncomplete {
                duration: exit_timeout,
            })
    }

    pub(crate) fn force_kill(&self) {
        self.shutdown_started.store(true, Ordering::Release);
        let _ = self.control.send(ProcessControl::ForceKill);
    }
}

impl Drop for LspProcess {
    fn drop(&mut self) {
        if matches!(self.state(), LspProcessState::Running) {
            let _ = self.control.send(ProcessControl::ForceKill);
        }
    }
}

async fn monitor_process(
    mut child: Child,
    mut process_group: ProcessGroupGuard,
    client: LspClient,
    protocol_closed: CancellationToken,
    mut control: mpsc::UnboundedReceiver<ProcessControl>,
    state: watch::Sender<LspProcessState>,
    mut stderr_task: JoinHandle<()>,
) {
    let (forced, result) = tokio::select! {
        result = child.wait() => (false, result),
        _ = protocol_closed.cancelled() => {
            match child.try_wait() {
                Ok(Some(status)) => (false, Ok(status)),
                Ok(None) => {
                    // A language server can close its protocol streams without
                    // exiting. Reap that generation before the workspace is
                    // allowed to start a replacement process.
                    process_group.kill();
                    let _ = child.start_kill();
                    (true, child.wait().await)
                }
                Err(error) => (false, Err(error)),
            }
        }
        command = control.recv() => {
            match command {
                Some(ProcessControl::ForceKill) | None => {
                    process_group.kill();
                    let _ = child.start_kill();
                    (true, child.wait().await)
                }
            }
        }
    };

    // A server can leave helper children behind even after its leader exits.
    process_group.kill();
    let final_state = match result {
        Ok(status) => LspProcessState::Exited {
            code: status.code(),
            forced,
        },
        Err(error) => LspProcessState::Failed {
            message: error.to_string(),
        },
    };
    let _ = state.send(final_state);
    client.close().await;

    if tokio::time::timeout(STDERR_SETTLEMENT_TIMEOUT, &mut stderr_task)
        .await
        .is_err()
    {
        stderr_task.abort();
    }
}

async fn wait_for_terminal_state(
    mut state: watch::Receiver<LspProcessState>,
    timeout: Duration,
) -> Option<LspProcessState> {
    if !matches!(*state.borrow(), LspProcessState::Running) {
        return Some(state.borrow().clone());
    }

    tokio::time::timeout(timeout, async {
        loop {
            state.changed().await.ok()?;
            if !matches!(*state.borrow(), LspProcessState::Running) {
                return Some(state.borrow().clone());
            }
        }
    })
    .await
    .ok()
    .flatten()
}

async fn read_stderr(
    mut stderr: tokio::process::ChildStderr,
    buffer: Arc<StdMutex<BoundedStderr>>,
) {
    let mut chunk = [0_u8; 4096];
    loop {
        match stderr.read(&mut chunk).await {
            Ok(0) | Err(_) => break,
            Ok(count) => buffer
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(&chunk[..count]),
        }
    }
}

#[derive(Debug)]
struct BoundedStderr {
    bytes: VecDeque<u8>,
    limit: usize,
    total_bytes: usize,
}

impl BoundedStderr {
    fn new(limit: usize) -> Self {
        Self {
            bytes: VecDeque::with_capacity(limit),
            limit,
            total_bytes: 0,
        }
    }

    fn push(&mut self, bytes: &[u8]) {
        self.total_bytes = self.total_bytes.saturating_add(bytes.len());
        self.bytes.extend(bytes.iter().copied());
        while self.bytes.len() > self.limit {
            self.bytes.pop_front();
        }
    }

    fn render(&self) -> String {
        let bytes = self.bytes.iter().copied().collect::<Vec<_>>();
        let stderr = String::from_utf8_lossy(&bytes);
        if self.total_bytes <= self.bytes.len() {
            stderr.into_owned()
        } else {
            format!(
                "[language server stderr truncated: retained the last {} of {} bytes]\n{}",
                self.bytes.len(),
                self.total_bytes,
                stderr
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::code_intelligence::lsp::router::{ServerRequestRouter, ServerRequestRouterConfig};
    use std::collections::BTreeMap;
    use std::ffi::OsString;
    use std::time::Instant;

    #[test]
    fn bounded_stderr_retains_only_the_tail() {
        let mut stderr = BoundedStderr::new(5);
        stderr.push(b"abc");
        stderr.push(b"defgh");

        let rendered = stderr.render();
        assert!(rendered.contains("last 5 of 8 bytes"));
        assert!(rendered.ends_with("defgh"));
    }

    #[tokio::test]
    async fn missing_executable_is_a_typed_spawn_error() {
        let command = LanguageServerCommand {
            program: PathBuf::from("a3s-code-missing-language-server-executable"),
            args: Vec::new(),
            env: BTreeMap::new(),
        };
        let directory = tempfile::tempdir().unwrap();
        let error = LspProcess::spawn(
            &command,
            directory.path(),
            ServerRequestRouter::new(ServerRequestRouterConfig::default()),
        )
        .unwrap_err();

        assert!(matches!(error, LspProcessError::Spawn { .. }));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn shutdown_kills_an_unresponsive_process_group() {
        let command = LanguageServerCommand {
            program: PathBuf::from("sh"),
            args: vec![OsString::from("-c"), OsString::from("sleep 30")],
            env: BTreeMap::new(),
        };
        let directory = tempfile::tempdir().unwrap();
        let process = LspProcess::spawn(
            &command,
            directory.path(),
            ServerRequestRouter::new(ServerRequestRouterConfig::default()),
        )
        .unwrap();
        let started = Instant::now();

        let state = process
            .shutdown(Duration::from_millis(50), Duration::from_millis(500))
            .await
            .unwrap();

        assert!(started.elapsed() < Duration::from_secs(2));
        assert!(matches!(
            state,
            LspProcessState::Exited { forced: true, .. }
        ));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn protocol_eof_reaps_a_still_running_process() {
        let command = LanguageServerCommand {
            program: PathBuf::from("sh"),
            args: vec![OsString::from("-c"), OsString::from("exec 1>&-; sleep 30")],
            env: BTreeMap::new(),
        };
        let directory = tempfile::tempdir().unwrap();
        let process = LspProcess::spawn(
            &command,
            directory.path(),
            ServerRequestRouter::new(ServerRequestRouterConfig::default()),
        )
        .unwrap();

        let state = wait_for_terminal_state(process.subscribe_state(), Duration::from_secs(2))
            .await
            .expect("protocol EOF should force the live process to exit");

        assert!(matches!(
            state,
            LspProcessState::Exited { forced: true, .. }
        ));
    }
}