Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refresh AI cache on proposal update #254

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

wa0x6e
Copy link
Contributor

@wa0x6e wa0x6e commented Apr 2, 2024

🧿 Current issues / What's wrong ?

AI summary and speech are cached for all proposals state (pending, active, closed).

Pending proposals can still be updated, and the AI cache is not refreshed after creation.

Closes #242

💊 Fixes / Solution

Refresh the AI cache on proposal update

🚧 Changes

  • On proposal start, force a cache refresh if it exist
  • When fetching cache for pending proposals, do not serve the existing cache if the proposal has been update meanwhile
  • Refactor queue.ts to handle other class than just VoteReport

🛠️ Tests

Test instructions are only for ai/TextToSpeech, and we're assuming that it will work for ai/Summary since it's getting the exact same changes.

Testing cache refresh from webhook

Goal:

  • Prove that on proposal start, all existing AI cache will be refreshed
  • Proposals without cache should not generate a new cache

Add/Set the following var in .env:

HUB_URL=https://hub.snapshot.org
WEBHOOK_AUTH_TOKEN=0
STORAGE_ENGINE=file
QUEUE_INTERVAL=3000
  • Run yarn dev
  • Run curl -X POST http://localhost:3005/api/ai/tts/0xcae036faf24a47a692748911c7d2701a7992d03585150ce63fc777bc590eb6ac
  • You should see in yarn dev console the cache creation (storage:file] File saved to). You can confirm the creation by checking that the file tmp/ai-tts-test/snapshot-proposal-ai-tts-0xcae036faf24a47a692748911c7d2701a7992d03585150ce63fc777bc590eb6ac.mp3 exist (from your project root). Give a little time for the item to appear in the console, as the queue is run every 3s.
  • Run the curl command again
  • the yarn dev console should confirm that the cache is served ([storage:file] File fetched from).
  • Run curl --header "Content-Type: application/json" --header "Authentication: 0" --request POST --data '{"id":"proposal/0xcae036faf24a47a692748911c7d2701a7992d03585150ce63fc777bc590eb6ac","event":"proposal/start"}' http://localhost:3005/webhook
  • In yarn dev console, you should see the cache creation ([storage:file] File saved), even though the cache already exist. This is because we refresh all existing cache on proposal start, to make sure.
  • Delete the cached file (tmp/ai-tts-test/snapshot-proposal-ai-tts-0xcae036faf24a47a692748911c7d2701a7992d03585150ce63fc777bc590eb6ac.mp3)
  • Run the webhook curl command again
  • It should not create a cache (we create a cache only if it was already previously cached)

Testing cache refresh for pending proposals

Goal:

  • Prove that when a proposal has been updated, it should not serve the cached file, and generate a new one
HUB_URL=https://hub.snapshot.org
WEBHOOK_AUTH_TOKEN=0
STORAGE_ENGINE=file
QUEUE_INTERVAL=3000
  • Create a very simple pending proposal on your test space, with a short body
  • Run yarn dev
  • Run curl -X POST http://localhost:3005/api/ai/tts/[YOUR_PROPOSAL_ID]
  • You should see the cache creation from the yarn dev console, and that the file exist in tmp/ai-tts-test/
  • Running the curl command again should serve the cache
  • Edit your proposal's body by something else
  • Run the curl command
  • It should create a new cache (listen to the mp3, it should match the updated body)

@wa0x6e wa0x6e changed the title Feat-refresh-ai-cache-on-proposal-update feat: refresh AI cache on proposal update Apr 2, 2024
Copy link

codecov bot commented Apr 2, 2024

Codecov Report

Attention: Patch coverage is 33.82353% with 45 lines in your changes are missing coverage. Please review.

Files Patch % Lines
src/lib/queue.ts 5.88% 16 Missing ⚠️
src/lib/ai/textToSpeech.ts 0.00% 15 Missing ⚠️
src/webhook.ts 0.00% 10 Missing ⚠️
src/api.ts 0.00% 2 Missing ⚠️
src/lib/cache.ts 60.00% 2 Missing ⚠️
Additional details and impacted files

📢 Thoughts on this report? Let us know!

@wa0x6e wa0x6e marked this pull request as ready for review April 2, 2024 14:02
@wa0x6e wa0x6e requested a review from ChaituVR April 2, 2024 14:03
@wa0x6e wa0x6e self-assigned this Apr 2, 2024
throw new Error('RECORD_NOT_FOUND');
}
if (!this.proposal) throw new Error('RECORD_NOT_FOUND');
if (this.#cacheExpired()) return false;
Copy link
Member

Choose a reason for hiding this comment

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

If cache is expired, isCacheable should return true right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, false, since the item is not cache-able anymore

Comment on lines +63 to +68
return tempCacheIds.has(id) && tempCacheIds.get(id) !== updated;
};

afterCreateCache() {
tempCacheIds.set(this.proposal!.id, this.proposal!.updated);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can't relay on updated param for cache, a pending proposal can be updated any number of times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The update value will be cached only after cache creation. So each time it changes, it will invalidate the cache

Copy link
Member

Choose a reason for hiding this comment

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

oh updated is timestamp. got it

@bonustrack
Copy link
Member

Can you confirm it wouldnt cache AI summary if proposal is edited but no-one ask for AI summary?

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.

fix: refresh AI Summary cache on proposal update
3 participants