Skip to content

perf(hub): 聊天页轮询 ETag/304 + >256KB 正文 gzip 1 级 - #3801

Merged
vansin merged 3 commits into
mainfrom
feat/tasks-conditional-poll
Nov 23, 2026
Merged

vansin merged 3 commits into
mainfrom
feat/tasks-conditional-poll

Conversation

@vansin

@vansin vansin commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

聊天页三条轮询的条件请求 + 大正文压缩降级(PR-B,接 #3800 之后)。文中所有数字都是单次测量(线上库只读单条查询 / 本地合成表),不是压测;上线后以 stall 归因的线上数为准。

改动

① /api/tasks(skip_stats 与非 skip_stats 两处返回):改成 conditionalJson —— 内容哈希 ETag + If-None-Match 命中回 304 空体 + gzip 结果按 ETag 复用
   to_name(入站)与 from_name(出站)走同一个 handler,两条查询都覆盖
② /api/messages(默认分支)与 /api/messages?scope=user:同样 conditionalJson
③ http-gzip:正文 > 256KB 用 1 级压缩,≤256KB 维持原级别(带键的仍压到 9 级——只压一次)

ETag = 响应体 sha256 的前 27 位(conditionalJson 既有实现),所以任何会改变正文的状态都必变:task 的 status/result/late_replies、user_inbox 的 acked、created_at 等。

测试:server/src/conditional-poll-etag-http.test.ts(真 HTTP)

① /api/tasks?to_name=… 首取 200 + ETag;同 ETag 再取 ⇒ 304、空体、ETag 原样返回
② /api/tasks?from_name=… 同上(出站查询也覆盖)
③ **状态一变 ETag 必变**:改 result ⇒ ETag 变;改 status ⇒ ETag 变
④ /api/messages?scope=user:未变 304;把 user_inbox.acked 置 1 ⇒ ETag 变
⑤ /api/messages?alias=…(默认分支):未变 304、空体

gzip 级别实测(单次测量,供你定阈值)

limit=200(690KB JSON):gzip-1 14.3ms/181KB | gzip-3 18.5ms/165KB | gzip-6 41.8ms/136KB
limit=30 (117KB JSON):gzip-1  3.3ms/ 38KB | gzip-3  6.6ms/ 35KB | gzip-6 16.5ms/ 31KB
⇒ >256KB 用 1 级:省 ~2/3 压缩 CPU,多发 33% 字节;≤256KB 维持 6 级(这档 1 级省不到 4ms,不值得多发字节)

字段占比(30 条一页,仅报数据不改字段)

result 41% / content 38% / meta_json 2% / 其余各 ≤2%(200 条时 result 46%、content 29%、meta_json 4%)
⇒ 单条 3.8KB 主要是聊天正文与回复正文本身,没有「列表页用不到的大字段」可白砍;要瘦得和 App 对齐(另开卡)

预期效果

聊天进行中:仍然每次 200(内容在变),但 >256KB 时压缩从 41.8ms 降到 14.3ms(单次测量)
安静时:If-None-Match 命中 ⇒ 304 空体,正文与 gzip 全省(App #1680 的轮询收敛会一起放大这个收益)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T04:32:22.335489Z 3204cf9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vansin

vansin commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

按你的三条改完(commit 4608f76a)。全部是单次测量,上线后以 stall 归因的线上数为准。

1. 114KB(30 条)四级实测

level 1:  2.28ms ->  41.5KB
level 3:  3.05ms ->  37.8KB
level 6:  7.75ms ->  33.3KB
level 9: 15.30ms ->  32.8KB   ← 你担心的「活跃会话每变一次就用 9 级重压」确认为真:比 6 级慢一倍

2. 三档按大小,不再区分带不带键

>256KB      → 1 级
32–256KB    → 3 级(实测 125KB:3.0ms/37.8KB vs 6 级 7.8ms/33.3KB,省 ~2.5× CPU,多发 13% 字节)
<32KB       → 维持现状(带键 9 级 / 不带键默认)

改动点在 http-gzip.ts(GZIP_MID_BODY_BYTES + 三档选择),条件请求走同一套。

3. 「很少变」的调用方显式要高压缩 + 进缓存 —— 我选这种,理由如下

需求表 708KB:1 级 690KB 实测 186KB(+33% vs 6 级的 140KB);放到 708KB 约 190KB vs 现在的 163KB(+17%)。
  链路上多发 ~27KB,按中→美 ~13KB/s 就是多 2 秒 —— 对「15 秒轮询、每次只差几行」的表是净亏。
⇒ 所以不把需求表拖进 1 级;给 conditionalJson 加两个正交开关:
  { level } 显式压缩级别(需求表传 9)
  { cache } 是否让这份正文进 gzip 缓存(默认**不进**)
轮询端点(tasks / messages)都不传 cache ⇒ **永不进缓存**,因此聊天轮询无论多少种正文都挤不掉需求表那几份;
需求表显式 { level: 9, cache: true } ⇒ 行为与现在完全一致(708KB / 163KB / 压一次用很多次)。

关于「16MB 上限会不会被挤掉」:旧实现在「带键」下所有条件响应都会进缓存,聊天每来一条消息就换新键(≈114KB 一份)⇒ 十几份就能把需求表顶出去;现在缓存准入改成显式 opt-in,这个问题从根上不存在了(不是靠调大上限)。

测试没变(同一套 conditional-poll-etag-http)

to_name / from_name 200+ETag → 同 ETag 304 空体;改 result、改 status ⇒ ETag 变
/api/messages?scope=user 未变 304、改 acked ⇒ ETag 变;/api/messages?alias 未变 304

@vansin
vansin force-pushed the feat/tasks-conditional-poll branch from d4dfd7f to 5c77a2e Compare November 23, 2026 04:57
@vansin
vansin merged commit 2fdd6f2 into main Nov 23, 2026
74 checks passed
vansin added a commit that referenced this pull request Nov 24, 2026
…h current code (#3860)

* fix(tests): bring the #5402 B-group suites back in line with current code

- test697: pin the exact non-Codex runtime set (antigravity-acp joined in #3670)
- test635: the preread mutation targeted ensureGlobalAnetConfig, removed on
  purpose in #3627; guard the removal instead. Writer mutation: match the
  failing test by name (count moved 3 -> 2)
- test638: image lacked agent-node/package.json + deps and
  docs/rest-route-table.json read by three server tests
- test519: proxy forwarded Content-Encoding: gzip with an already
  decompressed body (ZlibError) once the Hub gzips /api/nodes (#3801)
- test2123: node-permissions schema check is dialect-aware on PostgreSQL;
  agent-teams byte-identity check now names the body that changed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(tests): test638 copies the tracked package-lock.json; test635 guard ignores comments

- test638: agent-node/bun.lock is not tracked, so the image build failed
  ("/agent-node/bun.lock: not found"); use the tracked package-lock.json.
- test635: the global-config guard matched the comment that explains the
  removal; match calls only, skipping comment lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(agent-teams): put the changed keys on the error's first line

The PostgreSQL ladder (test2123) prints only the first line of an error, so
"node config changed after team operations" came without the diff. The
first line now lists each changed top-level key as before -> after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(agent-teams): report the node config error instead of diffing two errors

On PostgreSQL (test2123) both node-config reads were error bodies differing
only in the random error_id, so GET /api/nodes/:id/config fails there. The
test now fails with that error's text on its first line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: ANet负责人 <agent@anet.sh>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant