Skip to content

Commit aab9675

Browse files
committed
fix(harness): hide host offload path from sandbox agents
Keep sandbox path omission while delegating to SessionTranscriptWriter for local filesystems.
1 parent 9b77823 commit aab9675

2 files changed

Lines changed: 44 additions & 108 deletions

File tree

‎agentscope-harness/src/main/java/io/agentscope/harness/agent/memory/MemoryFlushManager.java‎

Lines changed: 17 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@
2424
import io.agentscope.core.message.ToolUseBlock;
2525
import io.agentscope.core.model.Model;
2626
import io.agentscope.core.util.JsonUtils;
27+
import io.agentscope.harness.agent.filesystem.sandbox.AbstractSandboxFilesystem;
2728
import io.agentscope.harness.agent.memory.compaction.ConversationCompactor;
28-
import io.agentscope.harness.agent.memory.session.SessionEntry;
29-
import io.agentscope.harness.agent.memory.session.SessionTree;
29+
import io.agentscope.harness.agent.memory.session.SessionTranscriptWriter;
3030
import io.agentscope.harness.agent.workspace.WorkspaceConstants;
3131
import io.agentscope.harness.agent.workspace.WorkspaceManager;
32-
import java.nio.file.Path;
3332
import java.time.LocalDate;
3433
import java.time.format.DateTimeFormatter;
3534
import java.util.ArrayList;
36-
import java.util.HashSet;
3735
import java.util.List;
38-
import java.util.Set;
3936
import java.util.stream.Collectors;
4037
import org.slf4j.Logger;
4138
import org.slf4j.LoggerFactory;
@@ -191,125 +188,37 @@ public Mono<Void> flushMemories(RuntimeContext rc, List<Msg> messages) {
191188
* Returns the string path of the session JSONL file where messages for the given agent and
192189
* session are offloaded. Used by the compaction layer to embed the archive location in the
193190
* summary message so the agent can retrieve full history if needed.
191+
*
192+
* @deprecated Prefer {@link SessionTranscriptWriter#resolveContextPath}.
194193
*/
194+
@Deprecated
195195
public String resolveOffloadPath(RuntimeContext rc, String agentId, String sessionId) {
196-
try {
197-
Path p = workspaceManager.resolveSessionContextFile(rc, agentId, sessionId);
198-
return p != null ? p.toString() : "";
199-
} catch (Exception e) {
200-
log.debug(
201-
"Could not resolve offload path for agent={}, session={}: {}",
202-
agentId,
203-
sessionId,
204-
e.getMessage());
196+
// Session archives are persisted in the host-side workspace. A sandboxed agent cannot
197+
// resolve that path, so do not advertise it as agent-readable in the summary prompt.
198+
if (workspaceManager.getFilesystem() instanceof AbstractSandboxFilesystem) {
205199
return "";
206200
}
201+
return new SessionTranscriptWriter(workspaceManager)
202+
.resolveContextPath(rc, agentId, sessionId);
207203
}
208204

209205
/**
210206
* Offloads raw messages to the JSONL session tree.
207+
*
208+
* @deprecated Prefer {@link SessionTranscriptWriter#appendMessages}. Kept as a thin
209+
* delegate so compaction / overflow-recovery call sites keep compiling during the
210+
* migration; new code should use {@link SessionTranscriptWriter} directly.
211211
*/
212+
@Deprecated
212213
public void offloadMessages(
213214
RuntimeContext rc, List<Msg> messages, String agentId, String sessionId) {
214-
offloadToSessionTree(rc, messages, agentId, sessionId);
215-
215+
new SessionTranscriptWriter(workspaceManager)
216+
.appendMessages(rc, messages, agentId, sessionId);
216217
log.debug(
217218
"Offloaded {} messages for agent={}, session={}",
218219
messages.size(),
219220
agentId,
220221
sessionId);
221-
workspaceManager.updateSessionIndex(rc, agentId, sessionId, "conversation offloaded");
222-
}
223-
224-
private void offloadToSessionTree(
225-
RuntimeContext rc, List<Msg> messages, String agentId, String sessionId) {
226-
try {
227-
Path contextFile = workspaceManager.resolveSessionContextFile(rc, agentId, sessionId);
228-
String contextRelPath =
229-
WorkspaceConstants.AGENTS_DIR
230-
+ "/"
231-
+ agentId
232-
+ "/"
233-
+ WorkspaceConstants.SESSIONS_DIR
234-
+ "/"
235-
+ sessionId
236-
+ WorkspaceConstants.SESSION_CONTEXT_EXT;
237-
SessionTree tree =
238-
new SessionTree(
239-
contextFile,
240-
workspaceManager.getWorkspace(),
241-
workspaceManager.getFilesystem(),
242-
workspaceManager.getIndex(),
243-
contextRelPath)
244-
.setRuntimeContext(rc);
245-
tree.load();
246-
// Sync from remote before appending so that entries written by a previous replica
247-
// (cross-machine handoff) are included in the merged file pushed to remote.
248-
tree.syncFromRemote();
249-
250-
List<SessionEntry> existingEntries = new ArrayList<>(tree.getAllEntries());
251-
Set<String> existingIds =
252-
existingEntries.stream()
253-
.map(SessionEntry::getId)
254-
.collect(Collectors.toCollection(HashSet::new));
255-
String lastId =
256-
existingEntries.isEmpty()
257-
? null
258-
: existingEntries.get(existingEntries.size() - 1).getId();
259-
260-
// The caller passes the full conversation on every turn. Use the stable Msg IDs
261-
// to keep the session JSONL append-only and idempotent across repeated offloads.
262-
for (Msg msg : messages) {
263-
if (msg.getRole() == null || isSessionContextMessage(msg)) {
264-
continue;
265-
}
266-
String rendered = renderContentBlocks(msg);
267-
if (rendered == null || rendered.isBlank()) {
268-
continue;
269-
}
270-
String entryId = normalizeEntryId(msg.getId());
271-
if (entryId == null) {
272-
log.warn(
273-
"Msg without stable ID encountered (role={}); dedup skipped",
274-
msg.getRole());
275-
}
276-
if (entryId != null && existingIds.contains(entryId)) {
277-
continue;
278-
}
279-
String toolCallId = extractToolCallId(msg);
280-
SessionEntry.MessageEntry entry =
281-
new SessionEntry.MessageEntry(
282-
entryId, lastId, null, msg.getRole().name(), rendered, toolCallId);
283-
tree.append(entry);
284-
existingIds.add(entry.getId());
285-
lastId = entry.getId();
286-
}
287-
288-
tree.flush();
289-
} catch (Exception e) {
290-
log.warn("Failed to offload to JSONL session tree: {}", e.getMessage());
291-
}
292-
}
293-
294-
/**
295-
* Extracts a representative tool call ID from a message, if present.
296-
* For TOOL messages, returns the first ToolResultBlock's id.
297-
* For ASSISTANT messages with tool calls, returns the first ToolUseBlock's id.
298-
*/
299-
private static String extractToolCallId(Msg msg) {
300-
for (ContentBlock block : msg.getContent()) {
301-
if (block instanceof ToolResultBlock tr && tr.getId() != null) {
302-
return tr.getId();
303-
}
304-
if (block instanceof ToolUseBlock tu && tu.getId() != null) {
305-
return tu.getId();
306-
}
307-
}
308-
return null;
309-
}
310-
311-
private static String normalizeEntryId(String id) {
312-
return id != null && !id.isBlank() ? id : null;
313222
}
314223

315224
/**

‎agentscope-harness/src/test/java/io/agentscope/harness/agent/memory/MemoryFlushManagerOffloadTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package io.agentscope.harness.agent.memory;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.mockito.Mockito.mock;
1920

2021
import io.agentscope.core.agent.RuntimeContext;
2122
import io.agentscope.core.message.Msg;
2223
import io.agentscope.core.message.MsgRole;
2324
import io.agentscope.core.message.TextBlock;
25+
import io.agentscope.harness.agent.filesystem.sandbox.AbstractSandboxFilesystem;
2426
import io.agentscope.harness.agent.memory.compaction.ConversationCompactor;
2527
import io.agentscope.harness.agent.memory.session.SessionEntry;
2628
import io.agentscope.harness.agent.memory.session.SessionTree;
@@ -36,6 +38,31 @@ class MemoryFlushManagerOffloadTest {
3638

3739
@TempDir Path workspace;
3840

41+
@Test
42+
void resolveOffloadPath_omitsHostPathForSandboxFilesystem() {
43+
AbstractSandboxFilesystem filesystem = mock(AbstractSandboxFilesystem.class);
44+
try (WorkspaceManager workspaceManager = new WorkspaceManager(workspace, filesystem)) {
45+
MemoryFlushManager flushManager = new MemoryFlushManager(workspaceManager, null);
46+
47+
assertEquals(
48+
"",
49+
flushManager.resolveOffloadPath(
50+
RuntimeContext.empty(), "agent-a", "session-1"));
51+
}
52+
}
53+
54+
@Test
55+
void resolveOffloadPath_keepsReachablePathForLocalFilesystem() {
56+
try (WorkspaceManager workspaceManager = new WorkspaceManager(workspace)) {
57+
MemoryFlushManager flushManager = new MemoryFlushManager(workspaceManager, null);
58+
59+
assertEquals(
60+
"agents/agent-a/sessions/session-1.jsonl",
61+
flushManager.resolveOffloadPath(
62+
RuntimeContext.empty(), "agent-a", "session-1"));
63+
}
64+
}
65+
3966
@Test
4067
void offloadMessages_skipsAlreadyPersistedPrefix_andKeepsChain() throws Exception {
4168
RuntimeContext rc = RuntimeContext.builder().sessionId("session-1").build();

0 commit comments

Comments
 (0)