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): a sub-agent checks its request fits the window before it sends (CA)

purple8292126d8f77
diff --git a/src/api/agents/sub-agent.ts b/src/api/agents/sub-agent.ts
index e06ecc1a..3b6b84be 100644
--- a/src/api/agents/sub-agent.ts
+++ b/src/api/agents/sub-agent.ts
@@ -606,7 +606,26 @@ export async function defaultSubAgentRunner(
// the user stopped, and that is paid compute the product promised would
// not happen ("Stop means stop"). Threading the same signal the tool
// gates already use into the request makes the abort reach the wire.
- const raw = await provider.chatWithTools(modelId, messages, promptTransport ? [] : llmTools, { signal: gates.abortSignal })
+ // The last check before the request leaves (lib/fit-to-window.ts), as in
+ // the main loops (CA, 14.11.2026): a sub-agent's results pile up step by
+ // step, and nothing checked they still fit. Too big: the longest older
+ // results are shortened for this request. Still too big: it stops and
+ // hands back what it found, instead of a silent cut or a refused request.
+ let sendThese = messages
+ try {
+ const { resolveAgentNumCtx } = await import('../../lib/agent-num-ctx')
+ const { fitToWindow } = await import('../../lib/fit-to-window')
+ const { estimateTokens } = await import('../../lib/context-compaction')
+ const { useSettingsStore } = await import('../../stores/settingsStore')
+ const ctx = await resolveAgentNumCtx(modelId, getProviderIdFromModel(laufmodell), useSettingsStore.getState().settings.contextWindowOverride, laufmodell)
+ const fit = fitToWindow(messages, ctx, promptTransport ? 0 : estimateTokens(JSON.stringify(llmTools)))
+ if (!fit.fits) {
+ const material = finalContent || gesammeltesMaterial(messages)
+ return `Stopped: what this sub-agent gathered no longer fits the model's context window. ${material || '(no partial answer)'}`
+ }
+ sendThese = fit.messages
+ } catch { /* an unknown window checks nothing */ }
+ const raw = await provider.chatWithTools(modelId, sendThese, promptTransport ? [] : llmTools, { signal: gates.abortSignal })
// Its credits count toward the hour like the main loop's (CA, 14.11.2026):
// a sub-agent on LU Cloud spent outside both limits. Past the hour limit
// it stops with what it found, as the main loop pauses.
diff --git a/src/lib/__tests__/sub-agent-credits-count.test.ts b/src/lib/__tests__/sub-agent-credits-count.test.ts
index 3ab1a717..f5c65823 100644
--- a/src/lib/__tests__/sub-agent-credits-count.test.ts
+++ b/src/lib/__tests__/sub-agent-credits-count.test.ts
@@ -31,3 +31,14 @@ describe('the sub-agent feeds the hour ledger', () => {
expect(src).toMatch(/Paused to protect your credits: agent runs used the hourly limit/)
})
})
+
+describe('the sub-agent checks its request fits, like the main loops', () => {
+ it('fitToWindow runs right before every request, and an overfull one stops with the material', () => {
+ const src = readFileSync('src/api/agents/sub-agent.ts', 'utf8')
+ const fit = src.indexOf('const fit = fitToWindow(messages, ctx,')
+ const call = src.indexOf('const raw = await provider.chatWithTools(modelId, sendThese')
+ expect(fit).toBeGreaterThan(0)
+ expect(call).toBeGreaterThan(fit)
+ expect(src).toMatch(/Stopped: what this sub-agent gathered no longer fits the model's context window\./)
+ })
+})