-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
335 lines (308 loc) · 18.1 KB
/
Copy pathindex.html
File metadata and controls
335 lines (308 loc) · 18.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Agent Loop Detector — catch an AI agent stuck in a loop</title>
<meta name="description" content="Paste an autonomous agent's run — one step per line — and Jev, TypeSafe AI's decision model, grades each step (progress / repeating / stuck / escalate) with one typed API call, then flags when the agent is looping. A single-file open-source tool on the Jev decision API." />
<style>
:root{
--bg:#0b0d13; --panel:#12141c; --panel2:#171a24; --border:#242838; --border2:#2f3446;
--text:#e9ebf2; --muted:#8b90a6; --accent:#7c8cff; --rose:#ff6b6b; --cyan:#6be0ff;
--amber:#edc22e; --lime:#7ff0a8; --mono:ui-monospace,SFMono-Regular,Menlo,monospace;
--radius:14px;
}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,system-ui,Segoe UI,Roboto,sans-serif;
background-image:radial-gradient(1200px 600px at 70% -10%,rgba(124,140,255,.10),transparent);}
.wrap{max-width:680px;margin:0 auto;padding:26px 16px 40px}
a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
h1{font-size:26px;letter-spacing:-.02em;margin:0 0 4px}
.lede{color:var(--muted);margin:0 0 16px;font-size:14.5px}
.live{display:inline-flex;align-items:center;gap:6px;font-weight:600;font-size:13.5px;
background:rgba(124,140,255,.12);border:1px solid rgba(124,140,255,.4);border-radius:999px;padding:4px 12px;margin-bottom:16px}
.set{border:1px solid var(--border);background:var(--panel);border-radius:12px;padding:12px 14px;margin-bottom:16px}
.set summary{cursor:pointer;font-size:13.5px;color:var(--muted);list-style:none}
.set summary::-webkit-details-marker{display:none}
.set summary b{color:var(--text)}
.set-body{margin-top:12px;display:flex;flex-direction:column;gap:10px}
.set label{font-size:12px;color:var(--muted);display:block;margin-bottom:4px}
.set input,.set select{width:100%;background:var(--bg);border:1px solid var(--border2);color:var(--text);
border-radius:8px;padding:8px 10px;font:13px var(--mono)}
.set .row{display:flex;gap:10px}.set .row>div{flex:1}
.set .save{align-self:flex-start;background:var(--accent);color:#0b0d13;border:none;border-radius:8px;
padding:8px 16px;font-weight:700;cursor:pointer;font-size:13px}
.set .hint{font-size:12px;color:var(--muted)}
.btn{border:1px solid var(--border2);background:var(--panel2);color:var(--text);border-radius:9px;
padding:9px 15px;font-size:14px;font-weight:600;cursor:pointer}
.btn:hover{border-color:var(--accent)}
.btn:disabled{opacity:.5;cursor:default;border-color:var(--border2)}
.btn-primary{background:var(--accent);color:#0b0d13;border-color:var(--accent)}
/* tool body */
.label{font-size:13px;color:var(--muted);margin-bottom:8px}
.ta{width:100%;background:var(--bg);border:1px solid var(--border2);color:var(--text);border-radius:10px;
padding:11px 12px;font:13px/1.55 var(--mono);resize:vertical;min-height:150px}
.ta:focus{outline:none;border-color:var(--accent)}
.top{display:flex;gap:12px;align-items:center;flex-wrap:wrap;margin-top:12px}
.hint2{font-size:12.5px;color:var(--muted);flex:1;min-width:200px}
.stats{font-family:var(--mono);font-size:12.5px;color:var(--muted);display:flex;gap:10px;flex-wrap:wrap;margin:14px 0}
.stats b{color:var(--text)}
.verdict{display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap;
margin:2px 0 14px;padding:11px 14px;border-radius:10px;font-size:14.5px;line-height:1.5;border:1px solid var(--border)}
.verdict.bad{border-color:rgba(255,107,107,.5);background:rgba(255,107,107,.08)}
.verdict.ok{border-color:rgba(127,240,168,.4);background:rgba(127,240,168,.07)}
.copy{background:none;border:1px solid var(--border2);color:var(--text);border-radius:6px;font-size:12.5px;padding:5px 11px;cursor:pointer;flex:none}
.copy:hover{border-color:var(--accent)}
.trace{display:flex;flex-direction:column;gap:6px}
.step{display:grid;grid-template-columns:14px 22px 1fr auto;gap:10px;align-items:center;padding:9px 12px;
border:1px solid var(--border);border-radius:10px;background:var(--panel2);opacity:.5;transition:opacity .25s}
.step.in{opacity:1}
.step.fail{border-color:rgba(237,194,46,.5)}
.sdot{width:10px;height:10px;border-radius:50%;background:#3a3a42}
.snum{font-size:12px;color:var(--muted);font-variant-numeric:tabular-nums}
.stext{margin:0;font-size:14px;line-height:1.45;color:var(--text);word-break:break-word}
.sstate{display:flex;align-items:center;gap:8px;white-space:nowrap}
.vb{font-weight:700;font-size:12.5px}
.vb-ok{color:var(--lime)}.vb-warn{color:var(--amber)}.vb-bad{color:var(--rose)}
.conf{font-size:12px;color:var(--muted)}
.dim{color:var(--muted)}
.err{margin-top:12px;color:var(--amber);font-size:13.5px}
.cta-line{font-size:13.5px;color:var(--muted);margin-top:18px}
.disc{font-size:12.5px;color:var(--muted);margin-top:6px;line-height:1.6}
footer{margin-top:28px;text-align:center;font-size:12.5px;color:var(--muted);line-height:1.8}
@media (max-width:560px){
.step{grid-template-columns:14px 1fr;row-gap:6px}
.snum{display:none}
.sstate{grid-column:1/-1;justify-content:flex-start}
}
</style>
</head>
<body>
<div class="wrap">
<a class="live" href="https://jevtypesafeai.com/tools/agent-loop-detector" target="_blank" rel="noopener">▶ Use it live (no setup) on jevtypesafeai.com</a>
<h1>Agent Loop Detector</h1>
<p class="lede">Paste an autonomous agent's run — one step per line. <b>Jev</b> — TypeSafe AI's decision model — grades each step in one real API call: <b>progress</b>, <b>repeating</b>, <b>stuck</b>, or <b>escalate</b> — then flags when the agent is looping so a supervisor can halt it before it burns tokens.</p>
<details class="set" id="settings">
<summary>⚙︎ <b>Jev API key</b> — needed to run locally (or just <a href="https://jevtypesafeai.com/tools/agent-loop-detector" target="_blank" rel="noopener">use it live</a>)</summary>
<div class="set-body">
<div class="row">
<div><label>API key</label><input id="k-key" type="password" placeholder="jv_live_… (from jevtypesafeai.com) or ts_… (TypeSafe)" /></div>
</div>
<div><label>Endpoint</label>
<select id="k-base">
<option value="https://jevtypesafeai.com/api/v1/decide">jevtypesafeai.com — hosted (browser-ready)</option>
<option value="https://api.typesafe.ai/v1/systemone">api.typesafe.ai — official (may need a proxy: CORS)</option>
</select>
</div>
<button class="save" id="k-save">Save</button>
<p class="hint">Get a key free at <a href="https://jevtypesafeai.com" target="_blank" rel="noopener">jevtypesafeai.com</a>. Your key is stored only in this browser (localStorage) and sent straight to the endpoint above.</p>
</div>
</details>
<div class="label">Paste an agent's run — one step per line. (Sample loaded — edit or paste your own trace.)</div>
<textarea class="ta" id="ta" rows="9" spellcheck="false"></textarea>
<div class="top">
<button class="btn btn-primary" id="run">Grade steps</button>
<span class="hint2">Each step is one Jev decision — real progress, a repeated action, spinning in place, or something only a human can unblock — so a supervisor can stop a runaway loop.</span>
</div>
<div class="stats" id="stats"></div>
<div id="verdict"></div>
<div class="trace" id="trace"></div>
<div class="err" id="err" style="display:none"></div>
<p class="cta-line">Wrap your agent loop in a Jev health check and auto-halt on STUCK / ESCALATE. <a href="https://jevtypesafeai.com" target="_blank" rel="noopener">Get an API key →</a></p>
<p class="disc">A live demo — the same typed <code>choice</code> returns a clean enum you switch on directly (no parsing). Your text is sent to Jev only to grade each step and is not stored.</p>
<footer>
Built on <a href="https://jevtypesafeai.com" target="_blank" rel="noopener"><b>Jev</b></a> — TypeSafe AI's System One decision model.<br/>
Every grade is a real typed decision. <a href="https://jevtypesafeai.com/tools/agent-loop-detector" target="_blank" rel="noopener">Use it live</a> · <a href="https://jevtypesafeai.com" target="_blank" rel="noopener">Get an API key</a> · <a href="https://github.com/codaaiteam/jev-loop-detector" target="_blank" rel="noopener">Source</a>
</footer>
</div>
<script>
// ---------------------------------------------------------------------------
// Jev connection — one typed decision per call. Works with a key from
// jevtypesafeai.com (hosted, browser-ready) or the official TypeSafe API.
// (Shared harness — identical across the jev-agent-tools single-file tools.)
// ---------------------------------------------------------------------------
const DEFAULT_BASE = "https://jevtypesafeai.com/api/v1/decide";
const JEV = {
get key(){ try { return localStorage.getItem("jev_key") || ""; } catch { return ""; } },
set key(v){ try { localStorage.setItem("jev_key", v); } catch {} },
get base(){ try { return localStorage.getItem("jev_base") || DEFAULT_BASE; } catch { return DEFAULT_BASE; } },
set base(v){ try { localStorage.setItem("jev_base", v); } catch {} },
};
async function jevDecide(state, questions){
if(!JEV.key) { const e = new Error("Add your Jev API key in settings above (or use it live)."); e.code = "nokey"; throw e; }
const res = await fetch(JEV.base, {
method:"POST",
headers:{ "Content-Type":"application/json", "Authorization":"Bearer "+JEV.key },
body: JSON.stringify({ model:"jev-latest", state, questions }),
});
if(res.status===401){ const e=new Error("Invalid or missing API key."); e.code=401; throw e; }
if(res.status===429){ const e=new Error("Rate limited — one moment."); e.code=429; throw e; }
const data = await res.json().catch(()=>({}));
if(!res.ok) throw new Error(data.error || ("HTTP "+res.status));
return data; // { answers:{k:{choice,probabilities,confidence}}, usage:{input_tokens} }
}
// settings UI
(function(){
const key = document.getElementById("k-key"), base = document.getElementById("k-base");
key.value = JEV.key; base.value = JEV.base;
if(!JEV.key) document.getElementById("settings").open = true;
document.getElementById("k-save").onclick = () => {
JEV.key = key.value.trim(); JEV.base = base.value;
document.getElementById("settings").open = false;
};
})();
// ---------------------------------------------------------------------------
// Agent Loop Detector — a BATCH tool. Each step is one typed `choice` decision
// (progress / repeating / stuck / escalate); the verdict flags a loop.
// Items run in parallel through the shared harness with a small concurrency cap.
// ---------------------------------------------------------------------------
const JEV_COST_PER_MTOK = 0.42, MAX_STEPS = 40, POOL = 8;
// Same question the React demo / demoConfigs `loopcheck` uses (LOOP_Q).
const QUESTION = {
state: {
type: "choice",
instructions: "Judge this step of an autonomous agent's run in context of a run that should be making progress.",
criteria: {
progress: "real progress — new ground, new information",
repeating: "repeating a previous action with no new result",
stuck: "no progress and out of ideas — spinning",
escalate: "needs a human or access it cannot get",
},
},
};
const buildState = (step) => `One step from an autonomous agent's run:\n"${step}"`;
const STATE = {
progress: { label: "PROGRESS", cls: "ok", dot: "#3fae6a" },
repeating:{ label: "REPEATING", cls: "warn", dot: "#e6a94b" },
stuck: { label: "STUCK", cls: "bad", dot: "#e0564f" },
escalate: { label: "ESCALATE", cls: "bad", dot: "#c2559e" },
};
const SAMPLE_LOOP_STEPS = [
"Read the failing test output: 3 of 20 tests fail in auth.test.js with 'token undefined'.",
"Opened auth.js and found getToken() returns undefined when the header is missing.",
"Added a null check in getToken(); re-ran tests — now 2 fail.",
"Edited getToken() again with the same null check; re-ran tests — still 2 fail.",
"Edited getToken() a third time with essentially the same change; still 2 fail.",
"Re-ran the exact same test command a fourth time; identical failure, no new information.",
"Tried to read the CI secret JWT_SIGNING_KEY to reproduce locally, but it is not accessible.",
"Searched the repo for JWT_SIGNING_KEY; only referenced in the deploy config I can't open.",
"Discovered the token is signed by an external auth service; found its docs and the expected header format.",
"Updated the test setup to inject a valid signed token per the docs; re-ran — now 1 fails.",
"Same assertion has failed 5 times with no change in approach or output.",
"Blocked: fixing the last test needs a staging credential only the platform team can grant.",
];
const parse = (t) => t.split("\n").map((s) => s.trim()).filter(Boolean).slice(0, MAX_STEPS);
function errMsg(e){
if(e && e.code===429) return "Rate limited — wait a minute.";
if(e && e.code==="nokey") return "Add your Jev API key above (or use it live on the site).";
return (e && e.message) ? String(e.message) : "Something went wrong — try again.";
}
const $ = (id) => document.getElementById(id);
const taEl = $("ta"), runBtn = $("run");
taEl.value = SAMPLE_LOOP_STEPS.join("\n");
let ran = SAMPLE_LOOP_STEPS.slice(); // steps the trace renders
let results = []; // per index: {state,conf} | {error:true} | null
let running = false, doneRun = false;
let cost = 0, count = 0, tStart = 0, elapsedMs = 0, globalErr = "";
let elapsedTimer = null;
function esc(s){ return String(s).replace(/[&<>]/g, c => ({ "&":"&", "<":"<", ">":">" }[c])); }
function verdictData(){
const counts = { progress:0, repeating:0, stuck:0, escalate:0 };
for(const r of results){ if(r && r.state && counts[r.state]!=null) counts[r.state]++; }
let stall=0, run=0;
for(const r of results){ if(r && r.state && r.state!=="progress"){ run++; stall=Math.max(stall,run); } else run=0; }
const flagged = doneRun && (counts.stuck>0 || counts.escalate>0 || stall>=3);
return { counts, stall, flagged };
}
function renderStats(){
const total = ran.length;
$("stats").innerHTML =
`<span><b>${count}</b>/${total} graded</span>` +
`<span>· <b>${(elapsedMs/1000).toFixed(1)}</b>s</span>` +
`<span>· <b>$${cost.toFixed(5)}</b></span>`;
}
function renderVerdict(){
const box = $("verdict");
if(!doneRun){ box.innerHTML=""; return; }
const { counts, stall, flagged } = verdictData();
const body = flagged
? `<div><b>Loop detected — intervene.</b> ${counts.stuck + counts.escalate} step(s) flagged stuck/escalate, longest stall ${stall} steps.</div>`
: `<div><b>Healthy run.</b> The agent kept making progress — no sustained stall.</div>`;
box.innerHTML = `<div class="verdict ${flagged ? "bad" : "ok"}">${body}${flagged ? `<button class="copy" id="copyflag">Copy flagged steps</button>` : ""}</div>`;
if(flagged){ $("copyflag").onclick = copyFlagged; }
}
function renderTrace(){
$("trace").innerHTML = ran.map((step, i) => {
const r = results[i];
const st = r && r.state ? (STATE[r.state] || { label:r.state, cls:"warn", dot:"#e6a94b" }) : null;
const pct = r && r.conf != null ? Math.round(r.conf*100) : null;
const failed = r && r.error;
const stateCell = failed
? `<span class="vb vb-warn">failed</span>`
: st
? `<b class="vb vb-${st.cls}">${st.label}</b>${pct!=null ? `<span class="conf">${pct}%</span>` : ""}`
: `<span class="dim">${running ? "…" : "—"}</span>`;
return `<div class="step${r ? " in" : ""}${failed ? " fail" : ""}">
<span class="sdot" style="background:${st ? st.dot : (failed ? "var(--amber)" : "#3a3a42")}"></span>
<span class="snum">${i+1}</span>
<p class="stext">${esc(step)}</p>
<span class="sstate">${stateCell}</span>
</div>`;
}).join("");
}
function renderAll(){ renderStats(); renderVerdict(); renderTrace(); renderErr(); }
function renderErr(){ const e=$("err"); if(globalErr){ e.style.display="block"; e.textContent=globalErr; } else e.style.display="none"; }
function copyFlagged(){
const text = ran.map((step, i) => {
const r = results[i]; if(!r || !r.state || r.state==="progress") return null;
return `[${(STATE[r.state]||{}).label || r.state}] ${step}`;
}).filter(Boolean).join("\n");
try { navigator.clipboard.writeText(text); } catch {}
const b = $("copyflag"); if(b){ const o=b.textContent; b.textContent="Copied ✓"; setTimeout(()=>b.textContent=o, 1200); }
}
function syncBtn(){
const n = parse(taEl.value).length;
runBtn.disabled = running || !n;
runBtn.textContent = running ? "Grading…" : `Grade ${n} step${n===1?"":"s"}`;
}
async function run(){
const steps = parse(taEl.value);
if(!steps.length || running) return;
ran = steps;
results = new Array(steps.length).fill(null);
running = true; doneRun = false; cost = 0; count = 0; globalErr = ""; tStart = performance.now(); elapsedMs = 0;
syncBtn(); renderAll();
elapsedTimer = setInterval(() => { elapsedMs = performance.now()-tStart; renderStats(); }, 200);
let idx = 0;
async function worker(){
while(idx < steps.length){
const i = idx++;
try {
const data = await jevDecide(buildState(steps[i]), QUESTION);
const ans = (data && data.answers && data.answers.state) || {};
const choice = ans.choice;
const probs = ans.probabilities || ans.probs || {};
const conf = typeof probs[choice]==="number" ? probs[choice]
: typeof ans.confidence==="number" ? ans.confidence : null;
cost += ((data && data.usage && data.usage.input_tokens || 0)/1e6) * JEV_COST_PER_MTOK;
results[i] = { state: choice, conf };
} catch(e){
results[i] = { error: true };
if(!globalErr) globalErr = errMsg(e);
}
count++;
renderStats(); renderTrace(); renderErr();
}
}
await Promise.all(Array.from({ length: Math.min(POOL, steps.length) }, worker));
clearInterval(elapsedTimer); elapsedTimer = null;
elapsedMs = performance.now()-tStart;
running = false; doneRun = true;
syncBtn(); renderAll();
}
runBtn.addEventListener("click", run);
taEl.addEventListener("input", () => { if(!running) syncBtn(); });
syncBtn(); renderAll();
</script>
</body>
</html>