Skip to content

Commit ff58b75

Browse files
fold a tail into the source text and agree on when a session is finished
two more from review, both correct truncation keeps the start and the end of the output, which i had wrong. i was treating a tail found at session end as past the cap and leaving it out, but it belongs in the end that truncation is meant to keep, so that branch could hide the last thing a process said. the window now carries its decoded text before truncation, and a tail is folded into that and truncated once. for the reported case the display goes from ending AAAAAAAA to ending ERROR>>> and the count from 25 to 30, which is what truncating the real source gives unix_local finalised on process.returncode while collection waited for output_closed, which is only set after the process is reaped and the pumps have drained. so the finaliser could remove the session and cancel a pump that still held the rest of a character. docker had the same split through exit_code. both now use the same drained predicate collection uses, which is the shape cloudflare already had the carried text is internal, no public field holds it
1 parent 2070572 commit ff58b75

12 files changed

Lines changed: 127 additions & 57 deletions

File tree

‎src/agents/extensions/sandbox/blaxel/sandbox.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ async def pty_exec_start(
856856
)
857857

858858
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
859-
output, original_token_count = await self._collect_pty_output(
859+
output, original_token_count, source_text = await self._collect_pty_output(
860860
entry=entry,
861861
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
862862
max_output_tokens=max_output_tokens,
@@ -866,6 +866,7 @@ async def pty_exec_start(
866866
entry=entry,
867867
output=output,
868868
original_token_count=original_token_count,
869+
source_text=source_text,
869870
max_output_tokens=max_output_tokens,
870871
)
871872

@@ -891,7 +892,7 @@ async def pty_write_stdin(
891892
await asyncio.sleep(0.1)
892893

893894
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
894-
output, original_token_count = await self._collect_pty_output(
895+
output, original_token_count, source_text = await self._collect_pty_output(
895896
entry=entry,
896897
yield_time_ms=resolve_pty_write_yield_time_ms(
897898
yield_time_ms=yield_time_ms, input_empty=chars == ""
@@ -904,6 +905,7 @@ async def pty_write_stdin(
904905
entry=entry,
905906
output=output,
906907
original_token_count=original_token_count,
908+
source_text=source_text,
907909
max_output_tokens=max_output_tokens,
908910
)
909911

@@ -966,7 +968,7 @@ async def _collect_pty_output(
966968
entry: _BlaxelPtySessionEntry,
967969
yield_time_ms: int,
968970
max_output_tokens: int | None,
969-
) -> tuple[bytes, int | None]:
971+
) -> tuple[bytes, int | None, str]:
970972
return await collect_pty_output(
971973
output_chunks=entry.output_chunks,
972974
output_lock=entry.output_lock,
@@ -983,6 +985,7 @@ async def _finalize_pty_update(
983985
entry: _BlaxelPtySessionEntry,
984986
output: bytes,
985987
original_token_count: int | None,
988+
source_text: str = "",
986989
max_output_tokens: int | None = None,
987990
) -> PtyExecUpdate:
988991
exit_code = entry.exit_code if entry.done else None
@@ -993,6 +996,7 @@ async def _finalize_pty_update(
993996
output_chunks=entry.output_chunks,
994997
output_lock=entry.output_lock,
995998
output=output,
999+
source_text=source_text,
9961000
original_token_count=original_token_count,
9971001
max_output_tokens=max_output_tokens,
9981002
)

‎src/agents/extensions/sandbox/cloudflare/sandbox.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ async def _collect_pty_output(
10331033
entry: _CloudflarePtyProcessEntry,
10341034
yield_time_ms: int,
10351035
max_output_tokens: int | None,
1036-
) -> tuple[bytes, int | None]:
1036+
) -> tuple[bytes, int | None, str]:
10371037
return await collect_pty_output(
10381038
output_chunks=entry.output_chunks,
10391039
output_lock=entry.output_lock,
@@ -1050,6 +1050,7 @@ async def _finalize_pty_update(
10501050
entry: _CloudflarePtyProcessEntry,
10511051
output: bytes,
10521052
original_token_count: int | None,
1053+
source_text: str = "",
10531054
max_output_tokens: int | None = None,
10541055
) -> PtyExecUpdate:
10551056
exit_code = entry.exit_code if entry.output_closed.is_set() else None
@@ -1059,6 +1060,7 @@ async def _finalize_pty_update(
10591060
output_chunks=entry.output_chunks,
10601061
output_lock=entry.output_lock,
10611062
output=output,
1063+
source_text=source_text,
10621064
original_token_count=original_token_count,
10631065
max_output_tokens=max_output_tokens,
10641066
)
@@ -1210,7 +1212,7 @@ async def pty_exec_start(
12101212
)
12111213

12121214
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
1213-
output, original_token_count = await self._collect_pty_output(
1215+
output, original_token_count, source_text = await self._collect_pty_output(
12141216
entry=entry,
12151217
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
12161218
max_output_tokens=max_output_tokens,
@@ -1220,6 +1222,7 @@ async def pty_exec_start(
12201222
entry=entry,
12211223
output=output,
12221224
original_token_count=original_token_count,
1225+
source_text=source_text,
12231226
max_output_tokens=max_output_tokens,
12241227
)
12251228

@@ -1244,7 +1247,7 @@ async def pty_write_stdin(
12441247
await asyncio.sleep(0.1)
12451248

12461249
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
1247-
output, original_token_count = await self._collect_pty_output(
1250+
output, original_token_count, source_text = await self._collect_pty_output(
12481251
entry=entry,
12491252
yield_time_ms=resolve_pty_write_yield_time_ms(
12501253
yield_time_ms=yield_time_ms,
@@ -1258,6 +1261,7 @@ async def pty_write_stdin(
12581261
entry=entry,
12591262
output=output,
12601263
original_token_count=original_token_count,
1264+
source_text=source_text,
12611265
max_output_tokens=max_output_tokens,
12621266
)
12631267

‎src/agents/extensions/sandbox/daytona/sandbox.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ async def _on_data(chunk: bytes | str) -> None:
755755
)
756756

757757
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
758-
output, original_token_count = await self._collect_pty_output(
758+
output, original_token_count, source_text = await self._collect_pty_output(
759759
entry=entry,
760760
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
761761
max_output_tokens=max_output_tokens,
@@ -765,6 +765,7 @@ async def _on_data(chunk: bytes | str) -> None:
765765
entry=entry,
766766
output=output,
767767
original_token_count=original_token_count,
768+
source_text=source_text,
768769
max_output_tokens=max_output_tokens,
769770
)
770771

@@ -833,7 +834,7 @@ async def pty_write_stdin(
833834
await asyncio.sleep(0.1)
834835

835836
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
836-
output, original_token_count = await self._collect_pty_output(
837+
output, original_token_count, source_text = await self._collect_pty_output(
837838
entry=entry,
838839
yield_time_ms=resolve_pty_write_yield_time_ms(
839840
yield_time_ms=yield_time_ms, input_empty=chars == ""
@@ -846,6 +847,7 @@ async def pty_write_stdin(
846847
entry=entry,
847848
output=output,
848849
original_token_count=original_token_count,
850+
source_text=source_text,
849851
max_output_tokens=max_output_tokens,
850852
)
851853

@@ -856,6 +858,7 @@ async def _finalize_pty_update(
856858
entry: _DaytonaPtySessionEntry,
857859
output: bytes,
858860
original_token_count: int | None,
861+
source_text: str = "",
859862
max_output_tokens: int | None = None,
860863
) -> PtyExecUpdate:
861864
exit_code = entry.exit_code if entry.done else None
@@ -866,6 +869,7 @@ async def _finalize_pty_update(
866869
output_chunks=entry.output_chunks,
867870
output_lock=entry.output_lock,
868871
output=output,
872+
source_text=source_text,
869873
original_token_count=original_token_count,
870874
max_output_tokens=max_output_tokens,
871875
)
@@ -897,7 +901,7 @@ async def _collect_pty_output(
897901
entry: _DaytonaPtySessionEntry,
898902
yield_time_ms: int,
899903
max_output_tokens: int | None,
900-
) -> tuple[bytes, int | None]:
904+
) -> tuple[bytes, int | None, str]:
901905
return await collect_pty_output(
902906
output_chunks=entry.output_chunks,
903907
output_lock=entry.output_lock,

‎src/agents/extensions/sandbox/e2b/sandbox.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ async def _append_output(payload: bytes | bytearray | str | object) -> None:
10501050
)
10511051

10521052
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
1053-
output, original_token_count = await self._collect_pty_output(
1053+
output, original_token_count, source_text = await self._collect_pty_output(
10541054
entry=entry,
10551055
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
10561056
max_output_tokens=max_output_tokens,
@@ -1060,6 +1060,7 @@ async def _append_output(payload: bytes | bytearray | str | object) -> None:
10601060
entry=entry,
10611061
output=output,
10621062
original_token_count=original_token_count,
1063+
source_text=source_text,
10631064
max_output_tokens=max_output_tokens,
10641065
)
10651066

@@ -1088,7 +1089,7 @@ async def pty_write_stdin(
10881089
await asyncio.sleep(0.1)
10891090

10901091
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
1091-
output, original_token_count = await self._collect_pty_output(
1092+
output, original_token_count, source_text = await self._collect_pty_output(
10921093
entry=entry,
10931094
yield_time_ms=resolve_pty_write_yield_time_ms(
10941095
yield_time_ms=yield_time_ms, input_empty=chars == ""
@@ -1101,6 +1102,7 @@ async def pty_write_stdin(
11011102
entry=entry,
11021103
output=output,
11031104
original_token_count=original_token_count,
1105+
source_text=source_text,
11041106
max_output_tokens=max_output_tokens,
11051107
)
11061108

@@ -1213,7 +1215,7 @@ async def _collect_pty_output(
12131215
entry: _E2BPtyProcessEntry,
12141216
yield_time_ms: int,
12151217
max_output_tokens: int | None,
1216-
) -> tuple[bytes, int | None]:
1218+
) -> tuple[bytes, int | None, str]:
12171219
return await collect_pty_output(
12181220
output_chunks=entry.output_chunks,
12191221
output_lock=entry.output_lock,
@@ -1248,6 +1250,7 @@ async def _finalize_pty_update(
12481250
entry: _E2BPtyProcessEntry,
12491251
output: bytes,
12501252
original_token_count: int | None,
1253+
source_text: str = "",
12511254
max_output_tokens: int | None = None,
12521255
) -> PtyExecUpdate:
12531256
exit_code = self._entry_exit_code(entry)
@@ -1258,6 +1261,7 @@ async def _finalize_pty_update(
12581261
output_chunks=entry.output_chunks,
12591262
output_lock=entry.output_lock,
12601263
output=output,
1264+
source_text=source_text,
12611265
original_token_count=original_token_count,
12621266
max_output_tokens=max_output_tokens,
12631267
)

‎src/agents/extensions/sandbox/modal/sandbox.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ async def pty_exec_start(
909909
)
910910

911911
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
912-
output, original_token_count = await self._collect_pty_output(
912+
output, original_token_count, source_text = await self._collect_pty_output(
913913
entry=entry,
914914
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
915915
max_output_tokens=max_output_tokens,
@@ -919,6 +919,7 @@ async def pty_exec_start(
919919
entry=entry,
920920
output=output,
921921
original_token_count=original_token_count,
922+
source_text=source_text,
922923
max_output_tokens=max_output_tokens,
923924
)
924925

@@ -943,7 +944,7 @@ async def pty_write_stdin(
943944
await asyncio.sleep(0.1)
944945

945946
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
946-
output, original_token_count = await self._collect_pty_output(
947+
output, original_token_count, source_text = await self._collect_pty_output(
947948
entry=entry,
948949
yield_time_ms=resolve_pty_write_yield_time_ms(
949950
yield_time_ms=yield_time_ms, input_empty=chars == ""
@@ -956,6 +957,7 @@ async def pty_write_stdin(
956957
entry=entry,
957958
output=output,
958959
original_token_count=original_token_count,
960+
source_text=source_text,
959961
max_output_tokens=max_output_tokens,
960962
)
961963

@@ -985,7 +987,7 @@ async def _collect_pty_output(
985987
entry: _ModalPtyProcessEntry,
986988
yield_time_ms: int,
987989
max_output_tokens: int | None,
988-
) -> tuple[bytes, int | None]:
990+
) -> tuple[bytes, int | None, str]:
989991
deadline = time.monotonic() + (yield_time_ms / 1000)
990992
# a character split across two windows starts in the tail the last one held back. the
991993
# field is left alone until the decode below commits, so a cancelled call keeps it
@@ -1019,7 +1021,7 @@ async def _collect_pty_output(
10191021
exited = await self._peek_exit_code(entry.process) is not None
10201022
text, entry.pending_output = decode_pty_window(chunks, is_final=exited)
10211023
truncated_text, original_token_count = truncate_text_by_tokens(text, max_output_tokens)
1022-
return truncated_text.encode("utf-8", errors="replace"), original_token_count
1024+
return truncated_text.encode("utf-8", errors="replace"), original_token_count, text
10231025

10241026
async def _drain_modal_stream(
10251027
self,
@@ -1117,6 +1119,7 @@ async def _finalize_pty_update(
11171119
entry: _ModalPtyProcessEntry,
11181120
output: bytes,
11191121
original_token_count: int | None,
1122+
source_text: str = "",
11201123
max_output_tokens: int | None = None,
11211124
) -> PtyExecUpdate:
11221125
exit_code = await self._peek_exit_code(entry.process)
@@ -1125,6 +1128,7 @@ async def _finalize_pty_update(
11251128
output, original_token_count = close_pty_tail(
11261129
leftover=entry.pending_output,
11271130
output=output,
1131+
source_text=source_text,
11281132
original_token_count=original_token_count,
11291133
max_output_tokens=max_output_tokens,
11301134
)

‎src/agents/sandbox/sandboxes/docker.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ async def pty_exec_start(
10801080
)
10811081

10821082
yield_time_ms = 10_000 if yield_time_s is None else int(yield_time_s * 1000)
1083-
output, original_token_count = await self._collect_pty_output(
1083+
output, original_token_count, source_text = await self._collect_pty_output(
10841084
entry=entry,
10851085
yield_time_ms=clamp_pty_yield_time_ms(yield_time_ms),
10861086
max_output_tokens=max_output_tokens,
@@ -1090,6 +1090,7 @@ async def pty_exec_start(
10901090
entry=entry,
10911091
output=output,
10921092
original_token_count=original_token_count,
1093+
source_text=source_text,
10931094
max_output_tokens=max_output_tokens,
10941095
)
10951096

@@ -1127,7 +1128,7 @@ async def pty_write_stdin(
11271128
await asyncio.sleep(0.1)
11281129

11291130
yield_time_ms = 250 if yield_time_s is None else int(yield_time_s * 1000)
1130-
output, original_token_count = await self._collect_pty_output(
1131+
output, original_token_count, source_text = await self._collect_pty_output(
11311132
entry=entry,
11321133
yield_time_ms=resolve_pty_write_yield_time_ms(
11331134
yield_time_ms=yield_time_ms, input_empty=chars == ""
@@ -1140,6 +1141,7 @@ async def pty_write_stdin(
11401141
entry=entry,
11411142
output=output,
11421143
original_token_count=original_token_count,
1144+
source_text=source_text,
11431145
max_output_tokens=max_output_tokens,
11441146
)
11451147

@@ -1244,7 +1246,7 @@ async def _collect_pty_output(
12441246
entry: _DockerPtyProcessEntry,
12451247
yield_time_ms: int,
12461248
max_output_tokens: int | None,
1247-
) -> tuple[bytes, int | None]:
1249+
) -> tuple[bytes, int | None, str]:
12481250
return await collect_pty_output(
12491251
output_chunks=entry.output_chunks,
12501252
output_lock=entry.output_lock,
@@ -1261,19 +1263,24 @@ async def _finalize_pty_update(
12611263
entry: _DockerPtyProcessEntry,
12621264
output: bytes,
12631265
original_token_count: int | None,
1266+
source_text: str = "",
12641267
max_output_tokens: int | None = None,
12651268
) -> PtyExecUpdate:
12661269
if entry.output_closed.is_set() and entry.exit_code is None:
12671270
await self._refresh_pty_exit_code(entry)
12681271

1269-
exit_code = entry.exit_code
1272+
# _watch_pty_exit can set exit_code before _pump_pty_socket reaches its finally, so
1273+
# finalizing on exit_code alone would remove the session while the pump still holds
1274+
# output. Collection already waits for output_closed, so this matches it.
1275+
exit_code = entry.exit_code if entry.output_closed.is_set() else None
12701276
live_process_id: int | None = process_id
12711277

12721278
if exit_code is not None:
12731279
output, original_token_count = await flush_pty_tail(
12741280
output_chunks=entry.output_chunks,
12751281
output_lock=entry.output_lock,
12761282
output=output,
1283+
source_text=source_text,
12771284
original_token_count=original_token_count,
12781285
max_output_tokens=max_output_tokens,
12791286
)

0 commit comments

Comments
 (0)