Skip to content

refactor(task): reimplement environment_details block appending with cache control support#11268

Draft
hannesrudolph wants to merge 1 commit intomainfrom
revert-11256-revert-11198-fix/env-details-append
Draft

refactor(task): reimplement environment_details block appending with cache control support#11268
hannesrudolph wants to merge 1 commit intomainfrom
revert-11256-revert-11198-fix/env-details-append

Conversation

@hannesrudolph
Copy link
Collaborator

@hannesrudolph hannesrudolph commented Feb 6, 2026

Summary

Re-applies #11198 (reverted in #11256) and extends it with cache control support.

Appends <environment_details> onto the last user text block or tool result instead of adding a standalone trailing text block, avoiding message shapes that can break interleaved-thinking models.

Changes

Problem

Task messages currently append <environment_details> as a standalone trailing text block. Some interleaved-thinking models are sensitive to message content "shape" and can fail when an extra trailing block is introduced.

Desired behavior

Append <environment_details> into the final existing user text block (or tool result block) rather than adding a new standalone block.

Acceptance criteria

  • If the last user content item is a text block, append <environment_details> to that block.
  • If the last item is a tool result, append <environment_details> to that tool result text.
  • If there is no suitable final block (empty content), create a single text block containing <environment_details>.
  • Preserve existing non-text blocks (e.g. images) and keep original content immutable.

What's coming next on this branch

Cache control support will be added to this PR. The environment_details appending behavior is a prerequisite for proper cache breakpoint placement -- provider-level cache controls (ephemeral breakpoints via providerOptions) need to target the correct message parts. With environment_details merged into existing blocks, cache breakpoints can be placed accurately on the final content part rather than on an unintended standalone block.

Context

The original implementation was merged and then reverted. This reimplementation restores that work as a foundation for the cache control additions that will follow in subsequent commits on this branch.

Relates to EXT-749.


Important

Reintroduces and enhances appendEnvironmentDetails to append <environment_details> to existing message blocks, with comprehensive testing for various scenarios.

  • Behavior:
    • Reintroduces appendEnvironmentDetails() in appendEnvironmentDetails.ts to append <environment_details> to the last text or tool result block.
    • Updates Task in Task.ts to use appendEnvironmentDetails() for constructing user message content.
    • Handles cases where no suitable block exists by creating a new text block.
  • Testing:
    • Adds comprehensive unit tests in appendEnvironmentDetails.spec.ts to cover various scenarios, including empty content, text block handling, tool result handling, and mixed content.
  • Misc:
    • Removes existing <environment_details> blocks before appending new ones to avoid duplicates.

This description was created by Ellipsis for 19c02e1. You can customize this summary. It will automatically update as commits are pushed.

@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Feb 6, 2026
@roomote
Copy link
Contributor

roomote bot commented Feb 6, 2026

Oroocle Clock   See task

Reviewed the new appendEnvironmentDetails helper and its integration into Task.ts. The implementation is clean and well-tested. Found one issue where env details can accumulate on repeated processing.

  • removeEnvironmentDetailsBlocks cannot strip previously-appended env details, causing duplication (e.g. during delegation resume). stripAppendedEnvironmentDetails was written to handle this but is unused in production code.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Comment on lines +2576 to +2583
const contentWithoutEnvDetails = removeEnvironmentDetailsBlocks(
lastUserMsg.content as (
| Anthropic.Messages.TextBlockParam
| Anthropic.Messages.ImageBlockParam
| Anthropic.Messages.ToolResultBlockParam
)[],
)
// Add fresh environment details
lastUserMsg.content = [...contentWithoutEnvDetails, { type: "text" as const, text: environmentDetails }]
lastUserMsg.content = appendEnvironmentDetails(contentWithoutEnvDetails, environmentDetails)
Copy link
Contributor

Choose a reason for hiding this comment

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

removeEnvironmentDetailsBlocks only strips standalone text blocks that are entirely <environment_details>...</environment_details>. Now that env details are appended into existing blocks, this function won't find them on a second pass through the same content. Concretely, in the delegation resume path here: after the first call, the last user message in history has env details appended (e.g. "Tool result\n\n<environment_details>...") -- removeEnvironmentDetailsBlocks won't detect that, so appendEnvironmentDetails adds a second copy.

stripAppendedEnvironmentDetails was written to handle exactly this (it calls removeEnvironmentDetailsBlocks and regex-strips appended env details from the last block), but it's only tested and never used in production. Both this call site and the one at line 2750 should use stripAppendedEnvironmentDetails instead.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph marked this pull request as draft February 6, 2026 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant