fix: use reference identity instead of length check for assembled view

- Compare assembled.messages !== sourceMessages instead of length, so engines that rewrite content without changing array count are respected
- Add test for same-count-different-reference assembly
This commit is contained in:
Bikkies
2026-04-10 14:50:56 +10:00
committed by Josh Lehman
parent 88fa65957a
commit 954a1d7005
2 changed files with 20 additions and 6 deletions

View File

@@ -392,7 +392,25 @@ describe("installContextEngineLoopHook", () => {
expect(transformed).toBe(compactedView);
});
it("returns the source messages when the assembled view has the same length", async () => {
it("returns the assembled view when the engine rewrites content without changing count", async () => {
const agent = makeGuardableAgent();
const rewrittenView = [makeUser("rewritten-1"), makeUser("rewritten-2")];
const engine = makeMockEngine({
assemble: async () => ({ messages: rewrittenView, estimatedTokens: 0 }),
});
installHook(agent, engine);
const initial = [makeUser("first"), makeToolResult("call_1", "r")];
await callTransform(agent, initial);
const withNew = [...initial, makeToolResult("call_2", "r2")];
const transformed = await callTransform(agent, withNew);
// Same count (2) but different array reference — engine's view should be used
expect(transformed).toBe(rewrittenView);
});
it("returns the source when the engine returns the same array reference", async () => {
const agent = makeGuardableAgent();
const engine = makeMockEngine();
installHook(agent, engine);

View File

@@ -239,11 +239,7 @@ export function installContextEngineLoopHook(params: {
tokenBudget,
model: modelId,
});
if (
assembled &&
Array.isArray(assembled.messages) &&
assembled.messages.length !== sourceMessages.length
) {
if (assembled && Array.isArray(assembled.messages) && assembled.messages !== sourceMessages) {
lastAssembledView = assembled.messages;
return assembled.messages;
}