Changeset 3a0b0ce in flexoentity


Ignore:
Timestamp:
10/19/25 17:44:39 (3 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
8aea9a0
Parents:
811ce96
Message:

remove version from ensure_unique because its an old format regression

File:
1 edited

Legend:

Unmodified
Added
Removed
  • flexoentity/id_factory.py

    r811ce96 r3a0b0ce  
    7575
    7676    @staticmethod
    77     def _ensure_unique(hash_part: str, version: int | None = None) -> str:
    78         """Append disambiguator if hash was already seen this session."""
    79         candidate = f"{hash_part}-{version}" if version is not None else hash_part
    80         if candidate not in FlexOID._seen_hashes:
    81             FlexOID._seen_hashes.add(candidate)
    82             return candidate
    83         # fallback only if truly same hash+version (should never happen)
    84         for suffix in itertools.chain(map(chr, range(65, 91)), range(1, 100)):
    85             alt = f"{candidate}-{suffix}"
     77    def _ensure_unique(hash_part: str) -> str:
     78        """Append disambiguator only if the hash was already seen this session."""
     79        if hash_part not in FlexOID._seen_hashes:
     80            FlexOID._seen_hashes.add(hash_part)
     81            return hash_part
     82        # fallback only if truly same hash (rare)
     83        for suffix in range(1, 100):
     84            alt = f"{hash_part}-{suffix}"
    8685            if alt not in FlexOID._seen_hashes:
    8786                FlexOID._seen_hashes.add(alt)
     
    8988        raise RuntimeError("Too many collisions; adjust hash length or logic.")
    9089
     90
    9191    @staticmethod
    9292    def generate(domain: str, etype: str, estate: str, text: str,
     
    109109        base_hash = FlexOID._blake_hash(hash_seed)
    110110        unique_hash = (
    111             FlexOID._ensure_unique(base_hash, version) if enforce_unique else base_hash
     111            FlexOID._ensure_unique(base_hash) if enforce_unique else base_hash
    112112        )
    113113
     
    120120
    121121        return FlexOID(flexo_id_str, signature)    # ──────────────────────────────────────────────────────────────────────────
    122 
    123 
    124     @staticmethod
    125     def generate_old(domain: str, etype: str, estate: str, text: str,
    126                  version: int = 1, enforce_unique = True):
    127         """
    128         Generate a new, versioned, and state-aware Flex-O ID.
    129 
    130         This is the primary constructor for new entities. It combines the domain,
    131         entity type, creation date, unique hash (derived from the canonicalized
    132         text and lifecycle state), version number, and state suffix into a
    133         deterministic but unique identifier string.
    134 
    135         Format:
    136         <domain>-<etype><date>-<hash>@<version><state>
    137         Example:
    138         AF-Q251019-9B3E2@001A
    139 
    140         Parameters
    141         ----------
    142         domain : str
    143         Two-to-six letter domain or organizational prefix (e.g. "AF").
    144         etype : str
    145         One-to-three letter entity type identifier (e.g. "RQ" for Question).
    146         estate : str
    147         Lifecycle state whose one-letter code is appended to the version.
    148         text : str
    149         Entity-specific text seed (used to derive a stable hash).
    150         version : int, optional
    151         Starting version number (default is 1).
    152 
    153         Returns
    154         -------
    155         FlexOID
    156         A new Flex-O ID object with unique hash and digital signature.
    157 
    158         Notes
    159         -----
    160         - Changing the text or state will produce a new hash and signature.
    161         - Versions start at 1 and increment through `next_version()`.
    162         """
    163 
    164         if not (1 <= version <= FlexOID.MAX_VERSION):
    165             raise ValueError(f"Version {version} exceeds limit; mark obsolete.")
    166 
    167         date_part = datetime.now(timezone.utc).strftime("%y%m%d")
    168 
    169         # include state in the seed so hash & signature depend on lifecycle state
    170         seed = canonical_seed(f"{text}:{estate}")
    171 
    172         base_hash = FlexOID._blake_hash(seed)
    173         unique_hash = FlexOID._ensure_unique(base_hash, version)if enforce_unique else base_hash
    174 
    175         ver_part = f"{version:03d}{estate}"
    176         flexo_id_str = f"{domain}-{etype}{date_part}-{unique_hash}@{ver_part}"
    177 
    178         # short signature derived from state-aware seed
    179         signature = hashlib.blake2s(seed.encode("utf-8"), digest_size=8).hexdigest().upper()
    180 
    181         return FlexOID(flexo_id_str, signature)
    182122
    183123    @property
Note: See TracChangeset for help on using the changeset viewer.