Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

fix(antigravity): Buffer sequential thoughts and text tokens to prevent token-by-token streaming (#157)

anj-s75d80d437ffb
diff --git a/python/antigravity/harness_server.py b/python/antigravity/harness_server.py
index ac0d875..baff180 100644
--- a/python/antigravity/harness_server.py
+++ b/python/antigravity/harness_server.py
@@ -224,29 +224,58 @@ class AntigravityHarnessServiceServicer(ax_pb2_grpc.HarnessServiceServicer):
print(f"[gRPC] Running chat query: {latest_query_text}")
response = await conversation.chat(latest_query_text)
+ # To avoid streaming individual tokens inside TextContent messages (which is not
+ # supported by the Interactions proto/TextContent specifications), we buffer
+ # contiguous blocks of text and thought chunks, yielding them only when the
+ # contiguous block ends or a different chunk type is received.
+ text_chunks = []
+ thought_chunks = []
+
+ def flush_text():
+ if not text_chunks:
+ return None
+ msg = ax_pb2.Message(
+ role="assistant",
+ content=content_pb2.Content(text=content_pb2.TextContent(text="".join(text_chunks)))
+ )
+ text_chunks.clear()
+ return ax_pb2.HarnessResponse(
+ conversation_id=request.conversation_id,
+ outputs=ax_pb2.HarnessOutputs(messages=[msg])
+ )
+
+ def flush_thought():
+ if not thought_chunks:
+ return None
+ summary = [
+ content_pb2.ThoughtSummaryContent(text=content_pb2.TextContent(text="".join(thought_chunks)))
+ ]
+ thought_chunks.clear()
+ msg = ax_pb2.Message(
+ role="model",
+ content=content_pb2.Content(thought=content_pb2.ThoughtContent(summary=summary))
+ )
+ return ax_pb2.HarnessResponse(
+ conversation_id=request.conversation_id,
+ outputs=ax_pb2.HarnessOutputs(messages=[msg])
+ )
+
async for chunk in response.chunks:
if isinstance(chunk, Text):
- msg = ax_pb2.Message(
- role="assistant",
- content=content_pb2.Content(text=content_pb2.TextContent(text=chunk.text))
- )
- yield ax_pb2.HarnessResponse(
- conversation_id=request.conversation_id,
- outputs=ax_pb2.HarnessOutputs(messages=[msg])
- )
+ if (resp := flush_thought()):
+ yield resp
+ text_chunks.append(chunk.text)
elif isinstance(chunk, Thought):
- summary = [
- content_pb2.ThoughtSummaryContent(text=content_pb2.TextContent(text=chunk.text))
- ]
- msg = ax_pb2.Message(
- role="model",
- content=content_pb2.Content(thought=content_pb2.ThoughtContent(summary=summary))
- )
- yield ax_pb2.HarnessResponse(
- conversation_id=request.conversation_id,
- outputs=ax_pb2.HarnessOutputs(messages=[msg])
- )
+ if (resp := flush_text()):
+ yield resp
+ thought_chunks.append(chunk.text)
elif isinstance(chunk, ToolCall):
+ # Flush all pending text/thought buffers before dispatching the tool call
+ if (resp := flush_text()):
+ yield resp
+ if (resp := flush_thought()):
+ yield resp
+
struct_args = Struct()
struct_args.update(chunk.args)
@@ -265,6 +294,12 @@ class AntigravityHarnessServiceServicer(ax_pb2_grpc.HarnessServiceServicer):
conversation_id=request.conversation_id,
outputs=ax_pb2.HarnessOutputs(messages=[msg])
)
+
+ # Flush any remaining text/thought buffers after the generator loop ends
+ if (resp := flush_text()):
+ yield resp
+ if (resp := flush_thought()):
+ yield resp
# Yield completion end frame
yield ax_pb2.HarnessResponse(
diff --git a/python/antigravity/harness_server_test.py b/python/antigravity/harness_server_test.py
index f7befa7..29afcf0 100644
--- a/python/antigravity/harness_server_test.py
+++ b/python/antigravity/harness_server_test.py
@@ -323,3 +323,86 @@ def test_grpc_connect_programmatic_credentials(monkeypatch):
asyncio.run(_run())
+def test_grpc_connect_buffering(mock_config, monkeypatch):
+ async def _run():
+ server = grpc.aio.server()
+ servicer = AntigravityHarnessServiceServicer()
+ ax_pb2_grpc.add_HarnessServiceServicer_to_server(servicer, server)
+ port = server.add_insecure_port("localhost:0")
+ await server.start()
+
+ addr = f"localhost:{port}"
+ async with grpc.aio.insecure_channel(addr) as channel:
+ stub = ax_pb2_grpc.HarnessServiceStub(channel)
+
+ class MockConversation:
+ def __init__(self):
+ self._steps = []
+ async def chat(self, text):
+ class MockResponse:
+ def __init__(self):
+ self.chunks = self._chunk_generator()
+ async def _chunk_generator(self):
+ from google.antigravity.types import Text, Thought, ToolCall
+ yield Thought(text="Think1", step_index=0)
+ yield Thought(text=" Think2", step_index=0)
+ yield ToolCall(name="tool1", args={}, id="call1")
+ yield Text(text="Hello", step_index=0)
+ yield Text(text=" human", step_index=0)
+ return MockResponse()
+
+ class MockAgent:
+ def __init__(self, config):
+ self.conversation = MockConversation()
+ async def __aenter__(self):
+ return self
+ async def __aexit__(self, exc_type, exc, tb):
+ pass
+ monkeypatch.setattr("python.antigravity.harness_server.Agent", MockAgent)
+
+ start_payload = ax_pb2.HarnessStart(
+ messages=[
+ ax_pb2.Message(role="user", content=content_pb2.Content(text=content_pb2.TextContent(text="Hi")))
+ ]
+ )
+ req = ax_pb2.HarnessRequest(
+ conversation_id="conv-test-buffer",
+ harness_id="antigravity",
+ start=start_payload
+ )
+
+ async def request_iter():
+ yield req
+
+ responses = []
+ async for resp in stub.Connect(request_iter()):
+ responses.append(resp)
+
+ # Responses should be:
+ # 1. Thought ("Think1 Think2") - flushed when ToolCall is encountered
+ # 2. ToolCall ("tool1") - processed immediately
+ # 3. Text ("Hello human") - flushed at the end
+ # 4. End frame
+ assert len(responses) == 4
+
+ # Assert 1st response: Thought summary text is "Think1 Think2"
+ assert responses[0].outputs.messages[0].content.WhichOneof('type') == 'thought'
+ assert responses[0].outputs.messages[0].content.thought.summary[0].text.text == "Think1 Think2"
+
+ # Assert 2nd response: ToolCall name is "tool1"
+ assert responses[1].outputs.messages[0].content.WhichOneof('type') == 'tool_call'
+ assert responses[1].outputs.messages[0].content.tool_call.function_call.name == "tool1"
+
+ # Assert 3rd response: Text content is "Hello human"
+ assert responses[2].outputs.messages[0].content.WhichOneof('type') == 'text'
+ assert responses[2].outputs.messages[0].content.text.text == "Hello human"
+
+ # Assert 4th response: Completion end frame
+ assert responses[3].WhichOneof('type') == 'end'
+ assert responses[3].end.state == ax_pb2.STATE_COMPLETED
+
+ await server.stop(0)
+
+ asyncio.run(_run())
+
+