Repository navigation
fix(core): let JacksonJsonCodec read back every string it can write - #3468
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
39d84ae to
1e0171a
Compare
Jackson's default StreamReadConstraints reject any single string value above 20,000,000 characters on read, while serialization imposes no cap at all. Every AgentStateStore -- JSON file, JDBC, Redis, MongoDB, OSS, COS, the Service JPA store -- persists through JsonUtils.getJsonCodec(), i.e. this codec's default ObjectMapper, and an AgentState carrying inline Base64 media (a 25 MB video is 33.5 million characters) saved successfully and then failed to load on the very next turn. Because the load happens before the model is called, every later turn of that session failed too, text-only ones included: a session bricked by its own persisted state. Configure the default ObjectMapper's JsonFactory with maxStringLength set to Integer.MAX_VALUE so the reader accepts whatever the writer emitted. Nesting depth, number length and name length keep their Jackson defaults; a caller- supplied ObjectMapper is left untouched. Strings this large only ever reach the codec from the framework's own persisted state -- model output and tool arguments are bounded far below the old limit -- so no untrusted boundary loses protection. Offloading media out of AgentState (agentscope-ai#2867) remains the right long-term answer; this restores the round-trip invariant the stores already rely on. Fixes agentscope-ai#3465
1e0171a to
d853f25
Compare
|
The
Supporting evidence:
Nothing in this diff touches the failing paths. I'm happy to re-run again, but given |
|
Friendly bump — this has been approved since 2026-10-14 and the checks are still green on the same head ( |
|
@jujn — a question rather than a nudge. #3468 has had Buktal's approval since 14 October and CI has stayed green, so I have assumed it is simply waiting its turn. If there is something you would want changed before it can land — scope, a test you would rather see, or a different place for the change — I would much rather hear that and fix it than let it sit in the queue. The same question applies to the others still open. #3464 has two independent reproductions from @jacksonw111 and @lianYeah both recommending merge; #3888, #3889, #3890 and #3961 are the more recent ones. I do not want to flood the queue, so if there is an order you would prefer, or a subset you would rather look at first, I will follow that and hold the rest. Two of them are worth having in the next release if they fit: #3468 (a codec that cannot read back what it writes) and #3464 (the JDBC store rejecting valid session keys on PostgreSQL) are both defects users have already hit, not new features. Is there anything that would make these easier to get through — smaller PRs, a different split, more evidence on a specific point? Happy to do the work; I would just rather spend it on what you actually need. |
Symptom
An
AgentStatecarrying inline Base64 media (the report's case: a 25 MBVideoBlock, 33,563,888 Base64 characters) saves successfully and then cannot be loaded. The load happens before the model is called, so every later turn of that session fails too — text-only turns included. The session is bricked by its own persisted state.Reported in #3465 against
OssAgentStateStore, with a standalone repro that needs no OSS, video or model.Root cause
The store is not where the bug lives. Every
AgentStateStore— JSON file, JDBC, Redis (Jedis/Redisson), MongoDB, OSS, COS and the Service JPA store — persists throughJsonUtils.getJsonCodec(), i.e. the single defaultJacksonJsonCodec. ItscreateDefaultObjectMapper()is a barenew ObjectMapper(), so it inherits Jackson's defaultStreamReadConstraints: a single string value may not exceed 20,000,000 characters on read. Serialization has no such cap.That is a round-trip asymmetry inside one codec:
toJsonaccepts a state that the same codec'sfromJsonrejects withA Base64 payload crosses 20 M characters at ~15 MB of source bytes; an OpenAI-size image limit (20 MB) already does.
Fix
Build the default
ObjectMapperfrom aJsonFactorywhose read constraints setmaxStringLengthtoInteger.MAX_VALUE, so the reader accepts whatever the writer emitted. Nesting depth, number length and name length keep their Jackson defaults, and a caller-suppliedObjectMapper(new JacksonJsonCodec(mapper)) is left untouched — only the framework's own default changes.Why the reader cap, not the writer
Jackson's read cap exists to bound memory from hostile input. Strings of this size only ever reach this codec from the framework's own persisted state: model output and tool arguments are bounded by provider output limits (well under 1 M characters), and HTTP bodies are bounded at the transport layer. So lifting the per-string cap on the default codec does not weaken an untrusted boundary — it removes a limit that only ever fired on data the framework itself wrote. Any finite ceiling below what
toJsoncan emit re-creates the asymmetry for the next larger payload; if you would rather keep one, the single number to change is increateDefaultObjectMapper().Offloading media out of
AgentState(#2867) remains the right long-term answer; this PR restores the round-trip invariant the stores already rely on today.Tests
Added
roundTripsStringsLongerThanJacksonDefaultReadLimittoJacksonJsonCodecTest: a 20,000,001-character string (one over the old cap) throughtoJson→fromJson, compared back.Red — on current
main:Green — with the fix:
spotless:applyapplied. No configuration change and no database migration.Fixes #3465