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
fix(agent): a batch with a shell call runs in the model's order (C59)
purple451d429c2488
diff --git a/src/api/agents/__tests__/a-shell-step-keeps-its-place.test.ts b/src/api/agents/__tests__/a-shell-step-keeps-its-place.test.tsnew file mode 100644index 00000000..9cc2bc0d--- /dev/null+++ b/src/api/agents/__tests__/a-shell-step-keeps-its-place.test.ts@@ -0,0 +1,56 @@+/**+ * C59 bench, AG4 on the 9B: "back up config.json, then change the port".+ * The model sent `cp config.json config.json.bak` and the edit in one turn.+ * Shell and file edits sat on different queues and ran at the same time;+ * the edit won and the backup already held 8080. A shell command can touch+ * any file, so a batch with one keeps the order the model wrote.+ */+import { describe, it, expect } from 'vitest'+import { executeParallel, APPROVE_ALL, type ExecutorRuntime } from '../tool-executor'+import type { ToolArgs } from '../../mcp/types'++const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))++function runtime(order: string[]): ExecutorRuntime {+ const tools: Record<string, (a: ToolArgs) => Promise<string>> = {+ shell_execute: async (a) => { order.push(`start:shell:${String(a.command)}`); await sleep(40); order.push('end:shell'); return 'Done.' },+ file_edit: async (a) => { order.push(`start:edit:${String(a.path)}`); await sleep(5); order.push('end:edit'); return 'Edited' },+ file_read: async (a) => { order.push(`read:${String(a.path)}`); return 'x' },+ }+ return {+ getTool: (name) => (name in tools ? { name } : undefined),+ execute: async (name, args) => tools[name](args),+ awaitApproval: APPROVE_ALL,+ }+}++describe('a batch with a shell call keeps its order', () => {+ it('the copy finishes before the edit starts', async () => {+ const order: string[] = []+ await executeParallel([+ { id: '1', toolName: 'shell_execute', args: { command: 'cp config.json config.json.bak' } },+ { id: '2', toolName: 'file_edit', args: { path: 'config.json', old_string: '3000', new_string: '8080' } },+ ], runtime(order))+ expect(order).toEqual(['start:shell:cp config.json config.json.bak', 'end:shell', 'start:edit:config.json', 'end:edit'])+ })++ it('a read after the shell call sees what the shell did', async () => {+ const order: string[] = []+ await executeParallel([+ { id: '1', toolName: 'shell_execute', args: { command: 'npm run build' } },+ { id: '2', toolName: 'file_read', args: { path: 'dist/out.txt' } },+ ], runtime(order))+ expect(order.indexOf('read:dist/out.txt')).toBeGreaterThan(order.indexOf('end:shell'))+ })++ it('without a shell call, reads still run side by side', async () => {+ const order: string[] = []+ const t0 = Date.now()+ await executeParallel([+ { id: '1', toolName: 'file_read', args: { path: 'a' } },+ { id: '2', toolName: 'file_read', args: { path: 'b' } },+ ], runtime(order))+ expect(Date.now() - t0).toBeLessThan(200)+ expect(order.sort()).toEqual(['read:a', 'read:b'])+ })+})diff --git a/src/api/agents/tool-executor.ts b/src/api/agents/tool-executor.tsindex f2a4bc3b..9c529ea0 100644--- a/src/api/agents/tool-executor.ts+++ b/src/api/agents/tool-executor.ts@@ -171,6 +171,7 @@ export interface ExecutorOptions {* Run a batch of tool calls. Returns ordered results matching the input order.** Scheduling:+ * - A batch with a shell call (key 'exec') runs serially, in input order.* - Calls with no sideEffectKey run in parallel.* - Calls sharing a key run serially within their group.* - Groups with different keys run in parallel with each other and with the@@ -218,6 +219,15 @@ export async function executeParallel(opts.onComplete?.(res)}+ // A shell command can read or change any file, so a batch that holds one+ // runs in the order the model wrote it. Measured (C59 bench, 9B): in one+ // turn `cp config.json config.json.bak` and an edit of config.json ran on+ // two queues, the edit won, and the backup held the new port.+ if (tagged.some((t) => t.key === 'exec')) {+ for (let i = 0; i < tagged.length; i++) await runOne(i)+ return results.map((r, i) => r ?? abortedResult(tagged[i]))+ }+const solo = soloIndices.map(runOne)const grouped = [...groups.values()].map(async (indices) => {