Skip to main content
The CGIAR Risk Intelligence Tool implements comprehensive version control for AI prompts, allowing full audit trails, rollback capabilities, and change tracking.

Version Strategy

Current Version Pattern

Prompts follow a snapshot-on-update versioning strategy:
  • The prompts table stores the latest version only (no JOINs needed for reads)
  • On every update, the current state is snapshotted to prompt_versions table
  • Version number is auto-incremented on each update
  • Version 1 is created automatically when the prompt is first created

Database Schema

Endpoints

Get Prompt (Current or Historical Version)

GET /api/admin/prompts/:id Retrieve the current version or a specific historical version.

Query Parameters

number
Historical version number to retrieve (omit for current version)Example: 2

Example: Get Current Version

Example: Get Historical Version

Response

Get Change History

GET /api/admin/prompts/:id/history Returns the full version history of a prompt, including diffs between versions, who made each change, and when.

Response Fields

array
Array of change records
string
Change record UUID
number
Version number this change created
string
Type of changeValues: CREATE, UPDATE, DELETE, ACTIVATE, DEACTIVATE
object
Field-level diff objectFormat: { field: { old: value, new: value } }Example:
string
Optional comment explaining the change
object
User who made the change
  • id (string): User UUID
  • email (string): User email
string
ISO 8601 timestamp of the change

Example Request

Example Response

Version Lifecycle

1. Creation (v1)

When a prompt is created via POST /api/admin/prompts/create:

2. Update (v2, v3, …)

When a prompt is updated via PUT /api/admin/prompts/:id/update:

3. Toggle Active State

When toggling via POST /api/admin/prompts/:id/toggle-active:

4. Deletion

When deleting via DELETE /api/admin/prompts/:id:

Diff Computation

The changes field in PromptChange records contains field-level diffs:
Only changed fields are included in the diff.

Rollback Pattern

To rollback to a previous version:
  1. Fetch the historical version: GET /api/admin/prompts/:id?version=2
  2. Extract all fields from the response
  3. Update the prompt with those values: PUT /api/admin/prompts/:id/update

Version Retention

All versions are retained indefinitely unless:
  • The entire prompt is deleted (CASCADE deletes all versions)
  • A specific version is explicitly deleted: DELETE /api/admin/prompts/:id?version=2
Recommendation: Implement a cleanup policy for old versions (e.g., keep last 10 versions per prompt).

Conflict Detection

The update endpoint implements optimistic locking to detect concurrent modifications:
This prevents lost updates in multi-user scenarios.

Best Practices

1. Version Naming

Include version numbers in the prompt name for easy identification:

2. Change Comments

Use the comment field in change records to explain significant updates:

3. Version Review

Before activating a new version, review the change history:

4. Version Cleanup

Periodically delete old version snapshots to reduce database size:

Source Code

  • Versioning logic: packages/api/src/prompts/prompts.service.ts
  • Change history service: packages/api/src/prompts/change-history.service.ts
  • Get prompt endpoint: packages/api/src/prompts/prompts.controller.ts:141
  • Get history endpoint: packages/api/src/prompts/prompts.controller.ts:244