Skip to content

Settle the relations a deleted relation retired instead of leaving them dangling - #58

Merged
arrry-hydraDB merged 2 commits into
hydra-db:mainfrom
IronicRayquaza:fix/restore-on-delete
Nov 16, 2026
Merged

arrry-hydraDB merged 2 commits into
hydra-db:mainfrom
IronicRayquaza:fix/restore-on-delete

Conversation

@IronicRayquaza

@IronicRayquaza IronicRayquaza commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements #23. Deleting a relation at a max_count endpoint now settles that endpoint again, as supersede would have settled it without the deleted relation. Before, whatever the deleted relation had retired stayed retired, with an invalidated_by naming a relation that no longer existed.

Rule. For each constrained endpoint a deleted relation sat at:

  1. Both endpoints of the deleted relation are locked, then the endpoint is read.
  2. Every relation a supersede retired there is admitted back into Contradictions, with its stored rank. A retirement counts as a supersede one when its invalidated_by is a relation of that endpoint group.
  3. Contradictions.settle keeps the newest max_count current.
  4. Then, for each relation:
    • Makes the cut: it comes back as a new current revision with its old properties, its source count, and its original effective time, so as_of reads show it valid from its start.
    • Does not make the cut: it stays retired. Its newest version is re-pointed in place when settle names another relation or time, and invalidated_known_at is kept, so known_at reads from before the delete are unchanged.
    • LLM-judged or replace_source retirement: its invalidated_by names no relation of the endpoint, so it is never admitted and stays as it is.
Writes Delete Result
max_count: 1: Acme, Globex Globex Acme is current again
max_count: 1: Acme, Globex, Initech Globex Acme stays retired, now invalidated_by Initech at Initech's time
max_count: 1: Acme, Globex, Initech Initech Globex is restored; Acme stays retired by Globex
max_count: 2: A, B, C, D C B is restored (current B, D); A stays retired, now by D
max_count: 2: A, B, C B A is restored (current A, C), even though B retired nothing

Where it applies

  • relations.delete, entities.delete, and maintenance.prune. All three go through delete_relation / delete_entities, which now take the schema's caps from the resource.
  • maintenance.dedupe_relations: a relation a folded duplicate had retired is re-pointed to the survivor, which states the same fact.

Implementation

  • The decision is Contradictions.admit and Contradictions.settle, unchanged. The only refactors are lifting _cap_slots out of Contradictions._slots, and _endpoint_edges out of _contradiction_check (endpointEdges out of constrainedCurrent in TypeScript), so delete and insert read endpoints the same way.
  • A delete of a relation with no max_count, or at an endpoint with no supersede history, writes nothing extra.
  • DeleteResult gains restored_relations, which prune also reports under deleted. It is mirrored in TypeScript, and the CLI and MCP output pick it up through DeleteResult.

Tests, mirrored in tests/test_contradiction.py / tests/test_trust.py and typescript/test/temporal.test.ts / trust.test.ts:

  • every row of the table above;
  • the entities.delete path, with as_of reads after a restore and in a re-pointed gap;
  • an LLM-judged retirement staying retired after its judged relation is deleted;
  • dedupe re-pointing to the survivor.

The new tests fail on main. I also updated the existing tests that compare a whole delete result, since the dict now has the new key.

Verified with pytest on Kuzu and HydraDB, npm test on Kuzu (conformance included), and the temporal, maintenance, integration, and trust suites on HydraDB. ruff check, ruff format --check, npm run lint, and npm run typecheck are clean. The only failures are two existing tests that also fail on main on macOS: test_error_reasons_fill_empty_messages_and_strip_driver_boilerplate hard-codes Linux errno 111, and the CLI pipe test runs head -c 0, which macOS head rejects.

Docs: resources.md (updating and deleting), schema.md (contradictions), and maintenance.md (prune, dedupe).

Linked issue

Closes #23

AI Assistance

Model Effort
Claude Opus 5.5 (Claude Code) Default

Checklist

  • Implements an issue a maintainer has /approved.
  • Small and scoped to that issue.
  • Breaking changes described below, or none.

Breaking changes

None to the API. Behaviour changes in one case: deleting a relation at a max_count endpoint now settles that endpoint again, which is the point of #23. DeleteResult gains a restored_relations field (0 when nothing comes back), so code that compares a whole delete result to a fixed dict sees one more key.

@github-actions

Copy link
Copy Markdown

This is PR 1 of 3 you may keep open at a time.

@arrry-hydraDB arrry-hydraDB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

With max_count 2 and facts A, B, C, D, deleting C restores A instead of B. Please re-settle the whole endpoint, leave LLM-judged retirements retired, and lock both endpoints, as noted on #23. Reuse the existing settle logic instead of a second copy.

@IronicRayquaza

Copy link
Copy Markdown
Contributor Author

Thanks, all three are addressed in f2c677a.

  • Whole endpoint, newest first. A delete now admits every relation that supersede retired at the endpoint back into Contradictions, with its stored rank, and lets settle keep the newest max_count. With A, B, C, D and max_count: 2, deleting C now restores B, and A stays retired with invalidated_by D. I also covered the case where the deleted relation retired nothing: with A, B, C, deleting B brings A back.
  • LLM-judged retirements stay retired. A retirement is admitted only when its invalidated_by is a relation of the same endpoint group. LLM-judged and replace_source retirements never are, and there is a test for a deleted judged relation.
  • Locks and reuse. Both endpoints of each deleted relation are locked before the endpoint is read. The second settle copy (_resettle / resettle) is gone. The decision is Contradictions.admit and settle, unchanged; I only lifted _cap_slots and _endpoint_edges (endpointEdges in TS) out so delete and insert read endpoints the same way.

One behaviour to check with you: if an endpoint holds more current relations than max_count (written with warn or off), the re-settle retires the oldest uncertified ones, as settle does on an insert. With supersede-only history the current relations are already the newest, so nothing changes for them. If you'd rather a delete never retire a current relation, I can switch that off.

Both SDKs are updated, with tests for each case. The PR description now has the full table.

@arrry-hydraDB arrry-hydraDB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please inline the one-use _slots and _retired_by wrappers (_ops.py:1764, 2048), drop the docstring on the pass-through at access.ts:148, and add a conformance case for re-settling.

@IronicRayquaza

Copy link
Copy Markdown
Contributor Author

Done in 6b653b9.

  • Wrappers inlined. Contradictions._slots is gone; its three callers call _cap_slots(self._caps, ...) directly. _retired_by is folded into repoint_retired, its only caller. The TS side matches (slots, retiredBy).
  • Docstring dropped on caps() in access.ts.
  • Conformance case. Both sides' write now run the A, B, C, D case with max_count: 2 under supersede, delete C, and record the delete result and the versions of A, B, and D. The mask now covers invalidated_known_at too, since it is the write's wall-clock time. I checked that the case fails when TS skips the re-settle on relations.delete.

@arrry-hydraDB arrry-hydraDB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

check this

Comment thread src/selfgraph/_ops.py
)
for edge in stale:
rel = rels[edge["props"]["id"]]
tracker.admit(rel.id, rel.type, rel.from_entity, rel.to_entity, _rank(rel))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

With max_count on both ends, this can restore a fact that replaying the inserts without the deleted relation does not have, in 2 of 12 seeded cases, in both SDKs on every backend. Please settle both endpoints together as insert does. Main also changed _ended in Contradictions, so please rebase.

@IronicRayquaza

Copy link
Copy Markdown
Contributor Author

Rebased on main and fixed in 2410d96.

  • Both endpoints settled together. The delete now builds its tracker through _contradiction_check (contradictionCheck in TS) and settles with main's Contradictions.settle, so each restored relation is settled at both of its endpoints with retired history counted, as insert does. The failing shape was a relation retired at the freed endpoint that a now-retired newer fact still displaced at its other endpoint: Ann–Acme 2020, Ann–Globex 2021, Bob–Acme 2022, Bob–Initech 2023 with max_count: 1 on both ends, then delete Ann–Globex. Ann–Acme used to come back; it now stays retired by Bob–Acme at 2022, as a replay does.
  • Matches main's corrections. A corrected retirement now takes a new revision via _retired instead of being re-pointed in place, so known_at reads keep the earlier one. Source-withdrawn retirements stay out of the history, as on insert.
  • Tests. That case is in both SDKs' tests and in the supersede conformance case. A replay fuzz (delete a relation or an entity, compare with re-inserting without it, single and batched inserts, max_count 1 or 2 per end) matches on all 300 Python and 150 TypeScript seeds, where the previous head failed 9 and 4.

@arrry-hydraDB arrry-hydraDB left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@arrry-hydraDB
arrry-hydraDB merged commit bd55fd2 into hydra-db:main Nov 16, 2026
17 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.

Deleting a relation that superseded another leaves the endpoint with no current relation and a dangling invalidated_by

2 participants