Skip to content

fix(delegate-task): guard subagent prompt size against model context limits (fixes #951)#3432

Open
MoerAI wants to merge 3 commits intocode-yeongyu:devfrom
MoerAI:fix/subagent-prompt-context-limit
Open

fix(delegate-task): guard subagent prompt size against model context limits (fixes #951)#3432
MoerAI wants to merge 3 commits intocode-yeongyu:devfrom
MoerAI:fix/subagent-prompt-context-limit

Conversation

@MoerAI
Copy link
Copy Markdown
Contributor

@MoerAI MoerAI commented Apr 14, 2026

Summary

  • detach subagent sessions from parent context when the resolved model has a small context window
  • derive subagent system prompt budgets from model metadata and trim low-priority example blocks first
  • cover the new small-context behavior in delegate-task, call_omo_agent, and background spawner tests

Problem

Small local models were inheriting parent session context and still using a hardcoded 24k prompt budget. On 16k-context Ollama models this overflowed the available window, so prompts were truncated mid-instruction and subagents returned empty or corrupted results.

Fix

This change reads the resolved model context window from cached provider metadata and switches small-context subagents to isolated sessions so they stop inheriting the full parent conversation. It also caps injected system content to 50% of the detected context window and trims low-priority example-heavy blocks before falling back to hard truncation.

Changes

File Change
src/shared/subagent-context-window.ts Added shared helpers for context-window budgets and detached session creation
src/tools/delegate-task/prompt-builder.ts Applied model-aware prompt budgeting for delegated subagents
src/tools/delegate-task/token-limiter.ts Preferentially trims low-priority example blocks before hard truncation
src/tools/delegate-task/sync-session-creator.ts Stops inheriting parent context for small-context delegate sessions
src/tools/call-omo-agent/session-creator.ts Stops inheriting parent context for small-context direct subagent sessions
src/features/background-agent/spawner.ts Applies the same detached-session behavior to background subagents

Fixes #951


Summary by cubic

Prevents subagent prompt overflow on small-context models by detaching sessions and capping system content, avoiding truncation and bad outputs on 16k models. Also defaults Ralph loop to start fresh sessions and improves plugin config file detection. Fixes #951.

  • Bug Fixes

    • Subagents read model context window and detach from parent when ≤32k tokens.
    • System content capped to 50% of detected window; trims low-priority example/code blocks first, then truncates.
    • Applied to delegated tasks, call_omo_agent, and background spawner; new tests cover small-context behavior.
    • Plugin config detection now finds both canonical and legacy files and prefers the canonical name when both exist.
  • Migration

    • ralph-loop now defaults to "reset" strategy. Set default_strategy: "continue" in your loop config if you prefer the old behavior.

Written for commit 9da9119. Summary will update on new commits.

@MoerAI
Copy link
Copy Markdown
Contributor Author

MoerAI commented Apr 14, 2026

I have read the CLA Document and I hereby sign the CLA

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9da9119bc0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +102 to +107
body: buildSubagentSessionCreateBody({
parentSessionID: input.parentSessionID,
title: input.description,
permission: input.sessionPermission,
model: input.model,
}),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move detached-session logic into the active background path

This change applies buildSubagentSessionCreateBody(...) only in spawner.startTask, but background delegations in production go through BackgroundManager.launch() (used by both src/tools/delegate-task/background-task.ts and src/tools/call-omo-agent/background-executor.ts), which calls BackgroundManager.startTask in src/features/background-agent/manager.ts and still unconditionally sets parentID. That means small-context background subagents still inherit the full parent transcript and can hit the same prompt/context overflow this commit is trying to prevent.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 19 files

Confidence score: 3/5

  • There is a concrete regression risk in src/config/schema/ralph-loop.ts: changing the schema default from continue to reset will silently alter runtime behavior when default_strategy is omitted.
  • Given the high confidence and user-facing impact of default behavior changes, this is more than a housekeeping tweak and introduces some merge risk.
  • This can still be merged if intentional, but it should be validated against existing configs and release-noted to avoid surprises for current users.
  • Pay close attention to src/config/schema/ralph-loop.ts - default fallback behavior changed and may break implicit configuration expectations.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/config/schema/ralph-loop.ts">

<violation number="1" location="src/config/schema/ralph-loop.ts:8">
P2: Changing the schema default from `continue` to `reset` silently changes behavior for configs that omit `default_strategy`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

/** Custom state file directory relative to project root (default: .opencode/) */
state_dir: z.string().optional(),
default_strategy: z.enum(["reset", "continue"]).default("continue"),
default_strategy: z.enum(["reset", "continue"]).default("reset"),
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Changing the schema default from continue to reset silently changes behavior for configs that omit default_strategy.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/config/schema/ralph-loop.ts, line 8:

<comment>Changing the schema default from `continue` to `reset` silently changes behavior for configs that omit `default_strategy`.</comment>

<file context>
@@ -5,7 +5,7 @@ export const RalphLoopConfigSchema = z.object({
   /** Custom state file directory relative to project root (default: .opencode/) */
   state_dir: z.string().optional(),
-  default_strategy: z.enum(["reset", "continue"]).default("continue"),
+  default_strategy: z.enum(["reset", "continue"]).default("reset"),
 })
 
</file context>
Suggested change
default_strategy: z.enum(["reset", "continue"]).default("reset"),
+ default_strategy: z.enum(["reset", "continue"]).default("continue"),
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Subagent prompts exceed 24K tokens, breaking local models with limited context windows

1 participant