Skip to content

fix(context): prevent orphaned tool results in MemoryFirst drain#2380

Merged
bug-ops merged 1 commit intomainfrom
2366-memoryfirst-orphan
Mar 29, 2026
Merged

fix(context): prevent orphaned tool results in MemoryFirst drain#2380
bug-ops merged 1 commit intomainfrom
2366-memoryfirst-orphan

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Mar 29, 2026

Summary

  • Extracts memory_first_keep_tail() helper in assembly.rs that extends the keep-tail window past any Role::User/ToolResult messages at the drain boundary, ensuring the first retained message is always the matching Role::Assistant/ToolUse or a plain user message
  • Fixes HTTP 400 errors from OpenAI when a tool-call turn lands at the MemoryFirst boundary (reproducible on every 5-turn session with tool use using the default crossover_turn_threshold=5)
  • Adds 4 unit tests: normal case, single tool round at boundary, two consecutive tool rounds, degenerate (no panic)

Closes #2366

Test plan

  • cargo +nightly fmt --check — clean
  • cargo clippy --workspace --features full -- -D warnings — clean
  • cargo nextest run --config-file .github/nextest.toml --workspace --features full --lib --bins — 7163 passed, 0 failed

@github-actions github-actions bot added bug Something isn't working documentation Improvements or additions to documentation rust Rust code changes core zeph-core crate size/M Medium PR (51-200 lines) and removed bug Something isn't working labels Mar 29, 2026
@bug-ops bug-ops enabled auto-merge (squash) March 29, 2026 01:09
The MemoryFirst strategy drained conversation history with a fixed
keep_tail=2, which left a Role::User/ToolResult message without its
preceding Role::Assistant/ToolUse when a tool-call turn landed at the
boundary. OpenAI rejects such sequences with HTTP 400.

Extract memory_first_keep_tail() to walk backward from the drain
boundary, extending keep_tail past any ToolResult messages until the
first retained message is the matching assistant tool-call or a plain
user message. Add four unit tests covering normal, single-round,
multi-round, and degenerate cases.
@bug-ops bug-ops force-pushed the 2366-memoryfirst-orphan branch from fd3d00d to a6a0872 Compare March 29, 2026 01:11
@github-actions github-actions bot added the bug Something isn't working label Mar 29, 2026
@bug-ops bug-ops merged commit c060b58 into main Mar 29, 2026
26 checks passed
@bug-ops bug-ops deleted the 2366-memoryfirst-orphan branch March 29, 2026 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core zeph-core crate documentation Improvements or additions to documentation rust Rust code changes size/M Medium PR (51-200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(context): MemoryFirst drain creates orphaned tool results causing OpenAI 400 errors

1 participant