Repository navigation
fix(session): truncate branch context_messages at fork prefix (#5096 Bug A) #5124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,29 @@ def _truncation_watermark_for(messages): | |
| return 0.0 | ||
|
|
||
|
|
||
| def truncate_context_for_display_keep( | ||
| context_messages: list | None, | ||
| full_messages: list | None, | ||
| keep: int, | ||
| ) -> list: | ||
| """Align model context with display prefix ``full_messages[:keep]``.""" | ||
| if keep <= 0: | ||
| return [] | ||
| ctx = context_messages if isinstance(context_messages, list) else [] | ||
| msgs = full_messages if isinstance(full_messages, list) else [] | ||
| if not ctx: | ||
| return [] | ||
| if len(ctx) == len(msgs): | ||
| return ctx[:keep] | ||
| if len(msgs) == 0: | ||
| return [] | ||
| # Context tail aligns with full display transcript; preserve leading-only rows. | ||
| prefix_len = max(0, len(ctx) - len(msgs)) | ||
| prefix = ctx[:prefix_len] | ||
| suffix = ctx[prefix_len:] | ||
| return prefix + suffix[:keep] | ||
|
Comment on lines
+86
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def retry_last(session_id: str) -> dict[str, Any]: | ||
| """Truncate the session to before the last user message, return its text. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| """Branch must not inherit full parent context_messages (#5096 A).""" | ||||||
|
|
||||||
| import copy | ||||||
|
|
||||||
| from api.session_ops import truncate_context_for_display_keep | ||||||
|
|
||||||
|
|
||||||
| def test_truncate_context_for_display_keep_drops_parent_tail(): | ||||||
| msgs = [ | ||||||
| {"role": "user", "content": "u1"}, | ||||||
| {"role": "assistant", "content": "a1"}, | ||||||
| {"role": "user", "content": "after-fork"}, | ||||||
| ] | ||||||
| # Same-length context as display — fork at keep=2 matches messages[:2] | ||||||
| ctx = copy.deepcopy(msgs) | ||||||
| out = truncate_context_for_display_keep(ctx, msgs, 2) | ||||||
| assert len(out) == 2 | ||||||
| assert out[-1]["content"] == "a1" | ||||||
| assert "after-fork" not in [m["content"] for m in out] | ||||||
|
|
||||||
|
|
||||||
| def test_truncate_context_preserves_leading_compaction_row(): | ||||||
| msgs = [ | ||||||
| {"role": "user", "content": "u1"}, | ||||||
| {"role": "assistant", "content": "a1"}, | ||||||
| ] | ||||||
| ctx = [ | ||||||
| {"role": "user", "content": "compaction-ref-only"}, | ||||||
| {"role": "user", "content": "u1"}, | ||||||
| {"role": "assistant", "content": "a1"}, | ||||||
| ] | ||||||
| out = truncate_context_for_display_keep(ctx, msgs, 2) | ||||||
| assert len(out) == 3 | ||||||
| assert out[0]["content"] == "compaction-ref-only" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
len(ctx) < len(msgs). When context_messages is shorter than the display transcript (e.g. after partial context pruning or a future compaction strategy that drops old LLM-only rows),prefix_lenbecomes 0, so the function returnsctx[:keep]treating context as front-aligned with msgs. But the code's own comment says "Context tail aligns with full display transcript", meaning the correct slice for the shorter case would bectx[len(ctx) - min(len(ctx), keep):]. As written, a fork atkeep=1with a 2-item ctx and 3-item msgs returnsctx[0]instead ofctx[-2](the entry that actually corresponds tomsgs[0]). Adding an explicit branch or at minimum a guard assertion would prevent silent mistruncation if this case is ever reachable.