Skip to content

Behavior report: Element.decompile() replaces all attributes and does not roll back when validation fails (expected?) #15

Open

Description

@mzargham

Behavior report, not a feature request. This documents what 0.2.0 does so it can be marked as expected or unexpected.

Observed behavior

Element.decompile() reads attributes from the element's codec and replaces all of the element's model attributes with what the codec returned, then re-validates. Two consequences:

  1. An attribute the codec does not return is removed from the element without any warning. (For example, a codec that covers only some of a type's attributes silently drops the others.)
  2. If re-validation then fails, ValidationError is raised but the graph keeps the changed, invalid state. By contrast add_vertex rolls back when validation fails.

Minimal reproduction (kc only)

from knowledgecomplex import SchemaBuilder, KnowledgeComplex, text

class DictCodec:
    """Stands in for any codec that covers only some attributes."""
    def __init__(self): self.store = {}
    def compile(self, element):
        self.store[element["id"]] = {k: v for k, v in element.items()
                                     if k not in ("id", "type", "uri")}
    def decompile(self, uri): return dict(self.store[uri])

sb = SchemaBuilder(namespace="ex")
sb.add_vertex_type("doc", attributes={"title": text(), "notes": text(required=False)})
kc = KnowledgeComplex(schema=sb)
codec = DictCodec(); kc.register_codec("doc", codec)
kc.add_vertex("d1", type="doc", title="T", notes="N", uri="d1")
print("before:", kc.element("d1").attrs)

codec.store["d1"] = {"title": "T2"}                 # codec returns only `title`
kc.element("d1").decompile()
print("after:", kc.element("d1").attrs)              # `notes` is gone, no warning

codec.store["d1"] = {"notes": "N2"}                 # codec omits the required `title`
try:
    kc.element("d1").decompile()
except Exception as e:
    print("decompile raised", type(e).__name__)
print("graph afterwards:", kc.element("d1").attrs)   # title gone, notes changed

try:
    kc.add_vertex("bad", type="doc", notes="no title")   # compare: add_vertex
except Exception as e:
    print("add_vertex raised", type(e).__name__)
print("'bad' present:", "bad" in kc.element_ids())       # False: rolled back

Output (knowledgecomplex 0.2.0, Python 3.14.3)

before: {'title': 'T', 'notes': 'N'}
after: {'title': 'T2'}
decompile raised ValidationError
graph afterwards: {'notes': 'N2'}
'bad' present: False

Why this looks unexpected

  • A failed write leaves the complex in a state that does not conform to its own schema. add_vertex shows the intended behavior is to roll back.
  • The README describes decompile as "markdown file -> KC", which reads like a refresh from the artifact. Dropping attributes the artifact does not mention is a stronger operation and is not documented.

Not checked

MarkdownCodec behaves the same way as far as I can tell, since the replacement happens in Element.decompile, but I have not run it. I also did not check compile or decompile_uri.

Related

Issue 14 (whole-graph validation on every write). It describes the validation behavior this one builds on: decompile ends with the same whole-graph validation.

The question

Is replacing all attributes, and not rolling back on a failed re-validation, intended? If so, a note in the documentation would help.

Workaround (untested beyond the first part)

Make the codec return every attribute of the element. The second part, checking the complex after a failed decompile, I have not tried.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions