Skip to content

Commit 1cc69bf

Browse files
committed
test(sandbox): align retention tests with main
- Stub the state store's agent id, which SandboxManager now checks. - Recovery and volume tests reach the recreate path through a 404 connect response, since only a confirmed expiry recreates. - Set the E2B workspace root along with the spec root, which the sandbox now aligns at construction. - A fresh allocation now clears stale ready metadata before start, so the new-allocation cleanup test fails start in setup instead of the probe.
1 parent f011df7 commit 1cc69bf

5 files changed

Lines changed: 59 additions & 22 deletions

File tree

‎agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bSandboxRecoveryTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void reconnectPreservesWorkspaceProjectionState() throws Exception {
102102

103103
@Test
104104
void startFallsBackToTemplateWhenNativeSnapshotIsMissing() throws Exception {
105-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
105+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
106106
server.enqueue(new MockResponse().setResponseCode(404).setBody("snapshot not found"));
107107
server.enqueue(sandboxResponse("fresh-sandbox"));
108108
server.enqueue(envdStartResponse());
@@ -127,7 +127,7 @@ void startFallsBackToTemplateWhenNativeSnapshotIsMissing() throws Exception {
127127

128128
@Test
129129
void startFailsOnSnapshotCreateServerErrorWithoutTemplateFallback() throws Exception {
130-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
130+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
131131
server.enqueue(new MockResponse().setResponseCode(500).setBody("snapshot unavailable"));
132132
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");
133133

@@ -149,7 +149,7 @@ void startFailsOnSnapshotCreateServerErrorWithoutTemplateFallback() throws Excep
149149

150150
@Test
151151
void startFailsWhenSnapshotArchiveCannotBeRestoredWithoutTemplateFallback() throws Exception {
152-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
152+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
153153
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");
154154
state.setSnapshot(
155155
new FakeSnapshot(nativeArchive("snap-123"), true) {
@@ -175,7 +175,7 @@ public InputStream restore() throws Exception {
175175

176176
@Test
177177
void startFailsWhenSnapshotRestorabilityCheckFailsWithoutTemplateFallback() throws Exception {
178-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
178+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
179179
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");
180180
state.setSnapshot(
181181
new FakeSnapshot(nativeArchive("snap-123"), true) {
@@ -201,7 +201,7 @@ public boolean isRestorable() throws Exception {
201201

202202
@Test
203203
void startFailsWhenNativeSnapshotReferenceIsCorruptWithoutTemplateFallback() throws Exception {
204-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
204+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
205205
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");
206206
byte[] corruptArchive =
207207
(new String(E2bSnapshotRefs.MAGIC_PREFIX, StandardCharsets.UTF_8)
@@ -225,7 +225,7 @@ void startFailsWhenNativeSnapshotReferenceIsCorruptWithoutTemplateFallback() thr
225225

226226
@Test
227227
void startFallsBackToTemplateWhenSnapshotIsConfirmedNotRestorable() throws Exception {
228-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
228+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
229229
server.enqueue(sandboxResponse("fresh-sandbox"));
230230
server.enqueue(envdStartResponse());
231231
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");
@@ -346,7 +346,7 @@ void startPropagatesConfigurationFailureWithoutRelabelingItAsSnapshotLoss() thro
346346

347347
@Test
348348
void startRestoresSnapshotAfterConnectFailureWithoutReopeningSnapshotStore() throws Exception {
349-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
349+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
350350
server.enqueue(sandboxResponse("restored-sandbox"));
351351
server.enqueue(envdStartResponse());
352352
E2bSandboxState state = stateForResumeWithNativeSnapshot("snap-123");

‎agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bSandboxRetentionTest.java‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import static org.junit.jupiter.api.Assertions.assertThrows;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
2626
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.atLeastOnce;
2728
import static org.mockito.Mockito.doAnswer;
2829
import static org.mockito.Mockito.doThrow;
2930
import static org.mockito.Mockito.mock;
30-
import static org.mockito.Mockito.verifyNoInteractions;
31+
import static org.mockito.Mockito.verify;
32+
import static org.mockito.Mockito.verifyNoMoreInteractions;
3133
import static org.mockito.Mockito.when;
3234

3335
import io.agentscope.core.agent.RuntimeContext;
@@ -126,7 +128,7 @@ void callerCanPersistExplicitStateResumeAndDeleteWithoutIsolationKey() throws Ex
126128
WorkspaceClient localClient = new WorkspaceClient();
127129
SandboxSnapshotSpec snapshots = new LocalSnapshotSpec(temp);
128130
SandboxState initial = localClient.create(new WorkspaceSpec(), snapshots, null).getState();
129-
SessionSandboxStateStore store = mock(SessionSandboxStateStore.class);
131+
SessionSandboxStateStore store = agentStore();
130132
SandboxLifecycleMiddleware lifecycle =
131133
new SandboxLifecycleMiddleware(
132134
new SandboxManager(localClient, store, "agent"),
@@ -177,7 +179,9 @@ void callerCanPersistExplicitStateResumeAndDeleteWithoutIsolationKey() throws Ex
177179
request("POST", "/sandboxes/caller-owned-record/connect");
178180
request("DELETE", "/sandboxes/caller-owned-record");
179181
assertEquals(3, server.getRequestCount());
180-
verifyNoInteractions(store);
182+
// The manager reads the store's agent id when it is built; nothing else touches it.
183+
verify(store, atLeastOnce()).getAgentId();
184+
verifyNoMoreInteractions(store);
181185
}
182186

183187
@Test
@@ -317,8 +321,7 @@ void failedNativeSnapshotUploadDoesNotPrunePreviousArchiveSnapshot() throws Exce
317321
server.enqueue(
318322
new MockResponse().setResponseCode(200).setBody("{\"snapshotID\":\"failed-new\"}"));
319323
E2bSandbox sandbox = new E2bSandbox(state, options);
320-
SandboxManager manager =
321-
new SandboxManager(client, mock(SessionSandboxStateStore.class), "agent");
324+
SandboxManager manager = new SandboxManager(client, agentStore(), "agent");
322325
manager.release(
323326
SandboxAcquireResult.selfManaged(sandbox, null, SandboxReleasePolicy.RETAIN));
324327
request("POST", "/sandboxes/live/snapshots");
@@ -349,7 +352,7 @@ void successfulNativeSnapshotUploadPrunesOnlyAfterPersistence() throws Exception
349352
new MockResponse().setResponseCode(200).setBody("{\"snapshotID\":\"latest\"}"));
350353
respond(200);
351354
E2bSandbox sandbox = new E2bSandbox(state, options);
352-
new SandboxManager(client, mock(SessionSandboxStateStore.class), "agent")
355+
new SandboxManager(client, agentStore(), "agent")
353356
.release(
354357
SandboxAcquireResult.selfManaged(
355358
sandbox, null, SandboxReleasePolicy.RETAIN));
@@ -494,4 +497,11 @@ protected void doHydrateWorkspace(InputStream archive) throws Exception {
494497
};
495498
}
496499
}
500+
501+
/** SandboxManager requires the store to be scoped to the same agent id. */
502+
private static SessionSandboxStateStore agentStore() {
503+
SessionSandboxStateStore store = mock(SessionSandboxStateStore.class);
504+
when(store.getAgentId()).thenReturn("agent");
505+
return store;
506+
}
497507
}

‎agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bSandboxStartFailureTest.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Sandbox resume(SandboxState restored) {
103103
state.setSandboxId("retained-live");
104104
state.setWorkspaceRootReady(true);
105105
state.setSnapshotIds(List.of("old", "latest"));
106-
store = mock(SessionSandboxStateStore.class);
106+
store = agentStore();
107107
lease = mock(SandboxLease.class);
108108
middleware =
109109
new SandboxLifecycleMiddleware(
@@ -180,6 +180,7 @@ private void failedCallPreservesStoredState() throws Exception {
180180
@ValueSource(strings = {"restore", "entries", "projection"})
181181
void retryAfterWorkspaceMutationFailureRestoresInsteadOfTrustingPartialTree(String stage)
182182
throws Exception {
183+
state.setWorkspaceRoot(temp.resolve("workspace").toString());
183184
state.getWorkspaceSpec().setRoot(temp.resolve("workspace").toString());
184185
state.setWorkspaceProjectionHash("previous-projection");
185186
state.setSnapshot(new LocalSnapshotSpec(temp).build("saved-archive"));
@@ -236,10 +237,12 @@ void retryAfterWorkspaceMutationFailureRestoresInsteadOfTrustingPartialTree(Stri
236237
}
237238

238239
@Test
239-
void failedProbeOfNewAllocationDoesNotRestoreStaleReadyMetadata() throws Exception {
240+
void failedStartOfNewAllocationDoesNotKeepStaleReadyMetadata() throws Exception {
240241
state.setSandboxId(null);
241242
state.setWorkspaceProjectionHash("old-instance-projection");
243+
// A fresh template allocation clears the ready flag before start, so it never probes.
242244
failProbe = true;
245+
failSetup = true;
243246
connected("new-allocation");
244247
Sandbox sandbox = sandbox();
245248
assertThrows(Exception.class, sandbox::start);
@@ -440,4 +443,11 @@ void failedNewAllocationIsDeletedWithoutSnapshotGc(boolean expired) throws Excep
440443
assertEquals(List.of("old", "latest"), state.getSnapshotIds());
441444
assertEquals(expired ? 1 : 0, restores);
442445
}
446+
447+
/** SandboxManager requires the store to be scoped to the same agent id. */
448+
private static SessionSandboxStateStore agentStore() {
449+
SessionSandboxStateStore store = mock(SessionSandboxStateStore.class);
450+
when(store.getAgentId()).thenReturn("agent");
451+
return store;
452+
}
443453
}

‎agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bVolumeMountTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void serdeRoundTripAndLegacyCompat() {
233233

234234
@Test
235235
void recreateCarriesMounts() throws Exception {
236-
server.enqueue(new MockResponse().setResponseCode(500).setBody("connect failed"));
236+
server.enqueue(new MockResponse().setResponseCode(404).setBody("sandbox expired"));
237237
server.enqueue(
238238
new MockResponse()
239239
.setResponseCode(200)

‎agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/SandboxRetentionTest.java‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525
import static org.mockito.ArgumentMatchers.any;
2626
import static org.mockito.ArgumentMatchers.eq;
27+
import static org.mockito.Mockito.atLeastOnce;
2728
import static org.mockito.Mockito.doCallRealMethod;
2829
import static org.mockito.Mockito.doThrow;
2930
import static org.mockito.Mockito.inOrder;
3031
import static org.mockito.Mockito.mock;
3132
import static org.mockito.Mockito.never;
3233
import static org.mockito.Mockito.verify;
3334
import static org.mockito.Mockito.verifyNoInteractions;
35+
import static org.mockito.Mockito.verifyNoMoreInteractions;
3436
import static org.mockito.Mockito.when;
3537

3638
import io.agentscope.core.agent.RuntimeContext;
@@ -50,7 +52,7 @@ class SandboxRetentionTest {
5052
@SuppressWarnings("unchecked")
5153
private final SandboxClient<SandboxClientOptions> client = mock(SandboxClient.class);
5254

53-
private final SessionSandboxStateStore store = mock(SessionSandboxStateStore.class);
55+
private final SessionSandboxStateStore store = agentStore();
5456
private final Sandbox sandbox = mock(Sandbox.class);
5557
private final SandboxManager manager = new SandboxManager(client, store, "agent");
5658

@@ -135,7 +137,7 @@ void unsupportedBackendRejectedBeforeAcquire() throws Exception {
135137
SandboxException.SandboxConfigurationException.class,
136138
() -> manager.acquire(context(SandboxReleasePolicy.RETAIN), runtime()));
137139
verify(client, never()).create(any(), any(), any());
138-
verifyNoInteractions(store);
140+
assertStoreNotUsed();
139141
}
140142

141143
@Test
@@ -147,7 +149,7 @@ void missingIsolationKeyRejectedBeforeAcquire() throws Exception {
147149
manager.acquire(
148150
context(SandboxReleasePolicy.RETAIN), RuntimeContext.empty()));
149151
verify(client, never()).create(any(), any(), any());
150-
verifyNoInteractions(store);
152+
assertStoreNotUsed();
151153
}
152154

153155
@Test
@@ -163,7 +165,8 @@ void externalSandboxRemainsCallerOwnedEvenWithRetain() throws Exception {
163165
verify(sandbox, never()).stop();
164166
verify(sandbox, never()).shutdown();
165167
verify(sandbox, never()).onRetained();
166-
verifyNoInteractions(client, store);
168+
verifyNoInteractions(client);
169+
assertStoreNotUsed();
167170
}
168171

169172
@Test
@@ -180,7 +183,7 @@ void explicitStateCanBeRetainedWithoutHarnessIsolationKey() throws Exception {
180183
verify(sandbox).stop();
181184
verify(sandbox).onRetained();
182185
verify(sandbox, never()).shutdown();
183-
verifyNoInteractions(store);
186+
assertStoreNotUsed();
184187
}
185188

186189
@Test
@@ -316,7 +319,8 @@ void defaultMaintenancePreservesResourcesAndDefaultFailedStartCleanupDestroysThe
316319
void discardWithoutAnAcquiredSandboxIsSafe() {
317320
assertDoesNotThrow(() -> manager.discard(null));
318321
assertDoesNotThrow(() -> manager.discard(SandboxAcquireResult.selfManaged(null)));
319-
verifyNoInteractions(sandbox, client, store);
322+
verifyNoInteractions(sandbox, client);
323+
assertStoreNotUsed();
320324
}
321325

322326
@Test
@@ -348,4 +352,17 @@ void nullPolicyRejected() {
348352
assertThrows(
349353
NullPointerException.class, () -> SandboxContext.builder().releasePolicy(null));
350354
}
355+
356+
/** SandboxManager requires the store to be scoped to the same agent id. */
357+
private static SessionSandboxStateStore agentStore() {
358+
SessionSandboxStateStore store = mock(SessionSandboxStateStore.class);
359+
when(store.getAgentId()).thenReturn("agent");
360+
return store;
361+
}
362+
363+
/** The manager reads the store's agent id when it is built; nothing else may touch it. */
364+
private void assertStoreNotUsed() {
365+
verify(store, atLeastOnce()).getAgentId();
366+
verifyNoMoreInteractions(store);
367+
}
351368
}

0 commit comments

Comments
 (0)