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): the gate can be reopened, and it knows the english words too

David1c7c72ca7d8f
diff --git a/src/hooks/__tests__/codex-create-tool-gate.test.ts b/src/hooks/__tests__/codex-create-tool-gate.test.ts
index 49bfec5d..cb4538fd 100644
--- a/src/hooks/__tests__/codex-create-tool-gate.test.ts
+++ b/src/hooks/__tests__/codex-create-tool-gate.test.ts
@@ -148,15 +148,17 @@ describe('the prompt and the tool list answer the same question', () => {
describe('the wiring in useCodex', () => {
it('the gate runs on the routed list, so it covers all three branches', () => {
- expect(src).toMatch(/const relevantDefs = gateCreateTools\(routedDefs, lastUserMsg\)/)
+ expect(src).toMatch(/const relevantDefs = gateCreateTools\(routedDefs, lastUserMsg, createGateOpened\)/)
})
it('the hermes fallback is gated too, it carries the weakest models', () => {
- expect(src).toMatch(/gateCreateTools\(\s*\n?\s*toolRegistry\.toHermesToolDefs\(permissions\), instruction,/)
+ expect(src).toMatch(/gateCreateTools\(\s*\n?\s*toolRegistry\.toHermesToolDefs\(permissions\), instruction, createGateOpened,/)
})
- it('the asset line is conditional and never fires in review mode', () => {
- expect(src).toMatch(/!reviewMode && !settings\.smallModelMode && wantsMediaTools\(instruction\)/)
+ it('the asset line never fires in review mode or on a read-only command', () => {
+ // A read-only slash command strips MUTATING_TOOLS for the turn, so the
+ // generators are gone there too. Promising them is the same broken promise.
+ expect(src).toMatch(/!reviewMode && !readOnlyTurn && !settings\.smallModelMode/)
})
it('the asset promise left the always-on prompt body', () => {
@@ -171,3 +173,53 @@ describe('the wiring in useCodex', () => {
expect(src).toMatch(/!isLocalModelByName\(activeModel\)\s*\n?\s*\?\s*codexTools/)
})
})
+
+describe('a run that discovers halfway through that it needs a picture', () => {
+ // The hole the review found in this very fix (2026-08-14). "build me a
+ // landing page for my bakery" matches no keyword, so the two generators are
+ // stripped AND the asset line is absent. Before the gate the model simply
+ // called image_generate and it worked, because toolRegistry.execute resolves
+ // by name and never looks at the offered list. Without a way back the model
+ // writes <img src="hero.jpg"> against a file that will never exist and then
+ // reports success.
+
+ it('the gate reopens for the rest of the run once a create tool is called', () => {
+ const defs = [{ name: 'file_read' }, { name: 'image_generate' }, { name: 'video_generate' }]
+ const ask = 'build me a landing page for my bakery'
+ expect(gateCreateTools(defs, ask).map((d) => d.name)).toEqual(['file_read'])
+ expect(gateCreateTools(defs, ask, true).map((d) => d.name)).toEqual(
+ ['file_read', 'image_generate', 'video_generate'],
+ )
+ })
+
+ it('useCodex flips it on the call itself, not on the offered list', () => {
+ expect(src).toMatch(/if \(CREATE_TOOLS\.includes\(name\)\) createGateOpened = true/)
+ // Declared per run, so one run's discovery does not leak into the next.
+ expect(src).toMatch(/let createGateOpened = false/)
+ })
+
+ it('a closed gate still tells the model the hatch is there', () => {
+ expect(src).toContain('const CODEX_ASSET_HINT')
+ expect(src).toContain('They are not in your tool list until you do, and the call still works.')
+ expect(src).toMatch(/: `\\n\$\{CODEX_ASSET_HINT\}`/)
+ })
+})
+
+describe('the English words for the thing are in the list too', () => {
+ // 'foto' and 'grafik' were there, 'photo' and 'graphic' were not, and the
+ // match is a substring test, so neither German entry covers its English
+ // twin. On the cloud path a missed keyword is a lost capability.
+ it.each([
+ 'add a photo of the product',
+ 'I need a graphic for the header',
+ 'generate a photo-realistic hero',
+ ])('%s wants the generators', (ask) => {
+ expect(wantsMediaTools(ask)).toBe(true)
+ expect(gateCreateTools([{ name: 'image_generate' }], ask).length).toBe(1)
+ })
+
+ it('a plain refactor still gets none of them', () => {
+ const defs = [{ name: 'image_generate' }, { name: 'video_generate' }, { name: 'run_workflow' }]
+ expect(gateCreateTools(defs, 'rename the handler and update its callers')).toEqual([])
+ })
+})
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 38e03a2f..d922621b 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -52,7 +52,7 @@ import { CREDITS_EXHAUSTED_MESSAGE } from '../lib/credits-exhausted'
import { streamOllamaChatWithTools } from '../lib/ollama-stream-tools'
import { extractToolCallsWithRanges, stripRanges } from '../lib/tool-call-repair'
import { canonicalToolName } from '../lib/loose-tool-parse'
-import { selectRelevantTools, selectRelevantToolsAsync, SMALL_MODEL_MAX_TOOLS, gateCreateTools, wantsMediaTools } from '../lib/tool-selection'
+import { selectRelevantTools, selectRelevantToolsAsync, SMALL_MODEL_MAX_TOOLS, gateCreateTools, wantsMediaTools, CREATE_TOOLS } from '../lib/tool-selection'
import { generateEmbeddings } from '../api/rag'
import { truncateToolResult } from '../lib/truncate-tool-result'
import { toolCallCapMs, raceWithToolTimeout, SHELL_EXECUTE_DEFAULT_TIMEOUT_MS, CODE_EXECUTE_DEFAULT_TIMEOUT_MS } from '../lib/tool-timeout'
@@ -122,6 +122,12 @@ Rules:
// two generators in the catalog. It used to sit inside the prompt above and
// promised a tool the local keyword router had already removed, which is a
// wasted step: the model calls it and gets "unknown tool".
+// The short form, carried on every run whose instruction did not ask for
+// media. It names the escape hatch without paying for the schemas: calling the
+// tool works even when it is not in the offered list, and the call reopens the
+// gate for the rest of the run.
+const CODEX_ASSET_HINT = `- If the task turns out to need an image or a short clip, call image_generate or video_generate by name. They are not in your tool list until you do, and the call still works.`
+
const CODEX_ASSET_LINE = `- Asset generation: when the task needs an image or a short video (placeholder art, hero image, demo clip), call image_generate / video_generate as a real tool call, they run on-device (Apple MLX on macOS, ComfyUI elsewhere). To animate a generated image, call video_generate with inputImage set to that image's filename.`
// Small-Model Mode (Knob 2): a lean Codex prompt (~500 chars vs ~1700 above)
@@ -260,6 +266,12 @@ 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
+ // The create gate reads the run's instruction, which cannot know that step
+ // six of "build me a landing page" wants a hero image. The registry
+ // executes by name and never consults the offered list, so that first call
+ // still works: honour it, and carry the two generators openly from the next
+ // step on rather than leaving the model to reference a file it cannot make.
+ let createGateOpened = false
// A brand-new instruction clears a previous stop; a /loop pass inherits it.
if (!opts?.loop) userStoppedRef.current = false
// `/loop [30s] …` — the interval is the PAUSE BETWEEN PASSES, and the
@@ -459,10 +471,19 @@ export function useCodex() {
// The prompt promises asset generation only while the tool list carries it.
// Same question, same helper as the gate on the catalog below, so the two
// cannot disagree. Never in review mode, which generates nothing.
- const assetLine =
- !reviewMode && !settings.smallModelMode && wantsMediaTools(instruction)
+ // A read-only slash command generates nothing either, and its own
+ // MUTATING_TOOLS filter has already stripped the generators, so promising
+ // them there is the same broken promise reviewMode was fixed for.
+ const assetsPossible = !reviewMode && !readOnlyTurn && !settings.smallModelMode
+ const assetLine = !assetsPossible
+ ? ''
+ : wantsMediaTools(instruction)
? `\n${CODEX_ASSET_LINE}`
- : ''
+ // The schemas stay out of the catalog, but the knowledge stays in: one
+ // line costs a few tokens against the 1.963 the three schemas cost, and
+ // without it a run that discovers it needs a picture halfway through
+ // silently writes a reference to a file nobody will ever create.
+ : `\n${CODEX_ASSET_HINT}`
// Ground the model in its cwd. In sandbox mode (workDir '.') say so
// concretely + repeat the relative-path rule, since a bare "." led small
// models to fall back to drive-root absolute paths (e.g. /package.json).
@@ -914,7 +935,7 @@ export function useCodex() {
: !isLocalModelByName(activeModel)
? codexTools
: selectRelevantTools(lastUserMsg, codexTools, permissions)
- const relevantDefs = gateCreateTools(routedDefs, lastUserMsg)
+ const relevantDefs = gateCreateTools(routedDefs, lastUserMsg, createGateOpened)
const tools: ToolDefinition[] = relevantDefs.map(t => ({
type: 'function' as const,
function: { name: t.name, description: t.description, parameters: t.inputSchema },
@@ -1114,7 +1135,7 @@ export function useCodex() {
// `instruction`, not the newest user-role message: same reason as the
// native path above, steers and nudges are pushed as role 'user'.
const hermesTools = gateCreateTools(
- toolRegistry.toHermesToolDefs(permissions), instruction,
+ toolRegistry.toHermesToolDefs(permissions), instruction, createGateOpened,
).filter(
(t) => {
const def = toolRegistry.getToolByName(t.name)
@@ -1685,7 +1706,13 @@ export function useCodex() {
const td = toolRegistry.getToolByName(name)
return td ? { name: td.name, inputSchema: td.inputSchema } : undefined
},
- execute: (name: string, args: Record<string, any>) => dispatchTool(name, args),
+ execute: (name: string, args: Record<string, any>) => {
+ // A create tool the gate had closed still reaches the registry, so
+ // the run self-heals: this call runs, and the next step offers the
+ // schemas instead of pretending the capability is gone.
+ if (CREATE_TOOLS.includes(name)) createGateOpened = true
+ return dispatchTool(name, args)
+ },
lookupCache: convId ? makeInTurnCacheLookup({ convId, turnStartMs }) : undefined,
explainError: (toolName, err) => explainToolError(toolName, err),
// Codex is auto-approve by default (the coding agent runs unattended).
diff --git a/src/lib/tool-selection.ts b/src/lib/tool-selection.ts
index 14c1114a..ed0170f3 100644
--- a/src/lib/tool-selection.ts
+++ b/src/lib/tool-selection.ts
@@ -29,9 +29,13 @@ interface ToolGroup {
// list, on the cloud path it means a capability the user had yesterday is gone.
// A false positive here costs tokens, a false negative costs the feature, so
// the list leans generous.
+// 'photo' and 'graphic' were missing next to their German twins 'foto' and
+// 'grafik' (review 2026-08-14). The match is a substring test, so 'foto' does
+// not cover 'photo' and 'grafik' does not cover 'graphic': the two commonest
+// English words for the thing lost the capability on the paid path.
const MEDIA_KEYWORDS = [
- 'image', 'picture', 'generate image', 'draw', 'create image', 'bild', 'foto', 'zeichne',
- 'logo', 'icon', 'thumbnail', 'banner', 'illustration', 'avatar', 'artwork', 'grafik', 'poster',
+ 'image', 'picture', 'photo', 'generate image', 'draw', 'create image', 'bild', 'foto', 'zeichne',
+ 'logo', 'icon', 'thumbnail', 'banner', 'illustration', 'avatar', 'artwork', 'graphic', 'grafik', 'poster',
'video', 'animate', 'animation', 'clip', 'mp4', 'make a video', 'turn into a video', 'movie', 'gif', 'animiere',
]
const WORKFLOW_KEYWORDS = ['workflow', 'run workflow', 'automate']
@@ -260,13 +264,21 @@ export function selectRelevantTools(
* a creative intent surfaces the two generators, a workflow intent surfaces
* run_workflow, and naming a tool verbatim always wins.
*
- * Honest limit: the decision is made once from the run's instruction. A model
- * that discovers halfway through that it wants a placeholder image will not
- * have the tool. That is already how the local path behaves, and the system
- * prompt only promises asset generation when the gate opened, so the model is
- * never told it can do something it cannot.
+ * The decision starts from the run's instruction, and `opened` reopens it for
+ * the rest of the run. That second half is not a nicety: "build me a landing
+ * page for my bakery" hits no keyword, and at step six the model wants a hero
+ * image. Before the gate it simply called image_generate and it worked, because
+ * toolRegistry.execute resolves by name and never consults the offered list.
+ * Without a way back the model instead writes a reference to a file that will
+ * never exist and reports success. So the first real call is honoured, and from
+ * the next step the schemas ride along openly.
*/
-export function gateCreateTools<T extends { name: string }>(defs: T[], userMessage: string): T[] {
+export function gateCreateTools<T extends { name: string }>(
+ defs: T[],
+ userMessage: string,
+ opened = false,
+): T[] {
+ if (opened) return defs
const msg = userMessage.toLowerCase()
const wantsMedia = MEDIA_KEYWORDS.some((k) => msg.includes(k))
const wantsFlow = WORKFLOW_KEYWORDS.some((k) => msg.includes(k))