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.

merge: fix/301-dtimeout in integ/301 (Nebenlaeufigkeit, ABGENOMMEN)

purpleadca6682123b
diff --cc src/api/mcp/builtin-tools.ts
index c6f5b5b1,a5869475..64ad1953
--- a/src/api/mcp/builtin-tools.ts
+++ b/src/api/mcp/builtin-tools.ts
@@@ -1638,28 -1608,27 +1638,47 @@@ async function executeRunWorkflow(args
// it against the actual holder at run time before skipping its own
// booking, so a stale or absent proof falls back to booking normally
// instead of silently trusting the marker.
- const engine = new WorkflowEngine(workflow, 'tool-execution', callbacks, initialVars, _workflowDepth, run?.heldLocalLane ?? null)
+ //
+ // The gate (Nebenbefund, bau/review-wfplay.md Teil B): one approval for
+ // `run_workflow` itself already fell before this executor ran, but that
+ // approval was for "run this workflow", not for whatever it does inside.
+ // Passing `APPROVE_ALL` here, the reviewer's own first suggestion, would
+ // turn that single approval into a blank check for every tool the
+ // workflow's steps (or a model inside a prompt step) go on to call,
+ // including a `shell_execute` the user never saw. `buildWorkflowApprovalGate`
+ // resolves the REAL gate from `run`'s own conversation and read-only flag,
+ // same decision table `delegate_task` already goes through, so a
+ // `confirm` tool inside the workflow still asks, on the same queue the
+ // rest of Agent mode uses.
+ const approve = buildWorkflowApprovalGate(run)
+ // `run` itself is also passed as `invokingRun` (Auflage 2,
+ // bau/review-wfgate.md): 'tool-execution' above is only a lane-booking
+ // placeholder, not a real conversation, so IF this workflow's own step
+ // calls `run_workflow` again, the nested engine needs the REAL outer
+ // `run` (its conversation, `abortSignal`, `mode`) to build that
+ // third-level call's context from, not this placeholder. See
+ // `effectiveOuterRun`/`effectiveConversationId` in workflow-engine.ts.
+ const engine = new WorkflowEngine(workflow, 'tool-execution', callbacks, approve, initialVars, _workflowDepth, run?.heldLocalLane ?? null, run)
- await engine.run()
+ // klaerung-n5a Fix 2: `abort` is the merged signal toolRegistry.execute()
+ // hands every executor now (the run's Stop AND, since run_workflow sits
+ // in tool-timeout.ts's AGENT_LOOP_TOOLS, the JS race's own timeout
+ // controller). WorkflowEngine already has everything needed to react:
+ // `cancel()` flips its internal AbortController, checked at the top of
+ // every step and threaded into every provider call, so this executor
+ // just has to call it. Without this wire, a timed-out or Stop-hit
+ // `run_workflow` kept its engine running underneath, orphaned, holding
+ // the local lane `runInLane` had booked for it, the same failure class
+ // measured for delegate_task.
+ const onAbort = () => engine.cancel()
+ if (abort) {
+ if (abort.aborted) onAbort()
+ else abort.addEventListener('abort', onAbort, { once: true })
+ }
+ try {
+ await engine.run()
+ } finally {
+ abort?.removeEventListener('abort', onAbort)
+ }
} finally {
_workflowDepth--
}
diff --cc src/lib/__tests__/agent-loop-tools-timeout-abort.test.ts
index 00000000,b9f13477..867ce665
mode 000000,100644..100644
--- a/src/lib/__tests__/agent-loop-tools-timeout-abort.test.ts
+++ b/src/lib/__tests__/agent-loop-tools-timeout-abort.test.ts
@@@ -1,0 -1,197 +1,206 @@@
+ /**
+ * klaerung-n5a, Frage 1, Fix 2: wenn der JS-Werkzeug-Deckel (tool-timeout.ts)
+ * doch einmal zuschlaegt, oder der Nutzer waehrenddessen Stop drueckt, darf
+ * der verlierende Lauf nicht mehr verwaist weiterlaufen und seine lokale
+ * Spur besetzt halten. Vorher tat er das: `Promise.race` ignoriert seinen
+ * Verlierer nur, es bricht ihn nie ab.
+ *
+ * Diese Datei prueft die beiden Werkzeuge mit eigener Spur-Belegung, fuer
+ * die tool-timeout.ts inzwischen KEINEN erreichbaren Deckel mehr setzt
+ * (`delegate_task` im Vordergrund, `run_workflow`): dass das Signal, das
+ * `raceWithToolTimeout` bei einem Abbruch abbricht (Timeout ODER Stop), auch
+ * wirklich bis in ihren jeweiligen Lauf durchgereicht wird und die Spur
+ * freigibt, und zwar nicht ueber den echten 615-Tage-Deckel, der in der
+ * Praxis nie feuert, sondern direkt am dritten `signal`-Argument, das
+ * `toolRegistry.execute()` jedem Ausfuehrer reicht (siehe tool-registry.ts).
+ *
+ * Lauf: npx vitest run src/lib/__tests__/agent-loop-tools-timeout-abort.test.ts
+ */
+ import { describe, it, expect, beforeEach, vi } from 'vitest'
+ import { localLaneHolder, queuedRunIds, __resetRunLanesForTests } from '../run-lanes'
+ import { useGenerationStore } from '../../stores/generationStore'
+ import { useModelStore } from '../../stores/modelStore'
+ import { useMemoryStore } from '../../stores/memoryStore'
+ import { useAgentWorkflowStore } from '../../stores/agentWorkflowStore'
+ import { toolRegistry, registerBuiltinTools } from '../../api/mcp'
+ import { buildDelegateExecutor, _setDepth, type SubAgentRunner } from '../../api/agents/sub-agent'
+ import type { AgentRunContext } from '../../api/agent-context'
+ import type { AgentWorkflow } from '../../types/agent-workflows'
+
+ const LOCAL_MODEL = 'qwen3:8b'
+
+ function makeRun(conversationId: string): AgentRunContext {
+ return {
+ token: 'run', chatId: null, conversationId, workspace: null,
+ artifactMode: false, readOnlyShellTurn: false, mode: null, artifacts: [],
+ }
+ }
+
+ const tick = () => new Promise((r) => setTimeout(r, 0))
+ const takte = async (n = 20) => { for (let i = 0; i < n; i++) await tick() }
+
+ beforeEach(() => {
+ __resetRunLanesForTests()
+ useGenerationStore.setState({ generating: {}, aborters: {}, runs: {} })
+ useModelStore.setState({ activeModel: LOCAL_MODEL })
+ _setDepth(0)
+ })
+
+ // ── delegate_task (Vordergrund) ─────────────────────────────────────────
+
+ describe('delegate_task im Vordergrund: das dritte signal-Argument bricht den Sub-Agenten wirklich ab', () => {
+ it('Abbruch (Stellvertreter fuer den Race-Timeout) beendet den Lauf schnell und gibt die Spur frei', async () => {
+ // Steht fuer defaultSubAgentRunner's echte Schleife, die `gates.abortSignal
+ // ?.aborted` an jedem Schleifenkopf prueft (sub-agent.ts:545), hier als
+ // schneller Poll nachgebaut, damit der Test nicht auf echte Provider-
+ // Anbindung angewiesen ist.
+ const runner: SubAgentRunner = (_goal, _context, opts) =>
+ new Promise((resolve) => {
+ const check = () => {
+ if (opts.run?.abortSignal?.aborted) { resolve('(sub-agent stopped by the user)'); return }
+ setTimeout(check, 2)
+ }
+ check()
+ })
+
+ const exec = buildDelegateExecutor(runner)
+ const controller = new AbortController()
+ const lauf = exec({ goal: 'lang laufend' }, makeRun('conv-fg'), controller.signal)
+
+ await takte()
+ // Der Vordergrundzweig bucht seine EIGENE, generierte Kennung (nicht
+ // 'conv-fg'), aber IRGENDEINER haelt jetzt die lokale Spur.
+ expect(localLaneHolder()).not.toBeNull()
+ expect(queuedRunIds()).toEqual([])
+
+ const start = Date.now()
+ controller.abort()
+ const out = await lauf
+ const elapsedMs = Date.now() - start
+
+ expect(out).toMatch(/stopped by the user/)
+ // Schnell: der Poll steht alle 2ms an, kein Warten auf einen echten
+ // Deckel (der fuer dieses Werkzeug praktisch nie feuert, siehe
+ // tool-timeout.test.ts).
+ expect(elapsedMs).toBeLessThan(500)
+ expect(localLaneHolder()).toBeNull()
+ })
+
+ it('Negativkontrolle: ohne Abbruch laeuft derselbe Aufbau normal durch und gibt die Spur ebenso frei', async () => {
+ const runner: SubAgentRunner = async () => 'echtes Ergebnis, kein Abbruch'
+ const exec = buildDelegateExecutor(runner)
+ const controller = new AbortController()
+
+ const out = await exec({ goal: 'kurz' }, makeRun('conv-fg2'), controller.signal)
+
+ expect(out).toBe('echtes Ergebnis, kein Abbruch')
+ expect(controller.signal.aborted).toBe(false)
+ expect(localLaneHolder()).toBeNull()
+ })
+
+ it('Stop des Nutzers (run.abortSignal, kein separates signal-Argument) bricht ebenfalls ab', async () => {
+ const runner: SubAgentRunner = (_goal, _context, opts) =>
+ new Promise((resolve) => {
+ const check = () => {
+ if (opts.run?.abortSignal?.aborted) { resolve('(sub-agent stopped by the user)'); return }
+ setTimeout(check, 2)
+ }
+ check()
+ })
+ const exec = buildDelegateExecutor(runner)
+ const run = makeRun('conv-stop')
+ const stopController = new AbortController()
+ run.abortSignal = stopController.signal
+
+ // Kein drittes Argument: die einzige Quelle ist run.abortSignal, genau
+ // wie ein Aufrufer vor dem vierten `signal`-Parameter der Registry.
+ const lauf = exec({ goal: 'x' }, run)
+ await takte()
+ expect(localLaneHolder()).not.toBeNull()
+
+ stopController.abort()
+ const out = await lauf
+
+ expect(out).toMatch(/stopped by the user/)
+ expect(localLaneHolder()).toBeNull()
+ })
+ })
+
+ // ── run_workflow ─────────────────────────────────────────────────────────
+
+ function userInputWorkflow(): AgentWorkflow {
++ // Zwei user_input-Schritte, nicht einer: seit dem wfgate-Merge weist
++ // run_workflow einen Ablauf mit EINEM user_input-Schritt ohne `input`
++ // sofort ab (bau/review-wfgate.md Runde 2 klein 1), der Lauf erreicht die
++ // Engine also nie und bucht keine Spur. Der erste Schritt hier wird ueber
++ // `args.input` vorbefuellt (workflow-engine.ts prefilledUserInput), der
++ // zweite bleibt der reale Haenger, den engine.cancel() beenden muss.
+ return {
+ id: 'wf', name: 'haengt', description: '', icon: 'Zap',
- steps: [{ id: 'warte', type: 'user_input', label: 'warte', userInputPrompt: '?' }],
++ steps: [
++ { id: 'erst', type: 'user_input', label: 'erst', userInputPrompt: '?' },
++ { id: 'zweit', type: 'user_input', label: 'zweit', userInputPrompt: '??' },
++ ],
+ variables: {}, isBuiltIn: false, createdAt: 0, updatedAt: 0,
+ }
+ }
+
+ function memoryWorkflow(): AgentWorkflow {
+ return {
+ id: 'wf2', name: 'kurz', description: '', icon: 'Zap',
+ steps: [{
+ id: 'merk', type: 'memory_save', label: 'merk',
+ memorySave: { type: 'reference', titleTemplate: 't', contentTemplate: 'x', tags: [] },
+ }],
+ variables: {}, isBuiltIn: false, createdAt: 0, updatedAt: 0,
+ }
+ }
+
+ describe('run_workflow: das signal-Argument bricht die WorkflowEngine wirklich ab (engine.cancel())', () => {
+ beforeEach(() => {
+ registerBuiltinTools(toolRegistry)
+ })
+
+ it('Abbruch waehrend eines wartenden Schritts beendet den Lauf und gibt die Spur frei', async () => {
+ useAgentWorkflowStore.setState({ workflows: [userInputWorkflow()] })
+ const controller = new AbortController()
+
- const lauf = toolRegistry.execute('run_workflow', { name: 'haengt' }, 1, undefined, controller.signal)
++ const lauf = toolRegistry.execute('run_workflow', { name: 'haengt', input: 'erste Antwort' }, 1, undefined, controller.signal)
+ await takte()
+ // executeRunWorkflow bucht die feste Kennung 'tool-execution' (builtin-
+ // tools.ts), unabhaengig vom aufrufenden Lauf.
+ expect(localLaneHolder()).toBe('tool-execution')
+
+ const start = Date.now()
+ controller.abort()
+ await lauf
+ const elapsedMs = Date.now() - start
+
+ expect(elapsedMs).toBeLessThan(500)
+ expect(localLaneHolder()).toBeNull()
+ expect(queuedRunIds()).toEqual([])
+ })
+
+ it('Negativkontrolle: ohne Abbruch laeuft ein kurzer Arbeitsablauf normal durch und gibt die Spur frei', async () => {
+ useAgentWorkflowStore.setState({ workflows: [memoryWorkflow()] })
+ vi.spyOn(useMemoryStore.getState(), 'addMemory').mockImplementation(() => 'mem-id')
+ const controller = new AbortController()
+
+ const out = await toolRegistry.execute('run_workflow', { name: 'kurz' }, 1, undefined, controller.signal)
+
+ expect(controller.signal.aborted).toBe(false)
+ expect(out).not.toMatch(/Cancelled/)
+ expect(localLaneHolder()).toBeNull()
+ })
+
+ it('ein schon VOR dem Aufruf abgebrochenes Signal laesst den Lauf nie anfangen (generischer Schutz in toolRegistry.execute)', async () => {
+ useAgentWorkflowStore.setState({ workflows: [userInputWorkflow()] })
+ const controller = new AbortController()
+ controller.abort()
+
+ const out = await toolRegistry.execute('run_workflow', { name: 'haengt' }, 1, undefined, controller.signal)
+
+ expect(out).toMatch(/cancelled by the user before "run_workflow" started/)
+ expect(localLaneHolder()).toBeNull()
+ })
+ })