Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

from __future__ import annotations

from collections import Counter
from collections.abc import Iterable, Sequence
from dataclasses import dataclass, field
from functools import cached_property
from uuid import uuid4

from graphon.engine_events.base import NodeEvent
Expand Down Expand Up @@ -52,6 +54,15 @@ def from_node(cls, node: NodeProtocol) -> _ResponseSession:
def is_complete(self) -> bool:
return self.index >= len(self.template.segments)

@cached_property
def repeated_selectors(self) -> set[Selector]:
selector_counts = Counter(
tuple(segment.selector)
for segment in self.template.segments
if isinstance(segment, VariableSegment)
)
return {selector for selector, count in selector_counts.items() if count > 1}


@dataclass
class _StreamBuffers:
Expand Down Expand Up @@ -391,10 +402,18 @@ def _read_root_variable_chunks(
else:
output_node_id = source_selector_prefix
execution_id = self._get_or_create_execution_id(output_node_id)
has_stream_events = self.stream_buffers.has_events(segment.selector)
selector = tuple(segment.selector)
has_stream_events = self.stream_buffers.has_events(selector)
stream_is_closed = self.stream_buffers.is_closed(selector)
if (
stream_is_closed
and not self.stream_buffers.has_unread(selector)
and selector in session.repeated_selectors
):
self.stream_buffers.positions[selector] = 0
Comment thread
WH-2099 marked this conversation as resolved.
Resolved

while self.stream_buffers.has_unread(segment.selector):
event = self.stream_buffers.pop(segment.selector)
while self.stream_buffers.has_unread(selector):
event = self.stream_buffers.pop(selector)
if event is None:
continue

Expand All @@ -414,7 +433,7 @@ def _read_root_variable_chunks(
else:
events.append(event)

if self.stream_buffers.is_closed(segment.selector):
if stream_is_closed:
is_complete = True
elif not has_stream_events and (value := variable_pool.get(segment.selector)):
is_last_segment = session.index == len(session.template.segments) - 1
Expand Down
2 changes: 1 addition & 1 deletion src/graphon/nodes/llm/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def _yield_run_completion(
)
yield StreamChunkEvent(
selector=[self._node_id, "text"],
chunk="",
chunk=clean_text if self._polling_model_instance() is not None else "",
is_final=True,
)
yield StreamCompletedEvent(
Expand Down
20 changes: 16 additions & 4 deletions tests/engine/test_response_stream_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ def test_response_stream_filters_keep_interleaved_buffers_and_cursors_separate()


@pytest.mark.parametrize("streamed", [False, True])
def test_response_stream_filter_preserves_shared_selector_consumption(
def test_response_stream_filter_repeats_variable_text_within_one_reply(
streamed: bool,
) -> None:
source = _TestNode("source")
Expand Down Expand Up @@ -1394,14 +1394,26 @@ def test_response_stream_filter_preserves_shared_selector_consumption(
output = list(event_filter.on_event(GraphRunStartedEvent()))
if streamed:
output.extend(
event_filter.on_event(_stream_chunk("value", selector=["source", "text"]))
event_filter.on_event(
_stream_chunk("val", selector=["source", "text"], is_final=False)
)
)
assert (
"".join(
event.chunk
for event in output
if isinstance(event, NodeRunStreamChunkEvent)
)
== "val"
)
output.extend(
event_filter.on_event(_stream_chunk("ue", selector=["source", "text"]))
)

text = "".join(
event.chunk for event in output if isinstance(event, NodeRunStreamChunkEvent)
)
# Root streams currently share one read cursor per selector, unlike saved values.
assert text == ("value/!next:" if streamed else "value/value!next:value")
assert text == ("value/value!next:" if streamed else "value/value!next:value")
Comment thread
WH-2099 marked this conversation as resolved.
Resolved
assert list(event_filter.flush()) == []


Expand Down
8 changes: 8 additions & 0 deletions tests/nodes/llm/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,14 @@ def test_polling_llm_checks_until_success(monkeypatch: pytest.MonkeyPatch) -> No
"cursor": "2",
}
assert completed_event.node_run_result.outputs["text"] == "checked"
text_chunks = [
event
for event in events
if isinstance(event, StreamChunkEvent) and event.selector == ["llm", "text"]
]
assert "".join(event.chunk for event in text_chunks) == "checked"
assert not any(event.is_final for event in text_chunks[:-1])
assert text_chunks[-1].is_final


def test_polling_llm_saves_video_output_file(
Expand Down
Loading