Class AlwaysEvictStrategy

java.lang.Object
org.springframework.ai.tool.toolsearch.eviction.AlwaysEvictStrategy
All Implemented Interfaces:
ToolIndexEvictionStrategy

public final class AlwaysEvictStrategy extends Object implements ToolIndexEvictionStrategy
A ToolIndexEvictionStrategy that clears a session's tool index before every request, ensuring tools are always re-indexed on each conversation turn.

Use this strategy when the tool set may change between turns (e.g. dynamically generated tool descriptions) and you want to guarantee freshness at the cost of re-indexing on every request. For stable tool sets, NeverEvictStrategy is more efficient.

Contract note: onAccess(java.lang.String) intentionally returns the currently-accessed session ID, which diverges from the general recommendation in ToolIndexEvictionStrategy.onAccess(java.lang.String). This is safe because the advisor's doEvict + fingerprint-recheck sequence is idempotent: evicting the current session removes its cached fingerprint, so the subsequent fingerprint comparison always sees null and triggers a full re-index. When combined with other strategies in a CompositeEvictionStrategy, the always-evict behaviour is preserved (the more aggressive strategy wins).

Since:
2.0.0
Author:
Christian Tzolov
See Also:
  • Field Details

  • Method Details

    • onAccess

      public Set<String> onAccess(String sessionId)
      Returns a set containing sessionId, causing the advisor to clear the session's index and remove its cached fingerprint before the fingerprint-comparison step. The comparison then always sees a missing fingerprint and re-indexes the full tool set.
      Specified by:
      onAccess in interface ToolIndexEvictionStrategy
      Parameters:
      sessionId - the session being accessed
      Returns:
      a singleton set containing sessionId
    • onRemoved

      public void onRemoved(String sessionId)
      Description copied from interface: ToolIndexEvictionStrategy
      Called after a session's tool index has been cleared — either because this strategy returned it from ToolIndexEvictionStrategy.onAccess(java.lang.String), or because of an explicit eviction request.

      Implementations should remove any internal tracking state for the evicted session.

      Specified by:
      onRemoved in interface ToolIndexEvictionStrategy
      Parameters:
      sessionId - the session that was evicted