Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ public void stop() throws Exception {

@Override
public void shutdown() throws Exception {
// Match E2b/Daytona: a non-owned sandbox is shared/reused — keep the MCP channel open so
// background SessionTree mirrors can still uploadFiles() after agent teardown (#2259).
if (!arState.isSandboxOwned()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Observability: The early return for non-owned sandboxes is silent. Consider adding a log.debug before returning so that lifecycle issues (e.g., unexpected double-shutdown or leaked MCP connections) are easier to trace in production.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Observability: The early return for non-owned sandboxes is silent. Consider adding a log.debug before returning so that lifecycle issues (e.g., unexpected double-shutdown or leaked MCP connections) are easier to trace in production.

return;
}
try {
mcp.close();
} catch (Exception ignore) {
// best-effort
}
if (!arState.isSandboxOwned()) {
return;
}
String id = arState.getSandboxId();
if (id != null && !id.isBlank()) {
http.deleteSandbox(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.agentscope.extensions.sandbox.agentrun;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import io.agentscope.harness.agent.sandbox.WorkspaceSpec;
import org.junit.jupiter.api.Test;

class AgentRunSandboxShutdownTest {

private static AgentRunSandboxState state(String id, boolean owned) {
AgentRunSandboxState state = new AgentRunSandboxState();
state.setSandboxId(id);
state.setSandboxOwned(owned);
state.setWorkspaceRoot(AgentRunSandboxState.DEFAULT_WORKSPACE_ROOT);
WorkspaceSpec ws = new WorkspaceSpec();
ws.setRoot(AgentRunSandboxState.DEFAULT_WORKSPACE_ROOT);
state.setWorkspaceSpec(ws);
return state;
}

private static AgentRunSandboxClientOptions options() {
AgentRunSandboxClientOptions opt = new AgentRunSandboxClientOptions();
opt.setApiKey("test-key");
opt.setTemplateName("agentscope-default");
return opt;
}

@Test
void shutdown_whenNotOwned_keepsMcpOpenAndSkipsDelete() throws Exception {
AgentRunMcpChannel mcp = mock(AgentRunMcpChannel.class);
AgentRunDataPlaneHttp http = mock(AgentRunDataPlaneHttp.class);

AgentRunSandbox sandbox =
new AgentRunSandbox(state("sbx-shared", false), options(), http, mcp);
sandbox.shutdown();

verify(mcp, never()).close();
verify(http, never()).deleteSandbox("sbx-shared");
}

@Test
void shutdown_whenOwned_closesMcpAndDeletesSandbox() throws Exception {
AgentRunMcpChannel mcp = mock(AgentRunMcpChannel.class);
AgentRunDataPlaneHttp http = mock(AgentRunDataPlaneHttp.class);

AgentRunSandbox sandbox =
new AgentRunSandbox(state("sbx-owned", true), options(), http, mcp);
sandbox.shutdown();

verify(mcp, times(1)).close();
verify(http, times(1)).deleteSandbox("sbx-owned");
}
}
Loading