Skip to content

engine: patch UpsertVariable canonicalizes the variable ident in place, destroying the human-facing display name #890

Closed

Description

@bpowers

Problem

When a variable is round-tripped through the edit/patch path (pysimlin model.edit() -> simlin_project_apply_patch_json -> the Rust JSON patch UpsertVariable path), the variable's display name is replaced by its canonical identifier.

src/simlin-engine/src/patch.rs runs canonicalize_stock / canonicalize_flow / canonicalize_aux / canonicalize_module on every upserted variable, which does *ident = canonicalize(ident) in place. Because the datamodel stores a single ident field that doubles as the human-facing display name (there is no separate display-name field), this is lossy: the display casing and XMILE \n line breaks are destroyed for the edited variable, while untouched variables keep theirs.

Concrete examples

  • test/conveyors/minimal_conveyor.xmile: upserting the stock named Students (only changing its documentation) leaves the project serialized with the stock renamed to students. The sibling Alumni (untouched) keeps its casing.
  • test/conveyors/covid19_severity.stmx: upserting the flow displayed as testing\nassymptomatic (literal backslash-n line break in the XMILE display name) leaves it as testing_assymptomatic.

Repro

import json, simlin
m = simlin.load("test/conveyors/minimal_conveyor.xmile")
with m.edit() as (current, patch):
    s = current["Students"]
    s.documentation = "x"
    patch.upsert_stock(s)
print([s["name"] for s in json.loads(m.project.serialize_json())["models"][0]["stocks"]])
# -> ['Alumni', 'students']

The Python side sends "Students" verbatim in the patch JSON; the canonicalization happens in the Rust patch-application path.

Why it matters

  • A routine pysimlin edit (e.g. touching documentation) silently rewrites the user's chosen display name -- lowercased, line breaks collapsed to underscores.
  • Diagram labels churn: the view still shows the original name while the variable's name has changed underneath, or the label degrades on the next render.
  • The lossiness is inconsistent (only edited variables are affected), so repeated edits gradually erode a model's presentation.

Components affected

  • src/simlin-engine/src/patch.rs (canonicalize_ident and callers, applied to UpsertVariable)
  • src/pysimlin (model.edit() surface), src/libsimlin (simlin_project_apply_patch_json)

Possible approaches

  • Match variables by canonical ident for lookup/dedup purposes but preserve the incoming (or existing) display-form ident when storing the upserted variable. E.g. canonicalize a copy for matching, and if the canonical form equals the canonical form of an existing variable, keep the existing display name unless the patch explicitly renames.
  • Longer term: separate display name from canonical ident in the datamodel, so canonicalization can never be lossy. Related old report: XMILE export from simlin: variable name to lowercase but not in equations #82 (XMILE export lowercases variable names in the <variables> block but not in equations), which stems from the same single-field name/ident conflation.

Discovery context

Identified while working on #882 (pysimlin JSON converter dropping Compat fields) on branch conveyor-engine; out of scope for that fix.

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