Skip to content

CFM-18: lower attachment threshold so large saves succeed (backport of #428) - #432

Merged
kswenson merged 4 commits into
v1.9.xfrom
CFM-18-large-save-failing-1.9.x
May 22, 2026
Merged

CFM-18: lower attachment threshold so large saves succeed (backport of #428)#432
kswenson merged 4 commits into
v1.9.xfrom
CFM-18-large-save-failing-1.9.x

Conversation

@kswenson

Copy link
Copy Markdown
Member

Backports the bug fix from #428 onto the v1.9.x release branch.

Summary

Large interactive states near 480 KiB produced ~1.06 MB Firestore answer documents that exceeded the 1 MB limit and failed to save. Activity Player stores the state twice as escaped JSON strings inside a report wrapper, expanding it to ~2.2 bytes of document per character of state. This lowers kDynamicAttachmentSizeThreshold from 480 KiB to 400 KiB so oversized states are offloaded to an S3 attachment before the document grows too large.

Additionally, shouldSaveAsAttachment() now measures the serialized state's UTF-8 byte length (via TextEncoder) instead of JSON.stringify(content).length, which counts UTF-16 code units. The previous check undercounted non-ASCII content relative to Firestore's byte-based 1 MB limit, so multi-byte states could exceed the limit even while reported as "below" the threshold. A regression test covers this case, and a jsdom test-environment shim provides TextEncoder for tests.

Details

Cherry-picked (git cherry-pick -x) the three commits from #428. One conflict in src/setupTests.ts: the v2.2.x branch has additional jestSpyConsole setup that does not exist on v1.9.x. Resolved by keeping only the TextEncoder polyfill (the part the bug fix needs) and omitting the unrelated jestSpyConsole lines. The interactive-api-provider test suite passes (15/15), including the new multi-byte regression test.

See the original investigation: https://gist.github.com/dougmartin/54bafa93a9dc5185a609da8b5b63b4ac

Relates to CFM-18.

Test plan

  • CI passes
  • npx jest src/code/providers/interactive-api-provider.test.ts passes locally

Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com

dougmartin and others added 4 commits May 22, 2026 16:25
Large interactive states near 480 KiB produced ~1.06 MB Firestore
answer documents that exceeded the 1 MB limit and failed to save.
Activity Player stores the state twice as escaped JSON strings
inside a report wrapper, expanding it to ~2.2 bytes of document
per character of state. Lower kDynamicAttachmentSizeThreshold
from 480 KiB to 400 KiB so oversized states are offloaded to an
S3 attachment before the document grows too large.

(cherry picked from commit 41cd3cd)
- Clarify that state at or above the threshold is offloaded, matching
  the inclusive >= check in shouldSaveAsAttachment.
- Use consistent KiB/MiB units in the size math.

(cherry picked from commit de0d63a)
shouldSaveAsAttachment() measured String.prototype.length (UTF-16 code
units), which undercounts non-ASCII content relative to Firestore's
byte-based 1 MiB document limit. Switch to UTF-8 byte counting via
TextEncoder so the size check matches the byte-based threshold.

Adds a jsdom test-environment shim for TextEncoder and a regression
test covering multi-byte content. (spec R6)

(cherry picked from commit ddc46e9)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@kswenson
kswenson merged commit a460d07 into v1.9.x May 22, 2026
2 checks passed
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.

2 participants