adaptive_decision
zocrates.agents.common.adaptive_decision ¶
Post-turn decision engine core for Google ADK 2.8+ adaptive context compaction.
Evaluates multi-turn dialogue state against a probabilistic decision model rubric to determine if earlier turns should be compacted into a concise summary while retaining the most recent turns verbatim.
DECISION_PROMPT_INSTRUCTIONS
module-attribute
¶
DECISION_PROMPT_INSTRUCTIONS = 'Task: Decide whether `earlier_discussion` should be compacted into a concise summary to preserve context space for upcoming turns.\n\nRubric:\n- CLOSED-UNIT: Did `earlier_discussion` conclude a complete sub-topic or sub-analysis?\n- PROGRESS/PIVOT: In the latter part of `earlier_discussion` or in `latest_turn`, has the user pivoted to a different research topic or advanced to a new phase?\n- NOT STUCK: Is the conversation free from an unresolved clarification or correction loop?\n\nDecision Rule:\n- Compaction is WARRANTED if and only if ALL criteria are satisfied: earlier turns form a resolved unit, the conversation is advancing to new ground, and there is no active confusion or clarification loop.\n- Compaction is NOT WARRANTED if the user is still actively drilling down, clarifying, or troubleshooting the exact same point, or if the conversation is mid-thought.\n\nDetermine if compacting `earlier_discussion` is warranted.'
DEFAULT_ADAPTIVE_COMPACTION_DECISION_MODEL
module-attribute
¶
DEFAULT_ADAPTIVE_COMPACTION_RETENTION_TURNS
module-attribute
¶
DEFAULT_ADAPTIVE_COMPACTION_THRESHOLD
module-attribute
¶
DEFAULT_ADAPTIVE_COMPACTION_TOKEN_CEILING
module-attribute
¶
DEFAULT_ADAPTIVE_COMPACTION_TOKEN_FLOOR
module-attribute
¶
DEFAULT_LOCAL_HEALTH_URL
module-attribute
¶
OPENROUTER_DECISIONS_URL
module-attribute
¶
__all__
module-attribute
¶
__all__ = ['DECISION_PROMPT_INSTRUCTIONS', 'CompletedTurn', 'count_completed_turns_since_last_compaction', 'evaluate_adaptive_compaction_rubric', 'extract_decision_state', 'get_completed_turns', 'get_coordinator_prompt_tokens', 'is_local_model', 'probe_local_laya_health', 'should_trigger_adaptive_compaction', '_is_compaction_event', '_safe_apply_rewinds']
CompletedTurn
dataclass
¶
Represents a completed dialogue turn with user query and assistant response.
_decision_span ¶
Context manager yielding an active OpenTelemetry span or None on fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the OpenTelemetry span. |
'compaction_decision_probe'
|
Yields:
| Type | Description |
|---|---|
Any
|
Active span if tracing is available, otherwise None. |
Source code in zocrates/agents/common/adaptive_decision.py
_extract_completed_turns_from_events ¶
Group unscoped events into completed turns, extracting user queries and assistant responses.
- For research pipeline turns: extracts concise findings summary (<150 words) from
conclude_outputinRESEARCH_TURN_HISTORYmatched byinvocation_id. Raw chunk IDs, full syntheses, and bibliographies are strictly excluded. - For conversational turns: extracts coordinator direct text untruncated (thoughts excluded).
- Zero tool leakage: intermediate function calls and tool parameters are excluded.
Source code in zocrates/agents/common/adaptive_decision.py
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 | |
_format_turn ¶
Format a single turn into clean serial Markdown without headers or bullet points.
Source code in zocrates/agents/common/adaptive_decision.py
_is_compaction_event ¶
Check if event is an ADK compaction event.
_safe_apply_rewinds ¶
Safely apply rewinds to events list, failing open to original list if unneeded.
Source code in zocrates/agents/common/adaptive_decision.py
_safe_set ¶
Safely set an attribute on an active OpenTelemetry span, no-oping on error or None.
Source code in zocrates/agents/common/adaptive_decision.py
count_completed_turns_since_last_compaction ¶
Count completed dialogue turns since the latest compaction event (or start of session).
Source code in zocrates/agents/common/adaptive_decision.py
evaluate_adaptive_compaction_rubric ¶
Evaluate whether earlier_discussion warrants compaction via the decision model rubric probe.
Queries OpenRouter Decisions API or local Laya with a 10.0s timeout and a 250ms retry on HTTP 429.
Wrapped in OpenTelemetry span compaction_decision_probe (kind=EVALUATOR). Fails open to None on any error.
Source code in zocrates/agents/common/adaptive_decision.py
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 | |
extract_decision_state ¶
Extract clean serial Markdown dialogue state partitioned into earlier_discussion and latest_turn.
- Turns 1 .. N-1 are mapped to
earlier_discussion. - Turn N is mapped to
latest_turn. - Returns {"earlier_discussion": earlier_discussion, "latest_turn": latest_turn}.
Source code in zocrates/agents/common/adaptive_decision.py
extract_event_parts ¶
Extract text, thoughts, function calls, and responses from an event.
Source code in zocrates/utils/session_formatting.py
get_completed_turns ¶
Extract all completed turns from session events, respecting rewinds and isolation scopes.
Source code in zocrates/agents/common/adaptive_decision.py
get_coordinator_prompt_tokens ¶
Extract prompt token count strictly from the coordinator agent's latest unscoped event.
Filters out subagent research pipeline events (where isolation_scope is not None) and returns prompt_token_count from usage_metadata of the most recent unscoped event.
Source code in zocrates/agents/common/adaptive_decision.py
get_secure_api_key ¶
get_secure_api_key(key_name='openrouter_api_key', env_vars=('OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY'))
Load a named API key from in-memory session environment variables.
Zotero Preferences / Login Manager serves as the secure persistent vault. Backend keys are in-memory session variables (os.environ) synced from Zotero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_name
|
str
|
Entry identifier (e.g., 'openrouter_api_key', 'openai_api_key'). |
'openrouter_api_key'
|
env_vars
|
list[str] | tuple[str, ...] | None
|
Environment variable names to check. |
('OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY')
|
Returns:
| Type | Description |
|---|---|
str | None
|
The API key string or None if not found. |
Source code in zocrates/config.py
is_local_model ¶
Check if model name indicates local Laya provider dispatch.
Source code in zocrates/agents/common/adaptive_decision.py
probe_local_laya_health ¶
probe_local_laya_health(health_url=DEFAULT_LOCAL_HEALTH_URL, timeout=DEFAULT_LOCAL_HEALTH_TIMEOUT_SEC)
Fast health check probe against local Laya server.
Source code in zocrates/agents/common/adaptive_decision.py
should_trigger_adaptive_compaction ¶
Evaluate whether adaptive compaction should trigger post-turn for the given session.
Enforces:
1. Global enable toggle (enable_adaptive_compaction).
2. Minimum completed turns invariant: effective_min_turns = max(configured_min_turns, k + 1).
3. Coordinator prompt token envelope (token floor and ceiling):
- If tokens >= token_ceiling (default 100k): forces COMPRESS immediately without probing.
- If tokens < token_floor (default 30k): skips compaction without probing.
4. Fast decision model rubric probe on state (Returns COMPRESS if p >= threshold, else CONTINUE).
Fails open to CONTINUE on any probe error or timeout.
Source code in zocrates/agents/common/adaptive_decision.py
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 | |