popoto.fields.cyclic_decay_field¶
popoto.fields.cyclic_decay_field
¶
CyclicDecayField — Temporal Rhythms + Homeostatic Pressure.
Extends DecayingSortedField with two additional temporal forces computed atomically in the same Lua script:
-
Cyclical resonance: Periodic boosts following cosine curves. A record about Q1 renewals resurfaces every January.
-
Homeostatic pressure: Urgency that builds linearly over time when an item goes unresolved. Discharged by
resolve_pressure().
The effective score is: decay + cyclic_resonance + pressure
When cycles=[] and pressure_rate=0.0, behavior is identical to
DecayingSortedField (the Lua script short-circuits on nil HGET lookups).
Companion Redis hashes store per-member cycle and pressure data
$CyclicDecayF:{Model}:{field}:{partitions}:cycles— msgpack cycle tuples$CyclicDecayF:{Model}:{field}:{partitions}:pressure— msgpack pressure dict
Example
class Directive(Model): agent_id = KeyField() content = Field(type=str) relevance = CyclicDecayField( decay_rate=0.5, cycles=[(TemporalPeriod.QUARTERLY, 5.0, 0)], pressure_rate=0.1, )
top = Directive.query.filter(agent_id="agent-1").top_by_decay("relevance", n=10) directive.resolve_pressure("relevance")
CyclicDecayField
¶
Bases: DecayingSortedField
A DecayingSortedField with cyclical resonance and homeostatic pressure.
Extends the parent's power-law decay with two additional forces:
-
Cyclical resonance via
cyclesparameter: each cycle is a(period, amplitude, phase)tuple defining a cosine curve. The resonance contribution isamplitude * cos(2*pi*(now-phase)/period). -
Homeostatic pressure via
pressure_rate: linearly increasing urgency. Pressure =pressure_rate * unresolved_days. Reset by callingmodel.resolve_pressure(field_name).
When cycles=[] and pressure_rate=0.0, behavior is identical
to DecayingSortedField.
Ranking is deterministic: equal effective-scored members are ordered by member key (redis_key) ascending, byte-wise, broken inside the Lua script before top-N truncation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decay_rate
|
Controls how fast scores drop. Higher = faster decay. Default 0.5. Must be > 0. (Inherited from DecayingSortedField.) |
required | |
base_score_field
|
Name of a companion field whose value multiplies the decay curve. When None, base score is 1.0. (Inherited.) |
required | |
cycles
|
List of |
required | |
pressure_rate
|
Rate at which urgency builds per unresolved day.
Default |
required | |
partition_by
|
Partition the sorted set by key field values. Inherited from SortedFieldMixin. |
required |
Example
from popoto.fields.constants import TemporalPeriod
class Directive(Model): agent_id = KeyField() content = Field(type=str) relevance = CyclicDecayField( decay_rate=0.5, cycles=[(TemporalPeriod.QUARTERLY, 5.0, 0)], pressure_rate=0.1, )
Source code in src/popoto/fields/cyclic_decay_field.py
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 | |
export_state(model_instance, field_name, field_value, **kwargs)
classmethod
¶
Export the per-member cycles and pressure companion data.
Returns:
| Type | Description |
|---|---|
|
|
|
|
with either key omitted when that companion hash has no entry for |
|
|
this instance, or |
Source code in src/popoto/fields/cyclic_decay_field.py
import_state(model_instance, field_name, state, **kwargs)
classmethod
¶
Restore per-member cycles and pressure companion data after import.
Ordering note -- this looks wrong but is correct:
CyclicDecayField.on_save UNCONDITIONALLY overwrites the cycles
hash entry with the class-level field.cycles defaults, and seeds
pressure.last_resolved to now whenever the entry is fresh.
The transfer driver calls import_state after save(), so
these writes land on top of what on_save just clobbered and the
learned amplitudes / accumulated pressure age survive the round trip.
Inverting the order would silently discard both.
(The unconditional clobber in on_save is a known pre-existing bug
on ordinary saves -- deliberately not fixed here; see #556.)
Source code in src/popoto/fields/cyclic_decay_field.py
get_cycles_hash_key(model_instance, field_name)
¶
Build the Redis key for the cycles companion hash.
Public API for external callers that need direct Redis access to cycle data (e.g., bulk inspection, custom cycle updates, monitoring).
Pattern: $CyclicDecayF:{Model}:{field}:{partitions}:cycles
Source code in src/popoto/fields/cyclic_decay_field.py
get_pressure_hash_key(model_instance, field_name)
¶
Build the Redis key for the pressure companion hash.
Public API for external callers that need direct Redis access to pressure data (e.g., bulk pressure resets, monitoring dashboards).
Pattern: $CyclicDecayF:{Model}:{field}:{partitions}:pressure
Source code in src/popoto/fields/cyclic_decay_field.py
get_cycles_hash_key_from_parts(model_class, field_name, *partition_values)
classmethod
¶
Build cycles hash key from model class and explicit partition values.
Public API for query paths and external callers that have partition values but not a model instance.
Source code in src/popoto/fields/cyclic_decay_field.py
get_pressure_hash_key_from_parts(model_class, field_name, *partition_values)
classmethod
¶
Build pressure hash key from model class and explicit partition values.
Public API for query paths and external callers that have partition values but not a model instance.
Source code in src/popoto/fields/cyclic_decay_field.py
on_save(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Store timestamp (parent) then store cycle/pressure companion data.
On first save (no existing entry in pressure hash), writes the full pressure dict with last_resolved=now. On subsequent saves, only updates the rate — never overwrites last_resolved.
Source code in src/popoto/fields/cyclic_decay_field.py
on_delete(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Remove companion hash entries then delegate to parent.