Skip to content

[9.5] [Fleet] Clear stale inputs_for_versions when a package drops its agent version condition (#306187) - #306739

Open
kibanamachine wants to merge 1 commit into
elastic:9.5from
kibanamachine:backport/9.5/pr-306187
Open

kibanamachine wants to merge 1 commit into
elastic:9.5from
kibanamachine:backport/9.5/pr-306187

Conversation

@kibanamachine

Copy link
Copy Markdown
Contributor

Backport

This will backport the following commits from main to 9.5:

Questions ?

Please refer to the Backport tool documentation

…t version condition (elastic#306187)

Closes elastic#305367

## Summary

When a package upgrade drops `conditions.agent.version`, the package
policy kept the `inputs_for_versions` cached from the old version.
Agents kept receiving the stale per-version inputs, the agent policy
stayed flagged `has_agent_version_conditions: true`, and later edits
never reached them.

- `compilePackagePolicyForVersions` now clears a non-empty
`inputs_for_versions` when the package has no agent version condition
(feature flag still gates it). It returns `{ cleared }`.
- `update()` and `bulkUpdate()` (used by package upgrades) force a full
agent version conditions recompute when the cache was cleared. This also
self-heals package policies already affected, on their next edit.
- Stale `elastic#9.x` variants are removed by the existing reassign and sweep
logic once `has_agent_version_conditions` goes false.

## Test plan

- [x] Unit tests for the clear / no-op / flag-off cases and the
`bulkUpdate` recompute flag
- [x] FTR test for upgrade to a package without the condition (not run
locally, needs the package registry)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit 7cc2e49)
@kibanamachine kibanamachine added the backport This PR is a backport of another PR label Nov 20, 2026
@kibanamachine
kibanamachine enabled auto-merge (squash) November 20, 2026 15:06
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown
Contributor

Pinging @elastic/fleet (Team:Fleet)

@infra-vault-gh-plugin-prod

Copy link
Copy Markdown
Contributor
🤖 Jobs for this PR can be triggered through checkboxes. 🚧

ℹ️ To trigger the CI, please tick the checkbox below 👇

  • Click to trigger kibana-pull-request for this PR!
  • Click to trigger kibana-deploy-project-from-pr for this PR!
  • Click to trigger kibana-deploy-cloud-from-pr for this PR!
  • Click to trigger kibana-entity-store-performance-from-pr for this PR!
  • Click to trigger kibana-storybooks-from-pr for this PR!
  • Click to trigger kibana-static-checks for this PR!

Comment on lines +456 to +458
clearStaleInputsSpy = jest
.spyOn(packagePolicyService as any, 'clearStaleInputsForVersions')
.mockResolvedValue(false);

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.

Severity: P3 (Low)

Add a regression test that seeds a stale cache on an already-upgraded policy and exercises update() without mocking the clear. This file-wide stub bypasses that path, and the FTR edit starts after the bulk upgrade has already emptied the cache. Removing the single-update cleanup or its recompute flag would leave the new tests green.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Comment on lines +999 to +1005
fields: ['inputs_for_versions'],
}))
);
const stale = packagePolicySOs.filter(
(so): so is SavedObject<PackagePolicySOAttributes> =>
!isSavedObjectErrorResult(so) &&
Object.keys(so.attributes?.inputs_for_versions ?? {}).length > 0

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.

Severity: P3 (Low)

Tie the cache clear to the package-policy revision that was checked for conditions. If a concurrent update restores a version-conditioned package and compiles its inputs before this bulkGet, the read picks up that update’s version and the clear succeeds against its valid cache. For template-only conditions, the agent policy can then stop generating variants even though the template still needs them.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

Comment on lines +2101 to +2103
const clearedInputsForVersions = await this.clearStaleInputsForVersions(soClient, [
{ packagePolicyId: newPolicy.id, packageInfo: pkgInfo, assetsMap },
]);

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.

Severity: P2 (Medium)

Return the post-clear saved-object version from update(). newPolicy is fetched before this second write and returned unchanged, so a client that uses the successful PUT response’s version for its next edit gets a 409 even when nobody else edited the policy.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

assetsMap,
}),
agentVersionConditionsUnchanged:
!clearedInputsForVersions &&

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.

Severity: P3 (Low)

Keep the recompute retryable after the cache is gone. When a same-version edit clears a stale cache but the parent policy bump fails, retrying the edit no longer sees a cache to clear. If the parent had no stored manifest conditions, the retry can succeed while keeping has_agent_version_conditions: true, leaving agents on variants that are no longer needed.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.


await retry.tryForTime(20000, async () => {
const { body } = await supertest
.get(`/api/fleet/agent_policies/${agentPolicyId}/full`)

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.

Severity: P3 (Low)

Check the deployed policy revision when asserting that the agent receives 10m. The unversioned /full endpoint compiles from current saved objects, so this assertion passes even if Fleet Server still has the previous interval. Request /full?revision=… for the new revision or assert on the deployed .fleet-policies document.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

@infra-vault-gh-plugin-prod

infra-vault-gh-plugin-prod Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

💔 Build Failed

Failed CI Steps

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
maps 3.4MB 3.4MB +8.0B
visTypeVega 2.1MB 2.1MB +8.0B
total +16.0B

History

cc @juliaElastic

This branch has not been deployed

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

Labels

backport This PR is a backport of another PR Team:Fleet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants