Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(agent): a switch turns the loop guard off (C44)

purple90904797b88a
diff --git a/src/components/settings/SettingsPage.tsx b/src/components/settings/SettingsPage.tsx
index 09736816..957d84f9 100644
--- a/src/components/settings/SettingsPage.tsx
+++ b/src/components/settings/SettingsPage.tsx
@@ -2397,6 +2397,16 @@ export function SettingsPage() {
{FEATURE_FLAGS.AGENT_MODE && (
<Section title="Agent Permissions">
<PermissionSettings />
+ {/* C44 (Discord 05.11.2026): the loop guard can be switched off
+ completely. On by default (owner, 05.11.). */}
+ <InlineToggle
+ label="Loop guard"
+ enabled={settings.agentLoopGuard !== false}
+ onChange={() => updateSettings({ agentLoopGuard: settings.agentLoopGuard === false })}
+ />
+ <div className="t-micro text-gray-500 dark:text-gray-500 leading-relaxed pt-0.5">
+ Stops an agent run that keeps repeating the same steps (&quot;Loop guard halted the run&quot;). Off: the run goes on until its step limit or until you press Stop.
+ </div>
<button
onClick={() => useAgentModeStore.getState().resetTutorial()}
className="text-[0.6rem] text-gray-500 hover:text-gray-300 transition-colors"
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index 66633f50..fc6304e5 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -1637,7 +1637,7 @@ export function useAgentChat() {
// loop relied on the exact-repeat set alone, which the epoch reset (B1)
// rightly weakened. Windowed batch repeats, per-epoch identical reads and
// repeated narration now watch this loop too.
- const loopGuard = new AgentLoopGuard()
+ const loopGuard = new AgentLoopGuard({ enabled: useSettingsStore.getState().settings.agentLoopGuard !== false })
// The closing line a turn gets when the model itself said nothing usable.
// Read from the live counters at call time, so both the normal end of the
// run and the swallowed multimodal refusal below produce the same text.
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 401cffb6..7433f604 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -1231,7 +1231,7 @@ export function useCodex() {
// alternating calls, an injected nudge, or one varying argument reset
// it forever while the budget allowed 200 iterations of the loop
// (Morgan's 5-minute file_read loop, 2026-07-26).
- const loopGuard = new AgentLoopGuard()
+ const loopGuard = new AgentLoopGuard({ enabled: useSettingsStore.getState().settings.agentLoopGuard !== false })
// Which read produced which result message. The request builder reports
// back the results it sent CAPPED, and this map turns those messages
// into the loop-guard keys that must not be counted as a repeat (plan
diff --git a/src/lib/__tests__/loop-guard-switch.test.ts b/src/lib/__tests__/loop-guard-switch.test.ts
new file mode 100644
index 00000000..e13c9f87
--- /dev/null
+++ b/src/lib/__tests__/loop-guard-switch.test.ts
@@ -0,0 +1,52 @@
+/**
+ * C44 (Discord 05.11.2026, "Loop guard halted the run: same tool sequence
+ * repeated 3×"): a switch in Settings → Agent Permissions turns the loop guard
+ * off completely. On by default (owner, 05.11.).
+ *
+ * Run: npx vitest run src/lib/__tests__/loop-guard-switch.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { resolve } from 'node:path'
+import { AgentLoopGuard } from '../agent-loop-guard'
+import { DEFAULT_SETTINGS } from '../constants'
+import { findEntry } from '../settings-catalog'
+
+const batch = [{ name: 'file_read', args: '{"path":"a.ts"}' }]
+const src = (p: string) => readFileSync(resolve(__dirname, '..', '..', p), 'utf8')
+
+/** Feed the same batch until the guard halts or `n` rounds pass. */
+function verdicts(guard: AgentLoopGuard, n: number) {
+ const out: string[] = []
+ for (let i = 0; i < n; i++) {
+ out.push(guard.recordNarration('Let me read the file once more and check the rotation engine.').action)
+ out.push(guard.recordBatch(batch).action)
+ out.push(guard.recordResults([{ name: 'file_read', failed: true, error: 'boom' }]).action)
+ }
+ return out
+}
+
+describe('C44: the loop guard can be switched off', () => {
+ it('on (the default): the same batch over and over is halted', () => {
+ expect(verdicts(new AgentLoopGuard(), 8)).toContain('halt')
+ expect(verdicts(new AgentLoopGuard({ enabled: true }), 8)).toContain('halt')
+ })
+
+ it('off: no detector steers or halts, whatever repeats', () => {
+ expect(new Set(verdicts(new AgentLoopGuard({ enabled: false }), 20))).toEqual(new Set(['ok']))
+ })
+
+ it('the setting is on by default and in the catalog', () => {
+ expect(DEFAULT_SETTINGS.agentLoopGuard).toBe(true)
+ expect(findEntry('agentLoopGuard')).toBeTruthy()
+ })
+
+ it('both agent loops build their guard from the setting, and the page has the switch', () => {
+ for (const p of ['hooks/useAgentChat.ts', 'hooks/useCodex.ts']) {
+ expect(src(p)).toContain('new AgentLoopGuard({ enabled: useSettingsStore.getState().settings.agentLoopGuard !== false })')
+ expect(src(p)).not.toContain('new AgentLoopGuard()')
+ }
+ const page = src('components/settings/SettingsPage.tsx')
+ expect(page).toMatch(/label="Loop guard"\s+enabled=\{settings\.agentLoopGuard !== false\}/)
+ })
+})
diff --git a/src/lib/agent-loop-guard.ts b/src/lib/agent-loop-guard.ts
index c51f2f38..5ffe352c 100644
--- a/src/lib/agent-loop-guard.ts
+++ b/src/lib/agent-loop-guard.ts
@@ -140,6 +140,14 @@ const READ_ONLY_TOOLS = new Set<string>([
])
export class AgentLoopGuard {
+ /** Off (Settings → Agent Permissions, C44): every verdict is `ok`, the run
+ * goes on until its step and tool call limits or the user's Stop. */
+ private readonly enabled: boolean
+
+ constructor(opts: { enabled?: boolean } = {}) {
+ this.enabled = opts.enabled !== false
+ }
+
private lastSig: string | null = null
private consecutive = 0
/** Batch signatures since the last mutating batch (pure-read epoch). */
@@ -178,7 +186,7 @@ export class AgentLoopGuard {
calls: Array<{ name: string; args: string }>,
opts: { trimmedReadKeys?: ReadonlySet<string> } = {},
): LoopGuardVerdict {
- if (calls.length === 0) return { action: 'ok' }
+ if (!this.enabled || calls.length === 0) return { action: 'ok' }
const keys = calls.map((c) => `${c.name}|${c.args}`)
const sig = [...keys].sort().join('||')
const trimmed = opts.trimmedReadKeys
@@ -268,7 +276,7 @@ export class AgentLoopGuard {
recordResults(
results: Array<{ name: string; failed: boolean; rejected?: boolean; error?: string; args?: Record<string, unknown> }>,
): LoopGuardVerdict {
- if (results.length === 0) return { action: 'ok' }
+ if (!this.enabled || results.length === 0) return { action: 'ok' }
// ── Detector 7: the user keeps saying no to the same thing ──
// A declined call ran nothing, so it is no success: it resets no count
@@ -372,6 +380,7 @@ export class AgentLoopGuard {
/** Record the assistant text of one iteration (before its tools run). */
recordNarration(text: string): LoopGuardVerdict {
+ if (!this.enabled) return { action: 'ok' }
const norm = text.trim().replace(/\s+/g, ' ').toLowerCase()
if (norm.length < MIN_NARRATION_LEN) {
this.lastNarration = ''
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index f45e501e..2d07d845 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -61,6 +61,7 @@ export const DEFAULT_SETTINGS: Settings = {
// surfaces in finite wall-clock.
agentMaxToolCalls: 400,
agentMaxIterations: 200,
+ agentLoopGuard: true,
// Die heutigen festen Werte aus sub-agent.ts, unveraendert uebernommen:
// wer nichts einstellt, bekommt exakt das Verhalten von 2.6.7.
subAgentMaxToolCalls: 10,
diff --git a/src/lib/settings-catalog.ts b/src/lib/settings-catalog.ts
index cde6f8f8..83140b08 100644
--- a/src/lib/settings-catalog.ts
+++ b/src/lib/settings-catalog.ts
@@ -208,6 +208,7 @@ export const CATALOG: readonly CatalogEntry[] = [
// Agent limits: read at the start of a run, so a run cannot raise its own.
e('agentMaxToolCalls', 'Agent tool call limit', 'agent', 'Most tool calls per message. Applies from the next message.', int(0, 10_000, '', 'unlimited')),
e('agentMaxIterations', 'Agent step limit', 'agent', 'Most steps per message. Applies from the next message.', int(0, 10_000, '', 'unlimited')),
+ e('agentLoopGuard', 'Loop guard', 'agent', 'Stops an agent run that repeats the same steps. Off: the run goes on until its step limit or Stop.', bool()),
e('subAgentMaxToolCalls', 'Sub-agent tool call limit', 'agent', 'Most tool calls of one sub-agent.', int(0, SUB_AGENT_LIMIT_MAX, '', 'default')),
e('subAgentMaxIterations', 'Sub-agent step limit', 'agent', 'Most steps of one sub-agent.', int(0, SUB_AGENT_LIMIT_MAX, '', 'default')),
e('loopMaxPasses', '/loop passes', 'agent', 'How many passes a /loop may run.', int(0, 10_000, '', 'unlimited')),
diff --git a/src/lib/settings-reset.ts b/src/lib/settings-reset.ts
index e2b167c2..5e7a91c5 100644
--- a/src/lib/settings-reset.ts
+++ b/src/lib/settings-reset.ts
@@ -30,7 +30,7 @@ export const SETTINGS_TAB_RESET_KEYS: Record<SettingsTab, (keyof Settings)[]> =
backends: ['apiEndpoint', 'hfDownloadPathOverride', 'downloadLimitMBps', 'exclusiveVramMode', 'builtinEngine', 'engineIdleUnloadMinutes'],
agent: [
'personasEnabled', 'defaultWorkspace',
- 'agentMaxToolCalls', 'agentMaxIterations',
+ 'agentMaxToolCalls', 'agentMaxIterations', 'agentLoopGuard',
'subAgentMaxToolCalls', 'subAgentMaxIterations', 'loopMaxPasses',
'searchProvider', 'braveApiKey', 'tavilyApiKey', 'exaApiKey', 'searchUserAgent',
'codexArchitectMode', 'codexArchitectModel', 'codexArchitectAllowCloud',
diff --git a/src/types/settings.ts b/src/types/settings.ts
index 5098f640..5db9878f 100644
--- a/src/types/settings.ts
+++ b/src/types/settings.ts
@@ -182,6 +182,9 @@ export interface Settings {
agentMaxToolCalls: number
/** Hard cap on ReAct loop iterations per user turn. 0 = unlimited. */
agentMaxIterations: number
+ /** The agent loop guard (lib/agent-loop-guard.ts): steers and halts a run
+ * that repeats itself. On unless switched off (C44, Discord 05.11.2026). */
+ agentLoopGuard?: boolean
/**
* Dieselben zwei Schranken, aber fuer einen delegierten Sub-Agenten.
*