popoto.fields.supersession¶
popoto.fields.supersession
¶
SupersessionProtocol — Identity-scoped belief revision over ValidityField.
Where :class:~popoto.fields.validity_field.ValidityField owns the storage of
validity intervals and supersession chains, SupersessionProtocol owns the
vocabulary: "this new claim replaces whatever was previously believed about
(subject, predicate)". It is a stateless coordinator of @staticmethods
mirroring :class:~popoto.fields.observation.ObservationProtocol — never
inherited by a model, never instantiated.
Four operations
identity_key(subject, predicate): Normalize a claim identity into a 16-hex digest naming that identity's open-claim pointer (plan D7).supersede(new_instance, identity_key=...): Close whichever record is currently open for that identity, chain it tonew_instance, and repoint the pointer — one atomicEVAL.invalidate(instance, at=..., superseded_by=...): The direct, identity-free form. Close one specific record, optionally chaining it to the record that replaced it.superseded_by/supersedes/chain: Bidirectional provenance traversal over the two derived chain HASHes.
Design notes
- Chain links are derived state, never model-hash bytes (plan D3). The
forward/reverse links live in two HASHes owned by
ValidityField, so an append-only journal (#560) can adopt this protocol unchanged. There is noRelationshipfield and noHSETonto a record's own hash here. - All mutations route through
ValidityField.execute_supersede. This module never issues aZADD/HSETof its own, so there is exactly one place that knowsSUPERSEDE_LUA's KEYS/ARGV order. - Membership is decided inside the script, and an absent member raises
(#588).
_member_keyresolves a key string and issues no Redis command;SUPERSEDE_LUAruns theEXISTScheck at the instant of the write, so pipeline mode and immediate mode behave identically. An unsaved instance therefore raises :class:~popoto.fields.validity_field.ValidityMemberAbsentError— aValueErrorsubclass — instead of returningNone, which was byte-identical to the normal pipeline-mode return and gave the caller no signal at all. Nothing is written on that path: the script errors before its first write command. - The one exception is the observation signal path.
ObservationProtocol._apply_supersessionkeeps a client-sideEXISTSprobe of its own, because telemetry must not raise and, by construction, never has a same-transaction successor. - Cycle-safe traversal.
chain()carries aseenset and treats a link naming a record with no interval entry as a chain end, so a corrupt or hard-deleted chain terminates instead of hanging.
Example
from popoto import Model, KeyField, ValidityField, SupersessionProtocol
class Fact(Model): fact_id = KeyField() validity = ValidityField()
identity = SupersessionProtocol.identity_key("user_42", "subscription_plan")
old = Fact(fact_id="free").save() SupersessionProtocol.supersede(old, identity_key=identity) # -> None
new = Fact(fact_id="enterprise").save() SupersessionProtocol.supersede(new, identity_key=identity) # -> old's key
SupersessionProtocol.superseded_by(old) # -> new SupersessionProtocol.supersedes(new) # -> old SupersessionProtocol.chain(new) # -> [old, new]
IDENTITY_SEPARATOR = '\x00'
module-attribute
¶
Join byte between normalized identity components.
LOAD-BEARING: \x00 cannot occur in normalized component text (it is
rejected outright), so ("ab", "c") and ("a", "bc") can never hash to the
same digest. Any printable delimiter would reintroduce that false-merge.
IDENTITY_DIGEST_BYTES = 8
module-attribute
¶
blake2b digest size, giving a 16-hex-character key segment.
Hashing (rather than embedding the caller's text) is what keeps arbitrary user strings out of the Redis keyspace.
SupersedeDeclinedError
¶
Bases: RuntimeError
Raised when the successor's save() was declined, so nothing closed.
Model.save() has several early-return gates -- the never-record firewall
and the write filter among them -- that return instead of raising. In
pipeline mode the value they return is the pipeline itself, which is
byte-identical to success, so a caller that queued a close on top of it
would commit a membership change with no record backing it.
:func:_save_and_close checks for both shapes (a falsy return and a
_never_record_verdict stamped on the instance) and raises this instead.
A RuntimeError subclass, not a ValueError: it reports a broken
invariant inside the write path rather than a malformed argument, and
RuntimeError is what this path raised before the error was given a type,
so existing handlers keep working.
Attributes:
| Name | Type | Description |
|---|---|---|
verdict |
The blocking |
Source code in src/popoto/fields/supersession.py
SupersedeResult
dataclass
¶
Outcome of :meth:SupersessionProtocol.save_and_supersede (plan D6).
Attributes:
| Name | Type | Description |
|---|---|---|
instance |
Any
|
The successor, saved (or queued for saving). |
closed_key |
Optional[str]
|
The superseded record's Redis key, or |
pipeline |
Optional[Pipeline]
|
The caller's pipeline, unexecuted; |
close_index |
Optional[int]
|
Index of the queued supersede command in the caller's
pipeline, or |
Source code in src/popoto/fields/supersession.py
SupersessionProtocol
¶
Identity-scoped belief revision and provenance traversal.
All methods are static — the protocol is a stateless coordinator over a
model's :class:~popoto.fields.validity_field.ValidityField. It is NOT a
mixin and must not be inherited by a model.
Every method takes an optional field_name (which ValidityField to
act on, auto-detected when the model declares exactly one) and an optional
pipeline (threaded straight through to the underlying EVAL).
Source code in src/popoto/fields/supersession.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | |
identity_key(subject, predicate)
staticmethod
¶
Normalize a claim identity into a 16-hex digest (plan D7).
Casefolds, strips, and collapses internal whitespace in each component,
joins them with :data:IDENTITY_SEPARATOR, and hashes the result with
blake2b(digest_size=8). Deterministic and LLM-free: semantic
normalization ("is plan the same predicate as subscription_tier?")
is a downstream opt-in, not core.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subject
|
str
|
What the claim is about, e.g. |
required |
predicate
|
str
|
Which property of the subject, e.g. |
required |
Returns:
| Type | Description |
|---|---|
str
|
16 lowercase hex characters, safe to embed in a Redis key. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either component is empty, whitespace-only, or
contains a literal |
Source code in src/popoto/fields/supersession.py
supersede(new_instance, *, identity_key, at=None, field_name=None, pipeline=None)
staticmethod
¶
Replace whichever record is currently open for identity_key.
One EVAL: closes the incumbent's interval, writes both chain links,
opens new_instance's interval, and repoints the identity's open
pointer. There is no observable state in which the incumbent is closed
but unchained, or chained but still index-visible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_instance
|
Any
|
The saved model instance carrying the new claim. |
required |
identity_key
|
Union[str, Sequence[str]]
|
Either a digest from :meth: |
required |
at
|
Optional[float]
|
Valid-time instant of the transition (epoch seconds). Defaults
to now. Transaction time ( |
None
|
field_name
|
Optional[str]
|
Name of the |
None
|
pipeline
|
Optional[Pipeline]
|
Optional Redis pipeline; the |
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The closed (superseded) record's Redis key, or |
Optional[str]
|
was no incumbent for this identity — the first claim about an |
Optional[str]
|
identity simply opens, writing no chain link. Also |
Optional[str]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValidityMemberAbsentError
|
If |
ValidityCloseBeforeStartError
|
If |
Note
at is a close-time assertion about the incumbent, never a
start-time assertion about new_instance. To set a successor's
valid-time, construct it with validity=t (plan D3).
Source code in src/popoto/fields/supersession.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
invalidate(instance, at=None, superseded_by=None, field_name=None, pipeline=None)
staticmethod
¶
Close one specific record's interval — the identity-free direct form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
The saved record to close. |
required |
at
|
Optional[float]
|
Close instant (epoch seconds). Defaults to now. |
None
|
superseded_by
|
Any
|
Optional saved instance that replaced |
None
|
field_name
|
Optional[str]
|
Name of the |
None
|
pipeline
|
Optional[Pipeline]
|
Optional Redis pipeline; the |
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The closed record's Redis key, or |
Optional[str]
|
or a |
Raises:
| Type | Description |
|---|---|
ValidityMemberAbsentError
|
If |
ValidityCloseBeforeStartError
|
If |
Note
A same-pipeline successor now works and is the recommended spelling::
pipe = popoto.get_redis().pipeline()
new.save(pipeline=pipe)
SupersessionProtocol.invalidate(
old, superseded_by=new, pipeline=pipe
)
pipe.execute()
Source code in src/popoto/fields/supersession.py
save_and_supersede(new_instance, *, identity_key, at=None, field_name=None, pipeline=None)
staticmethod
¶
Save new_instance and close the identity's incumbent, atomically.
The append-and-close shape a bitemporal store wants, as one supported call instead of a pipeline the caller assembles by hand: the successor's hash, its indexes, its open interval, the incumbent's close, both chain links, and the pointer repoint all apply in a single MULTI/EXEC, so no reader ever sees both records open or neither present.
This is also the only place a caller gets a typed error in pipeline
shape (plan D4): execute_supersede's remap cannot live on its own
pipeline branch, because redis-py raises during execute() result
parsing long after that method returned. This method owns the
execute(), so it can remap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_instance
|
Any
|
The unsaved model instance carrying the new claim. |
required |
identity_key
|
Union[str, Sequence[str]]
|
A digest from :meth: |
required |
at
|
Optional[float]
|
Valid-time instant of the transition. Defaults to now. |
None
|
field_name
|
Optional[str]
|
Name of the |
None
|
pipeline
|
Optional[Pipeline]
|
Optional caller pipeline to compose onto. When given,
nothing is executed here — see :class: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SupersedeResult
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the model declares no |
SupersedeDeclinedError
|
If |
ValidityMemberAbsentError
|
If the incumbent named by the identity pointer was hard-deleted, or the successor's save was declined. |
ValidityCloseBeforeStartError
|
If |
Source code in src/popoto/fields/supersession.py
save_and_invalidate(new_instance, *, closes, at=None, field_name=None, pipeline=None)
staticmethod
¶
Save new_instance and close closes, atomically.
The identity-free form of :meth:save_and_supersede: the incumbent is
named explicitly rather than resolved through an open-claim pointer, and
being named makes it a caller assertion — a closes that does not
exist at EXEC time raises rather than being read as "no incumbent".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_instance
|
Any
|
The unsaved successor. |
required |
closes
|
Any
|
The saved record this successor replaces. |
required |
at
|
Optional[float]
|
Valid-time instant of the transition. Defaults to now. |
None
|
field_name
|
Optional[str]
|
Name of the |
None
|
pipeline
|
Optional[Pipeline]
|
Optional caller pipeline to compose onto. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SupersedeResult
|
class: |
SupersedeResult
|
Redis key when this call closed it. |
Raises:
| Type | Description |
|---|---|
The same set as
|
meth: |
Source code in src/popoto/fields/supersession.py
superseded_by(instance, field_name=None)
staticmethod
¶
Return the instance that superseded instance, or None.
One HGET against the forward chain HASH, then a hydrate by Redis key.
Returns None at the head of a chain, for an unsaved instance, for a
model without a ValidityField, or when the link names a record that
no longer exists.
Source code in src/popoto/fields/supersession.py
supersedes(instance, field_name=None)
staticmethod
¶
Return the instance that instance superseded, or None.
The mirror of :meth:superseded_by: one HGET against the reverse
chain HASH. Returns None at the tail of a chain.
Source code in src/popoto/fields/supersession.py
chain(instance, field_name=None)
staticmethod
¶
Return the full supersession chain, oldest first, including instance.
Walks the reverse links back to the oldest ancestor, then the forward links out to the newest descendant, so the chain is recoverable from any member — not just its head or tail.
Termination is guaranteed two ways, because a chain read from a live store cannot be assumed well-formed:
- A
seenset stops a cycle. A corrupt chain terminates; it never hangs. - A link naming a record with no
valid_fromentry is treated as a chain end.ValidityField.on_deleteremoves a hard-deleted record's interval entries and its own chain fields, but a neighbor's link may still name it as a value; that dangling value ends the walk rather than yielding a phantom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
Any member of the chain. |
required |
field_name
|
Optional[str]
|
Name of the |
None
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
|
list[Any]
|
the record has no chain links, and |
list[Any]
|
|