Open History to see the last commit
1#!/usr/bin/env bash
2# mock-provider.sh — Deterministic JSON-RPC stdio mock provider
3#
4# Simulates a Codex-like app-server that responds to JSON-RPC over stdio.
5# Used for the GC cross-contamination stress test (Test B).
6#
7# Usage: mock-provider.sh [deltas=10] [delay_ms=100] [memory_kb=0]
8#
9# Behavior:
10# - Responds to "initialize" with {ok: true}
11# - Responds to "thread/start" with a thread ID
12# - On "turn/start": emits turn/started, N item/agentMessage/delta events
13# at fixed intervals, then turn/completed
14# - If memory_kb > 0, allocates garbage string per delta (for GC testing)
15
16set -euo pipefail
17
18DELTAS="${1:-10}"
19DELAY_MS="${2:-100}"
20MEMORY_KB="${3:-0}"
21
22DELAY_S=$(echo "scale=3; $DELAY_MS / 1000" | bc)
23
24# Generate garbage string of ~N KB
25generate_garbage() {
26 local kb="$1"
27 if [ "$kb" -le 0 ]; then
28 echo ""
29 return
30 fi
31 # Each char ~1 byte, so kb*1024 chars
32 head -c "$((kb * 1024))" /dev/urandom | base64 | head -c "$((kb * 1024))"
33}
34
35send_notification() {
36 local method="$1"
37 local params="$2"
38 echo "{\"jsonrpc\":\"2.0\",\"method\":\"$method\",\"params\":$params}"
39}
40
41send_response() {
42 local id="$1"
43 local result="$2"
44 echo "{\"jsonrpc\":\"2.0\",\"id\":$id,\"result\":$result}"
45}
46
47THREAD_COUNTER=0
48TURN_COUNTER=0
49
50while IFS= read -r line; do
51 # Parse JSON-RPC request
52 id=$(echo "$line" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('id','null'))" 2>/dev/null || echo "null")
53 method=$(echo "$line" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('method',''))" 2>/dev/null || echo "")
54 params=$(echo "$line" | python3 -c "import sys,json; d=json.load(sys.stdin); print(json.dumps(d.get('params',{})))" 2>/dev/null || echo "{}")
55
56 case "$method" in
57 initialize)
58 send_response "$id" '{"ok":true,"serverInfo":{"name":"mock-provider","version":"1.0.0"}}'
59 ;;
60
61 "thread/start")
62 THREAD_COUNTER=$((THREAD_COUNTER + 1))
63 thread_id="mock-thread-${THREAD_COUNTER}"
64 send_response "$id" "{\"threadId\":\"$thread_id\"}"
65 ;;
66
67 "turn/start")
68 TURN_COUNTER=$((TURN_COUNTER + 1))
69 turn_id="mock-turn-${TURN_COUNTER}"
70
71 # Acknowledge the request
72 send_response "$id" "{\"turnId\":\"$turn_id\"}"
73
74 # Emit turn/started
75 send_notification "turn/started" "{\"turn\":{\"id\":\"$turn_id\",\"status\":\"running\"}}"
76
77 # Emit deltas
78 for i in $(seq 1 "$DELTAS"); do
79 sleep "$DELAY_S"
80 garbage=""
81 if [ "$MEMORY_KB" -gt 0 ]; then
82 garbage=$(generate_garbage "$MEMORY_KB")
83 fi
84 delta_text="Delta $i of $DELTAS"
85 if [ -n "$garbage" ]; then
86 # Include garbage in metadata (forces allocation in consumer)
87 garbage_bytes=$((MEMORY_KB * 1024))
88 send_notification "item/agentMessage/delta" "{\"delta\":\"$delta_text\",\"_garbage\":\"${garbage:0:$garbage_bytes}\"}"
89 else
90 send_notification "item/agentMessage/delta" "{\"delta\":\"$delta_text\"}"
91 fi
92 done
93
94 # Emit turn/completed
95 send_notification "turn/completed" "{\"turn\":{\"id\":\"$turn_id\",\"status\":\"completed\"}}"
96 ;;
97
98 "turn/cancel"|"session/stop")
99 send_response "$id" '{"ok":true}'
100 ;;
101
102 *)
103 # Unknown method — reply with error
104 if [ "$id" != "null" ]; then
105 echo "{\"jsonrpc\":\"2.0\",\"id\":$id,\"error\":{\"code\":-32601,\"message\":\"Method not found: $method\"}}"
106 fi
107 ;;
108 esac
109done