popoto.integrations.service¶
popoto.integrations.service
¶
MemoryService -- the harness-agnostic core shared by hooks and MCP tools.
One object with four operations: :meth:~MemoryService.assemble,
:meth:~MemoryService.capture, :meth:~MemoryService.feedback, and
:meth:~MemoryService.status. Every harness adapter, and every MCP tool,
goes through it, so there is exactly one code path to Redis and exactly one
schema (:class:popoto.recipes.DefaultMemory).
Two contracts this module keeps deliberately:
It returns a context string; it never touches a message array. On the
hook path the harness places the returned string in the user turn (Claude
Code and Codex additionalContext, Hermes context, OpenClaw
appendContext), which appends after all sealed history and so preserves
the cached prefix. Message placement is the harness's decision, not this
module's. The library recipe's inject_context() appends at the tail for
the same reason; it can still be asked for the old position="system"
behavior, which invalidates a cached system prefix on every turn.
It suppresses what it already injected. Selected keys go into a
per-session set (:data:INJECTED_KEY_PREFIX) and come back as
assemble(exclude_keys=...). Injected blocks stay resident for the life of
a session, so re-retrieving the same top-k every turn -- which topically
similar consecutive prompts produce -- makes cumulative cache-read grow with
the square of turn count. Declining to re-add is append-only and therefore
free; pruning an already-sent block would cost every token behind it.
It writes through :class:~popoto.extraction.RawTurnExtractionProvider
by default. See :attr:MemoryService.extractor and issue #489.
Failures are swallowed -- a memory error must never break a user's turn --
but never silently. Each swallowed exception appends a line to the
configured log file and increments a Redis counter that
popoto-memory doctor reads back.
COUNTER_KEY_PREFIX = '$popoto_memory:counter'
module-attribute
¶
Redis key prefix for failure and activity counters. INCR is atomic,
so doctor can read these while a hook writes them.
NON_FAILURE_COUNTERS = frozenset({'evicted', 'heuristic_notice'})
module-attribute
¶
Counter names that are reports, not integration errors.
Both renderers bucket every counter that does not end in _ok under
"failures"; these two are neither successes nor failures. evicted is the
DefaultMemory data-loss report (#596) and heuristic_notice is the
one-time ingest-mode marker set by :meth:MemoryService._warn_heuristic_cost.
Subtract this set from any failure bucket rather than re-spelling the names.
PENDING_KEY_PREFIX = '$popoto_memory:pending'
module-attribute
¶
Redis key prefix for the read-hook-to-write-hook handoff.
INJECTED_KEY_PREFIX = '$popoto_memory:injected'
module-attribute
¶
Redis key prefix for the per-session set of already-injected record keys,
read back as assemble(exclude_keys=...) so a memory surfaced once is not
re-injected every turn. Separate from the pending FIFO, which feedback
consumes one turn at a time; suppression needs the session-wide union.
LAST_EVENT_KEY_PREFIX = '$popoto_memory:last'
module-attribute
¶
Redis key prefix for last-success timestamps, so a silently broken
injection is visible in doctor without reading the log.
MAX_PENDING_TURNS = 32
module-attribute
¶
Cap on queued unresolved turns per session. A harness that reads without ever firing its write event (or a crashed session) would otherwise grow the handoff list without bound.
MemoryService
¶
Shared memory core over :class:popoto.recipes.DefaultMemory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Optional[MemoryConfig]
|
Resolved :class: |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
config |
The configuration in force. |
Example::
from popoto.integrations import MemoryService
service = MemoryService()
context = service.assemble("how do we deploy?", session_id="s1")
service.capture("Deploys are blue-green with auto rollback", "s1")
Source code in src/popoto/integrations/service.py
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 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 | |
model
property
¶
The :class:popoto.recipes.DefaultMemory class.
Imported on first access. This package defines no model of its own: a second memory schema would compete with the shipped default permanently, because it would be embedded in every installed harness config.
extractor
property
¶
The extraction provider selected by POPOTO_MEMORY_INGEST.
raw (the default) is
:class:~popoto.extraction.RawTurnExtractionProvider: one verbatim
record per turn. heuristic is the sentence-splitting provider,
which issue #489 measured at 0.2078 judged accuracy against raw's
0.3636 on the same slice. Selecting it logs that cost once.
memory
property
¶
The :class:popoto.recipes.SubconsciousMemory instance.
Built lazily with this service's agent id, budgets, and extraction provider. The provider is always passed explicitly, so the recipe's heuristic default is never reached by accident.
redis
property
¶
Popoto's shared Redis or Valkey client.
assemble(query, session_id=None, turn_id=None)
¶
Read path: retrieve memories relevant to query.
Assembles through :class:~popoto.recipes.ContextAssembler on the
lexical/BM25 path, then records the selected record keys as a
pending turn so a later :meth:feedback call can report outcomes
against exactly those records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The user's prompt text, used as the |
required |
session_id
|
Optional[str]
|
Harness session identifier. Used only to scope the
pending-turn handoff; |
None
|
turn_id
|
Optional[str]
|
Harness turn identifier, when the payload carried one.
Tags the pending entry so :meth: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The formatted context block, or |
str
|
the query is empty, nothing matched, or retrieval failed. An |
str
|
empty string means the caller must emit no context key at all: |
str
|
a bare "Relevant context:" header with nothing under it is |
str
|
worse than silence. |
Source code in src/popoto/integrations/service.py
capture(text, session_id=None, importance=0.5)
¶
Write path: save an assistant turn as memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The assistant's final message for the turn. Empty or whitespace-only input writes nothing. |
required |
session_id
|
Optional[str]
|
Harness session identifier, recorded for symmetry
with :meth: |
None
|
importance
|
float
|
Base importance for the written record, feeding the
decay score. Default |
0.5
|
Returns:
| Type | Description |
|---|---|
List[str]
|
The Redis keys of the written records. One key per turn on the |
List[str]
|
default |
List[str]
|
disabled. |
Source code in src/popoto/integrations/service.py
feedback(session_id, outcome='used', turn_id=None)
¶
Report how the memories injected for a turn were used.
Claims one unresolved turn for session_id and applies
outcome to its records through
:class:~popoto.fields.observation.ObservationProtocol, which is
what drives the confidence and decay loop.
Given turn_id, claims the entry :meth:assemble staged for that
same turn; otherwise pops the oldest. A missing pending turn
degrades to a no-op, and a turn id that matches nothing reports
against nothing rather than falling back to the head of the queue.
Either way this method consumes exactly one entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Harness session identifier. |
required |
turn_id
|
Optional[str]
|
Harness turn identifier, when the payload carried
one. Must be the same value the paired :meth: |
None
|
outcome
|
str
|
One of |
'used'
|
Returns:
| Type | Description |
|---|---|
int
|
Number of records the outcome was applied to. |
Source code in src/popoto/integrations/service.py
search(query, limit=None)
¶
Discretionary search, backing the memory_search MCP tool.
Unlike :meth:assemble this returns structured records rather than
an injection block, and it does not create a pending turn: an
explicit search is not a subconscious injection and must not consume
the outcome-reporting slot of one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search text. |
required |
limit
|
Optional[int]
|
Maximum records. Defaults to the configured
|
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of |
List[Dict[str, Any]]
|
relevant first. Empty on failure. |
Source code in src/popoto/integrations/service.py
correct(key, outcome='contradicted')
¶
Apply a corrective outcome to one record by Redis key.
Backs the memory_feedback MCP tool, which is how a model marks a
retrieved memory wrong without deleting it -- the confidence field
then down-ranks it over time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The record's Redis key, as returned by :meth: |
required |
outcome
|
str
|
Outcome to apply. Default |
'contradicted'
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/popoto/integrations/service.py
status()
¶
Describe the live state of the integration.
Everything popoto-memory doctor prints and the memory_status
MCP tool returns. Never raises: each probe degrades to an error
string in its own field, because the whole point of this call is to
run when something is broken.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dict with connection, configuration, retrieval mode, record |
Dict[str, Any]
|
count, counters, and last-success timestamps. |
Source code in src/popoto/integrations/service.py
log_tail(lines=5)
¶
Return the last lines entries of the failure log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
int
|
How many trailing lines to return. Default 5. |
5
|
Returns:
| Type | Description |
|---|---|
List[str]
|
The lines, oldest first. Empty when the log does not exist. |
Source code in src/popoto/integrations/service.py
resolve_log_path()
¶
Return the effective log path without constructing a service.
Returns:
| Type | Description |
|---|---|
str
|
The absolute path swallowed errors are written to. |