Skip to content

feat(calm-models): add implementation for metadata diff - #3014

Open
aamanrebello wants to merge 5 commits into
finos:mainfrom
aamanrebello:metadata-diff
Open

feat(calm-models): add implementation for metadata diff#3014
aamanrebello wants to merge 5 commits into
finos:mainfrom
aamanrebello:metadata-diff

Conversation

@aamanrebello

@aamanrebello aamanrebello commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Description

Same as #2732 but for metadata. Only flows left to go after this!

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🎨 Code style/formatting changes
  • ♻️ Refactoring (no functional changes)
  • ⚡ Performance improvements
  • ✅ Test additions or updates
  • 🔧 Chore (maintenance, dependencies, CI, etc.)

Affected Components

  • CLI (cli/)
  • Schema (calm/)
  • CALM AI (calm-ai/)
  • CALM Hub (calm-hub/)
  • CALM Hub UI (calm-hub-ui/)
  • CALM Server (calm-server/)
  • CALM Widgets (calm-widgets/)
  • Documentation (docs/)
  • Shared (shared/)
  • VS Code Extension (calm-plugins/vscode/)
  • Dependencies
  • CI/CD

Commit Message Format ✅

Testing

  • I have tested my changes locally
  • I have added/updated unit tests
  • All existing tests pass

Checklist

  • My commits follow the conventional commit format
  • I have updated documentation if necessary
  • I have added tests for my changes (if applicable)
  • My changes follow the project's coding standards

@aamanrebello
aamanrebello requested a review from a team as a code owner August 22, 2026 18:39

@rocketstack-matt rocketstack-matt left a comment

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.

Three real defects in the metadata diff logic — details inline, each with a suggested fix.

Comment on lines +93 to +99
function stringifyMetadata(metadata: CalmMetadataSchema): string[] {
if (Array.isArray(metadata)) {
return metadata.map((obj: Record<string, unknown>) => JSON.stringify(obj));
} else {
return [JSON.stringify(metadata)];
}
}

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.

Array metadata items are matched by raw JSON.stringify, so two objects with identical content but reordered keys are reported as removed+added instead of unchanged. Every other equality check in this file (valuesEqual/normalizeValue/canonicalKey, and diffControlItem's diffArrays(..., { comparator: valuesEqual })) normalizes key order first — this should too.

Suggested change
function stringifyMetadata(metadata: CalmMetadataSchema): string[] {
if (Array.isArray(metadata)) {
return metadata.map((obj: Record<string, unknown>) => JSON.stringify(obj));
} else {
return [JSON.stringify(metadata)];
}
}
function stringifyMetadata(metadata: CalmMetadataSchema): string[] {
if (Array.isArray(metadata)) {
return metadata.map((obj: Record<string, unknown>) => JSON.stringify(normalizeValue(obj)));
} else {
return [JSON.stringify(normalizeValue(metadata))];
}
}

Comment on lines +361 to +363
if (valuesEqual(metadataA, metadataB)) {
result.metadataObjectsUnchanged.push(metadataA);
} else {

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.

archA.metadata ?? {} at the call site means two architectures with no metadata at all both resolve to {}, and valuesEqual({}, {}) is true, so every diff between metadata-less architectures reports a phantom metadataObjectsUnchanged: [{}] instead of [] (unlike the no-controls/no-ADRs case, which reports empty results). Skipping the push when the object is empty avoids the phantom entry without needing to distinguish "caller passed {}" from "caller had no metadata" at the call site.

Suggested change
if (valuesEqual(metadataA, metadataB)) {
result.metadataObjectsUnchanged.push(metadataA);
} else {
if (valuesEqual(metadataA, metadataB)) {
if (Object.keys(metadataA).length > 0) {
result.metadataObjectsUnchanged.push(metadataA);
}
} else {

const result: MetadataItemDiffResult = {};

Object.keys(metadataObjA).forEach((key: string) => {
if (!(key in metadataObjB)) {

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.

in walks the prototype chain, not just own properties. Since CALM metadata is arbitrary key/value data, a metadata object using a key like toString or constructor will appear "present" on the other side (inherited from Object.prototype) even when actually absent, misclassifying a removal as a modification with a non-serializable Function as the value.

Suggested change
if (!(key in metadataObjB)) {
if (!Object.hasOwn(metadataObjB, key)) {

});

Object.keys(metadataObjB).forEach((key: string) => {
if (!(key in metadataObjA)) {

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.

Same prototype-chain issue as above, other direction.

Suggested change
if (!(key in metadataObjA)) {
if (!Object.hasOwn(metadataObjA, key)) {

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