popoto.fields.confidence_field¶
popoto.fields.confidence_field
¶
ConfidenceField — Bayesian certainty tracking with entrainment.
Maintains a Bayesian confidence score per member, updated atomically via Lua script. Precision grows with sqrt(n), so early evidence has outsized effect while established beliefs resist change.
Partitioning
When partition_by is specified, the companion hash is split into
per-partition hashes, avoiding O(N) HGETALL on the global hash:
ConfidenceField(partition_by='project')
# Hash key: $ConfidencF:{Model}:{field}:data:{project_value}
This mirrors SortedField's partition_by API. Queries on partitioned ConfidenceFields must include the partition field value(s).
Example
class Memory(Model): key = UniqueKeyField() project = KeyField() content = StringField() certainty = ConfidenceField(initial_confidence=0.5, partition_by='project')
memory = Memory.create(key="fact1", project="atlas", content="The sky is blue") ConfidenceField.update_confidence(memory, "certainty", signal=0.9) print(ConfidenceField.get_confidence(memory, "certainty"))
ConfidenceField
¶
Bases: Field
A Field that tracks Bayesian confidence metadata in a companion Redis hash.
Not a SortedField — confidence is metadata, not a ranking dimension. Stores {confidence, evidence_count, corroborations, contradictions} per member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_confidence
|
Starting confidence for new members. Default 0.5. |
required | |
partition_by
|
Optional field name or tuple of field names to partition the companion hash. When set, each unique combination of partition field values gets its own Redis hash, reducing the cost of reads from O(total_members) to O(partition_members). Mirrors SortedField's partition_by API. |
required |
Example
class Memory(Model): key = UniqueKeyField() content = StringField() certainty = ConfidenceField(initial_confidence=0.5)
memory = Memory.create(key="fact1", content="The sky is blue") ConfidenceField.update_confidence(memory, "certainty", signal=0.9) print(ConfidenceField.get_confidence(memory, "certainty"))
Partitioned example
class Memory(Model): key = UniqueKeyField() project = KeyField() content = StringField() certainty = ConfidenceField(initial_confidence=0.5, partition_by='project')
memory = Memory.create(key="fact1", project="atlas", content="...") ConfidenceField.update_confidence(memory, "certainty", signal=0.9)
Source code in src/popoto/fields/confidence_field.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |
get_data_hash_key(model_instance, field_name)
¶
Build the Redis key for the confidence companion hash.
Public API for external callers that need to perform direct Redis operations on the companion hash (e.g., bulk reads, migrations, or monitoring). Prefer the higher-level class methods (get_confidence, update_confidence) for normal operations.
When partition_by is set and a model instance is provided, appends partition field values to the key.
Pattern (unpartitioned): $ConfidencF:{Model}:{field}:data Pattern (partitioned): $ConfidencF:{Model}:{field}:data:{partition_val1}:{partition_val2}
Source code in src/popoto/fields/confidence_field.py
get_data_hash_key_from_values(model_class, field_name, **partition_values)
¶
Build the companion hash key from explicit partition values.
Public API for query paths and external callers that have partition field values but not a model instance (e.g., custom query builders, bulk operations scoped to a partition).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
The Model class. |
required | |
field_name
|
Name of the ConfidenceField. |
required | |
**partition_values
|
Mapping of partition field names to values. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
The Redis hash key. |
Raises:
| Type | Description |
|---|---|
QueryException
|
If a required partition field is missing. |
Source code in src/popoto/fields/confidence_field.py
get_old_data_hash_key(model_instance, field_name)
¶
Build the companion hash key using saved (old) partition field values.
Used during on_save/on_delete to find the old partition hash when a partition key has changed. External callers performing custom partition migrations may also need this to locate stale hash entries.
Returns:
| Type | Description |
|---|---|
|
str or None: The old hash key, or None if no saved values exist. |
Source code in src/popoto/fields/confidence_field.py
on_save(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Initialize companion hash with initial_confidence on first save.
For partitioned fields, detects partition key changes and moves the entry from the old partition hash to the new one. Also handles key migration (migrate_key=True) where on_delete preserves old confidence data on the instance for restoration here.
Source code in src/popoto/fields/confidence_field.py
on_delete(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Remove companion hash entry on delete.
When saved_redis_key is provided (key migration), preserves the raw confidence data on the model instance so on_save can restore it into the new partition hash without data loss.
Source code in src/popoto/fields/confidence_field.py
update_confidence(model_instance, field_name, signal, pipeline=None)
classmethod
¶
Update confidence for a member using Bayesian formula.
Note: Always executes immediately via Lua EVAL (not batched into pipeline) because the Lua script needs to read-modify-write atomically. The pipeline parameter is accepted for API consistency but unused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
The Model instance to update. |
required | |
field_name
|
Name of the ConfidenceField on the model. |
required | |
signal
|
Float 0-1. Values >= 0.5 corroborate, < 0.5 contradict. |
required | |
pipeline
|
Optional Redis pipeline (unused — Lua EVAL is atomic). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
The new confidence value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If field is not a ConfidenceField or model is unsaved. |
ValueError
|
If signal is not between 0 and 1. |
Source code in src/popoto/fields/confidence_field.py
get_confidence(model_instance, field_name)
classmethod
¶
Get the current confidence value for a member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
The Model instance. |
required | |
field_name
|
Name of the ConfidenceField. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
Current confidence value, or initial_confidence if no data. |
Source code in src/popoto/fields/confidence_field.py
get_confidence_data(model_instance, field_name)
classmethod
¶
Get all confidence metadata for a member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
The Model instance. |
required | |
field_name
|
Name of the ConfidenceField. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
{confidence, evidence_count, corroborations, contradictions} or defaults if no data exists. |
Source code in src/popoto/fields/confidence_field.py
get_confidence_filtered(model_class, field_name, pattern='*')
classmethod
¶
Get confidence data for members matching an HSCAN pattern.
Useful for filtered reads on unpartitioned hashes without loading all entries into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
The Model class. |
required | |
field_name
|
Name of the ConfidenceField. |
required | |
pattern
|
Redis MATCH pattern for HSCAN (default '*' matches all). |
'*'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
{member_key: {confidence, evidence_count, ...}} for matches. |
Source code in src/popoto/fields/confidence_field.py
migrate_to_partitioned(model_class, field_name, dry_run=False)
classmethod
¶
Migrate existing unpartitioned hash data into partitioned hashes.
Reads all entries from the single companion hash, loads each model instance from Redis to determine its partition field values, and writes each entry to the appropriate partitioned hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
The Model class with the ConfidenceField. |
required | |
field_name
|
Name of the ConfidenceField to migrate. |
required | |
dry_run
|
If True, report what would happen without modifying data. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Migration report with counts per partition and any errors. |
Raises:
| Type | Description |
|---|---|
ModelException
|
If the field has no partition_by configured. |
Source code in src/popoto/fields/confidence_field.py
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 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | |