Skip to content

Fix tight install loop on continous automations feature - #46823

Merged
lucasmrod merged 4 commits into
mainfrom
continnuous-automations-fix-tight-loop
Jun 4, 2026
Merged

Fix tight install loop on continous automations feature#46823
lucasmrod merged 4 commits into
mainfrom
continnuous-automations-fix-tight-loop

Conversation

@lucasmrod

@lucasmrod lucasmrod commented Jun 4, 2026

Copy link
Copy Markdown
Member

Related issue: Resolves #45149 (adds to)

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

  • Bug Fixes

    • Throttle continuous policy automations to avoid repeated install attempts within the policy update interval.
  • New Features

    • Install records now include an updated timestamp for accurate cooldown decisions.
    • Added tracking of recently verified VPP app installs to avoid redundant re-installs.
  • Tests

    • New and updated unit and integration tests covering cooldown behavior and VPP verification lookups.

Copilot AI review requested due to automatic review settings June 4, 2026 15:30
@lucasmrod
lucasmrod requested a review from a team as a code owner June 4, 2026 15:30

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 70c93863-522a-49b6-9007-757aefa2f229

📥 Commits

Reviewing files that changed from the base of the PR and between bcb5e5a and 0f57287.

📒 Files selected for processing (1)
  • server/service/osquery_test.go

Walkthrough

This PR implements cooldown throttling for continuous policy automations to prevent tight install-refetch-rerun loops. It adds a datastore method to query recently verified VPP installs, exposes UpdatedAt on HostLastInstallData, integrates cooldown checks in software and VPP policy automation loops (newly failing policies bypass cooldown), and adds unit and integration tests plus mock wiring to validate the behavior.

Possibly related issues

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete; it lacks required sections like input validation, security checks, database migration checks, and detailed testing information. Complete the PR description by filling out all applicable checklist items and sections from the template, including security, testing, and database migration reviews.
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing a tight install loop issue in the continuous automations feature.
Linked Issues check ✅ Passed The PR implements cooldown throttling for continuous policy automations to prevent tight install loops, directly addressing issue #45149's objective of behavioral changes for policies with continuous retry mode.
Out of Scope Changes check ✅ Passed All changes are within scope: new datastore methods for tracking recent VPP installs, cooldown helpers, and throttling logic for continuous automations directly support the linked issue requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch continnuous-automations-fix-tight-loop

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
server/service/osquery_test.go (1)

4802-4809: ⚡ Quick win

Make cooldown interval explicit in test setup.

Both tests currently rely on config.TestConfig() defaulting the policy update interval to 1 hour. Setting the interval explicitly in these test setups will make the assertions resilient to future config-default changes.

Also applies to: 4822-4823

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/service/osquery_test.go` around lines 4802 - 4809, The test depends on
config.TestConfig() defaulting the policy update interval to 1 hour; make that
explicit by creating a local config, set its PolicyUpdateInterval (e.g.
cfg.PolicyUpdateInterval = 1*time.Hour), and pass that cfg into
newTestServiceWithConfig (used with TestServerOpts/mockClock) before calling
svcImpl.continuousAutomationOnCooldown; apply the same change for the second
test block as well so the assertions no longer rely on TestConfig() defaults.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@server/datastore/mysql/vpp.go`:
- Around line 1289-1292: The SQL predicate is using created_at for the cooldown
window causing recently verified but long-running installs to be excluded;
update the query in the host_vpp_software_installs lookup (the SELECT
DISTINCT(adam_id) ... WHERE host_id = ? AND canceled = 0 AND verification_at IS
NOT NULL ...) to compare verification_at against NOW() - INTERVAL ? SECOND
(using the existing hostID and seconds parameters) instead of created_at,
keeping the DISTINCT(adam_id) and existing error handling (err != nil && err !=
sql.ErrNoRows) intact.

---

Nitpick comments:
In `@server/service/osquery_test.go`:
- Around line 4802-4809: The test depends on config.TestConfig() defaulting the
policy update interval to 1 hour; make that explicit by creating a local config,
set its PolicyUpdateInterval (e.g. cfg.PolicyUpdateInterval = 1*time.Hour), and
pass that cfg into newTestServiceWithConfig (used with TestServerOpts/mockClock)
before calling svcImpl.continuousAutomationOnCooldown; apply the same change for
the second test block as well so the assertions no longer rely on TestConfig()
defaults.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ae3a8052-ff6a-43b8-92e4-0ce86b8247ec

📥 Commits

Reviewing files that changed from the base of the PR and between 19d7dec and 83f9e2a.

📒 Files selected for processing (8)
  • server/datastore/mysql/software_installers.go
  • server/datastore/mysql/vpp.go
  • server/datastore/mysql/vpp_test.go
  • server/fleet/datastore.go
  • server/fleet/software_installer.go
  • server/mock/datastore_mock.go
  • server/service/osquery.go
  • server/service/osquery_test.go

Comment thread server/datastore/mysql/vpp.go

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a tight install→refetch→policy re-run→install loop that can occur when continuous policy automations are enabled and a policy keeps failing even after successful installs, by adding a cooldown aligned to the policy update interval.

Changes:

  • Add a service-level cooldown helper and apply it to continuous software installer policy automations.
  • Add VPP continuous automation throttling based on recent verified installs within the policy update interval.
  • Extend datastore plumbing (interfaces, mock, MySQL) and add tests for the cooldown + new MySQL query.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
server/service/osquery.go Adds cooldown helper and throttling logic for continuous software installer and VPP policy automations.
server/service/osquery_test.go Adds unit tests for cooldown helper and software installer throttling behavior.
server/mock/datastore_mock.go Adds mock support for the new VPP “recently verified installs” datastore method.
server/fleet/software_installer.go Extends HostLastInstallData with CreatedAt for throttling decisions.
server/fleet/datastore.go Adds MapAdamIDsRecentlyVerifiedInstalls to the datastore interface with docs.
server/datastore/mysql/vpp.go Implements MapAdamIDsRecentlyVerifiedInstalls in MySQL.
server/datastore/mysql/vpp_test.go Adds MySQL test coverage for MapAdamIDsRecentlyVerifiedInstalls.
server/datastore/mysql/software_installers.go Updates last-install queries to return created_at for throttling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/datastore/mysql/vpp_test.go Outdated
Comment thread server/datastore/mysql/vpp.go Outdated
Comment thread server/datastore/mysql/vpp.go Outdated
Comment thread server/service/osquery.go Outdated
@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.11111% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.94%. Comparing base (025c5b1) to head (0f57287).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
server/datastore/mysql/vpp.go 85.71% 1 Missing and 1 partial ⚠️
server/service/osquery.go 92.59% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46823      +/-   ##
==========================================
- Coverage   66.94%   66.94%   -0.01%     
==========================================
  Files        2840     2840              
  Lines      225348   225382      +34     
  Branches    11537    11537              
==========================================
+ Hits       150865   150886      +21     
- Misses      60789    60797       +8     
- Partials    13694    13699       +5     
Flag Coverage Δ
backend 68.65% <91.11%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +1293 to +1295
`SELECT DISTINCT(adam_id) FROM host_vpp_software_installs
WHERE host_id = ? AND canceled = 0 AND removed = 0
AND verification_at >= NOW() - INTERVAL ? SECOND`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: wondering if this will require a covering index in the short term

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Definitely. Though given it's only checked on some policies/automations (and on distributed/write not distributed/read) it's probably a-ok for now. We'll know more during load tests.

@lucasmrod
lucasmrod merged commit 339af29 into main Jun 4, 2026
42 checks passed
@lucasmrod
lucasmrod deleted the continnuous-automations-fix-tight-loop branch June 4, 2026 20:48
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.

[Manage automations] Backend: New setting for continuous retry of policies

3 participants