|
| 1 | +/// Phase 0: Fix Agent.fs timeout bug (Greptile finding from PR #177) |
| 2 | +/// |
| 3 | +/// Problem: Agent.postAndReply falls back to tryPostAndReply with a |
| 4 | +/// hardcoded 1-second timeout when DefaultTimeout = Timeout.Infinite. |
| 5 | +/// This causes slow operations (resource loading, constraint solving) |
| 6 | +/// to fail with "Timed out waiting for reply". |
| 7 | +/// |
| 8 | +/// Fix: Increase the fallback timeout to 30 seconds (30_000 ms). |
| 9 | +/// The postAndReply function already has a fast-path when DefaultTimeout |
| 10 | +/// is set to a specific value, so this only affects agents that haven't |
| 11 | +/// configured a timeout (i.e. left at Infinite). |
| 12 | +
|
| 13 | +#I __SOURCE_DIRECTORY__ |
| 14 | + |
| 15 | +#load "load.fsx" |
| 16 | + |
| 17 | +open System.Threading |
| 18 | +open Informedica.Agents.Lib |
| 19 | + |
| 20 | + |
| 21 | +// ============================================================ |
| 22 | +// 1. Demonstrate the bug: 1-second timeout is too short |
| 23 | +// ============================================================ |
| 24 | + |
| 25 | +// Create an agent that takes 2 seconds to reply (simulating slow work) |
| 26 | +let slowAgent = |
| 27 | + Agent.createReply<string, string>(fun msg -> |
| 28 | + Thread.Sleep(2000) // simulate slow operation |
| 29 | + $"processed: {msg}" |
| 30 | + ) |
| 31 | + |
| 32 | +// This will fail with the current 1-second timeout because |
| 33 | +// DefaultTimeout is Timeout.Infinite by default |
| 34 | +printfn $"DefaultTimeout = {slowAgent |> Agent.getDefaultTimeout}" |
| 35 | +printfn $"Timeout.Infinite = {Timeout.Infinite}" |
| 36 | + |
| 37 | +let result1 = |
| 38 | + try |
| 39 | + slowAgent |
| 40 | + |> Agent.postAndReply "slow-request" |
| 41 | + |> Some |
| 42 | + with ex -> |
| 43 | + printfn $"BUG: {ex.Message}" |
| 44 | + None |
| 45 | + |
| 46 | +printfn $"Result with 1s timeout (should fail): {result1}" |
| 47 | + |
| 48 | + |
| 49 | +// ============================================================ |
| 50 | +// 2. Workaround: set DefaultTimeout explicitly |
| 51 | +// ============================================================ |
| 52 | + |
| 53 | +slowAgent |> Agent.setDefaultTimeout 30_000 |
| 54 | + |
| 55 | +let result2 = |
| 56 | + try |
| 57 | + slowAgent |
| 58 | + |> Agent.postAndReply "slow-request-with-timeout" |
| 59 | + |> Some |
| 60 | + with ex -> |
| 61 | + printfn $"Error: {ex.Message}" |
| 62 | + None |
| 63 | + |
| 64 | +printfn $"Result with 30s timeout (should succeed): {result2}" |
| 65 | + |
| 66 | +slowAgent |> Agent.dispose |
| 67 | + |
| 68 | + |
| 69 | +// ============================================================ |
| 70 | +// 3. Test the fix: after patching Agent.fs line 289 |
| 71 | +// Change: tryPostAndReply 1000 → tryPostAndReply 30_000 |
| 72 | +// ============================================================ |
| 73 | + |
| 74 | +// After the fix, this should work without setting DefaultTimeout: |
| 75 | +let slowAgent2 = |
| 76 | + Agent.createReply<string, string>(fun msg -> |
| 77 | + Thread.Sleep(2000) |
| 78 | + $"processed: {msg}" |
| 79 | + ) |
| 80 | + |
| 81 | +let result3 = |
| 82 | + try |
| 83 | + slowAgent2 |
| 84 | + |> Agent.postAndReply "test-after-fix" |
| 85 | + |> Some |
| 86 | + with ex -> |
| 87 | + printfn $"Still failing after fix: {ex.Message}" |
| 88 | + None |
| 89 | + |
| 90 | +printfn $"Result after fix (should succeed): {result3}" |
| 91 | + |
| 92 | +slowAgent2 |> Agent.dispose |
| 93 | + |
| 94 | + |
| 95 | +// ============================================================ |
| 96 | +// 4. Verify fast agents still work fine |
| 97 | +// ============================================================ |
| 98 | + |
| 99 | +let fastAgent = |
| 100 | + Agent.createReply<int, int>(fun n -> n * 2) |
| 101 | + |
| 102 | +let result4 = fastAgent |> Agent.postAndReply 21 |
| 103 | +printfn $"Fast agent result (should be 42): {result4}" |
| 104 | + |
| 105 | +fastAgent |> Agent.dispose |
| 106 | + |
| 107 | + |
| 108 | +// ============================================================ |
| 109 | +// 5. Test stateful agent with slow init |
| 110 | +// ============================================================ |
| 111 | + |
| 112 | +let statefulAgent = |
| 113 | + Agent.createStatefulReply<string, string, int>( |
| 114 | + 0, |
| 115 | + fun state msg -> |
| 116 | + Thread.Sleep(1500) // simulate moderate work |
| 117 | + let newState = state + 1 |
| 118 | + $"call #{newState}: {msg}", newState |
| 119 | + ) |
| 120 | + |
| 121 | +let result5 = |
| 122 | + try |
| 123 | + statefulAgent |
| 124 | + |> Agent.postAndReply "stateful-test" |
| 125 | + |> Some |
| 126 | + with ex -> |
| 127 | + printfn $"Stateful agent failed: {ex.Message}" |
| 128 | + None |
| 129 | + |
| 130 | +printfn $"Stateful agent result (should succeed after fix): {result5}" |
| 131 | + |
| 132 | +statefulAgent |> Agent.dispose |
| 133 | + |
| 134 | +printfn "\nAll tests complete." |
0 commit comments