-
Notifications
You must be signed in to change notification settings - Fork 564
Cache trace branches by node count #1799
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
Open
xeophon
wants to merge
1
commit into
main
Choose a base branch
from
codex/reuse-rollout-token-counts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+12
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,6 +242,9 @@ class Trace(StrictBaseModel, Generic[TaskT, StateT]): | |
| _head_index: dict = PrivateAttr(default_factory=dict) | ||
| """`(parent, msg_hash) -> node_id` for the graph builder (`graph.prepare_turn` / `commit`); | ||
| rebuilt lazily from `nodes` after deserialization.""" | ||
| _branch_cache: list[Branch] = PrivateAttr(default_factory=list) | ||
| _branch_cache_node_count: int = PrivateAttr(default=-1) | ||
| """The derived branch view, rebuilt whenever the graph grows.""" | ||
|
|
||
| @property | ||
| def reward(self) -> float: | ||
|
|
@@ -295,6 +298,10 @@ def branches(self) -> list[Branch]: | |
| """The conversation segmented into linear branches — a view over the graph: each | ||
| leaf's root→leaf path is a branch (one when linear, several under compaction or | ||
| subagents). Branching falls out of walking each leaf's parents back to its root.""" | ||
| node_count = len(self.nodes) | ||
| if self._branch_cache_node_count == node_count: | ||
| return self._branch_cache.copy() | ||
|
Member
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. wait acc why do we copy here? this is expensive, no? |
||
|
|
||
| branches: list[Branch] = [] | ||
| for i, leaf in enumerate(graph.leaves(self)): | ||
| path: list[int] = [] | ||
|
|
@@ -304,11 +311,15 @@ def branches(self) -> list[Branch]: | |
| nid = self.nodes[nid].parent | ||
| path.reverse() | ||
| branches.append(Branch(index=i, nodes=[self.nodes[n] for n in path])) | ||
| return branches | ||
| self._branch_cache = branches | ||
| self._branch_cache_node_count = node_count | ||
| return branches.copy() | ||
|
|
||
| @property | ||
| def num_branches(self) -> int: | ||
| """How many branches (1 = linear; >1 = compaction/subagents).""" | ||
| if self._branch_cache_node_count == len(self.nodes): | ||
| return len(self._branch_cache) | ||
| return len(graph.leaves(self)) | ||
|
Comment on lines
+321
to
323
Member
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. shouldnt we compute this as |
||
|
|
||
| @property | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Fresh evidence after the prior review: this returns only a shallow copy of the cached list, so callers still receive the exact cached
Branchinstances. If any post-processing mutates a branch object or its publicnodeslist, such astrace.branches[0].nodes.pop(), later reads whilelen(self.nodes)is unchanged reuse the corrupted branch and can makeprompt_len,completion_len, ortool_messageswrong; before this change each access rebuilt newBranchobjects. Cache immutable data or return defensiveBranchcopies.Useful? React with 👍 / 👎.
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.
i think this is fine. dont wanna copy for that. branches should be read-only. if users mutate, thats on them