Skip to content

Commit cb1e81a

Browse files
fix: buffer streaming text to avoid per-token log lines in GitHub Actions (#946)
Each text delta from the harness was printed individually with flush, creating a separate log line per token. Now text is buffered and flushed as complete lines at block boundaries.
1 parent a365bf5 commit cb1e81a

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

.github/scripts/python/harness_review.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,21 @@ def print_stream(http_response):
9797
tool_input = ""
9898
tool_start = 0.0
9999
in_group = False
100-
had_text = False
100+
text_buffer = ""
101101

102102
def close_group():
103103
nonlocal in_group
104104
if in_group:
105105
print("::endgroup::", flush=True)
106106
in_group = False
107107

108+
def flush_text():
109+
nonlocal text_buffer
110+
if text_buffer:
111+
for line in text_buffer.splitlines():
112+
print(f"{DIM}{line}{RESET}", flush=True)
113+
text_buffer = ""
114+
108115
for event_type, payload in parse_events(http_response):
109116

110117
if event_type == "contentBlockStart":
@@ -119,13 +126,12 @@ def close_group():
119126
delta = payload.get("delta", {})
120127
if "text" in delta:
121128
close_group()
122-
print(flush=True)
123-
print(f"{DIM}{delta['text']}{RESET}", end="", flush=True)
124-
had_text = True
129+
text_buffer += delta["text"]
125130
if "toolUse" in delta:
126131
tool_input += delta["toolUse"].get("input", "")
127132

128133
elif event_type == "contentBlockStop":
134+
flush_text()
129135
if tool_name:
130136
elapsed = time.time() - tool_start
131137
try:
@@ -134,9 +140,6 @@ def close_group():
134140
parsed = tool_input
135141

136142
close_group()
137-
if had_text:
138-
print("\n", flush=True)
139-
had_text = False
140143

141144
cmd = parsed.get("command") if isinstance(parsed, dict) else None
142145
header = f"{CYAN}[{iteration}]{RESET} {YELLOW}{tool_name}{RESET} {DIM}({elapsed:.1f}s){RESET}"
@@ -155,6 +158,7 @@ def close_group():
155158
tool_input = ""
156159

157160
elif event_type == "messageStop":
161+
flush_text()
158162
close_group()
159163
if payload.get("stopReason") == "end_turn":
160164
total = time.time() - start_time

0 commit comments

Comments
 (0)