Skip to main content

SuggestService

Struct SuggestService 

Source
pub struct SuggestService { /* private fields */ }
Expand description

Thread-safe service for accessing suggestions

Provides read-only queries for LLM and UI consumers. Write operations go through SuggestEngine (not this service).

Implementations§

Source§

impl SuggestService

Source

pub fn new(registry: SuggestRegistry) -> Self

Create a new service with the given registry

Source

pub fn with_strategy( registry: SuggestRegistry, strategy: SuggestStrategy, ) -> Self

Create a service with custom strategy

Source

pub fn with_gc_config(registry: SuggestRegistry, gc_config: GcConfig) -> Self

Create a service with custom GC configuration

Source

pub fn get( &self, id: SuggestId, ) -> Option<MappedRwLockReadGuard<'_, StoredSuggestion>>

Get a suggestion by ID

Returns a mapped guard that holds the read lock while providing access to the stored suggestion.

Source

pub fn is_valid(&self, id: SuggestId) -> bool

Check if a suggestion ID is still valid

Source

pub fn query( &self, query: &SuggestQuery, ) -> Vec<(SuggestId, SuggestCategory, SafetyLevel)>

Query suggestions with filter

Source

pub fn auto_applicable(&self) -> Vec<SuggestId>

Get all suggestions for auto-application (safety=Auto)

Source

pub fn by_category(&self, category: SuggestCategory) -> Vec<SuggestId>

Get suggestions by category

Source

pub fn count(&self) -> usize

Get suggestion count

Source

pub fn is_empty(&self) -> bool

Check if there are any suggestions

Source

pub fn pattern_name(&self, id: SuggestId) -> Option<&'static str>

Get pattern names for a suggestion

Source

pub fn rule_id(&self, id: SuggestId) -> Option<&str>

Get rule ID for a suggestion (e.g., “RL021”). Returns None for non-pattern-based suggestions.

Source

pub fn registry(&self) -> &SuggestRegistry

Get registry access (for pattern lookup)

Source

pub fn to_mutation_specs( &self, id: SuggestId, ctx: &AnalysisContext, ) -> Option<Vec<MutationSpec>>

Generate MutationSpecs for a suggestion

Takes AnalysisContext to resolve types and generate accurate specs.

Source

pub fn generate_with_params( &self, ctx: &AnalysisContext, rule_id: &str, params: &SuggestParams, ) -> Option<Vec<SuggestOpportunity>>

Generate suggestions with external parameters.

This method enables code generation from patterns with user-provided parameters. For example, generating OrderAPI from an API pattern with { "name": "Order" }.

§Arguments
  • ctx - Analysis context for code graph queries
  • rule_id - Rule ID of the parameterized suggestion (e.g., “api-pattern”)
  • params - User-provided parameters (e.g., { "name": "Order" })
§Returns
  • Some(opportunities) - Generated opportunities if rule exists and accepts params
  • None - If rule not found or doesn’t accept params
§Example
let params = [("name".to_string(), "Order".to_string())].into_iter().collect();
let opps = service.generate_with_params(&ctx, "api-pattern", &params);
Source

pub fn generate_and_store( &self, ctx: &AnalysisContext, rule_id: &str, params: &SuggestParams, ) -> usize

Generate suggestions and store them.

Like generate_with_params, but also stores the generated opportunities in the service for later retrieval and application.

Returns the number of suggestions stored.

Source

pub fn list_parameterized(&self) -> Vec<ParameterizedSuggestInfo>

List all parameterized suggestions with their schemas.

Returns suggestions that accept external parameters, along with their parameter schemas. Useful for LLMs to discover available generation patterns.

Source

pub fn record_changes(&self, trigger: SuggestTrigger) -> bool

Record changes and check if evaluation should trigger

Source

pub fn take_pending(&self) -> (usize, AcChanges)

Take pending changes (for evaluation)

Source

pub fn insert(&self, suggestion: StoredSuggestion) -> Option<SuggestId>

Insert a new suggestion (write operation).

Returns None if an active suggestion with the same identity (pattern + opportunity_id) already exists.

Source

pub fn close(&self, id: SuggestId, reason: impl Into<String>) -> bool

Close a suggestion (write operation)

Source

pub fn invalidate_for_symbol(&self, symbol: &SymbolId)

Invalidate suggestions for a modified symbol

Source

pub fn remove_for_symbol(&self, symbol: &SymbolId)

Remove suggestions for a deleted symbol

Source

pub fn gc(&self, valid_symbols: impl Fn(&SymbolId) -> bool)

Run garbage collection

Source

pub fn clear(&self)

Clear all suggestions

Source

pub fn detect(&self, ctx: &AnalysisContext, symbols: &[SymbolId]) -> usize

Detect suggestions for given symbols using registered patterns.

This is the main entry point for suggestion detection. Automatically filters out suggestions for symbols with @spec:allow(...) directives.

Returns the number of new suggestions found.

Source

pub fn detect_with_allow( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, ) -> usize

Detect suggestions with custom AllowStore.

Use this when you want to reuse an AllowStore across multiple detect calls or when you have a pre-built AllowStore.

Source

pub fn detect_with_rule_filter<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], is_rule_enabled: F, ) -> usize
where F: Fn(&str, &str) -> bool,

Detect suggestions with rule filter.

The is_rule_enabled closure takes (rule_id, file_path) to check if a rule should be processed. Rules returning false are skipped. This enables project and module-level rule configuration.

Source

pub fn detect_with_allow_and_rule_filter<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, is_rule_enabled: F, ) -> usize
where F: Fn(&str, &str) -> bool,

Detect suggestions with custom AllowStore and rule filter.

Combines symbol-level allow directives with project/module-level rule filtering. The is_rule_enabled closure takes (rule_id, file_path) to support module configs.

Source

pub fn detect_with_config<F, S>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, is_rule_enabled: F, severity_override: S, ) -> usize
where F: Fn(&str, &str) -> bool, S: Fn(&str) -> Option<LintSeverity>,

Detect suggestions with full configuration support.

This is the most complete variant that supports:

  • Custom AllowStore for symbol-level @spec:allow directives
  • Rule filter closure for project-level rule configuration
  • Severity override closure for per-rule severity changes

The severity_override closure takes a rule_id and returns an optional new severity. If None, the default severity is used.

The is_rule_enabled closure takes (rule_id, file_path) to support module-level rule configuration.

Source

pub fn detect_with_config_and_scope<F, S>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, is_rule_enabled: F, severity_override: S, scope_filter: &[SymbolScope], ) -> usize
where F: Fn(&str, &str) -> bool, S: Fn(&str) -> Option<LintSeverity>,

Detect suggestions with full configuration and scope filtering.

Extends detect_with_config with scope-based filtering. Each opportunity is tagged with its SymbolScope (Lib/Bin/Test) based on symbol context. If scope_filter is non-empty, only opportunities matching one of the specified scopes are stored.

Source

pub fn detect_patterns( &self, ctx: &AnalysisContext, symbols: &[SymbolId], pattern_names: &[&str], ) -> usize

Detect suggestions for specific patterns only.

Automatically filters out suggestions for symbols with @spec:allow(...) directives.

Source

pub fn detect_patterns_with_allow( &self, ctx: &AnalysisContext, symbols: &[SymbolId], pattern_names: &[&str], allow_store: &AllowStore, ) -> usize

Detect suggestions for specific patterns with custom AllowStore.

Source

pub fn detect_with_precheck<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with pre-check filter.

The precheck callback is called for each opportunity with:

  • The opportunity itself
  • The generated MutationSpecs

Only opportunities where precheck returns true are stored. This enables scan-time verification to ensure Apply will succeed.

§Example
let count = service.detect_with_precheck(ctx, symbols, |opp, specs, ctx| {
    // Run GraphVerifier on specs
    verify_specs(specs, ctx)
});
Source

pub fn detect_with_precheck_and_allow<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with pre-check filter and custom AllowStore.

Source

pub fn detect_with_parallel_precheck<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with parallel pre-check execution.

This method parallelizes the expensive precheck phase using rayon:

  1. Phase 1 (sequential): Detect all opportunities and generate specs
  2. Phase 2 (parallel): Run precheck on each candidate
  3. Phase 3 (sequential): Insert passed suggestions

This significantly reduces wall-clock time when precheck involves expensive operations like fork_clone() (~100ms each).

§Example
let result = service.detect_with_parallel_precheck(ctx, symbols, |opp, specs, ctx| {
    // This closure runs in parallel - must be Sync
    let mut forked = ctx.fork_clone();
    executor.execute_v2(&blueprint, &mut forked).success
});
Source

pub fn detect_with_parallel_precheck_limited<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], limit: Option<usize>, precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with parallel pre-check and optional limit.

When limit is Some(N), only the top N candidates by priority are checked. Candidates are sorted by priority (confidence * safety_weight) before precheck.

Source

pub fn detect_with_parallel_precheck_and_allow<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with parallel pre-check, custom AllowStore, and optional limit.

Source

pub fn detect_with_parallel_precheck_and_allow_limited<F>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, limit: Option<usize>, precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with parallel pre-check, custom AllowStore, and optional limit.

Source

pub fn detect_with_parallel_precheck_and_rule_filter<F, R>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], limit: Option<usize>, is_rule_enabled: R, precheck: F, ) -> DetectWithPrecheckResult

Detect suggestions with parallel pre-check, rule filter, and optional limit.

This is the most complete variant that supports:

  • Custom AllowStore for symbol-level @spec:allow directives
  • Rule filter closure for project-level rule configuration
  • Optional limit on candidates to check
  • Parallel precheck execution
Source

pub fn detect_with_parallel_precheck_full<F, R>( &self, ctx: &AnalysisContext, symbols: &[SymbolId], allow_store: &AllowStore, limit: Option<usize>, is_rule_enabled: R, precheck: F, ) -> DetectWithPrecheckResult

Full-featured parallel precheck detection with all options.

This is the internal implementation that other parallel precheck methods delegate to.

Source§

impl SuggestService

Source

pub fn stats(&self) -> SuggestStats

Get statistics about current state

Source

pub fn take_store(&self) -> SuggestStore

Take the store contents, leaving an empty store.

Used for preserving suggestions across API reload.

Source

pub fn restore_store(&self, store: SuggestStore)

Restore store contents from a previous backup.

Used for preserving suggestions across API reload.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more