Skip to content

fix(ts/storage): return empty history for maxHistorySize 0 or 1 - #733

Open
yzf66665 wants to merge 1 commit into
2FastLabs:mainfrom
yzf66665:fix/ts-trim-conversation-zero
Open

yzf66665 wants to merge 1 commit into
2FastLabs:mainfrom
yzf66665:fix/ts-trim-conversation-zero

Conversation

@yzf66665

@yzf66665 yzf66665 commented Nov 7, 2026

Copy link
Copy Markdown

Issue Link (REQUIRED)

Fixes #732

Summary

Changes

The TypeScript ChatStorage.trimConversation() rounds an odd maxHistorySize down to keep complete user/assistant pairs, then slices conversation.slice(-adjustedMaxHistorySize):

const adjustedMaxHistorySize = maxHistorySize % 2 === 0 ? maxHistorySize : maxHistorySize - 1;
return conversation.slice(-adjustedMaxHistorySize);

For maxHistorySize=0 (and =1, rounded to 0) adjusted is 0, and conversation.slice(-0) is conversation.slice(0) — the whole array. Asking to keep at most 0 messages therefore kept all of them. InMemoryChatStorage.saveChatMessage() and fetchChat() both use this helper, so TS callers passing 0 or 1 get unbounded history.

This PR adds an explicit guard returning [] when adjustedMaxHistorySize <= 0, and a regression test in typescript/tests/storage/ChatStorage.test.ts covering 0, 1, a size >= 2, and undefined. No change for undefined or even/odd sizes >= 2.

The Python implementation had the same [-0:] defect and was fixed in #731. This is the independent TypeScript codebase (different file, different language, different test suite), so it needs its own fix; without it TS users remain affected.

User experience

Before: fetchChat(..., 0) and fetchChat(..., 1) returned the entire history. After: both return an empty list.

Checklist

  • Changes have been tested
  • I have linked this PR to an existing issue (required)
  • I have performed a self-review of this change
  • Changes are documented
Is this a breaking change?

No. Only the 0/1 boundary changes; all sizes >= 2 and undefined behave as before.

Verification

Compiled the real typescript/src/storage/chatStorage.ts (and memoryChatStorage.ts) with TypeScript 5.5.4 to CommonJS in an isolated task-directory environment, with minimal stubs only for the types/logger type imports. No AWS SDK, jest, or global install was needed. Node 22 executed the compiled production code.

  • Before the fix, trimConversation on a 10-item conversation: 0 -> 10, 1 -> 10, 2 -> 2, 3 -> 2, 4 -> 4, 5 -> 4, 10 -> 10, undefined -> 10.
  • After the fix: 0 -> 0, 1 -> 0, with 2/3/4/5/10/undefined unchanged. 8 assert.strictEqual checks passed.
  • Black-box check through the real InMemoryChatStorage public API (save 4 alternating-role messages, then fetchChat): (...,0) -> 0, (...,1) -> 0, (...,2) -> 2, (...,undefined) -> 4, and the last kept message is Another answer. These are exactly the assertions in the new jest regression test.
  • git diff --check passes. Net diff is 2 files, 26 insertions / 1 deletion.

Not done: the repository's full npm test (jest + ts-jest) was not run because installing the full dev dependency tree (AWS SDK and friends) did not complete in this environment. Instead the compiled production TypeScript was exercised directly with Node assertions, as described above. No cloud services were used.

AI Assistance

This PR was prepared with the help of an AI coding agent (WorkBuddy). The agent read the TypeScript and Python implementations, compiled the real TypeScript source, ran the reproduction and assertions above locally, and reviewed the resulting diff. The account owner has not personally run these tests.

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

This branch has not been deployed

No deployments
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(TS): trimConversation keeps full history for maxHistorySize 0 or 1

1 participant