Skip to content

[Feature]: ContextEngine.assemble should fire per LLM call, not per user prompt #73437

Description

@HaotianChen616

Summary

ContextEngine.assemble should fire per LLM call, not per user prompt

Current behavior

User prompt
→ assembleAttemptContextEngine() ← fires ONCE
→ activeSession.prompt() ← agentic loop
LLM call 1 ← ✅ assemble ran before this
→ tool result
→ LLM call 2 ← ❌ no assemble before this
→ tool result
→ LLM call 3 ← ❌ no assemble before this
→ final response

In attempt.ts:1904, assembleAttemptContextEngine() is called before activeSession.prompt(), which contains the agentic loop. Once the loop starts executing multiple LLM calls, assemble is never invoked again.

Why installContextEngineLoopHook is insufficient

transformContext's call frequency does not guarantee invocation before every LLM call — it fires at specific internal transition points.

Related

Problem to solve

ContextEngine.assemble() currently fires once per user prompt (per attempt), not per LLM call within the agentic loop. Context engines — especially those doing context compression — cannot make informed decisions about the message array before subsequent LLM calls in a multi-turn tool loop.

Proposed solution

Add a transform_context hook that fires before each LLM call within the agentic loop. Two approaches:

  1. New transform_context hook type — conservative, doesn't change existing assemble semantics
  2. Make assemble fire at each LLM call boundary — simpler but may have backward-compatibility implications
    Option 1 is recommended as the safer path.

Alternatives considered

No response

Impact

Context overflow on long agentic loops, stale context in subsequent LLM calls, inability to implement smart trimming strategies.

Evidence/examples

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Normal backlog priority with limited blast radius.clawsweeper:fix-shape-clearClawSweeper found a clear likely implementation shape for this issue.clawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:needs-product-decisionClawSweeper marked this issue as needing a product or behavior decision.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.enhancementNew feature or requestimpact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions