Skip to content

Conversation

@mtojek
Copy link
Member

@mtojek mtojek commented Oct 30, 2025

Updates #17917

Implements prebuild invalidation by setting last_invalidated_at timestamp on presets.

The reconciler automatically marks prebuilds as expired if prebuild.created_at < preset.last_invalidated_at and spawns fresh ones.

API: POST /templates/{template}/prebuilds/invalidate

Response:

{"invalidated_presets": ["preset-1", "preset-2", "preset-3"]}

In the follow-up PR, I will expose a dedicated button somewhere in the UI.

Implements new approach for prebuild invalidation as discussed in issue #17917.
Instead of directly deleting workspaces, this sets a timestamp on presets,
and the reconciler automatically marks prebuilds as expired and creates new ones.

Implementation:
- Add last_invalidated_at column to template_version_presets table
- API endpoint: POST /templates/{template}/prebuilds/invalidate
- Updates last_invalidated_at = NOW() for all active template version presets
- filterExpiredWorkspaces checks if prebuild.created_at < preset.last_invalidated_at
- Reconciler automatically deletes expired prebuilds and spawns fresh ones

Changes:
- Migration 000394: Add last_invalidated_at column
- Query: UpdatePresetsLastInvalidatedAt sets timestamp on active presets
- API: Returns count and list of invalidated preset names
- Logic: filterExpiredWorkspaces prioritizes last_invalidated_at over TTL
- SDK: InvalidateTemplatePrebuilds client method with structured response

Benefits:
- Non-blocking: API doesn't wait for reconciler
- Async: Reconciler handles actual deletion
- Safe: template_version_presets is no longer write-once

Updates #17917
@mtojek mtojek force-pushed the 17917-prebuilds-invalidation branch from 04ba875 to 6019903 Compare October 30, 2025 12:26
- Use uuid.NullUUID{UUID: template.ID, Valid: true}
- Use sql.NullTime{Time: dbtime.Now(), Valid: true}
@mtojek mtojek force-pushed the 17917-prebuilds-invalidation branch from 96249b9 to 90b75e1 Compare October 30, 2025 12:55
@mtojek mtojek marked this pull request as ready for review November 18, 2025 15:34
@mtojek
Copy link
Member Author

mtojek commented Nov 18, 2025

FYI Chromatic is failing now, but this seems to be unrelated. I didn't touch the frontend side.

Copy link
Contributor

@ssncferreira ssncferreira left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just some small improvements.
It might be worth documenting that this isn't a real-time operation, the endpoint marks presets as invalidated, but actual prebuild deletion happens asynchronously during the next reconciliation loop. It might also be a good idea to add this info in the UI changes.

mtojek and others added 3 commits November 19, 2025 12:32
Co-authored-by: Susana Ferreira <susana@coder.com>
Co-authored-by: Susana Ferreira <susana@coder.com>
Co-authored-by: Susana Ferreira <susana@coder.com>
@mtojek mtojek marked this pull request as draft November 19, 2025 11:34
Copy link
Member Author

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

It might be worth documenting that this isn't a real-time operation, the endpoint marks presets as invalidated, but actual prebuild deletion happens asynchronously during the next reconciliation loop. It might also be a good idea to add this info in the UI changes.

I want to add this message to docs and possibly in the UI in the follow-up. This PR contains mainly backend changes 👍

@mtojek mtojek requested a review from ssncferreira November 19, 2025 13:57
@mtojek mtojek marked this pull request as ready for review November 19, 2025 13:58
Copy link
Contributor

@ssncferreira ssncferreira left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:
Thanks for addressing the comments 🥇

@@ -0,0 +1 @@
ALTER TABLE template_version_presets ADD COLUMN last_invalidated_at TIMESTAMPTZ;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a reminder to check migration number before merge! 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

Checked 👍 Last in mainline 398.

Thanks for the diligence!

@mtojek mtojek merged commit d004710 into main Nov 20, 2025
33 of 35 checks passed
@mtojek mtojek deleted the 17917-prebuilds-invalidation branch November 20, 2025 16:12
@github-actions github-actions bot locked and limited conversation to collaborators Nov 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants