Skip to content

fix: detect external changes and prevent version_description drift#11

Merged
roh26it merged 1 commit intoPortkey-AI:mainfrom
austin-engle:fix/drift-detection-and-version-description
Feb 21, 2026
Merged

fix: detect external changes and prevent version_description drift#11
roh26it merged 1 commit intoPortkey-AI:mainfrom
austin-engle:fix/drift-detection-and-version-description

Conversation

@austin-engle
Copy link
Contributor

@austin-engle austin-engle commented Feb 20, 2026

Summary

Fixes drift detection, perpetual plan diff, and version numbering issues with portkey_prompt and portkey_prompt_partial resources.

Problems Fixed

  1. External edits invisible to Terraform — Both mapPromptToState and mapPartialToState preserved content from state during Read, ignoring API values. If someone edited a prompt or partial in the Portkey console, Terraform never detected the change.

  2. version_description perpetual drift — Both resources imported version_description from the API into state during Read. If set via console but not in Terraform config, this caused an infinite plan loop (state has value → config doesn't → plan nulls it → apply clears it → Read imports it again).

  3. MakeDefault targeting wrong version — After creating a new version, the provider called MakeDefault with state.Version + 1, which breaks when versions are created outside Terraform (console edits create gaps in the version sequence).

How It Works

Drift detection: During Read, both mapping functions now compare the API version to the state version:

if API.Version != state.Version → external change → refresh from API

This catches both new versions (console edits) and rollbacks (console sets an older version as default). When versions match, content is preserved from state to avoid false diffs from Portkey API eventual consistency.

Version lookup: After Update creates a new version, the provider calls the /versions list endpoint, matches the returned version UUID against the update response, and gets the real version number for MakeDefault. No more guessing with +1.

version_description: No longer imported from the API — only preserved from state.

Changes

File Change
client.go Added ListPromptVersions, ListPromptPartialVersions client methods and response types
prompt_partial_resource.go Version-comparison drift detection (!=), version lookup via list API, removed version_description API import
prompt_resource.go Version-comparison drift detection (!=), version lookup via list API, removed version_description API import, parameters refreshed on external change
map_state_test.go (new) 10 unit tests covering drift detection, rollback, state preservation, first population, and version_description behavior

Test Plan

  • Unit tests: 10 tests covering all drift detection scenarios
  • Manual testing against live Portkey resources in ai-core-dev:
    • Console edit to partial content → detected, Terraform overwrites ✓
    • Console edit to prompt template → detected, Terraform overwrites ✓
    • 5 consecutive console edits (version gap) → apply works, correct version made default ✓
    • Terraform content update → applies correctly, clean plan after ✓
    • No-change plan → clean, no perpetual drift ✓
    • version_description set via console → not imported, no perpetual drift ✓

@austin-engle austin-engle force-pushed the fix/drift-detection-and-version-description branch 2 times, most recently from 41f80d4 to 27e3017 Compare February 20, 2026 23:10
Fixes two issues with prompt and prompt_partial resources:

1. prompt_partial: External edits (via console/API) were invisible to
   Terraform because mapPartialToState always preserved content from
   state. Now compares API version to state version — if higher, content
   is refreshed from API so Terraform detects drift and overwrites.

2. Both resources: version_description set via console was imported into
   state during Read, causing perpetual plan drift when config didn't
   set it. Removed API-to-state import for version_description.

Adds unit tests for mapPartialToState and mapPromptToState covering
drift detection, state preservation, first population, and
version_description behavior.
@austin-engle austin-engle force-pushed the fix/drift-detection-and-version-description branch from 27e3017 to 7326f1c Compare February 20, 2026 23:23
Copy link
Contributor

@roh26it roh26it left a comment

Choose a reason for hiding this comment

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

Approved

Thoroughly reviewed and tested locally.

What was verified:

  • Build, go vet, and gofmt all pass
  • All 10 new unit tests pass
  • All prompt_partial acceptance tests pass (5/5)
  • The 2 failing prompt_resource tests are pre-existing failures on main branch (verified by running the same tests against main)

Code review findings:

  • Drift detection logic is sound: Using version comparison (!=) correctly detects both new versions (console edits) and rollbacks
  • Version lookup fix is correct: Fetching from /versions endpoint instead of +1 increment handles version gaps caused by out-of-band changes
  • version_description fix prevents perpetual drift: No longer importing from API avoids the state/config mismatch cycle
  • Tests are meaningful: Verify actual values, not just existence

The changes are well-documented, follow existing patterns, and fix real bugs.

@roh26it roh26it merged commit 69f84d6 into Portkey-AI:main Feb 21, 2026
2 checks passed
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.

2 participants