-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathindex.html
More file actions
298 lines (275 loc) · 11.6 KB
/
index.html
File metadata and controls
298 lines (275 loc) · 11.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chaos Testing — aimock</title>
<link rel="icon" type="image/svg+xml" href="../favicon.svg" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&family=Instrument+Sans:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="../style.css" />
</head>
<body>
<nav class="top-nav">
<div class="nav-inner">
<div style="display: flex; align-items: center; gap: 1rem">
<button
class="sidebar-toggle"
onclick="document.querySelector('.sidebar').classList.toggle('open')"
aria-label="Toggle sidebar"
>
☰
</button>
<a href="/" class="nav-brand"> <span class="prompt">$</span> aimock </a>
</div>
<ul class="nav-links">
<li><a href="/">Home</a></li>
<li><a href="/docs" style="color: var(--accent)">Docs</a></li>
<li>
<a href="https://github.com/CopilotKit/aimock" class="gh-link" target="_blank"
><svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"
/>
</svg>
GitHub</a
>
</li>
</ul>
</div>
</nav>
<div class="docs-layout">
<aside class="sidebar" id="sidebar"></aside>
<main class="docs-content">
<h1>Chaos Testing</h1>
<p class="lead">
aimock provides probabilistic failure injection to test how your application handles
unreliable LLM APIs. Three failure modes can be configured at the server, fixture, or
per-request level.
</p>
<h2>Failure Modes</h2>
<table class="endpoint-table">
<thead>
<tr>
<th>Mode</th>
<th>Action</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>drop</code></td>
<td>HTTP 500</td>
<td>
Returns a 500 error with
<code>{"error":{"message":"Chaos: request dropped","code":"chaos_drop"}}</code>
</td>
</tr>
<tr>
<td><code>malformed</code></td>
<td>Broken JSON</td>
<td>
Returns HTTP 200 with invalid JSON body:
<code>{malformed json: <<<chaos>>></code>
</td>
</tr>
<tr>
<td><code>disconnect</code></td>
<td>Connection destroyed</td>
<td>Destroys the TCP connection immediately with no response</td>
</tr>
</tbody>
</table>
<h2>Precedence</h2>
<p>
Chaos configuration is resolved with a three-level precedence hierarchy. Higher levels
override lower ones:
</p>
<ol>
<li><strong>Per-request headers</strong> (highest) — override everything</li>
<li><strong>Fixture-level config</strong> — overrides server defaults</li>
<li><strong>Server-level defaults</strong> (lowest)</li>
</ol>
<p>
Within a single level, modes are evaluated in order: drop, malformed, disconnect. The
first mode that triggers (based on its probability) wins.
</p>
<h2>Quick Start</h2>
<div class="code-block">
<div class="code-block-header">chaos-quick-start.ts <span class="lang-tag">ts</span></div>
<pre><code><span class="kw">import</span> { <span class="op">LLMock</span> } <span class="kw">from</span> <span class="str">"@copilotkit/aimock"</span>;
<span class="kw">const</span> <span class="op">mock</span> = <span class="kw">new</span> <span class="fn">LLMock</span>();
<span class="op">mock</span>.<span class="fn">onMessage</span>(<span class="str">"hello"</span>, { <span class="prop">content</span>: <span class="str">"Hi!"</span> });
<span class="cmt">// 50% of all requests will be dropped with a 500</span>
<span class="op">mock</span>.<span class="fn">setChaos</span>({ <span class="prop">dropRate</span>: <span class="num">0.5</span> });
<span class="kw">await</span> <span class="op">mock</span>.<span class="fn">start</span>();
<span class="cmt">// Later, remove chaos</span>
<span class="op">mock</span>.<span class="fn">clearChaos</span>();</code></pre>
</div>
<h2>Programmatic API</h2>
<div class="code-block">
<div class="code-block-header">
Programmatic chaos control <span class="lang-tag">ts</span>
</div>
<pre><code><span class="cmt">// Set server-level chaos (returns `this` for chaining)</span>
<span class="op">mock</span>.<span class="fn">setChaos</span>({
<span class="prop">dropRate</span>: <span class="num">0.1</span>, <span class="cmt">// 10% drop rate</span>
<span class="prop">malformedRate</span>: <span class="num">0.05</span>, <span class="cmt">// 5% malformed rate</span>
<span class="prop">disconnectRate</span>: <span class="num">0.02</span>, <span class="cmt">// 2% disconnect rate</span>
});
<span class="cmt">// Remove all server-level chaos</span>
<span class="op">mock</span>.<span class="fn">clearChaos</span>();</code></pre>
</div>
<h2>Fixture-Level Chaos</h2>
<p>
Attach a <code>chaos</code> config to individual fixtures so only specific responses
experience failures:
</p>
<div class="code-block">
<div class="code-block-header">chaos-fixture.json <span class="lang-tag">json</span></div>
<pre><code>{
"fixtures": [
{
"match": { "userMessage": "unstable" },
"response": { "content": "This might fail!" },
"chaos": {
"dropRate": 0.3,
"malformedRate": 0.2,
"disconnectRate": 0.1
}
},
{
"match": { "userMessage": "stable" },
"response": { "content": "This always works." }
}
]
}</code></pre>
</div>
<h2>Per-Request Headers</h2>
<p>
Override chaos rates on individual requests using HTTP headers. Values are floats between
0 and 1:
</p>
<table class="endpoint-table">
<thead>
<tr>
<th>Header</th>
<th>Controls</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>x-aimock-chaos-drop</code></td>
<td>Drop rate (0–1)</td>
</tr>
<tr>
<td><code>x-aimock-chaos-malformed</code></td>
<td>Malformed rate (0–1)</td>
</tr>
<tr>
<td><code>x-aimock-chaos-disconnect</code></td>
<td>Disconnect rate (0–1)</td>
</tr>
</tbody>
</table>
<div class="code-block">
<div class="code-block-header">
Per-request chaos via headers <span class="lang-tag">ts</span>
</div>
<pre><code><span class="cmt">// Force 100% disconnect on this specific request</span>
<span class="kw">await</span> <span class="fn">fetch</span>(<span class="str">`${mock.url}/v1/chat/completions`</span>, {
<span class="prop">method</span>: <span class="str">"POST"</span>,
<span class="prop">headers</span>: {
<span class="str">"Content-Type"</span>: <span class="str">"application/json"</span>,
<span class="str">"x-aimock-chaos-disconnect"</span>: <span class="str">"1.0"</span>,
},
<span class="prop">body</span>: <span class="fn">JSON.stringify</span>({ <span class="prop">model</span>: <span class="str">"gpt-4"</span>, <span class="prop">messages</span>: [...] }),
});</code></pre>
</div>
<h2>CLI Flags</h2>
<p>Set server-level chaos from the command line:</p>
<div class="cli-docker-tabs">
<div class="tab-cli">
<div class="code-block">
<div class="code-block-header">
CLI chaos flags <span class="lang-tag">shell</span>
</div>
<pre><code>$ npx @copilotkit/aimock --fixtures ./fixtures \
--chaos-drop 0.1 \
--chaos-malformed 0.05 \
--chaos-disconnect 0.02</code></pre>
</div>
</div>
<div class="tab-docker">
<div class="code-block">
<div class="code-block-header">
CLI chaos flags <span class="lang-tag">shell</span>
</div>
<pre><code>$ docker run -d -p 4010:4010 \
-v ./fixtures:/fixtures \
ghcr.io/copilotkit/aimock \
npx @copilotkit/aimock --fixtures /fixtures \
--chaos-drop 0.1 \
--chaos-malformed 0.05 \
--chaos-disconnect 0.02</code></pre>
</div>
</div>
</div>
<h2>Journal Tracking</h2>
<p>
When chaos triggers, the journal entry includes a <code>chaosAction</code> field recording
which failure mode was applied:
</p>
<div class="code-block">
<div class="code-block-header">
Journal entry with chaos <span class="lang-tag">json</span>
</div>
<pre><code>{
"method": "POST",
"path": "/v1/chat/completions",
"response": {
"status": 500,
"fixture": { "..." },
"chaosAction": "drop"
}
}</code></pre>
</div>
<p>
The <code>chaosAction</code> values are <code>"drop"</code>, <code>"malformed"</code>, or
<code>"disconnect"</code>. The status codes are 500 for drop, 200 for malformed, and 0 for
disconnect (connection destroyed).
</p>
<h2>Prometheus Metrics</h2>
<p>
When metrics are enabled (<code>--metrics</code>), each chaos trigger increments the
<code>aimock_chaos_triggered_total</code> counter with an <code>action</code> label:
</p>
<div class="code-block">
<div class="code-block-header">Metrics output <span class="lang-tag">text</span></div>
<pre><code># TYPE aimock_chaos_triggered_total counter
aimock_chaos_triggered_total{action="drop"} 3
aimock_chaos_triggered_total{action="malformed"} 1
aimock_chaos_triggered_total{action="disconnect"} 2</code></pre>
</div>
</main>
<aside class="page-toc" id="page-toc"></aside>
</div>
<footer class="docs-footer">
<div class="footer-inner">
<div class="footer-left"><span>$</span> aimock · MIT License</div>
<ul class="footer-links">
<li><a href="https://github.com/CopilotKit/aimock" target="_blank">GitHub</a></li>
<li>
<a href="https://www.npmjs.com/package/@copilotkit/aimock" target="_blank">npm</a>
</li>
</ul>
</div>
</footer>
<script src="../sidebar.js"></script>
<script src="../cli-tabs.js"></script>
</body>
</html>