Make NodeRunStreamChunkEvent immutable

Aperta
#438 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
50/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start with the NodeRunStreamChunkEvent definition in the graphon engine_events node module, then read how ResponseStreamFilter buffers and forwards events in src/graphon/engine/filter/builtin/response_stream/template_stream.py. Run the reproduction script against tests/engine/test_response_stream_filter.py helpers to confirm the shared-reference checkpoint. Done when assigning to event.chunk fails, the downstream copy pattern still works, and the existing response-filter tests pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

project#graphon

AI disclosure — I discussed this behavior with Codex (gpt-6.2-astra), which verified the example and helped draft this report. I am responsible for the report.

Description

ResponseStreamFilter can yield a NodeRunStreamChunkEvent while keeping a reference to that same object in its root stream buffer. A later filter can assign a new string to event.chunk. The next checkpoint of the earlier filter then contains the downstream edit. Python strings are immutable, but the event allows its chunk field to be reassigned. Buffer insertion · Forwarding the event · Serializing the buffer

Minimal reproduction

Run from the Graphon repository root. The example uses the existing response-filter test graph and one non-final source chunk. EscapeText is a custom filter added for this reproduction. It consumes the same public filter API a host can use.

PYTHONPATH=.:src uv run python - <<'PY'
import json
from html import escape

from graphon.engine.filter import ResponseStreamFilter, filter_engine_events
from graphon.engine_events.graph import GraphRunStartedEvent
from graphon.engine_events.node import NodeRunStreamChunkEvent
from tests.engine.test_response_stream_filter import (
    _context, _stream_chunk, _variable_response_graph,
)

class EscapeText:
    def initialize(self, context):
        pass

    def on_event(self, event):
        if isinstance(event, NodeRunStreamChunkEvent):
            event.chunk = escape(event.chunk)
        return [event]

    def flush(self):
        return []

selector = ["source", "text"]
response = ResponseStreamFilter()
events = filter_engine_events(
    [GraphRunStartedEvent(), _stream_chunk("&", selector=selector, is_final=False)],
    context=_context(_variable_response_graph(selector=selector)),
    filters=[response, EscapeText()],
)
print("display:", [e.chunk for e in events if isinstance(e, NodeRunStreamChunkEvent)])
buffer = json.loads(response.dumps())["stream_buffers"][0]
print("checkpoint:", buffer["events"][0]["chunk"])
PY

Expected and actual behavior

The source emits &. A display filter should be able to produce &amp; while the upstream response filter retains the & it observed. For a control, change just the assignment in EscapeText.on_event to event = event.model_copy(update={"chunk": escape(event.chunk)}).

Downstream transformation Displayed chunk Chunk in the upstream checkpoint
Assign to event.chunk &amp; &amp;
Return an updated copy &amp; &

Verified both rows on Python 3.12.12 at main (b79228b) and on #395 (d4032bf). The checkpoint example works with a single reference on main. Replaying a reference makes the same effect visible in the reply on #395: for source text &a and template {{#llm.text#}}/{{#llm.text#}}, the custom in-place filter produced &amp;a/&amp;amp;a. Returning a copy produced &amp;a/&amp;a.

Proposed change

I propose making NodeRunStreamChunkEvent immutable from construction. A filter can forward the event it received or return a new event when it transforms the text, for example event.model_copy(update={"chunk": escape(event.chunk)}). An attempt to assign to event.chunk should fail at the point of assignment.

With this contract, ResponseStreamFilter can retain the source event for checkpointing and replay. In the example above, the downstream consumer receives &amp; and the upstream checkpoint still contains &. This proposal can be handled independently of #395.

Lingua principale
Python
Stelle
90
Fork
47
Merge medio
3g 23h
PR unite (30g)
19

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di langgenius/graphon

Tutte le issue di langgenius/graphon

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.