Skip to content

Cap the cost of history.watch #214

Closed

Description

@HMarzban

Problem

Among the history read ops, history.list has a per-connection cooldown (listCoolingDown, apps/hocuspocus.server/src/hocuspocus.server.ts:119). history.watch has a count cap (watchOverBudget, :128-141). The gate checks both at :314-317. history.revert carries its own separate cooldown (revertCooldown, :111 and :171).

history.watch has a count cap, but no byte cap. Each call reads one row and base64-encodes the whole document (apps/hocuspocus.server/src/lib/history-stateless.ts:181-193, encoder at apps/hocuspocus.server/src/lib/history-stateless.ts:92).

That encode is synchronous work on the shared process event loop. The Throttle extension only covers onConnect, so no per-frame cap stands behind it (apps/hocuspocus.server/src/hocuspocus.server.ts:110). A client that loops over versions meets a count cap, but no byte cap.

What to do

Add a cost cap to the watch arm. Shape it as a bytes-per-connection budget, not as a cooldown.

The count cap at apps/hocuspocus.server/src/hocuspocus.server.ts:126-141 already refuses a tight loop, and its comment says the client now stops on a refusal. Add the byte budget beside that cap. Do not add a time cooldown.

Acceptance

  • A client that requests every version in a tight loop is refused once its budget is spent.
  • A single normal watch still returns the snapshot.

Notes

The cost of one watch encode is not measured. Size the budget against a real document before you pick a number.

Activity

  1. added
    enhancement
    on Aug 31, 2026
  2. HMarzban commented on Oct 15, 2026

    @HMarzban
    CollaboratorAuthor

    Related issues


    Cross-link review, 2026-10-15. Generated by Claude Code

  3. added
    Security
    on Oct 24, 2026
  4. added this to the v2.3.0 milestone on Oct 24, 2026
  5. HMarzban commented on Oct 24, 2026

    @HMarzban
    CollaboratorAuthor

    Fix, 2026-10-24

    The count cap from 2acb4613d now exists, and the Problem section names it. The count cap (WATCH_MAX = 8 per WATCH_WINDOW_MS = 10_000) does not bound the bytes per window on a large document. Two changes cap the cost of history.watch.

    1. Fix the read cost through Upgrade Prisma to 7.10.1 and make document types accountable #198, checked by its before-and-after timing. Ruling 6 on [Tracking] Test gaps and backend performance from the 2026-10-23 review #575 says to wait for Upgrade Prisma to 7.10.1 and make document types accountable #198, with no base64 stopgap. Upgrade Prisma to 7.10.1 and make document types accountable #198 may not remove the whole cost, so re-time this read after it lands.
    2. Add a byte budget per connection per window, beside the existing watchTimes map, with the same rate-limited refusal. Set it to about 20 MB, just below three frames of the largest stored head. The budget is provisional; Find out whether the ws package on Bun repeats large frames on amd64 #602 may change it.

    Drop the "same-version skip" idea. The client keeps no snapshot cache, so a skipped reply would leave it with no data.

    Acceptance items.

    • Move the budget check and watchOverBudget into a pure module under apps/hocuspocus.server/src/lib/, so bun test can reach it. hocuspocus.server.ts imports lib/queue.ts, which opens Redis at import, so no test can import that file.
    • A bun test case: a connection that passes the byte budget inside the window gets rate-limited, while its count stays under WATCH_MAX. It runs on every pull request.
    • Remove the budget locally, and see the test fail. Restore it.
    • Re-time the read after Upgrade Prisma to 7.10.1 and make document types accountable #198, and post the number here.

    This issue now carries the Security label and sits in milestone v2.3.0, under #575. Step 2 can land now. Step 1 waits on #198.

  6. HMarzban commented on Oct 25, 2026

    @HMarzban
    CollaboratorAuthor

    Built and committed on local main. Awaits the maintainer's final push.

    Remaining gate: the backend deploy. Measured after #198: the watch read now takes about 2.2 to 3.3 ms on the large test document, down from about 120 to 160 ms.

  7. HMarzban commented on Nov 3, 2026

    @HMarzban
    CollaboratorAuthor

    Shipped in d40b265b7 (changelog in 95c1b0acf). Each connection now has a 20 MiB byte budget per 10 s window, beside the count cap. A watch past the budget gets the same rate-limited refusal. After #198, the watch read blocks the loop for about 2.2 to 3.3 ms, down from about 120 to 160 ms.

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

    Securityenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions