Commit 39b07b1
fix: persist user input items to state.messages across callModel invocations
User input items (role: 'user') were never written to state.messages.
The two existing saveStateSafely write sites only persist response.output
and toolResults. Within a single callModel invocation this is masked
because makeFollowupRequest reconstructs input from the in-memory
this.resolvedRequest.input. But when a new callModel resumes from
persisted state, the loaded state.messages contains zero user items —
prior user turns are silently dropped.
This causes two problems:
1. cache_control prompt caching is defeated at every user-message
boundary (cachedTokens = 0 on first request of each new message)
2. Conversation fidelity loss — the model never sees prior user turns
Fix: In initStream(), after request resolution, persist fresh user input
items to state.messages via saveStateSafely before the API call. Both
code paths (loaded history and no history) now capture freshItemsForState
and write it to state.
Co-Authored-By: Ben Heidorn <ben.heidorn@openrouter.ai>1 parent d8a8f65 commit 39b07b1
3 files changed
Lines changed: 373 additions & 0 deletions
File tree
- .changeset
- packages/agent
- src/lib
- tests/unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| |||
1498 | 1499 | | |
1499 | 1500 | | |
1500 | 1501 | | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
1501 | 1509 | | |
1502 | 1510 | | |
1503 | 1511 | | |
| |||
1527 | 1535 | | |
1528 | 1536 | | |
1529 | 1537 | | |
| 1538 | + | |
| 1539 | + | |
1530 | 1540 | | |
1531 | 1541 | | |
1532 | 1542 | | |
| |||
1548 | 1558 | | |
1549 | 1559 | | |
1550 | 1560 | | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
1551 | 1578 | | |
1552 | 1579 | | |
1553 | 1580 | | |
| |||
0 commit comments