Skip to content

fix(#127): deleter identity for property deletes + drop orphaned per-property metadata - #128

Merged
nikoraes merged 2 commits into
mainfrom
fix/adx-property-delete-updatedby-127
Dec 2, 2026
Merged

nikoraes merged 2 commits into
mainfrom
fix/adx-property-delete-updatedby-127

Conversation

@nikoraes

@nikoraes nikoraes commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Problem

With ADX historization and UpdatedBy enabled (Parameters:TrackLastUpdatedBy=true), a property delete was recorded in AdtPropertyEvents with an empty UpdatedBy — and, when the property still carried per-property metadata, with the previous writer's id rather than the caller that performed the delete.

Closes #127.

Root cause

CloudEventFactory.CreatePropertyCloudEvent resolved updatedBy only from the property's own metadata ($metadata.<property>.lastUpdatedBy) — first from the JSON Patch, then as a fallback from NewValue.

A remove patch operation never refreshes that entry:

  • AgeDigitalTwinsClient.UpdateDigitalTwinAsync removes the property from the twin but leaves $metadata.<property> untouched (the loop over patchedTwin no longer sees the property, so it neither updates nor deletes its metadata entry);
  • for a property written before TrackLastUpdatedBy was enabled, that entry does not exist at all.

So the lookup found nothing (empty column, exactly as reported) or a stale value. The identity of the deleter was written by the same update — to the twin/relationship-level $metadata.$lastUpdatedBy — but the property-event path never read it.

Fix

1. Property event updatedBy

One resolver, GetPropertyEventUpdatedBy, with an explicit order of preference:

  1. the value the JSON Patch actually changed (per-property entry, or a bundled $metadata/<property> object) — the write that produced the event;
  2. for Remove: the twin/relationship-level $metadata.$lastUpdatedBy written by that same update — the identity of the deleter. The per-property value is deliberately not used here, so a delete can never report a stale writer;
  3. otherwise (non-remove): the per-property value already in NewValue, for same-user re-updates whose identical value never shows up in the patch.

Behaviour when TrackLastUpdatedBy=false, and when no userId was supplied on the delete (no identity is known), is unchanged: no updatedBy in the body, empty column.

2. The twin payload drops the orphaned entry

UpdateDigitalTwinAsync now removes $metadata.<property> for every property a patch deletes, in the same write:

  • only top-level remove operations whose property is absent from the final document — a remove + add of the same property in one patch keeps its metadata;
  • nested removals (/component/property) are skipped: the top-level component entry, and therefore its metadata, is still valid;
  • reserved $-entries are never touched; JSON Pointer segments are decoded per RFC 6901 (~1 then ~0).

The twin-level metadata was already correct for the same write — $metadata.$lastUpdatedBy and $metadata.$lastUpdateTime were written with the deleting caller before this commit. That is now pinned by a test rather than assumed: running the new test against the pre-change code with only the orphan assertion removed passes, so the only behaviour change is the dropped entry.

Verification

Unit — CloudEventFactoryTests (+4 tests):

test before after
delete, residual $metadata.<prop>.lastUpdatedBy = creator-user creator-user (stale) deleting-user
delete, no per-property metadata (the reported shape) null (empty column) deleting-user
delete, trackLastUpdatedBy: false absent absent
relationship property delete creator-user (stale) rel-deleting-user

Client payload — AgeDigitalTwins.Test/DigitalTwinsTests (+2 tests, real client against PG16 + AGE 1.6), both via JsonObject because Azure's BasicDigitalTwin converter rejects $lastUpdatedBy:

  • UpdateDigitalTwinAsync_RemoveProperty_DropsPerPropertyMetadata_AndUpdatesTwinLevelMetadata — creator writes diameter + depth; a different user deletes /diameter. Asserts the property is gone, its $metadata entry is gone, twin-level $lastUpdatedBy names the deleter, $lastUpdateTime advances, and depth's metadata still names its own writer. Red before this commit (the orphaned $metadata.diameter entry must be dropped with the property) and green after.
  • UpdateDigitalTwinAsync_RemoveThenReAddProperty_KeepsMetadata — remove + add of the same property in one patch document keeps the metadata and stamps the rewriting user.

End-to-end — EventsIntegrationTests.DeleteDigitalTwinProperty_ShouldGeneratePropertyEventWithDeletingUser goes through the real client and logical replication: create with the non-tracking client (so the property has no per-property lastUpdatedBy), delete with userId: "deleting-user", capture the Konnektr.Graph.Property.Event. Proven red before the events fix (Expected: "deleting-user" / Actual: null) and green after, on ghcr.io/konnektr-io/age-pgvector:16-1.6.0-standard-trixie (PG16 + AGE 1.6, CNPG_TEST=true) in-cluster.

Full suites on that database:

  • AgeDigitalTwins.Test: 358 passed / 8 skipped / 0 failed
  • AgeDigitalTwins.Events.Test: 106 passed / 0 failed (includes the delete e2e above against the new client behaviour)

CI runs the full five-leg PG × AGE matrix (PG16 + PG18 × AGE 1.6/1.7/1.8, apache + cnpg) — green on 9007ea0. The first run hit an unrelated flake in EntityRouteAliasIntegrationTests (a model-import race inserting dtmi:com:adt:dtsample:room;1 twice); rerunning the failed leg only was green.

Docs

CLAUDE.md and docs/advanced-topics/security.mdx now describe the removal behaviour and why the event resolver still refuses a per-property value for a delete (twins written before this change can carry a stale orphan).

Notes

  • Twins written before this change keep their existing orphaned $metadata.<property> entries until the next property write; they are payload debris only and cannot make a delete event report a stale writer, because the resolver never reads that entry for a delete.
  • Rows already written to AdtPropertyEvents with an empty UpdatedBy cannot be retroactively corrected — only new deletes carry the identity.

…perty deletes

Property deletes produced a property event with no `updatedBy` (an empty
`UpdatedBy` column in the ADX `AdtPropertyEvents` table), and — when the
property still carried per-property metadata — with the *previous* writer's id
instead of the caller that performed the delete.

Root cause: `CloudEventFactory.CreatePropertyCloudEvent` resolved `updatedBy`
only from the property's own metadata (`$metadata.<property>.lastUpdatedBy`,
via the JSON Patch and then from `NewValue`). A `remove` patch operation never
refreshes that entry: `AgeDigitalTwinsClient` deletes the property from the twin
but leaves `$metadata.<property>` behind untouched (or it never existed for
properties written before `TrackLastUpdatedBy` was enabled). So the lookup found
either nothing or a stale value, while the identity of the deleter was written
by that same update to the twin/relationship-level `$metadata.$lastUpdatedBy`.

Fix: resolve the property event's `updatedBy` in one place
(`GetPropertyEventUpdatedBy`) with an explicit order of preference — the value
the patch actually changed, then, for `Remove`, the twin/relationship-level
`$metadata.$lastUpdatedBy`, then (non-remove only) the per-property value in
`NewValue` for same-user re-updates. A property delete can therefore never
report a stale writer.

Tests
- `CloudEventFactoryTests`: four new unit tests — delete with residual
  per-property metadata (must not report the stale writer), delete with no
  per-property metadata (the reported symptom), `trackLastUpdatedBy: false`,
  and a relationship property delete.
- `EventsIntegrationTests`: end-to-end through the real client + logical
  replication (create without tracking, delete with `userId`), asserting the
  captured delete event carries the deleter. Verified red before the fix
  (`updatedBy = null`) and green after it on PG16 + AGE 1.6; the full
  `AgeDigitalTwins.Events.Test` suite (106 tests) passes locally against that
  database.

Docs: CLAUDE.md and docs/advanced-topics/security.mdx now state how `updatedBy`
is resolved for a property delete.

Closes #127

Co-authored-by: Hermes Agent <hermes@nikoraes.local>
@nikoraes

nikoraes commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

CI on head 2d4715a: all five matrix legs pass (PG16/AGE 1.6 apache + cnpg, PG18/AGE 1.8 apache, PG18/AGE 1.7 and 1.8 cnpg) — mergeStateStatus: CLEAN, base still 14e12d0 (no drift).

Local evidence behind that head, on ghcr.io/konnektr-io/age-pgvector:16-1.6.0-standard-trixie: the new integration test was run against the pre-fix CloudEventFactory first (Expected: "deleting-user" / Actual: null) and then with the fix (green); the full AgeDigitalTwins.Events.Test project is 106 passed / 0 failed. Ready for your review.

`UpdateDigitalTwinAsync` removed the property from the twin but left
`$metadata.<property>` behind: the metadata pass walks only the properties
still present in the patched twin, so a `remove` of `/<property>` never
visited the entry it invalidated. The result was a twin whose `$metadata`
described a property it no longer had, still naming whoever wrote that
property last — which is also why the first version of the events fix had to
be told never to trust that entry for a delete.

Drop `$metadata.<property>` in the same write. The rule is deliberately
narrow: only top-level `remove` operations whose property is gone from the
final document (a remove+add of the same property in one patch keeps its
metadata, since the property still exists). Nested removals such as
`/component/property` are skipped — the top-level component entry, and thus
its metadata, is still valid. Reserved `$`-entries are never touched. JSON
Pointer segments are decoded per RFC 6901 (`~1` then `~0`).

The twin-level metadata for that same write was already correct and is
unchanged: `$metadata.$lastUpdatedBy` and `$metadata.$lastUpdateTime` were
written with the deleting caller before this commit — verified by running
the new test against the pre-change code with only the orphan assertion
removed (it passed), then with the fix in place.

Tests (all through the real client against PG16 + AGE 1.6):
- `UpdateDigitalTwinAsync_RemoveProperty_DropsPerPropertyMetadata_AndUpdatesTwinLevelMetadata`
  — creator writes two properties, a different user deletes one: the property
  and its metadata entry are gone, the twin-level `$lastUpdatedBy` names the
  deleter, `$lastUpdateTime` advances, and the surviving property's metadata
  still names its own writer. Proven red before the change (fails exactly on
  the orphan assertion) and green after.
- `UpdateDigitalTwinAsync_RemoveThenReAddProperty_KeepsMetadata` — the same
  patch document removes and re-adds a property; its metadata survives and
  names the rewriting user.

Full `AgeDigitalTwins.Test`: 358 passed / 8 skipped / 0 failed.
Full `AgeDigitalTwins.Events.Test`: 106 passed / 0 failed — the delete event
still carries the deleter, now resolved from twin-level metadata while the
stored twin no longer carries a stale orphan.

Docs: CLAUDE.md and docs/advanced-topics/security.mdx describe the removal
behaviour and why the event resolver still refuses the per-property value for
a delete (twins written before this change can carry a stale orphan).

Co-authored-by: Hermes Agent <hermes@nikoraes.local>
@nikoraes nikoraes changed the title fix(#127): record the deleter's identity in AdtPropertyEvents for property deletes fix(#127): deleter identity for property deletes + drop orphaned per-property metadata Dec 2, 2026
@nikoraes
nikoraes merged commit 6d60bc8 into main Dec 2, 2026
9 of 10 checks passed
@nikoraes
nikoraes deleted the fix/adx-property-delete-updatedby-127 branch December 2, 2026 10:16
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.

ADX historization: UpdatedBy is empty in AdtPropertyEvents for property deletes

1 participant