Skip to content

A message from a client larger than 4 MB closes the connection, and nothing says why #289

Closed

Description

@manhpham90vn

The host splits a large message into pieces (MessageParts, Sources/AgentTermHost/HostServer.swift:1911) and the client joins them (Sources/AgentTermClient/AgentTermConnection.swift:267). The other way nothing is split or bounded: AgentTermConnection.send (AgentTermConnection.swift:146-152, transmit at 242-246) sends each message as one text frame, and the host's WebSocket takes at most 4 MB (webSocket.maximumMessageSize = 4 * 1024 * 1024, HostServer.swift:1756).

What happens.

  • On the iPhone, or on a Mac showing another Mac: a text file of 5 to 20 MB is opened with "Open Anyway" (FileContent.largestOpenedAnyway, Sources/AgentTermProtocol/Workspace.swift:789), edited and saved. writeFile(path, text) goes as one frame (iOS/AgentTerm/PhoneModel.swift:2279, Sources/AgentTermMac/FilesModel.swift:414). The connection drops, nothing is saved and no reason is given (principle 3).
  • browserReply(.image(png:)), the picture an agent asked of a page (Sources/AgentTermMac/BrowserAgent.swift:543, BrowserDevTools.swift:526): a whole page or a large viewport in base64 can pass 4 MB. Only browserFrame is capped (PagePicture.mostBytes).

Found by reading the code in the review of 2026-11-22, at commit 968516d; not reproduced yet. A second reading confirmed that the client sends one frame and that the host's limit is 4 MB. That a frame over the limit closes the connection is taken from the comment at Sources/AgentTermProtocol/PagePicture.swift:16-18, not tried.

A way to fix it, not decided: split in AgentTermConnection.send as the host does and join on the host, or refuse a message that is too large in words before it is sent. Which of the two is a design choice (a change to the protocol): the owner's.

  • Reproduced: a test that sends a writeFile over 4 MB to a host of its own and fails because the file is not written, seen failing
  • Fixed at its root, the test passing, the layers of rule 2 run, committed, CI green

Decided by the owner on 2026-11-23: a client's large messages are split and joined, as the host's are.

Definition of done

  1. Decided by the owner first: client messages split and joined, or refused in words above a size.
  2. A test (AgentTermHostTests, a host with a state folder and a port of its own) that saves a text of more than 4 MB through AgentTermConnection and fails because the connection closes, seen failing, then passing and kept; one for a browser picture's answer of that size.
  3. A file the apps let the user open and edit can be saved from the iPhone and from another Mac, or the user is told at once, in the top banner of both apps, why not and what to do; the connection is never lost over the size of a message.
  4. The words are in AgentTermProtocol (principle 9); the protocol's number is raised if a message changes or pieces are added the other way (principle 6).
  5. docs/DESIGN.md (Protocol) says how large messages go both ways; a case in docs/TESTING.md named by the test.
  6. swift build, swift test, scripts/test-mac.sh, scripts/test-ios.sh "iPhone Air", scripts/test-both.sh "iPhone Air".
  7. make format and make lint, committed on the milestone's branch and pushed, CI on the branch green.

Tiếng Việt

Đây là bản dịch của phần tiếng Anh ở trên; các ô đánh dấu chỉ có ở phần tiếng Anh.

Host chia một message lớn thành nhiều phần (MessageParts, Sources/AgentTermHost/HostServer.swift:1911) và client ghép lại (Sources/AgentTermClient/AgentTermConnection.swift:267). Chiều ngược lại không có gì được chia hay giới hạn: AgentTermConnection.send (AgentTermConnection.swift:146-152, transmit ở 242-246) gửi mỗi message thành một text frame, và WebSocket của host nhận tối đa 4 MB (webSocket.maximumMessageSize = 4 * 1024 * 1024, HostServer.swift:1756).

Điều xảy ra.

  • Trên iPhone, hoặc trên một Mac đang xem Mac khác: một file text 5 đến 20 MB được mở bằng "Open Anyway" (FileContent.largestOpenedAnyway, Sources/AgentTermProtocol/Workspace.swift:789), sửa rồi lưu. writeFile(path, text) đi thành một frame (iOS/AgentTerm/PhoneModel.swift:2279, Sources/AgentTermMac/FilesModel.swift:414). Kết nối rớt, không có gì được lưu và không có lý do nào được nêu (nguyên tắc 3).
  • browserReply(.image(png:)), ảnh agent xin của một trang (Sources/AgentTermMac/BrowserAgent.swift:543, BrowserDevTools.swift:526): cả trang hoặc một viewport lớn ở dạng base64 có thể vượt 4 MB. Chỉ browserFrame được giới hạn (PagePicture.mostBytes).

Tìm thấy khi đọc code trong đợt rà soát ngày 2026-11-22, tại commit 968516d; chưa tái hiện. Lần đọc thứ hai xác nhận client gửi một frame và giới hạn của host là 4 MB. Việc một frame vượt giới hạn làm đóng kết nối là theo comment ở Sources/AgentTermProtocol/PagePicture.swift:16-18, chưa thử.

Một cách sửa, chưa quyết định: chia trong AgentTermConnection.send như host làm và ghép ở host, hoặc từ chối bằng lời một message quá lớn trước khi gửi. Chọn cách nào là quyết định thiết kế (thay đổi protocol): của owner.

  • Tái hiện: một test gửi writeFile hơn 4 MB tới một host riêng của nó và fail vì file không được ghi, đã thấy fail
  • Sửa tận gốc, test pass, chạy các tầng của rule 2, commit, CI xanh

Đã quyết bởi owner ngày 2026-11-23: tin lớn của client được chia nhỏ rồi ghép lại, giống cách host đang làm với tin của mình.

Định nghĩa hoàn thành

  1. Owner quyết định trước: message của client được chia và ghép, hay bị từ chối bằng lời khi vượt một kích thước.
  2. Một test (AgentTermHostTests, host có state folder và port riêng) lưu một text hơn 4 MB qua AgentTermConnection và fail vì kết nối đóng, đã thấy fail, rồi pass và được giữ lại; một test cho câu trả lời ảnh browser cỡ đó.
  3. File mà app cho user mở và sửa thì lưu được từ iPhone và từ Mac khác, hoặc user được báo ngay, ở banner trên cùng của cả hai app, vì sao không và phải làm gì; kết nối không bao giờ mất vì kích thước của một message.
  4. Câu chữ nằm trong AgentTermProtocol (nguyên tắc 9); số của protocol được tăng nếu một message thay đổi hoặc thêm việc chia phần ở chiều ngược lại (nguyên tắc 6).
  5. docs/DESIGN.md (Protocol) nói message lớn đi thế nào ở cả hai chiều; một case trong docs/TESTING.md được test nêu tên.
  6. swift build, swift test, scripts/test-mac.sh, scripts/test-ios.sh "iPhone Air", scripts/test-both.sh "iPhone Air".
  7. make format và make lint, commit trên nhánh của milestone và push, CI trên nhánh xanh.

Activity

  1. added this to the 0.15.1 milestone on Nov 22, 2026
  2. added
    bug
    on Nov 22, 2026
  3. manhpham90vn commented on Nov 23, 2026

    @manhpham90vn
    CollaboratorAuthor

    Reproduced on 2026-11-23, on milestone-0.15.1:

    Reproduced on 2026-11-23, on milestone-0.15.1 at e5c1bd8, by ReviewCoreLargeFrameTests.testSavingAFileLargerThanAFrameIsDoneOrRefusedInWords in Tests/AgentTermHostTests/ReviewCoreLargeFrameTests.swift (new, not committed).

    A host of the test's own and the apps' own client (AgentTermConnection); a file of 6 bytes in a folder of the test's; writeFile(path:text:) with a text of 5 MB.

    swift test --filter ReviewCoreLargeFrameTests:

    XCTAssertFalse failed - the connection was closed; the host said nothing after the save was sent (0 other messages), and the file has 6 bytes
    XCTAssertTrue failed - neither saved nor refused in words
    

    What it proves: a message of a client over 4 MB closes the connection (in 0.09 s here), nothing is saved and no error is sent. Not tried: what each app then shows (the Mac app on another Mac, the iPhone), and the browser's browserReply(.image) of a large picture, which goes the same way by reading.

    The failing test, kept out of the branch while the issue is not done (it would make CI red), to be put back with the fix (Tests/AgentTermHostTests/ReviewCoreSupport.swift is on the branch):

    ReviewCoreLargeFrameTests.swift

    import AgentTermClient
    import AgentTermProtocol
    import XCTest
    
    @testable import AgentTermHost
    
    /// #289 (review of 2026-11-22): a message from a client larger than the
    /// 4 MB a frame may be. The host splits what it sends; the client does not.
    final class ReviewCoreLargeFrameTests: XCTestCase {
        /// Saving a file of 5 MB, which both apps let the user open and edit:
        /// it is saved, or the client is told in words why not. It is never the
        /// connection that goes, with nothing said.
        func testSavingAFileLargerThanAFrameIsDoneOrRefusedInWords() throws {
            let folder = try ReviewCore.Folder("frame")
            defer { folder.remove() }
            let server = HostServer(configuration: folder.configuration())
            let port = try server.start()
            defer { server.stop() }
            let client = ReviewCore.Client(port: port, name: "mac")
            defer { client.connection.close() }
            XCTAssertTrue(ReviewCore.spin { client.welcomed }, "the client was not welcomed")
            let file = folder.root.appendingPathComponent("large.txt")
            try "before".write(to: file, atomically: true, encoding: .utf8)
            let text = String(repeating: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\n", count: 5 * 1024 * 1024 / 64)
            let before = client.messages.count
            client.connection.send(.writeFile(path: file.path, text: text))
            func saved() -> Bool { client.messages.contains { if case .fileSaved(let path) = $0 { return path == file.path } else { return false } } }
            ReviewCore.spin(15) { saved() || !client.errors.isEmpty || client.isClosed }
            let after = client.messages.dropFirst(before).filter { if case .systemStats = $0 { return false } else { return true } }
            let onDisk = (try? Data(contentsOf: file))?.count ?? -1
            XCTAssertFalse(client.isClosed, "the connection was closed; the host said \(client.errors.isEmpty ? "nothing" : "\(client.errors)") after the save was sent (\(after.count) other messages), and the file has \(onDisk) bytes")
            XCTAssertTrue(saved() || !client.errors.isEmpty, "neither saved nor refused in words")
        }
    }
  4. manhpham90vn commented on Nov 23, 2026

    @manhpham90vn
    CollaboratorAuthor

    Fixed in 753bd75, as the owner decided: a client's message larger than 256 KB goes in pieces (MessageParts, binary frames numbered by the client) and the host joins them, up to MessageParts.largestFromAClient (64 MB); one larger is answered with message too large, said in both apps as HostErrorWords.tooLarge, and the connection goes on. Protocol 41. REMOTE-34 (LargeMessagesTests): saving 6 MB of text, a browser picture's answer of 5 MB, and a message over 64 MB each closed the connection without a reason before the fix (seen with the client's splitting turned off), and pass. docs/DESIGN.md (Protocol) says it.

    Run on milestone-0.15.1, 2026-11-23: swift build, swift test, scripts/test-mac.sh in full, scripts/test-ios.sh "iPhone Air" and scripts/test-both.sh "iPhone Air" passing on 022aa66; make lint; CI on the branch green (run 45152397234).

    The save from a real iPhone is in the milestone's pull request (#326). Done in full: closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions