File size: 12,170 Bytes
0d0f0f9 b358e9c 0d0f0f9 b358e9c fb93f5e b358e9c 5719aaa 71bd683 5719aaa b358e9c 18ddd73 90a0517 fb93f5e 90a0517 fb93f5e 90a0517 fb93f5e 90a0517 fb93f5e 90a0517 fb93f5e 90a0517 fb93f5e 90a0517 b358e9c 71bd683 b358e9c 37cc038 df85ebc 37cc038 fb93f5e 37cc038 fb93f5e 42f27f4 fb93f5e 42f27f4 fb93f5e 42f27f4 fb93f5e 42f27f4 423a525 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | from free_claude_code.messaging.rendering.telegram_markdown import (
escape_md_v2,
escape_md_v2_code,
mdv2_bold,
mdv2_code_inline,
render_markdown_to_mdv2,
)
from free_claude_code.messaging.transcript import RenderCtx, TranscriptBuffer
from free_claude_code.messaging.transcript.renderer import render_segments
from free_claude_code.messaging.transcript.segments import Segment, SubagentSegment
from free_claude_code.messaging.transcript.subagents import SubagentState
def _ctx() -> RenderCtx:
return RenderCtx(
bold=mdv2_bold,
code_inline=mdv2_code_inline,
escape_code=escape_md_v2_code,
escape_text=escape_md_v2,
render_markdown=render_markdown_to_mdv2,
thinking_tail_max=1000,
tool_input_tail_max=1200,
tool_output_tail_max=1600,
text_tail_max=2000,
)
def test_transcript_order_thinking_tool_text():
t = TranscriptBuffer()
t.apply({"type": "thinking_chunk", "text": "think1"})
t.apply({"type": "tool_use", "id": "tool_1", "name": "ls", "input": {"path": "."}})
t.apply({"type": "text_chunk", "text": "done"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert out.find("think1") < out.find("Tool call:") < out.find("done")
def test_transcript_can_hide_tool_results():
t = TranscriptBuffer(show_tool_results=False)
t.apply({"type": "tool_use", "id": "tool_1", "name": "ls", "input": {"path": "."}})
t.apply({"type": "tool_result", "tool_use_id": "tool_1", "content": "secret"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert "Tool call:" in out
assert "Tool result:" not in out
assert "secret" not in out
def test_transcript_subagent_suppresses_thinking_and_text_inside():
t = TranscriptBuffer()
# Enter subagent context (Task tool call).
t.apply(
{
"type": "tool_use",
"id": "task_1",
"name": "Task",
"input": {"description": "Fix bug"},
}
)
# These should be suppressed while inside subagent context.
t.apply({"type": "thinking_delta", "index": -1, "text": "secret"})
t.apply({"type": "text_chunk", "text": "visible?"})
# Tool activity should still show.
t.apply({"type": "tool_use", "id": "tool_2", "name": "ls", "input": {"path": "."}})
t.apply({"type": "tool_result", "tool_use_id": "tool_2", "content": "x"})
# Close subagent context (Task tool result).
t.apply({"type": "tool_result", "tool_use_id": "task_1", "content": "done"})
# Now text should show again.
t.apply({"type": "text_chunk", "text": "after"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert "Subagent:" in out
assert "secret" not in out
assert "visible?" not in out
# Only the current tool call should be shown (not the full history).
assert out.count("Tool call:") == 1
assert "\n π " in out or out.startswith(" π ") or " π " in out
assert "Tools used:" in out
assert "Tool calls:" in out
assert "after" in out
def test_transcript_subagent_closes_on_whitespace_tool_ids():
t = TranscriptBuffer()
# Provider emitted a Task tool_use id with leading whitespace.
t.apply(
{
"type": "tool_use",
"id": " functions.Task:0",
"name": "Task",
"input": {"description": "Outer"},
}
)
# Task completes, but tool_result references a trimmed id (or vice versa).
t.apply(
{"type": "tool_result", "tool_use_id": "functions.Task:0", "content": "done"}
)
# Next Task should be top-level, not nested under the previous subagent.
t.apply(
{
"type": "tool_use",
"id": "functions.Task:1",
"name": "Task",
"input": {"description": "Next"},
}
)
out = t.render(_ctx(), limit_chars=3900, status=None)
assert out.count("Subagent:") == 2
# If nesting is incorrect, the second subagent line will be indented under the first.
assert "\n π€ *Subagent:* `Next`" not in out
def test_transcript_subagent_closes_on_task_result_id_suffix_match():
t = TranscriptBuffer()
t.apply(
{
"type": "tool_use",
"id": "task_1",
"name": "Task",
"input": {"description": "Outer"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "task_1_result", "content": "done"})
t.apply(
{
"type": "tool_use",
"id": "task_2",
"name": "Task",
"input": {"description": "Next"},
}
)
out = t.render(_ctx(), limit_chars=3900, status=None)
assert out.count("Subagent:") == 2
assert "\n π€ *Subagent:* `Next`" not in out
def test_transcript_unmatched_non_task_tool_result_does_not_pop_subagent():
state = SubagentState()
state.push("task_1", SubagentSegment("Outer"))
assert not state.close_for_tool_result("totally_unrelated", tool_name=None)
assert state.open_ids == ("task_1",)
def test_transcript_sequential_tasks_mismatched_results_no_depth_drift():
t = TranscriptBuffer()
t.apply(
{
"type": "tool_use",
"id": "task_1",
"name": "Task",
"input": {"description": "A"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "task_1_result", "content": "done"})
t.apply(
{
"type": "tool_use",
"id": "task_2",
"name": "Task",
"input": {"description": "B"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "task_2_result", "content": "done"})
t.apply(
{
"type": "tool_use",
"id": "task_3",
"name": "Task",
"input": {"description": "C"},
}
)
t.apply({"type": "text_chunk", "text": "still hidden inside task three"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert "π€ *Subagent:* `A`\n π€ *Subagent:* `B`" not in out
assert "\n π€ *Subagent:* `C`" not in out
assert "still hidden inside task three" not in out
def test_transcript_synthetic_task_start_closes_on_functions_task_result_id():
t = TranscriptBuffer()
t.apply(
{
"type": "tool_use_start",
"index": 0,
"id": "",
"name": "Task",
"input": {"description": "Outer"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "functions.Task:0", "content": "x"})
t.apply(
{
"type": "tool_use_start",
"index": 1,
"id": "",
"name": "Task",
"input": {"description": "Next"},
}
)
out = t.render(_ctx(), limit_chars=3900, status=None)
assert out.count("Subagent:") == 2
assert "\n π€ *Subagent:* `Next`" not in out
def test_transcript_synthetic_task_not_closed_by_unknown_non_task_result_id():
t = TranscriptBuffer()
t.apply(
{
"type": "tool_use_start",
"index": 0,
"id": "",
"name": "Task",
"input": {"description": "Outer"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "call_deadbeef", "content": "x"})
t.apply({"type": "text_chunk", "text": "hidden while synthetic task is open"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert "hidden while synthetic task is open" not in out
def test_transcript_overlapping_tasks_are_flat_not_nested():
t = TranscriptBuffer()
t.apply(
{
"type": "tool_use",
"id": "task_a",
"name": "Task",
"input": {"description": "A"},
}
)
t.apply(
{
"type": "tool_use",
"id": "task_b",
"name": "Task",
"input": {"description": "B"},
}
)
t.apply({"type": "tool_result", "tool_use_id": "task_b", "content": "done"})
t.apply({"type": "tool_result", "tool_use_id": "task_a", "content": "done"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert "π€ *Subagent:* `A`" in out
assert "π€ *Subagent:* `B`" in out
assert out.find("π€ *Subagent:* `A`") < out.find("π€ *Subagent:* `B`")
assert "\n π€ *Subagent:* `B`" not in out
def test_transcript_truncates_by_dropping_oldest_segments():
t = TranscriptBuffer()
# Create many segments by opening/closing distinct text blocks.
for i in range(60):
t.apply({"type": "text_start", "index": i})
t.apply(
{"type": "text_delta", "index": i, "text": f"segment_{i} " + ("x" * 120)}
)
t.apply({"type": "block_stop", "index": i})
out = t.render(_ctx(), limit_chars=600, status="status")
assert escape_md_v2("... (truncated)") in out
# We keep the tail and drop the oldest segments when truncating.
assert escape_md_v2("segment_59") in out
assert escape_md_v2("segment_0") not in out
def test_transcript_render_many_segments_completes_quickly():
"""Render with 200+ segments exercises O(n) truncation (deque popleft)."""
t = TranscriptBuffer()
for i in range(200):
t.apply({"type": "text_start", "index": i})
t.apply({"type": "text_delta", "index": i, "text": f"seg_{i} " + ("y" * 80)})
t.apply({"type": "block_stop", "index": i})
out = t.render(_ctx(), limit_chars=500, status="ok")
assert escape_md_v2("... (truncated)") in out
assert "199" in out # last segment (MarkdownV2 escapes underscores)
assert "seg_0 " not in out # oldest segment dropped
def test_transcript_reused_index_closes_previous_open_block():
t = TranscriptBuffer()
# Open a text block at index 0, but never close it.
t.apply({"type": "text_start", "index": 0})
t.apply({"type": "text_delta", "index": 0, "text": "alpha visible"})
# Provider reuses index 0 for a new tool block without a stop.
t.apply(
{"type": "tool_use_start", "index": 0, "id": "t1", "name": "ls", "input": {}}
)
t.apply({"type": "text_delta", "index": 0, "text": "omega visible"})
out = t.render(_ctx(), limit_chars=3900, status=None)
assert out.find("alpha") < out.find("Tool call:") < out.find("omega")
def test_transcript_render_segment_exception_skipped():
"""When a segment's render() raises, that segment is skipped and rest is rendered."""
class StaticSegment(Segment):
def __init__(self, text: str) -> None:
super().__init__(kind="static")
self._text = text
def render(self, ctx: RenderCtx) -> str:
return self._text
class BrokenSegment(Segment):
def __init__(self) -> None:
super().__init__(kind="broken")
def render(self, ctx: RenderCtx) -> str:
raise ValueError("render failed")
out = render_segments(
[StaticSegment("before"), BrokenSegment(), StaticSegment("after")],
_ctx(),
limit_chars=3900,
status=None,
)
assert "before" in out
assert "after" in out
def test_transcript_render_status_only_exceeds_limit():
"""When all segments dropped, status-only output; long status returned as-is."""
t = TranscriptBuffer()
t.apply({"type": "text_chunk", "text": "x" * 5000})
long_status = "A" * 500
msg = t.render(_ctx(), limit_chars=100, status=long_status)
assert "... (truncated)" in msg or long_status in msg
def test_transcript_truncation_preserves_last_segment_tail():
"""When all segments exceed limit, preserve tail of last segment (not just marker+status)."""
t = TranscriptBuffer()
t.apply({"type": "thinking_chunk", "text": "Thinking..."})
t.apply(
{"type": "text_chunk", "text": "The actual output content here" + "x" * 500}
)
msg = t.render(_ctx(), limit_chars=100, status="β
*Complete*")
# Must include actual content (tail of last segment), not only "... (truncated)\nβ
*Complete*"
assert escape_md_v2("... (truncated)") in msg
assert "β
*Complete*" in msg
assert "actual output" in msg or "content" in msg or "x" in msg
|