popoto.recipes.memory_lifecycle¶
popoto.recipes.memory_lifecycle
¶
MemoryLifecycle — policy layer orchestrating memory tier transitions and auto-forget.
Composes existing Popoto decay primitives (DecayingSortedField, CyclicDecayField, ConfidenceField, AccessTrackerMixin) into a working → episodic → semantic lifecycle. Does not replace any existing primitive — purely a composition layer.
Architecture::
New memory created
|
v
[lifecycle.tag_new(record)] -- sets tier="episodic"
|
v
[lifecycle.tick()] -- periodic pass
├── Scan episodic tier (paginated)
├── Promote eligible records to "semantic"
├── Forget low-importance idle records
└── Log summary
Two-tier design
"episodic" -- default tier for new memories; specific events with temporal context "semantic" -- consolidated facts; decontextualized; protected from auto-forget
Working memory is approximated by CyclicDecayField rapid decay — no separate tier in v1.
Promotion criteria (episodic → semantic, ALL must hold): access_count >= PROMOTION_ACCESS_COUNT confidence >= PROMOTION_CONFIDENCE_THRESHOLD age_seconds >= PROMOTION_MIN_AGE_SECONDS
Auto-forget criteria (non-semantic records)::
(importance_score < FORGET_IMPORTANCE_FLOOR
OR (confidence < FORGET_CONFIDENCE_CEILING
AND evidence_count >= FORGET_MIN_EVIDENCE))
AND last_accessed_seconds_ago > FORGET_IDLE_SECONDS
Forgetting tombstones rather than deletes (#491). A forgotten record is
removed from the live corpus — so it is excluded from every retrieval mode by
construction, not by a filter each read path must remember — while its
fingerprint, death metadata, and archived payload are retained under
$TOMB:{Model}:*. restore() reverses the decision, retention is capped
at LIFECYCLE_TOMBSTONE_RETENTION_LIMIT, and forget_hard() remains
available for irreversible deletion.
Example::
from popoto.recipes.memory_lifecycle import MemoryLifecycle
lifecycle = MemoryLifecycle(
model_class=Memory,
importance_field="relevance", # DecayingSortedField name
)
# Tag a newly created memory
record = Memory.create(tier="episodic", content="...")
lifecycle.tag_new(record)
# Periodic lifecycle pass
lifecycle.tick()
# Inspect a record's lifecycle state
state = lifecycle.assess(record)
print(state.tier, state.promotion_eligible, state.forget_eligible)
LifecycleState
dataclass
¶
Snapshot of a record's lifecycle status.
Attributes:
| Name | Type | Description |
|---|---|---|
tier |
str
|
Current tier string ("episodic", "semantic", etc.). |
access_count |
int
|
Total confirmed read accesses (0 if no AccessTrackerMixin). |
last_accessed |
Optional[float]
|
Unix timestamp of most recent confirmed access, or None. |
importance_score |
float
|
Current importance score from the importance_field. |
promotion_eligible |
bool
|
Whether this record meets all promotion criteria. |
forget_eligible |
bool
|
Whether this record meets all auto-forget criteria. |
Source code in src/popoto/recipes/memory_lifecycle.py
Tombstone
dataclass
¶
Durable record that a memory was forgotten, and what it looked like.
Forgetting tombstones rather than deletes so an aggressive low-confidence forget policy stays reversible (Risk 6) and so each death becomes negative evidence a future write path can consult (#494).
Attributes:
| Name | Type | Description |
|---|---|---|
redis_key |
str
|
The forgotten record's Redis key. Restore handle. |
fingerprint |
str
|
ExistenceFilter fingerprint of the dead record — the identity token #494 matches new writes against. |
tier |
str
|
Tier the record held at death. |
importance_at_death |
float
|
Importance score at the moment of forgetting. |
confidence_at_death |
Optional[float]
|
ConfidenceField value at death, or None if the model carries no confidence signal. |
evidence_count |
int
|
Observations backing that confidence. |
dismissal_count |
int
|
Contradiction/dismissal count at death. |
tombstoned_at |
float
|
Unix timestamp of the forgetting. |
reason |
str
|
Free-form marker for what triggered it. |
Source code in src/popoto/recipes/memory_lifecycle.py
MemoryLifecycle
¶
Policy layer orchestrating memory tier transitions and auto-forget.
Composes existing Popoto decay primitives — does not replace them.
The two tiers are "episodic" (default for new memories) and "semantic" (consolidated, protected from auto-forget). A "working" tier can be added in v2 if benchmarks show benefit.
Class-level constants are tuning parameters for the benchmark sweep grid (see feedback_magic_numbers.md). They are NOT user-configurable init params.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
The Popoto Model class whose records to manage. |
required | |
importance_field
|
str
|
Name of a SortedFieldMixin field used as importance signal. Must be present on the model. Validated at init time. |
required |
tier_field
|
str
|
Name of the field carrying the tier partition value. Defaults to "tier". Must be a KeyField to enable filter queries. |
'tier'
|
should_promote
|
Optional[Callable]
|
Optional callable(record, lifecycle) → Optional[str]. Returns the new tier string, or None to skip. Defaults to _default_should_promote. |
None
|
should_forget
|
Optional[Callable]
|
Optional callable(record, lifecycle) → bool. Returns True to hard-delete the record. Defaults to _default_should_forget. |
None
|
partition_filters
|
Optional[dict]
|
Optional dict of extra filter kwargs passed to all query.filter() calls. Useful for multi-agent setups where each lifecycle instance manages a sub-partition (e.g. agent_id). |
None
|
Raises:
| Type | Description |
|---|---|
ModelException
|
If importance_field or tier_field is not found on model_class, or if importance_field is not a SortedFieldMixin. |
Example::
lifecycle = MemoryLifecycle(
model_class=Memory,
importance_field="relevance",
)
lifecycle.tag_new(record)
lifecycle.tick()
state = lifecycle.assess(record)
Source code in src/popoto/recipes/memory_lifecycle.py
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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 | |
tag_new(record, tier='episodic')
¶
Set the tier field on a newly created memory record.
Call this after record.save() to assign the starting tier. Idempotent — safe to call on already-tiered records (overwrites).
When the tier_field is a KeyField, the tier value is part of the Redis key identity. Changing it on an already-saved record requires migrate_key=True (key migration). tag_new() handles this automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
A saved Popoto model instance. |
required | |
tier
|
str
|
Tier string to assign. Defaults to "episodic". |
'episodic'
|
Source code in src/popoto/recipes/memory_lifecycle.py
tick()
¶
Run one lifecycle pass: promote eligible records and forget stale ones.
Loads all non-semantic records in a single non-tracking pass, evaluates promotion eligibility on the episodic subset, then evaluates forget eligibility on records that were not promoted this pass. The re-check-tier guard re-reads the authoritative tier from Redis immediately before deletion to prevent concurrent promotion races.
Forgetting tombstones rather than deletes (#491): the record leaves
the live corpus (and therefore every retrieval path) but its full
payload, fingerprint, and death metadata are archived, which is what
lets restore() undo the decision. Use forget_hard() for
irreversible deletion.
Safe to run concurrently — promotion and forgetting are idempotent at the record level. Worst case: two concurrent ticks both promote the same record (second write is a no-op) or both forget the same record (the second finds the hash gone and skips).
Returns:
| Type | Description |
|---|---|
dict
|
dict with keys:
promoted (int): Number of records promoted this tick.
forgotten (int): Number of records forgotten this tick.
tombstoned (int): Number of tombstones written this tick.
Equals |
Source code in src/popoto/recipes/memory_lifecycle.py
assess(record)
¶
Return the current lifecycle state of a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
A saved Popoto model instance. |
required |
Returns:
| Type | Description |
|---|---|
LifecycleState
|
LifecycleState with tier, access_count, last_accessed, |
LifecycleState
|
importance_score, promotion_eligible, and forget_eligible. |
Source code in src/popoto/recipes/memory_lifecycle.py
confidence_forget_eligible(record)
¶
Return True if accumulated outcome evidence alone justifies forgetting.
Requires BOTH a confidence below FORGET_CONFIDENCE_CEILING and at
least FORGET_MIN_EVIDENCE observations. Returns False whenever the
evidence cannot be read at all — absence of evidence is never evidence
for forgetting, and the kill switch (re-read on every call) forces
this path off entirely.
Source code in src/popoto/recipes/memory_lifecycle.py
tombstone(record, reason='policy')
¶
Forget a record by tombstoning it: remove from retrieval, keep the death.
The record is archived (its raw Redis hash, so restore() can bring
it back byte-for-byte) together with death metadata, then removed from
the live corpus. Removal — rather than an in-place "hidden" flag — is
what makes exclusion from every retrieval mode structural instead of
a filter each read path must remember to apply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Any
|
A saved Popoto model instance. |
required |
reason
|
str
|
Free-form marker for why it died (e.g. "policy"). |
'policy'
|
Returns:
| Type | Description |
|---|---|
Optional[Tombstone]
|
The Tombstone, or None if the record could not be archived. |
Source code in src/popoto/recipes/memory_lifecycle.py
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
tombstone_count()
¶
Return the number of retained tombstones for this model class.
Source code in src/popoto/recipes/memory_lifecycle.py
list_tombstones(limit=None)
¶
Return retained Tombstones, newest death first.
Source code in src/popoto/recipes/memory_lifecycle.py
get_tombstone(redis_key)
¶
Return the Tombstone for a redis_key, or None if not retained.
Source code in src/popoto/recipes/memory_lifecycle.py
restore(redis_key)
¶
Bring a tombstoned record back into the live corpus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_key
|
Union[str, Tombstone]
|
The tombstoned record's Redis key ( |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
The restored model instance, or None if no tombstone is retained |
Optional[Any]
|
for that key (it may have aged out). |
Source code in src/popoto/recipes/memory_lifecycle.py
purge_tombstone(redis_key)
¶
Drop a tombstone permanently. Returns True if one was removed.
Source code in src/popoto/recipes/memory_lifecycle.py
purge_all_tombstones()
¶
Drop every retained tombstone. Returns the number removed.
forget_hard(record)
¶
Delete a record outright, leaving no tombstone.
The explicit, irreversible counterpart to tombstone() — kept
available so an adopter can still purge a record entirely (e.g. a
deletion request) rather than merely retiring it from retrieval.