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.

fix(agent): enforce read-only where the tools run, not where they are offered

purple88f4e077e00f
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index 3c953269..1690e0be 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -1081,6 +1081,24 @@ export function useAgentChat() {
// pure reads fully parallel).
if (!runningRef.current || abort.signal.aborted) break
+ // Same execution-time guard as Code: the catalog filter is not enough on
+ // its own, because the loose-parse fallback lifts a call the model wrote
+ // as TEXT and the executor resolves it by name without asking whether
+ // this turn was allowed to offer it. Proven live on the Code side
+ // 2026-07-25, where a read-only /plan created a file while every request
+ // carried a read-only catalog.
+ if (opts?.readOnly) {
+ const blocked = toolCalls.filter((tc) => MUTATING_TOOLS.has(tc.function?.name ?? ''))
+ if (blocked.length) {
+ toolCalls = toolCalls.filter((tc) => !MUTATING_TOOLS.has(tc.function?.name ?? ''))
+ const names = [...new Set(blocked.map((tc) => tc.function.name))].join(', ')
+ messages.push({
+ role: 'user',
+ content: `${names} is not available on this turn — it is a read-only command. Do not try to change anything. Finish with the written answer using what you have already read.`,
+ })
+ }
+ }
+
type BatchEntry = { tc: typeof toolCalls[number]; ac: AgentToolCall; blockId: string }
const batch: BatchEntry[] = []
budget.addToolCalls(toolCalls.length)
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index b4452ad1..1d8d2a57 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -1009,6 +1009,28 @@ export function useCodex() {
})
}
+ // Enforce read-only at EXECUTION, not just at offer time.
+ //
+ // Stripping the mutating tools from the catalog was supposed to be
+ // belt-and-braces, and it was not: the loose-parse fallback lifts a call
+ // the model WROTE AS TEXT and hands the name straight to
+ // toolRegistry.execute, which resolves by name and never asks whether
+ // this turn was allowed to offer it. Live on the ship exe 2026-07-25:
+ // /plan (read-only) was offered file_read/file_list/file_search only, on
+ // every one of its six requests, and still created a file on disk.
+ // Review Mode carried the identical hole since 2.5.6.
+ if (settings.codexReviewMode || readOnlyTurn) {
+ const blocked = toolCalls.filter((tc) => MUTATING_TOOLS.has(tc.function?.name ?? ''))
+ if (blocked.length) {
+ toolCalls = toolCalls.filter((tc) => !MUTATING_TOOLS.has(tc.function?.name ?? ''))
+ const names = [...new Set(blocked.map((tc) => tc.function.name))].join(', ')
+ messages.push({
+ role: 'user',
+ content: `${names} is not available on this turn — it is ${readOnlyTurn ? `a read-only command (/${slash!.command.name})` : 'Code Review Mode'}. Do not try to change anything. Finish with the written answer using what you have already read.`,
+ })
+ }
+ }
+
// Repair near-miss tool names before anything tries to resolve them.
// The chat agent has done this since 2026-06-03 (gemma4 calling
// `video_generation`); Code never did, so the same class of miss ended
diff --git a/src/lib/__tests__/readonly-enforcement.test.ts b/src/lib/__tests__/readonly-enforcement.test.ts
new file mode 100644
index 00000000..829165db
--- /dev/null
+++ b/src/lib/__tests__/readonly-enforcement.test.ts
@@ -0,0 +1,86 @@
+/**
+ * Read-only turns must be enforced where the tools RUN, not only where they are
+ * offered.
+ *
+ * Live on the ship exe, 2026-07-25: /plan is read-only, every one of its six
+ * requests carried a catalog of file_read / file_list / file_search only, and
+ * it still created helper.js on disk. The loose-parse fallback lifts a call the
+ * model wrote as prose and hands the name to toolRegistry.execute, which
+ * resolves by name and never asks whether this turn was allowed to offer it.
+ * Code Review Mode carried the same hole and made the same promise.
+ *
+ * These tests pin the guard itself (the filter both hooks apply to a batch) and
+ * assert the source still contains it, so the belt cannot quietly come off
+ * again.
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'fs'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+import { MUTATING_TOOLS } from '../mutating-tools'
+
+const __dirname = dirname(fileURLToPath(import.meta.url))
+const read = (rel: string) => readFileSync(join(__dirname, rel), 'utf8')
+
+type Call = { function: { name: string } }
+/** The guard both hooks apply before a batch executes. */
+const applyGuard = (calls: Call[], readOnly: boolean) =>
+ readOnly ? calls.filter((c) => !MUTATING_TOOLS.has(c.function?.name ?? '')) : calls
+
+const call = (name: string): Call => ({ function: { name } })
+
+describe('read-only batch guard', () => {
+ it('drops a write the model smuggled in as text', () => {
+ const batch = [call('file_read'), call('file_write'), call('file_search')]
+ expect(applyGuard(batch, true).map((c) => c.function.name)).toEqual(['file_read', 'file_search'])
+ })
+
+ it('drops every mutating tool, not just the file ones', () => {
+ const batch = [...MUTATING_TOOLS].map(call)
+ expect(applyGuard(batch, true)).toEqual([])
+ })
+
+ it('leaves the read tools alone', () => {
+ const reads = ['file_read', 'file_list', 'file_search', 'git_status', 'git_diff', 'git_log'].map(call)
+ expect(applyGuard(reads, true)).toHaveLength(reads.length)
+ })
+
+ it('changes nothing on a normal turn', () => {
+ const batch = [call('file_write'), call('shell_execute')]
+ expect(applyGuard(batch, false)).toHaveLength(2)
+ })
+
+ it('git inspection is not classified as mutating', () => {
+ // A reviewer still has to be able to look at the diff it is reviewing.
+ for (const t of ['git_status', 'git_diff', 'git_log', 'file_read', 'file_list', 'file_search']) {
+ expect(MUTATING_TOOLS.has(t), `${t} must stay available`).toBe(false)
+ }
+ })
+
+ it('the tools that can actually change something are all listed', () => {
+ for (const t of ['file_write', 'file_edit', 'shell_execute', 'code_execute', 'git_commit', 'git_push', 'gh_pr_create']) {
+ expect(MUTATING_TOOLS.has(t), `${t} must be blocked`).toBe(true)
+ }
+ })
+})
+
+describe('both hooks still carry the guard', () => {
+ it('useCodex filters the batch, not just the catalog', () => {
+ const src = read('../../hooks/useCodex.ts')
+ expect(src).toContain('if (settings.codexReviewMode || readOnlyTurn) {')
+ expect(src).toContain('toolCalls = toolCalls.filter((tc) => !MUTATING_TOOLS.has(tc.function?.name ?? \'\'))')
+ })
+
+ it('useAgentChat filters the batch too', () => {
+ const src = read('../../hooks/useAgentChat.ts')
+ expect(src).toContain('if (opts?.readOnly) {')
+ expect(src).toContain('toolCalls = toolCalls.filter((tc) => !MUTATING_TOOLS.has(tc.function?.name ?? \'\'))')
+ })
+
+ it('the blocklist has exactly one definition', () => {
+ // It lived inline in useCodex, which is part of why Agent mode never had it.
+ for (const f of ['../../hooks/useCodex.ts', '../../hooks/useAgentChat.ts']) {
+ expect(read(f)).toContain("from '../lib/mutating-tools'")
+ }
+ })
+})