Repository navigation
fix(#9627): cap every read of the AI Assistant's portal and gateway answers - #9754
Conversation
…nswers Stream lines (portal NDJSON turn, gateway SSE) are read through a strict SupportLineReader that fails at 4M chars instead of BufferedReader.readLine(); refusal bodies of the streamed chat are read with readNBytes(64 KB); the four buffered gateway calls use LimitedStringBody with the 16 MB support cap. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 8 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| BestPractice | 2 medium |
🟢 Metrics 14 complexity
Metric Results Complexity 14
🟢 Coverage 90.74% diff coverage
Metric Results Coverage variation Report missing for 367be851 Diff coverage ✅ 90.74% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (367be85) Report Missing Report Missing Report Missing Head commit (b22385d) 262080 211469 80.69% Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#9754) 54 49 90.74% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%1 Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
ReviewOverall this is a solid, well-scoped fix for #9627. It reuses the existing What looks good
Suggestions (minor)
No security or performance regressions found. The change is bounded and adds no new dependencies. Looks good to merge once the points above are considered. |
…ror bodies in messages Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ReviewSolid, well-scoped fix for #9627. It reuses the existing support-module bounds ( Good
Minor, non-blocking
No security or performance concerns beyond the above. LGTM once (1) is considered. |
…user Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ReviewOverall this is a solid, well-scoped fix for #9627. It reuses the existing What looks good
Suggestions
No security concerns beyond the above, and no new dependencies. The code follows the CLAUDE.md conventions I checked ( |
…answer by its start Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ReviewSolid, well-scoped fix for #9627. It reuses the existing What looks good
Minor suggestions (non-blocking)
No blocking issues found. LGTM once point 3 is confirmed. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9754 +/- ##
============================================
+ Coverage 77.72% 77.73% +0.01%
Complexity 4438 4438
============================================
Files 2195 2195
Lines 229358 229463 +105
Branches 49632 49655 +23
============================================
+ Hits 178267 178378 +111
+ Misses 32203 32187 -16
- Partials 18888 18898 +10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Closes #9627
The AI Assistant read every answer from the customer portal and the AI gateway with no byte cap. Stream lines (the portal's NDJSON turn and the gateway's SSE chat) went through
BufferedReader.readLine(), which accumulates a line until it sees a newline, and the silence bound does not end a peer that keeps sending. The streamed chat's two refusal bodies usedreadAllBytes(), and the four buffered gateway calls usedBodyHandlers.ofString(), bounded in time, not in bytes. This PR applies the bounds the support module already has:SupportLineReadergets a strict variant,SupportLineReader.failingOver(reader, maxLine), which throwsLineTooLongExceptionas soon as a line goes past the cap, without reading the rest of it (the lenient log-reader mode is unchanged).AiPortalChat.readTurnandAiChatHandler.handleStreamingRequestread through it withAiPortalChat.MAX_EVENT_CHARS(4M chars). On the portal path an over-long line ends the turn asSupportPortalException("portal_error"). On the gateway path it ends the SSE stream in band, the same way any other mid-stream failure does.readNBytes(64 KB), the same asSupportPortalClient.openStream.callGatewayof chat and analyze-profiler,/api/activate, the tool-result POST):SupportPortalClient.LimitedStringBodywith the 16 MBSupportPortalClient.MAX_RESPONSE_BYTES. The class is now public and takes a peer name, so its error says "The AI gateway answered with more than ... KB".Test plan
SupportLineReaderTest(new, 4): strict reader stops within one chunk of the cap on an endless line; lines up to the cap read unchanged; the cap applies across chunk boundaries; the lenient mode still cuts and carries on.AiPortalChatTest(+2): a 32 MB newline-free turn line ends the turn asportal_error, and the fake portal could deliver less than half of it. A line under the cap reads as before.Issue9627AiGatewayUnboundedReadTest(new, 12; 8 at first push, 4 added in review): for each gateway read, a gateway that floods 48 MB is answered correctly and gets cut off before delivering half of it. The 8th test checks that an answer under the cap is unchanged.BufferedReader/readAllBytes/ofString(verified locally), and pass with the fix.server/.../ai/*Testandserver/.../support/*Testclasses plusHardcodedTestServerPortsTest: 355 run, all pass exceptAiSchemaDigestTest.aSchemaOfFiveHundredTypesStaysSmallAndFast. That test fails locally withToo many open files(macOSmaxfilessoft limit 256) while creating 500 types, and it touches no changed class.Coverage
Invariant: no read of an answer from the AI portal or gateway buffers more than a fixed cap on the heap.
Sweep:
grep -rnE "BodyHandlers\.of(String|InputStream|ByteArray)\(\)|readAllBytes\(\)|readLine\(\)" server/src/main/java/com/arcadedb/server/ai server/src/main/java/com/arcadedb/server/supportAiPortalChat.readTurn(portal NDJSON)readLine()AiPortalChatTestAiChatHandler.handleStreamingRequestSSE loopreadLine()aStreamedChatLineLongerThanTheCapEndsTheStreamAiChatHandler.handleStreamingRequest401/403 bodyreadAllBytes()aStreamedChatUnauthorizedWithAnEndlessBodyReadsOnlyItsStartAiChatHandler.handleStreamingRequestnon-200 bodyreadAllBytes()aStreamedChatRefusedWithAnEndlessBodyReadsOnlyItsStartAiChatHandler.postToolResultofString()aToolResultAnsweredWithAnEndlessBodyIsCappedAiChatHandler.callGatewayofString()aBufferedChatAnswerLargerThanTheCapIsRefusedAiAnalyzeProfilerHandler.callGatewayofString()aProfilerAnalysisAnswerLargerThanTheCapIsRefusedAiActivateHandler/api/activateofString()anActivationAnswerLargerThanTheCapIsRefusedAiPortalClient.send/stop/usageSupportPortalClient.callLimitedStringBody, 16 MB)SupportPortalClient.openStreamrefusal bodyreadNBytes(64 KB)AiConfiguration,ChatStorageFiles.readAllBytesSame-shape reads outside the AI module (
ha-raftpeer queries,PostBatchHandler,LeaderCommandForwarder,RemoteDatabase) read from authenticated cluster peers or, client-side, from an ArcadeDB server. They are not a third-party vendor stream, so this issue does not cover them.Known gaps: None.
Adversarial pass
No independent subagent ran: this session had no Task/Agent tool. The author did a self-check instead:
InputStreamReader.read(char[])blocking: likeBufferedReader.fill, it returns once nothing more isavailable(), so heartbeats are still relayed as they arrive (the existingIssue8473/Issue8642streaming tests pass).MAX_EVENT_CHARS. Before, an 8K-char buffer plus an unbounded line.Review cycles
f677e2d93etoken_invalidviarefusalBody) and #2 (error bodies go into exception messages and logs only as a 1 KBexcerpt, which also covers the bufferedresponse.body()messages and the tool-result log line)f5750cf782refusalBody,excerpt+ tests30df31b49eJSONException). This also showed thatJSONException's message embeds the whole input, soparseAnswernow reports a non-JSON 200 answer by its excerpt onlyb22385d51eparseAnswer, narrowed catch + testDeferred / skipped items
AiChatHandler.limitedBody()... could live in a small shared helper or onSupportPortalClient" (cycle 1): cosmetic. It stays next to the other AI gateway bounds (gatewayTimeoutMsand friends).gatewayTimeoutMs/streamSilenceMsseams, which are documented as JVM-wide and restored in@AfterEach/finally. The server module does not run test classes in parallel.Issue9627AiGatewayUnboundedReadTest, one test per call site plus the SSE line.maxEventCharsreuse" (cycles 2, 3): theMAX_EVENT_CHARSjavadoc already names both streams ("one NDJSON envelope, one SSE line"), and the SSE read site carries a comment.@Tag(\"slow\")" (cycles 2, 4): the whole class runs in about 1.5 s, because the readers cut each flood off early.LimitedStringBody(long)and theResponseTooLargeExceptionconstructor stay package-private.result/doneevent is kept, so the heap per stream is one line at most. Total duration stays under the silence bound andMAX_ROUNDSby design ("a conversation that keeps producing events runs as long as it needs to").allocateFreePorts" (cycle 4):HardcodedTestServerPortsTest's javadoc explicitly exempts a held-boundnew ServerSocket(0, backlog, address)for "the stub gateways of the AI tests". That scan passes with this class included.get_schema) go outbound in a POST, so they are not subject to the 4M-char line cap.Final state
clean-approvalafter 4 cycles. Merge is the developer's responsibility.🤖 Generated with Claude Code