Session Persistence
Session persistence helpers for the run pipeline. Only internal persistence/retry helpers live here; public session interfaces stay in higher-level modules.
prepare_compaction_model_input
prepare_compaction_model_input(
session: Session | None,
wrapper: RunContextWrapper[Any],
input_items: list[TResponseInputItem],
) -> tuple[str, ...] | None
Snapshot the model input without retaining mutable or plaintext history.
ソースコード位置: src/agents/run_internal/session_persistence.py
record_compaction_model_response
record_compaction_model_response(
session: Session | None,
wrapper: RunContextWrapper[Any],
input_digests: tuple[str, ...] | None,
response: ModelResponse,
reasoning_item_id_policy: ReasoningItemIdPolicy | None,
) -> None
Authorize automatic compaction only from the latest successful model exchange.
ソースコード位置: src/agents/run_internal/session_persistence.py
admit_pending_input
async
admit_pending_input(
*,
run_state: RunState[Any],
agent: Any,
session: Session | None,
server_conversation_tracker: OpenAIServerConversationTracker
| None,
store: bool | None,
wrapper: RunContextWrapper[Any],
) -> list[RunItem]
Admit staged RunState input into the active conversation ownership boundary.
The caller must run pending-input guardrails first. Client-managed sessions accept the input before the model call, while server-managed conversations keep it pending until a model response confirms that the server accepted the request.
ソースコード位置: src/agents/run_internal/session_persistence.py
commit_server_pending_input
commit_server_pending_input(
*,
run_state: RunState[Any],
tracker: OpenAIServerConversationTracker,
admission_items: list[InputItem],
generated_items: list[RunItem],
session_items: list[RunItem],
model_response: ModelResponse,
processed_response: ProcessedResponse | None,
current_turn: int,
) -> bool
Commit only pending-input occurrences accepted by a server-managed request.
ソースコード位置: src/agents/run_internal/session_persistence.py
resolve_nested_history_owned_session_item_refs
resolve_nested_history_owned_session_item_refs(
session_items: Sequence[RunItem],
current_input: str | Sequence[TResponseInputItem],
history_owned_items: Sequence[NestedHistoryOwnedItem],
) -> list[NestedHistoryOwnedItemRef]
Locate explicitly owned nested-history occurrences in full session history.
ソースコード位置: src/agents/run_internal/session_persistence.py
reconcile_nested_history_owned_session_item_refs
reconcile_nested_history_owned_session_item_refs(
session_items: Sequence[RunItem],
previous_refs: Sequence[NestedHistoryOwnedItemRef],
previous_input: str | Sequence[TResponseInputItem],
current_input: str | Sequence[TResponseInputItem],
history_owned_items: Sequence[NestedHistoryOwnedItem],
) -> list[NestedHistoryOwnedItemRef]
Retain surviving ownership and add provenance introduced by a history rewrite.
ソースコード位置: src/agents/run_internal/session_persistence.py
prepare_input_with_session
async
prepare_input_with_session(
input: str | list[TResponseInputItem],
session: Session | None,
session_input_callback: SessionInputCallback | None,
session_settings: SessionSettings | None = None,
*,
include_history_in_prepared_input: bool = True,
preserve_dropped_new_items: bool = False,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
wrapper: RunContextWrapper[Any] | None = None,
) -> tuple[
str | list[TResponseInputItem], list[TResponseInputItem]
]
Prepare model input from session history plus the new turn input.
Returns a tuple of:
- The prepared input that should be sent to the model after normalization and dedupe.
- The subset of items that should be appended to the session store for this turn.
The second value is intentionally not "everything returned by the callback". When a
session_input_callback reorders or filters history, we still need to persist only the
items that belong to the new turn. This function therefore compares the callback output
against deep-copied history and new-input lists, first by object identity and then by
content frequency, so retries and custom merge strategies do not accidentally re-persist
old history as fresh input.
ソースコード位置: src/agents/run_internal/session_persistence.py
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 | |
persist_session_items_for_guardrail_trip
async
persist_session_items_for_guardrail_trip(
session: Session | None,
server_conversation_tracker: OpenAIServerConversationTracker
| None,
session_input_items_for_persistence: list[
TResponseInputItem
]
| None,
original_user_input: str
| list[TResponseInputItem]
| None,
run_state: RunState | None,
store: bool | None = None,
wrapper: RunContextWrapper[Any] | None = None,
) -> list[TResponseInputItem] | None
Persist input items when a guardrail tripwire is triggered.
ソースコード位置: src/agents/run_internal/session_persistence.py
session_items_for_turn
session_items_for_turn(
turn_result: SingleStepResult,
) -> list[RunItem]
Return the items to persist for a turn, preferring session_step_items when set.
ソースコード位置: src/agents/run_internal/session_persistence.py
resumed_turn_items
resumed_turn_items(
turn_result: SingleStepResult,
session_items: list[RunItem],
run_state: RunState,
) -> tuple[list[RunItem], list[RunItem], list[RunItem]]
Order this attempt's pending outputs and preserve previously published history.
ソースコード位置: src/agents/run_internal/session_persistence.py
update_run_state_after_resume
update_run_state_after_resume(
run_state: RunState,
*,
turn_result: SingleStepResult,
generated_items: list[RunItem],
session_items: list[RunItem] | None = None,
) -> None
Update run state fields after resolving an interruption.
ソースコード位置: src/agents/run_internal/session_persistence.py
save_result_to_session
async
save_result_to_session(
session: Session | None,
original_input: str | list[TResponseInputItem],
new_items: list[RunItem],
run_state: RunState | None = None,
*,
response_id: str | None = None,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
store: bool | None = None,
wrapper: RunContextWrapper[Any] | None = None,
resumed_write_state: RunState | None = None,
settling_held_batch: bool = False,
) -> int
Persist a turn to the session store, keeping track of what was already saved so retries during streaming do not duplicate tool outputs or inputs.
settling_held_batch marks the calls that carry a withheld batch through
original_input; only those look for local tool outputs in that slot, because on
an ordinary save the same slot holds the caller's own input. Those calls also count
the batch items this append actually wrote, which is not the batch's raw length: a
resolved turn re-delivers the outputs the batch already folded in and they dedup
away here.
戻り値:
| タイプ | デスクリプション |
|---|---|
int
|
The number of new run items persisted for this call, plus the settled batch |
int
|
items when |
ソースコード位置: src/agents/run_internal/session_persistence.py
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 | |
save_resumed_turn_items
async
save_resumed_turn_items(
*,
session: Session | None,
items: list[RunItem],
persisted_count: int,
response_id: str | None,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
store: bool | None = None,
wrapper: RunContextWrapper[Any] | None = None,
run_state: RunState | None = None,
held_write: _PendingSessionWrite | None = None,
claim_held: bool = False,
handoff_input_filtered: bool = False,
filtered_context_items: Sequence[RunItem] | None = None,
) -> int
Persist resumed turn items and return the updated persisted count.
held_write carries a claimed held batch (see take_held_session_write) into
the same append as the resolved turn's items, ahead of them. One ordered write
keeps the interrupted function_call before its output and lets the whole batch
register as the one pending append with digest-based crash recovery; settling the
batch separately would either trip the single-slot rule or advance the persisted
count and slice the resolved items out of their own save.
ソースコード位置: src/agents/run_internal/session_persistence.py
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 | |
resumed_response_store
resumed_response_store(
run_state: RunState | None,
response_id: str | None,
store: bool | None,
) -> bool | None
Resolve response-owned storage before settlement or redaction claims its batch.
ソースコード位置: src/agents/run_internal/session_persistence.py
persist_resumed_turn
async
persist_resumed_turn(
*,
run_state: RunState | None,
session: Session | None,
items: list[RunItem],
persisted_count: int,
response_id: str | None,
defer_settlement: bool = False,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
store: bool | None = None,
wrapper: RunContextWrapper[Any] | None = None,
handoff_input_filtered: bool = False,
filtered_context_items: Sequence[RunItem] | None = None,
current_response_items: Sequence[RunItem] = (),
) -> int
Fold, defer, or settle a resumed turn's authoritative Session view.
Detached turns retain the view for reattachment. A gated re-interruption keeps deferring its write. Every other attached exit settles through the canonical append path, even when a filter emptied the turn: earlier accepted history can still remain in the held batch.
ソースコード位置: src/agents/run_internal/session_persistence.py
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 | |
held_session_items_for_save
held_session_items_for_save(
pending: _PendingSessionWrite | None,
run_state: RunState | None,
items: Sequence[RunItem],
*,
handoff_input_filtered: bool = False,
filtered_context_items: Sequence[RunItem] | None = None,
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
) -> list[TResponseInputItem]
Reconcile only the current response; earlier held turns are already accepted.
The park records the response boundary before any approval executes. It therefore covers rejected approvals and outputs completed before the park as well as resumed tool outputs. A guarded final sweep replaces this suffix, not the whole record.
ソースコード位置: src/agents/run_internal/session_persistence.py
defer_interrupted_session_write
defer_interrupted_session_write(
run_state: RunState,
session: Session | None,
*,
run_items: Sequence[RunItem],
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
response_id: str | None = None,
store: bool | None = None,
run_items_are_the_session_view: bool = False,
handoff_input_filtered: bool = False,
filtered_context_items: Sequence[RunItem] | None = None,
) -> None
Register the interruption's withheld batch as a held pending Session write.
Registering is not writing: this touches only the checkpoint, never the Session, so the output-guardrail persistence gate stays intact. The batch settles, extends or is discarded only at a gate-legal point of a later resume. A standing held record is replaced by the superset of both batches, so a repeated park (a partial approval interrupting again) keeps one canonical batch. A standing record that is not held means a resumed append is mid-flight, which the existing single-slot rule treats as a caller bug.
Items are converted and deduplicated with the same helpers the real save uses, and
the count is taken over the converted items: approval placeholders drop out in
conversion, so counting the raw run items would corrupt the persisted count. A
detached re-park has no Session and takes its session_id from the standing
declaration.
The batch carries only the withheld response: run input is never registered here, because the gate withholds model output, not the user's accepted input, and a tripwire discards the batch without inspecting it.
ソースコード位置: src/agents/run_internal/session_persistence.py
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 | |
extend_held_session_write
extend_held_session_write(
run_state: RunState | None,
*,
run_items: Sequence[RunItem],
reasoning_item_id_policy: ReasoningItemIdPolicy
| None = None,
response_id: str | None = None,
store: bool | None = None,
run_items_are_the_session_view: bool = False,
handoff_input_filtered: bool = False,
filtered_context_items: Sequence[RunItem] | None = None,
) -> None
Fold a detached exit's resolved items into the standing held batch.
With no Session attached the resolved turn's save is a no-op, so the executed tool output exists only in this process; folding it into the held batch lets the reattaching resume settle call and output together. Does nothing when no held batch stands. The fold converts under the batch's registration policy, not the caller's: a detached run cannot see the original backend, and a server reasoning id stripped here could not be restored at the settle.
ソースコード位置: src/agents/run_internal/session_persistence.py
discard_held_current_response
discard_held_current_response(
run_state: RunState | None,
) -> None
Remove blocked current-response items while retaining accepted prior turns.
ソースコード位置: src/agents/run_internal/session_persistence.py
take_held_session_write
take_held_session_write(
run_state: RunState | None,
) -> _PendingSessionWrite | None
Claim the withheld record, retaining its response boundary until settlement.
ソースコード位置: src/agents/run_internal/session_persistence.py
resume_pending_session_write
async
resume_pending_session_write(
run_state: RunState,
session: Session | None,
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None
Settle a resumed output batch before allowing further model work.
The application must supply the original backend and serialize access to its history, including independently restored RunState copies. Session has no distributed compare-and-swap or backend identity contract. A changed tail is not repaired or searched for similar items.
ソースコード位置: src/agents/run_internal/session_persistence.py
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 | |
rewind_session_items
async
rewind_session_items(
session: Session | None,
items: Sequence[TResponseInputItem],
server_tracker: OpenAIServerConversationTracker
| None = None,
*,
wrapper: RunContextWrapper[Any] | None = None,
) -> None
Best-effort helper to roll back items recently persisted to a session when a conversation retry is needed, so we do not accumulate duplicate inputs on lock errors.
ソースコード位置: src/agents/run_internal/session_persistence.py
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 | |
wait_for_session_cleanup
async
wait_for_session_cleanup(
session: Session | None,
serialized_targets: Sequence[str],
*,
max_attempts: int = 5,
ignore_ids_for_matching: bool = False,
wrapper: RunContextWrapper[Any] | None = None,
) -> None
Confirm that rewound items are no longer present in the session tail so the store stays consistent before the next retry attempt begins.