Skip to content

fix(#2419): add content-aware cache to MysqlSkillRepository and PostgresSkillRepository for hot-reload support [FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH] - #2429

Open
waterWang wants to merge 1 commit into
agentscope-ai:mainfrom
waterWang:fix-2419-hot-reload-cache
Open

fix(#2419): add content-aware cache to MysqlSkillRepository and PostgresSkillRepository for hot-reload support [FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH]#2429
waterWang wants to merge 1 commit into
agentscope-ai:mainfrom
waterWang:fix-2419-hot-reload-cache

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Adds a lightweight cache to both DB-backed skill repositories, mirroring the mtime-based cache pattern already used by FileSystemSkillRepository.

Changes

  • MysqlSkillRepository: queries MAX(updated_at) first; returns cached list when the timestamp is unchanged. Cache is cleared on save() and delete().
  • PostgresSkillRepository: uses an in-memory version counter incremented on save() and delete(). Cached list is returned when the version matches.

Problem

Previously, DynamicSkillMiddleware's content-keyed short-circuit could only detect skill changes for FileSystemSkillRepository (which uses mtime-based cache invalidation). DB-backed repositories always returned fresh data from the database, but the short-circuit's signature never changed because the content was identical — preventing the SkillBox from being rebuilt after external updates.

Fix

With this fix, external DB updates are detected via the timestamp/version check, and the DynamicSkillMiddleware correctly rebuilds the SkillBox on the next system-prompt pass.

Fixes #2419

…esSkillRepository for hot-reload support

Adds a lightweight cache to both DB-backed skill repositories, mirroring the
mtime-based cache pattern already used by FileSystemSkillRepository:

- MysqlSkillRepository: queries MAX(updated_at) first; returns cached list
  when the timestamp is unchanged. Cache is cleared on save() and delete().
- PostgresSkillRepository: uses an in-memory version counter incremented on
  save() and delete(). Cached list is returned when the version matches.

Previously, DynamicSkillMiddleware's content-keyed short-circuit could only
detect skill changes for FileSystemSkillRepository (which uses mtime-based
cache invalidation). DB-backed repositories always returned fresh data from
the database, but the short-circuit's signature never changed because the
content was identical — preventing the SkillBox from being rebuilt after
external updates.

With this fix, external DB updates are detected via the timestamp/version
check, and the DynamicSkillMiddleware correctly rebuilds the SkillBox on
the next system-prompt pass.

Fixes agentscope-ai#2419
[FaaFyfxR9WAQrL7FcAgEHJvztd8cVMxvjHRS55rw1nwH]
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


waterWang seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

CLA Not Signed

The Contributor License Agreement (CLA) check is currently pending on this PR (license/cla: Contributor License Agreement is not signed yet.). This PR cannot be merged until the CLA is signed.

@waterWang please sign the CLA via the CLA assistant badge in the comment above, or visit https://cla-assistant.io/agentscope-ai/agentscope-java. Once signed, the license/cla status will turn green.


Automated check by github-manager-bot

@oss-maintainer

Copy link
Copy Markdown
Collaborator

CI Failure — Spotless formatting + CLA

Two issues blocking CI:

  1. Spotless: PostgresSkillRepository.java line 173 — missing blank line between cachedSkills and cacheVersion fields. Also MysqlSkillRepository.java has similar formatting violations.
  2. CLA: Contributor License Agreement is not signed yet.

Fixes:

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI Failures

Two issues:

1. Spotless Formatting (build failure)

Missing blank line between cachedSkills field declaration and the @FunctionalInterface annotation. Run:

mvn spotless:apply

2. CLA Not Signed (pending check)

The license/cla check is pending — the Contributor License Agreement must be signed before this PR can be merged. Please visit the CLA link and sign it.


Code Review

The content-aware cache pattern (mirroring FileSystemSkillRepository's mtime-based cache) is a good approach for hot-reload support in DB-backed skill repositories. A few observations:

  1. Thread safety: cachedSkills and cachedMaxUpdatedAt are declared volatile, which ensures visibility but not atomicity of the pair. In a race where one thread reads cachedMaxUpdatedAt and another updates both fields, you could see a stale cachedSkills with a new cachedMaxUpdatedAt. Consider using a single immutable holder object (e.g., a record CacheSnapshot(List<AgentSkill> skills, long maxUpdatedAt)) assigned atomically via a single volatile reference.

  2. Cache invalidation on external changes: The cache is cleared by save() and delete(), but if the DB is modified outside this repository instance (e.g., another process), the cache won't be invalidated. The queryMaxUpdatedAt() check handles this well — good design.

  3. PostgreSQL parity: The PR title mentions both MySQL and PostgreSQL, but the diff only shows MySQL changes. Please ensure the PostgreSQL repository gets the same treatment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:skill热加载问题

3 participants