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
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
//! Local manifest-backed Code Intelligence provider.

use std::{
    convert::Infallible,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc, Mutex as StdMutex, Weak,
    },
    time::Duration,
};

use async_trait::async_trait;
use tokio::sync::{broadcast, watch, RwLock};
use tokio_util::sync::CancellationToken;

use super::{
    project_layout::ProjectLayoutResolver,
    registry::{
        LocalCodeIntelligenceRegistry, RegistryAcquireError, RegistryConfig, RegistryKey,
        RegistryKeyError, RegistryReport, RegistryShutdownError, RegistryShutdownFailure,
        RuntimeLease,
    },
    workspace_runtime::WorkspaceRuntime,
    CodeDiagnostic, CodeIntelligenceError, CodeIntelligenceResult, CodeIntelligenceState,
    CodeIntelligenceStatus, CodeLocation, CodePosition, CodeQueryResult, DocumentSymbol,
    NavigationKind, SymbolInformation, WorkspaceCodeIntelligence,
};
use crate::workspace::{
    LocalWorkspaceManifest, LocalWorkspaceManifestSnapshot, WorkspaceFileChange,
    WorkspaceFileSystem, WorkspacePath,
};

const DEFAULT_QUERY_TIMEOUT: Duration = Duration::from_secs(15);
type RuntimeRegistry = LocalCodeIntelligenceRegistry<WorkspaceRuntime, Infallible>;
type WorkspaceRuntimeLease = RuntimeLease<WorkspaceRuntime, Infallible>;

/// Native local provider sharing the workspace manifest's existing watcher.
pub struct LocalCodeIntelligence {
    isolation_scope: String,
    manifest: Arc<LocalWorkspaceManifest>,
    file_system: Arc<dyn WorkspaceFileSystem>,
    registry: RuntimeRegistry,
    current: RwLock<Option<WorkspaceRuntimeLease>>,
    status: watch::Sender<CodeIntelligenceStatus>,
    generation: Arc<AtomicU64>,
    lifetime: CancellationToken,
    manifest_task: StdMutex<Option<tokio::task::JoinHandle<()>>>,
    query_timeout: Duration,
}

impl std::fmt::Debug for LocalCodeIntelligence {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LocalCodeIntelligence")
            .field("isolation_scope", &self.isolation_scope)
            .field("manifest_root", &self.manifest.snapshot().root)
            .field("generation", &self.generation.load(Ordering::Acquire))
            .finish_non_exhaustive()
    }
}

impl LocalCodeIntelligence {
    /// Create a provider and acquire a cheap, lazily-started runtime generation.
    pub async fn start(
        isolation_scope: impl Into<String>,
        manifest: Arc<LocalWorkspaceManifest>,
        file_system: Arc<dyn WorkspaceFileSystem>,
    ) -> CodeIntelligenceResult<Arc<Self>> {
        Self::start_with_timeout(
            isolation_scope,
            manifest,
            file_system,
            DEFAULT_QUERY_TIMEOUT,
        )
        .await
    }

    pub(crate) async fn start_with_timeout(
        isolation_scope: impl Into<String>,
        manifest: Arc<LocalWorkspaceManifest>,
        file_system: Arc<dyn WorkspaceFileSystem>,
        query_timeout: Duration,
    ) -> CodeIntelligenceResult<Arc<Self>> {
        let isolation_scope = isolation_scope.into();
        let snapshot_rx = manifest.subscribe();
        let changes_rx = manifest.subscribe_changes();
        let registry = RuntimeRegistry::new(
            RegistryConfig::new(Duration::ZERO, 0),
            |runtime: Arc<WorkspaceRuntime>| async move {
                runtime.shutdown().await;
                Ok(())
            },
        );
        let (status, _) = watch::channel(CodeIntelligenceStatus {
            state: CodeIntelligenceState::Starting,
            message: Some("Code Intelligence is preparing the saved workspace".to_owned()),
            ..CodeIntelligenceStatus::default()
        });
        let provider = Arc::new(Self {
            isolation_scope,
            manifest,
            file_system,
            registry,
            current: RwLock::new(None),
            status,
            generation: Arc::new(AtomicU64::new(0)),
            lifetime: CancellationToken::new(),
            manifest_task: StdMutex::new(None),
            query_timeout,
        });

        provider
            .refresh_snapshot(provider.manifest.snapshot())
            .await?;
        let weak = Arc::downgrade(&provider);
        let task = tokio::spawn(run_manifest_updates(
            weak,
            snapshot_rx,
            changes_rx,
            provider.lifetime.clone(),
        ));
        *provider
            .manifest_task
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(task);
        Ok(provider)
    }

    /// Stop manifest forwarding and all language processes owned by this provider.
    pub async fn shutdown(&self) {
        self.lifetime.cancel();
        let task = self
            .manifest_task
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take();
        if let Some(task) = task {
            let _ = task.await;
        }
        let old = self.current.write().await.take();
        drop(old);
        report_registry_cleanup("shutdown", self.registry.shutdown_all().await);
        self.status.send_replace(CodeIntelligenceStatus {
            state: CodeIntelligenceState::Unavailable,
            message: Some("Code Intelligence is shut down".to_owned()),
            ..CodeIntelligenceStatus::default()
        });
    }

    async fn refresh_snapshot(
        self: &Arc<Self>,
        snapshot: LocalWorkspaceManifestSnapshot,
    ) -> CodeIntelligenceResult<()> {
        let layout = ProjectLayoutResolver::resolve(&snapshot);
        {
            let current = self.current.read().await;
            if let Some(runtime) = current
                .as_ref()
                .filter(|runtime| runtime.layout_hash() == layout.layout_hash)
            {
                runtime.update_snapshot(&snapshot).await;
                return Ok(());
            }
        }

        let key = RegistryKey::new(
            self.isolation_scope.clone(),
            &snapshot.root,
            layout.layout_hash,
        )
        .await
        .map_err(map_key_error)?;
        let canonical_root = key.canonical_root().to_path_buf();
        let runtime_snapshot = snapshot.clone();
        let file_system = Arc::clone(&self.file_system);
        let timeout = self.query_timeout;
        let lease = self
            .registry
            .acquire(key, move |_| async move {
                Ok(WorkspaceRuntime::new(
                    canonical_root,
                    layout,
                    &runtime_snapshot,
                    file_system,
                    timeout,
                ))
            })
            .await
            .map_err(map_acquire_error)?;

        lease.update_snapshot(&snapshot).await;
        let mut receiver = lease.subscribe_status();
        let generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
        self.status.send_replace(receiver.borrow().clone());
        let old = self.current.write().await.replace(lease);
        drop(old);
        report_registry_cleanup("layout refresh", self.registry.cleanup_idle().await);
        self.spawn_status_forwarder(generation, &mut receiver);
        Ok(())
    }

    fn spawn_status_forwarder(
        &self,
        generation: u64,
        receiver: &mut watch::Receiver<CodeIntelligenceStatus>,
    ) {
        let mut receiver = receiver.clone();
        let sender = self.status.clone();
        let current_generation = Arc::clone(&self.generation);
        let lifetime = self.lifetime.clone();
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    _ = lifetime.cancelled() => break,
                    changed = receiver.changed() => {
                        if changed.is_err()
                            || current_generation.load(Ordering::Acquire) != generation
                        {
                            break;
                        }
                        sender.send_replace(receiver.borrow().clone());
                    }
                }
            }
        });
    }

    async fn handle_changes(&self, changes: &[WorkspaceFileChange]) {
        let current = self.current.read().await;
        if let Some(runtime) = current.as_ref() {
            runtime.notify_file_changes(changes).await;
        }
    }

    async fn runtime(
        &self,
    ) -> CodeIntelligenceResult<tokio::sync::RwLockReadGuard<'_, Option<WorkspaceRuntimeLease>>>
    {
        let current = self.current.read().await;
        if current.is_none() {
            return Err(CodeIntelligenceError::Unavailable {
                message: "Code Intelligence has not prepared this workspace yet".to_owned(),
            });
        }
        Ok(current)
    }

    fn report_update_error(&self, error: &CodeIntelligenceError) {
        let mut status = self.status.borrow().clone();
        status.state = CodeIntelligenceState::Degraded;
        status.message = Some(format!("workspace refresh failed: {error}"));
        self.status.send_replace(status);
    }
}

#[async_trait]
impl WorkspaceCodeIntelligence for LocalCodeIntelligence {
    fn subscribe_status(&self) -> watch::Receiver<CodeIntelligenceStatus> {
        self.status.subscribe()
    }

    async fn document_symbols(
        &self,
        path: &WorkspacePath,
        cancellation: CancellationToken,
    ) -> CodeIntelligenceResult<CodeQueryResult<DocumentSymbol>> {
        let runtime = self.runtime().await?;
        let Some(runtime) = runtime.as_ref() else {
            return Err(runtime_unavailable());
        };
        runtime.document_symbols(path, cancellation).await
    }

    async fn search_symbols(
        &self,
        query: &str,
        limit: usize,
        cancellation: CancellationToken,
    ) -> CodeIntelligenceResult<CodeQueryResult<SymbolInformation>> {
        let runtime = self.runtime().await?;
        let Some(runtime) = runtime.as_ref() else {
            return Err(runtime_unavailable());
        };
        runtime.search_symbols(query, limit, cancellation).await
    }

    async fn navigate(
        &self,
        kind: NavigationKind,
        path: &WorkspacePath,
        position: CodePosition,
        cancellation: CancellationToken,
    ) -> CodeIntelligenceResult<CodeQueryResult<CodeLocation>> {
        let runtime = self.runtime().await?;
        let Some(runtime) = runtime.as_ref() else {
            return Err(runtime_unavailable());
        };
        runtime.navigate(kind, path, position, cancellation).await
    }

    async fn diagnostics(
        &self,
        path: Option<&WorkspacePath>,
        cancellation: CancellationToken,
    ) -> CodeIntelligenceResult<CodeQueryResult<CodeDiagnostic>> {
        let runtime = self.runtime().await?;
        let Some(runtime) = runtime.as_ref() else {
            return Err(runtime_unavailable());
        };
        runtime.diagnostics(path, cancellation).await
    }
}

impl Drop for LocalCodeIntelligence {
    fn drop(&mut self) {
        self.lifetime.cancel();
        if let Some(task) = self
            .manifest_task
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take()
        {
            task.abort();
        }
    }
}

async fn run_manifest_updates(
    provider: Weak<LocalCodeIntelligence>,
    mut snapshots: broadcast::Receiver<LocalWorkspaceManifestSnapshot>,
    mut changes: broadcast::Receiver<WorkspaceFileChange>,
    lifetime: CancellationToken,
) {
    loop {
        tokio::select! {
            _ = lifetime.cancelled() => break,
            update = snapshots.recv() => match update {
                Ok(snapshot) => {
                    let Some(provider) = provider.upgrade() else { break; };
                    if let Err(error) = provider.refresh_snapshot(snapshot).await {
                        provider.report_update_error(&error);
                    }
                }
                Err(broadcast::error::RecvError::Lagged(_)) => {
                    let Some(provider) = provider.upgrade() else { break; };
                    if let Err(error) = provider.refresh_snapshot(provider.manifest.snapshot()).await {
                        provider.report_update_error(&error);
                    }
                }
                Err(broadcast::error::RecvError::Closed) => break,
            },
            update = changes.recv() => match update {
                Ok(change) => {
                    let mut batch = vec![change];
                    while let Ok(change) = changes.try_recv() {
                        batch.push(change);
                    }
                    let Some(provider) = provider.upgrade() else { break; };
                    provider.handle_changes(&batch).await;
                }
                Err(broadcast::error::RecvError::Lagged(count)) => {
                    let Some(provider) = provider.upgrade() else { break; };
                    let mut status = provider.status.borrow().clone();
                    status.state = CodeIntelligenceState::Degraded;
                    status.message = Some(format!(
                        "workspace change stream skipped {count} events; saved documents will resynchronize on query"
                    ));
                    provider.status.send_replace(status);
                }
                Err(broadcast::error::RecvError::Closed) => break,
            },
        }
    }
}

fn map_key_error(error: RegistryKeyError) -> CodeIntelligenceError {
    CodeIntelligenceError::Unavailable {
        message: error.to_string(),
    }
}

fn map_acquire_error(error: RegistryAcquireError<Infallible>) -> CodeIntelligenceError {
    match error {
        RegistryAcquireError::ShuttingDown => CodeIntelligenceError::Unavailable {
            message: "the Code Intelligence registry is shutting down".to_owned(),
        },
        RegistryAcquireError::Factory(error) => match *error {},
        RegistryAcquireError::FactoryPanicked { message } => CodeIntelligenceError::Unavailable {
            message: format!("Code Intelligence runtime initialization panicked: {message}"),
        },
        RegistryAcquireError::LeaseLimit => CodeIntelligenceError::Unavailable {
            message: "the Code Intelligence runtime lease limit was exhausted".to_owned(),
        },
    }
}

fn runtime_unavailable() -> CodeIntelligenceError {
    CodeIntelligenceError::Unavailable {
        message: "Code Intelligence has not prepared this workspace yet".to_owned(),
    }
}

fn report_registry_cleanup(context: &'static str, report: RegistryReport<Infallible>) {
    if !report.removed.is_empty() {
        tracing::debug!(
            context,
            retired = report.removed.len(),
            "Code Intelligence retired workspace runtimes"
        );
    }
    for RegistryShutdownError { key, failure } in report.errors {
        match failure {
            RegistryShutdownFailure::Runtime(error) => {
                tracing::error!(
                    context,
                    workspace = ?key.canonical_root(),
                    ?error,
                    "Code Intelligence runtime cleanup returned an impossible error"
                );
            }
            RegistryShutdownFailure::Panicked { message } => {
                tracing::warn!(
                    context,
                    workspace = ?key.canonical_root(),
                    %message,
                    "Code Intelligence runtime cleanup panicked"
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::workspace::{
        LocalWorkspaceFile, LocalWorkspaceFileStatus, ManifestWorkspaceBackend,
    };

    fn file(path: &str) -> LocalWorkspaceFile {
        LocalWorkspaceFile {
            path: path.to_owned(),
            size: 1,
            modified_ms: Some(1),
            language: None,
            status: LocalWorkspaceFileStatus::Tracked,
            binary: false,
            generated: false,
        }
    }

    async fn scanned_snapshot(manifest: &LocalWorkspaceManifest) -> LocalWorkspaceManifestSnapshot {
        if manifest.snapshot().version > 0 {
            return manifest.snapshot();
        }
        let mut snapshots = manifest.subscribe();
        tokio::time::timeout(Duration::from_secs(5), async {
            loop {
                let snapshot = snapshots.recv().await.unwrap();
                if snapshot.version > 0 {
                    break snapshot;
                }
            }
        })
        .await
        .expect("initial manifest scan should finish")
    }

    #[tokio::test]
    async fn provider_reuses_unchanged_layout_and_shutdown_is_idempotent() {
        let workspace = tempfile::tempdir().unwrap();
        std::fs::create_dir(workspace.path().join("src")).unwrap();
        std::fs::write(workspace.path().join("Cargo.toml"), "[workspace]\n").unwrap();
        std::fs::write(workspace.path().join("src/lib.rs"), "pub fn saved() {}\n").unwrap();
        let backend = ManifestWorkspaceBackend::new(workspace.path());
        let manifest = backend.manifest();
        let initial = scanned_snapshot(&manifest).await;
        let file_system: Arc<dyn WorkspaceFileSystem> = backend;
        let provider = LocalCodeIntelligence::start_with_timeout(
            "test-session",
            manifest,
            file_system,
            Duration::from_secs(1),
        )
        .await
        .unwrap();
        let generation = provider.generation.load(Ordering::Acquire);
        assert_eq!(generation, 1);

        let mut source_only = initial.clone();
        source_only.version += 1;
        source_only.files.push(file("src/new.rs"));
        provider.refresh_snapshot(source_only).await.unwrap();
        assert_eq!(provider.generation.load(Ordering::Acquire), generation);

        let mut changed_layout = initial;
        changed_layout.version += 2;
        changed_layout
            .files
            .retain(|entry| entry.path != "Cargo.toml");
        changed_layout.files.push(file("package.json"));
        provider.refresh_snapshot(changed_layout).await.unwrap();
        assert_eq!(provider.generation.load(Ordering::Acquire), generation + 1);

        provider.shutdown().await;
        provider.shutdown().await;
        assert!(provider.current.read().await.is_none());
        assert!(provider.lifetime.is_cancelled());
        assert_eq!(
            provider.status.borrow().state,
            CodeIntelligenceState::Unavailable
        );
    }
}