popoto.fields.write_filter¶
popoto.fields.write_filter
¶
WriteFilterMixin — selective encoding gate for persistence.
This module provides a mixin class that gates save() calls based on a scoring function. Low-value records are silently discarded; high-value records are tagged in a priority sorted set for preferential retrieval.
Design
- compute_filter_score() is abstract (raises NotImplementedError)
- Score < min_threshold (0.1, empirically tuned 2026-04-17) -> SkipSaveException -> save() returns without persisting
- Score >= min_threshold and < priority_threshold (0.7) -> normal save
- Score >= priority_threshold (0.7) -> normal save AND ZADD to priority sorted set
Redis Key Patterns
- $WF:{ClassName}:priority — sorted set of high-value record PKs scored by filter score
Example
class Memory(WriteFilterMixin, Model): key = UniqueKeyField() content = StringField() importance = FloatField(default=0.0)
def compute_filter_score(self):
return self.importance or 0.0
memory = Memory(key="low", content="noise", importance=0.05) memory.save() # silently discarded (0.05 < 0.1)
memory = Memory(key="mid", content="useful", importance=0.5) memory.save() # persisted normally
memory = Memory(key="high", content="critical", importance=0.9) memory.save() # persisted AND added to priority set
WriteFilterMixin
¶
Mixin that gates save() based on a scoring function.
Add as a base class alongside Model
class MyModel(WriteFilterMixin, Model): def compute_filter_score(self): return self.some_score_field or 0.0
Class Attributes (resolution order):
_wf_min_threshold (default 0.1, empirically tuned 2026-04-17):
- Subclass may set it as a plain class attribute to override the
default (e.g. _wf_min_threshold = 0.5). Because Python's
__getattribute__ consults the subclass dict first, a plain
subclass attribute shadows the parent property.
- Otherwise the mixin's property returns Defaults.WF_MIN_THRESHOLD
at access time so that runtime overrides (e.g. from
tests/benchmarks/overrides.apply_overrides) take effect.
_wf_priority_threshold (default 0.7): same semantics. Reads
Defaults.WF_PRIORITY_THRESHOLD at access time.
Note: Attributes prefixed with underscore to avoid conflict with Popoto's ModelBase metaclass, which requires public attributes to be Fields.
Source code in src/popoto/fields/write_filter.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 | |
compute_filter_score()
¶
Compute the write filter score for this instance.
Must be overridden by subclasses. Return a float between 0.0 and 1.0.
Returns:
| Name | Type | Description |
|---|---|---|
float |
Score indicating record importance. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not overridden by subclass. |