-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (53 loc) · 2.29 KB
/
Program.cs
File metadata and controls
63 lines (53 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Snapshot/restore example — fast sandbox reset without cold start.
//
// Demonstrates the snapshot/restore pattern used for agent tool calls:
// take a "warm" snapshot after initialization, then restore to it
// before each execution for a clean-but-fast environment.
using System.Diagnostics;
using HyperlightSandbox.Api;
using HyperlightSandbox.Guest.Python;
Console.WriteLine("=== Hyperlight Sandbox .NET — Snapshot Example ===\n");
using var sandbox = new SandboxBuilder()
.WithPythonModule()
.WithTempOutput()
.Build();
// --- Cold start: first run initializes the sandbox ---
Console.WriteLine("═══ Step 1: Cold start (first run) ═══");
var sw = Stopwatch.StartNew();
var result = sandbox.Run("print('Cold start complete')");
sw.Stop();
Console.WriteLine($" stdout: {result.Stdout.Trim()}");
Console.WriteLine($" Cold start time: {sw.ElapsedMilliseconds}ms");
// --- Take a snapshot of the warm state ---
Console.WriteLine("\n═══ Step 2: Take snapshot ═══");
using var snapshot = sandbox.Snapshot();
Console.WriteLine(" Snapshot taken.");
// --- Modify state ---
Console.WriteLine("\n═══ Step 3: Modify state ═══");
sandbox.Run("""
with open("/output/state.txt", "w") as f:
f.write("modified state")
print("State modified — wrote to /output/state.txt")
""");
Console.WriteLine($" Output files after modification: [{string.Join(", ", sandbox.GetOutputFiles())}]");
// --- Restore from snapshot —- state should be clean ---
Console.WriteLine("\n═══ Step 4: Restore from snapshot ═══");
sandbox.Restore(snapshot);
Console.WriteLine(" Snapshot restored.");
sw.Restart();
result = sandbox.Run("print('After restore — clean state')");
sw.Stop();
Console.WriteLine($" stdout: {result.Stdout.Trim()}");
Console.WriteLine($" Warm restore time: {sw.ElapsedMilliseconds}ms");
// --- Reuse the snapshot multiple times ---
Console.WriteLine("\n═══ Step 5: Reuse snapshot multiple times ═══");
for (int i = 1; i <= 3; i++)
{
sandbox.Restore(snapshot);
sw.Restart();
result = sandbox.Run($"print(f'Iteration {i} from clean state')");
sw.Stop();
Console.WriteLine($" Iteration {i}: {result.Stdout.Trim()} ({sw.ElapsedMilliseconds}ms)");
}
Console.WriteLine("\n✅ Snapshot example finished successfully!");
return 0;