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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat(commands): give /loop a real clock, and stop showing the model's own broken tag
purplee539ddebc8b3
diff --git a/src/components/chat/CodexConfirmDialog.tsx b/src/components/chat/CodexConfirmDialog.tsxindex 20a4dc34..80754aab 100644--- a/src/components/chat/CodexConfirmDialog.tsx+++ b/src/components/chat/CodexConfirmDialog.tsx@@ -64,9 +64,10 @@ export function CodexConfirmDialog() {)}<buttononClick={stopAsking}+ title="Run this and every later command in this session without asking"className="text-[0.58rem] text-gray-500 hover:text-gray-800 dark:hover:text-gray-200 transition-colors">- Don't ask again+ Accept without asking</button></div></div>diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.tsindex 5d98b6ef..0aca6edc 100644--- a/src/hooks/useCodex.ts+++ b/src/hooks/useCodex.ts@@ -19,7 +19,7 @@ import { setActiveChatId, clearActiveChatId, chatWorkspaceSlug, setActiveWorkspaimport { resolveWorkspace } from '../api/agents/workspace-resolve'import { useAgentModeStore } from '../stores/agentModeStore'import { loadLurules, renderRulesSection, type RulesReader } from '../lib/lurules'-import { parseAgentCommand } from '../lib/agent-commands'+import { parseAgentCommand, parseLoopBudget, formatDuration } from '../lib/agent-commands'import { useGenerationStore } from '../stores/generationStore'import { backendCall, isOllamaLocal } from '../api/backend'import { planWithArchitect, renderArchitectPlanSection } from '../api/agents/architect'@@ -212,6 +212,11 @@ export function useCodex() {// this turn, so /review and /security physically cannot rewrite the files// they were asked to look at.const readOnlyTurn = slash?.command.readOnly === true+ // `/loop [20m] …` — the budget is enforced by the loop below, not just+ // stated in the prompt. A model told "you have 20 minutes" and left to+ // police itself will happily run for an hour.+ const loopBudgetMs = slash?.command.name === 'loop' ? parseLoopBudget(slash.args).budgetMs : null+ const loopDeadline = loopBudgetMs ? Date.now() + loopBudgetMs : nullconst store = useChatStore.getState()const codexStore = useCodexStore.getState()@@ -665,6 +670,20 @@ export function useCodex() {// backstop. Floor of 1 so a stray 0 setting can't zero the loop.const MAX_CODEX_ITERATIONS = Math.max(settings.agentMaxIterations ?? 200, 1)for (let i = 0; i < MAX_CODEX_ITERATIONS && runningRef.current && !abort.signal.aborted; i++) {+ // `/loop 20m …` — a real wall-clock deadline, checked between+ // iterations. Never mid-tool: a run cut off inside file_edit or a shell+ // command would leave the workspace in a state nobody asked for. So the+ // budget is "no NEW step after this point", not a hard kill.+ if (loopDeadline && Date.now() > loopDeadline && i > 0) {+ const done = fullContent.trim()+ useChatStore.getState().updateMessageContent(+ convId!,+ assistantMsg.id,+ (done ? done + '\n\n' : '') ++ `_(time is up: the ${formatDuration(loopBudgetMs!)} budget for this loop ran out after ${i} step${i === 1 ? '' : 's'}. Nothing was cut off mid-step. Send /loop again with a longer budget to carry on.)_`,+ )+ break+ }budget.addIteration()const bx = budget.exceeded()if (bx.kind !== 'none') {diff --git a/src/lib/__tests__/agent-commands.test.ts b/src/lib/__tests__/agent-commands.test.tsindex f0d194e2..0738d27b 100644--- a/src/lib/__tests__/agent-commands.test.ts+++ b/src/lib/__tests__/agent-commands.test.ts@@ -4,6 +4,10 @@ import {getAgentCommand,parseAgentCommand,matchAgentCommands,+ parseLoopBudget,+ formatDuration,+ DEFAULT_LOOP_BUDGET_MS,+ MAX_LOOP_BUDGET_MS,} from '../agent-commands'import { MUTATING_TOOLS } from '../mutating-tools'@@ -210,3 +214,53 @@ describe('matchAgentCommands (autocomplete)', () => {expect(matchAgentCommands('/zzz')).toEqual([])})})++describe('/loop time budget', () => {+ it('reads the compact forms', () => {+ expect(parseLoopBudget('20m fix the tests').budgetMs).toBe(20 * 60_000)+ expect(parseLoopBudget('2h refactor').budgetMs).toBe(2 * 3_600_000)+ expect(parseLoopBudget('90s smoke').budgetMs).toBe(90_000)+ expect(parseLoopBudget('1h30m big job').budgetMs).toBe(90 * 60_000)+ expect(parseLoopBudget('45 something').budgetMs).toBe(45 * 60_000) // bare = minutes+ })++ it('strips the duration out of the task text', () => {+ expect(parseLoopBudget('20m fix the tests').rest).toBe('fix the tests')+ expect(parseLoopBudget('2h: refactor the parser').rest).toBe('refactor the parser')+ })++ it('falls back to a default when no duration is given', () => {+ const r = parseLoopBudget('make the build green')+ expect(r.budgetMs).toBe(DEFAULT_LOOP_BUDGET_MS)+ expect(r.rest).toBe('make the build green')+ })++ it('caps a runaway budget', () => {+ // A forgotten loop must not be able to bill a whole day of cloud.+ expect(parseLoopBudget('99h forever').budgetMs).toBe(MAX_LOOP_BUDGET_MS)+ })++ it('does not mistake a task that starts with a number for a budget', () => {+ // "3 failing tests" is the job, not the clock. Only a compact duration+ // token counts, and a bare number is read as minutes by design, so this+ // pins the shapes that must NOT be swallowed silently.+ expect(parseLoopBudget('fix 20m of flakiness').budgetMs).toBe(DEFAULT_LOOP_BUDGET_MS)+ expect(parseLoopBudget('fix 20m of flakiness').rest).toBe('fix 20m of flakiness')+ })++ it('states the budget in the prompt it sends', () => {+ const t = getAgentCommand('loop')!.build('20m make the tests pass')+ expect(t).toContain('You have 20m')+ expect(t).toContain('The app enforces it')+ expect(t).not.toContain('20m make the tests pass') // duration is consumed+ })+})++describe('formatDuration', () => {+ it('reads naturally at every scale', () => {+ expect(formatDuration(45_000)).toBe('45s')+ expect(formatDuration(20 * 60_000)).toBe('20m')+ expect(formatDuration(90 * 60_000)).toBe('1h 30m')+ expect(formatDuration(2 * 3_600_000)).toBe('2h')+ })+})diff --git a/src/lib/__tests__/loose-tool-parse.test.ts b/src/lib/__tests__/loose-tool-parse.test.tsindex 982e978a..b7ef8fd1 100644--- a/src/lib/__tests__/loose-tool-parse.test.ts+++ b/src/lib/__tests__/loose-tool-parse.test.ts@@ -302,4 +302,21 @@ describe('stripToolCallText — leftover Hermes tags', () => {const prose = 'the tool call failed, try again'expect(stripToolCallText(prose, KN)).toBe(prose)})++ // Captured from the wire, ship exe 2026-07-25. Qwen3-32B on LU Cloud put+ // `</think>\n\nool_call>` in its content next to a valid native tool_calls+ // array: the provider's harmony parser had already eaten the `<t`, so every+ // pattern anchored on `<` missed the remainder and the user saw `ool_call>`.+ it('removes a TRUNCATED tag the provider mangled before we ever saw it', () => {+ expect(stripToolCallText('I will look at it.\nool_call>', KN)).toBe('I will look at it.')+ expect(stripToolCallText('ool_call>', KN)).toBe('')+ expect(stripToolCallText('ool_calls>', KN)).toBe('')+ })++ it('only strips the fragment when it is the whole line', () => {+ // Anchored per line, so a sentence that happens to contain the characters+ // is never touched.+ const prose = 'the log said ool_call> which is odd'+ expect(stripToolCallText(prose, KN)).toBe(prose)+ })})diff --git a/src/lib/agent-commands.ts b/src/lib/agent-commands.tsindex bb556dab..73b8a736 100644--- a/src/lib/agent-commands.ts+++ b/src/lib/agent-commands.ts@@ -50,6 +50,50 @@ export interface AgentCommand {/** Shared closing line so every command nudges the agent to act, not narrate. */const ACT = 'Use your tools to do this for real — do not just describe the steps. Be concise in text; the work happens in tool calls. Reply in the user\'s language.'+/** Longest budget /loop will accept. Beyond this it is not a loop, it is a job. */+export const MAX_LOOP_BUDGET_MS = 4 * 60 * 60 * 1000+/** Used when the user gives no duration — long enough to be useful, short+ * enough that a forgotten loop cannot quietly bill an afternoon of cloud. */+export const DEFAULT_LOOP_BUDGET_MS = 15 * 60 * 1000++/**+ * Pull a leading duration off `/loop`'s arguments: `20m`, `2h`, `90s`,+ * `1h30m`, or a bare number read as minutes. Returns the budget in ms plus+ * the remaining task text.+ *+ * The budget is REAL — useCodex stops the run when the clock runs out — so+ * parsing has to be strict about what counts as a duration. "5 minutes to+ * midnight" is a task, not a budget, which is why only a compact token at the+ * very start is accepted.+ */+export function parseLoopBudget(args: string): { budgetMs: number; rest: string } {+ const text = args.trim()+ const m = /^(\d+\s*h(?:\s*\d+\s*m)?|\d+\s*m(?:in)?|\d+\s*s(?:ec)?|\d+)\b[\s,:-]*/i.exec(text)+ if (!m) return { budgetMs: DEFAULT_LOOP_BUDGET_MS, rest: text }++ const token = m[1].replace(/\s+/g, '').toLowerCase()+ let ms = 0+ const hm = /^(\d+)h(?:(\d+)m)?$/.exec(token)+ if (hm) ms = Number(hm[1]) * 3_600_000 + Number(hm[2] ?? 0) * 60_000+ else if (/^\d+m(in)?$/.test(token)) ms = parseInt(token, 10) * 60_000+ else if (/^\d+s(ec)?$/.test(token)) ms = parseInt(token, 10) * 1000+ else ms = parseInt(token, 10) * 60_000 // bare number = minutes++ if (!ms) return { budgetMs: DEFAULT_LOOP_BUDGET_MS, rest: text }+ return { budgetMs: Math.min(ms, MAX_LOOP_BUDGET_MS), rest: text.slice(m[0].length).trim() }+}++/** "20m" / "1h 30m" / "45s" for the prompt and the UI. */+export function formatDuration(ms: number): string {+ const total = Math.round(ms / 1000)+ if (total < 60) return `${total}s`+ const mins = Math.round(total / 60)+ if (mins < 60) return `${mins}m`+ const h = Math.floor(mins / 60)+ const m = mins % 60+ return m ? `${h}h ${m}m` : `${h}h`+}+/** Read-only commands cannot run shell, so they must be told how to look. */const LOOK = 'Read the real code with file_read / file_list / file_search first — never answer from memory. You have no write or shell tools on this turn, so do not plan to use them.'@@ -72,11 +116,15 @@ export const AGENT_COMMANDS: AgentCommand[] = [},{name: 'loop',- summary: 'Keep working until a finish condition is met',- argHint: '<task, and how you will know it is done>',+ summary: 'Keep working until it is done, within a time budget',+ argHint: '[20m] <task, and how you will know it is done>',build: (a) => {- const task = a.trim() || 'the task the user just described'- return `Work on this until it is genuinely finished: ${task}+ const { budgetMs, rest } = parseLoopBudget(a)+ const task = rest || 'the task the user just described'+ const budgetLine = budgetMs+ ? `\n\nYou have ${formatDuration(budgetMs)}. The app enforces it: when the time is up the run stops wherever it is, so spend it on the work and not on narration. If you can see you will not finish in time, say what you would do next and stop early rather than being cut off mid-edit.`+ : ''+ return `Work on this until it is genuinely finished: ${task}${budgetLine}Run it as a loop, not a single pass:1) Do the next concrete step.diff --git a/src/lib/loose-tool-parse.ts b/src/lib/loose-tool-parse.tsindex a192748d..1052ba1e 100644--- a/src/lib/loose-tool-parse.ts+++ b/src/lib/loose-tool-parse.ts@@ -309,6 +309,14 @@ export function stripToolCallText(text: string, known: string[]): string {// the bubble — where the markdown renderer swallowed the `<t` and left the// user staring at a bare `ool_call>` (live Agent run, ship exe 2026-07-25)..replace(/<[|/\s]*tool_calls?[|/\s]*>/gi, '')+ // A whole line that is nothing but a tool-call tag, INCLUDING a truncated+ // one. Captured from the wire on the ship exe, 2026-07-25: Qwen3-32B on LU+ // Cloud sent `</think>\n\nool_call>` as its content, next to a perfectly+ // good native tool_calls array. The provider's own parser had already eaten+ // the `<t`, so every pattern that starts at `<` misses the remainder and it+ // renders as a stray `ool_call>` above the answer. We cannot stop the model+ // doing it; we can stop showing it. Line-anchored, so prose can never match.+ .replace(/^[ \t]*<?[|/]*t?ool_calls?[|/]*>[ \t]*$/gim, '').replace(/\[\/?TOOL_CALLS?\]/gi, '').replace(/```(?:json|tool_code)?\s*```/gi, '').replace(/\n{3,}/g, '\n\n')