Skip to content

Build and run agents you can see, understand and trust.

fix(workspace): append nothing when offloading an empty context batch (#3254)

ffffff559bdc4e8daa06
diff --git a/src/agentscope/workspace/_base.py b/src/agentscope/workspace/_base.py
index f60d53b75..c769bd350 100644
--- a/src/agentscope/workspace/_base.py
+++ b/src/agentscope/workspace/_base.py
@@ -1075,6 +1075,11 @@ class WorkspaceBase:
msg.content = [await offload_block(b) for b in msg.content]
lines.append(msg.model_dump_json())
+ if not lines:
+ # Nothing to append: writing the empty join would still add a
+ # standalone blank line that is not a JSONL record.
+ return path
+
payload = "\n".join(lines) + "\n"
existing = b""
diff --git a/tests/workspace_local_test.py b/tests/workspace_local_test.py
index ecdc8643d..ccc4e858f 100644
--- a/tests/workspace_local_test.py
+++ b/tests/workspace_local_test.py
@@ -267,6 +267,27 @@ class TestLocalWorkspaceOffload(IsolatedAsyncioTestCase):
msg = Msg.model_validate_json(line)
self.assertIsNotNone(msg)
+ async def test_offload_context_empty_msgs_appends_nothing(self) -> None:
+ """Offloading an empty batch must not append a blank JSONL line.
+
+ The empty join used to be written as a standalone ``\\n``, which
+ is not a JSON record and broke every line-oriented consumer of
+ ``context.jsonl``.
+ """
+ session_id = "test_session_empty_batch"
+ msgs1 = [UserMsg(name="user", content="First message")]
+ file_path = await self.workspace.offload_context(session_id, msgs1)
+ await self.workspace.offload_context(session_id, [])
+
+ async with aiofiles.open(file_path, "rb") as f:
+ content = await f.read()
+
+ self.assertFalse(content.endswith(b"\n\n"))
+ for line in content.decode("utf-8").splitlines():
+ self.assertTrue(line.strip())
+ Msg.model_validate_json(line)
+ json.loads(line)
+
async def test_offload_context_with_datablock(self) -> None:
"""Test offloading messages with DataBlock content.