Skip to content

fix(agent-harness): keep the compressed window when a steering note is injected - #1815

Open
andyst-dev wants to merge 1 commit into
kodustech:mainfrom
andyst-dev:fix/compression-injectnote-merge
Open

fix(agent-harness): keep the compressed window when a steering note is injected#1815
andyst-dev wants to merge 1 commit into
kodustech:mainfrom
andyst-dev:fix/compression-injectnote-merge

Conversation

@andyst-dev

Copy link
Copy Markdown
Contributor

Fixes #1808

Problem

AiSdkAgentRunner.policyPrepareStep composed the conversation with an if (injectNote) ... else if (merged.messages) chain. When a policy injected a steering note (CompletionGatePolicy) at the same step that another policy produced a compressed window (CompressionPolicy), the note branch rebuilt the message list from the uncompressed original (msgs ?? messages) and discarded the compression — bypassing the hard per-request context clamp added for #1574 in #1578.

Root cause

if (merged.injectNote) {
  out.messages = [ ...(msgs ?? messages), note ];   // ignores merged.messages
} else if (merged.messages) {
  out.messages = merged.messages.map(toModelMessage);
}

mergeDirectives() legitimately returns both messages and injectNote, but the note branch appended to the original msgs.

Fix

When both fire, compute a single baseMessages = the compressed window if present, else the original, and append the note to it:

const baseMessages = merged.messages
  ? merged.messages.map(toModelMessage)
  : (msgs ?? messages);

The note branch now spreads baseMessages; the non-note branch uses it too.

Validation

Regression test in ai-sdk-agent-runner.compression.e2e.spec.ts drives CompressionPolicy (tiny window) + a note-injecting CompletionGatePolicy together and asserts the model sees the steering note without the full-size raw tool result:

  • without the fix the test is RED (the cached prompt carries the whole readFile result)
  • with the fix the test is GREEN (4/4 pass, including the 3 pre-existing compression tests)

No real model/network involved — mocked ai/test model only.

@kody-ai

kody-ai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the `@kody start-review` command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug
Performance
Security
Business Logic

Access your configuration settings here.

@kody-ai

kody-ai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

kody code-review Business Logic medium

🤔 Insufficient Task Context

I found a task linked to this PR, but it only contains minimal information (title only, no description or acceptance criteria). To perform a meaningful business rules validation, I need more details.

🔍 What I need to validate:

  • Business requirements and acceptance criteria
  • Expected behavior and business rules
  • Edge cases and constraints to consider

💡 How to improve the task context:

  • Add a description to the linked ticket
  • Include acceptance criteria or business rules
  • Describe the expected behavior after the change

⚠️ Important:

A task title alone is not sufficient to determine whether the implementation is correct or complete.

…s injected

AiSdkAgentRunner.policyPrepareStep tried to compose the conversation with an
`if (injectNote) ... else if (merged.messages)` chain, so when a policy injected
a steering note at the same step that another policy produced a compressed
window, the note branch rebuilt the message list from the uncompressed original
(msgs ?? messages) and silently discarded the compression. The hard per-request
context clamp from kodustech#1578 was bypassed.

When both directives fire in one prepareStep the note is now appended to the
compressed window (merged.messages mapped to model messages) instead of the raw
conversation.

Regression test drives CompressionPolicy + a note-injecting CompletionGatePolicy
and asserts the model sees the steering note WITHOUT the full-size raw tool
result (RED without the fix, GREEN with it).
@kody-ai

kody-ai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the `@kody start-review` command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug
Performance
Security
Business Logic

Access your configuration settings here.

@kody-ai

kody-ai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

kody code-review Business Logic medium

🤔 Insufficient Task Context

I found a task linked to this PR, but it only contains minimal information (title only, no description or acceptance criteria). To perform a meaningful business rules validation, I need more details.

🔍 What I need to validate:

  • Business requirements and acceptance criteria
  • Expected behavior and business rules
  • Edge cases and constraints to consider

💡 How to improve the task context:

  • Add a description to the linked ticket
  • Include acceptance criteria or business rules
  • Describe the expected behavior after the change

⚠️ Important:

A task title alone is not sufficient to determine whether the implementation is correct or complete.

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.

Agent context compression is discarded when a policy injects a steering note

1 participant