Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/backend/serving/analytics/automation_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"claude-code",
"cline",
"kilo-code",
"opencode",
"roo-code",
"cursor",
"aider",
Expand Down Expand Up @@ -126,7 +127,11 @@
# Ordered (regex, label) table mirroring apps/frontend RequestsTab.parseClientTool.
_CLIENT_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = (
(re.compile(r"claude-cli/|claude-code/", re.I), "claude-code"),
(re.compile(r"kilo[-_ ]?code/", re.I), "kilo-code"),
# `kilo/...` is what Kilo Code's LLM requests actually send; `opencode/...`
# is OpenCode's. Kilo is ordered first because it is an OpenCode fork and
# some of its other traffic still identifies as `opencode/`.
(re.compile(r"kilo[-_ ]?code/|kilo/", re.I), "kilo-code"),
(re.compile(r"opencode/", re.I), "opencode"),
(re.compile(r"roo[-_ ]?code/", re.I), "roo-code"),
(re.compile(r"cline/", re.I), "cline"),
(re.compile(r"cursor[-_ ]?(?:ide|agent|cli)?/", re.I), "cursor"),
Expand Down
14 changes: 13 additions & 1 deletion apps/backend/serving/utils/session_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
beside them: header names may contain underscores, but it is unusual enough
that intermediaries drop such headers by default (nginx does), so a client
can sensibly send either.
* ``x-session-affinity`` / ``x-opencode-session`` -- OpenCode and its Kilo Code
fork. Both already send ``X-Session-Id`` alongside the affinity header on any
provider they do not recognise as their own, so the canonical source usually
wins first; these are read because one arriving without the other means the
session is still knowable.
* ``metadata.session_id`` / ``client_metadata.session_id`` in the request body
-- a client that declares the session where it declares everything else.
Codex uses ``client_metadata``.
Expand Down Expand Up @@ -60,7 +65,14 @@
#: outlive the run that opened it, and both spellings of each are accepted
#: because header names with underscores, while legal, are dropped by default by
#: some intermediaries (nginx among them) and clients differ over which to send.
_AGENT_SESSION_HEADERS = ("session-id", "session_id", "thread-id", "conversation_id")
_AGENT_SESSION_HEADERS = (
"x-opencode-session",
"x-session-affinity",
"session-id",
"session_id",
"thread-id",
"conversation_id",
)

#: Body objects a client declares its session in, most specific first. Codex
#: puts it in ``client_metadata``; the Anthropic and OpenAI surfaces both define
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ export function parseClientTool(ua: string | null | undefined): string | null {
if (!s) return null;
const patterns: Array<[RegExp, string]> = [
[/claude-cli\/|claude-code\//i, 'claude-code'],
[/kilo[-_ ]?code\//i, 'kilo-code'],
// Kilo Code's LLM requests carry `kilo/<channel>/<version>/<client>`, not
// the `kilo-code/` its extension marketing uses, so the bare `kilo/` form
// has to be matched or every Kilo request falls through to the catch-all.
// Ordered before the `opencode` rule below because Kilo is an OpenCode
// fork and some of its non-LLM traffic still identifies as `opencode/`.
[/kilo[-_ ]?code\/|kilo\//i, 'kilo-code'],
[/opencode\//i, 'opencode'],
Comment thread
1a1a11a marked this conversation as resolved.
Resolved
[/roo[-_ ]?code\//i, 'roo-code'],
[/cline\//i, 'cline'],
[/cursor[-_ ]?(ide|agent|cli)?\//i, 'cursor'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ describe('parseClientTool', () => {
expect(parseClientTool('codex-cli/0.5.0')).toBe('codex');
});

it('recognizes OpenCode and Kilo Code by the agent they actually send', () => {
// Neither matched before: OpenCode had no rule at all, and Kilo's LLM
// requests identify as `kilo/...`, which the `kilo-code/` rule missed. Both
// fell through to the leading-token fallback below.
expect(parseClientTool('opencode/0.4.11')).toBe('opencode');
expect(parseClientTool('opencode/latest/0.4.11/cli')).toBe('opencode');
expect(parseClientTool('kilo/latest/1.9.0/cli')).toBe('kilo-code');
// The marketing spelling keeps working.
expect(parseClientTool('Kilo-Code/1.2.3')).toBe('kilo-code');
});

it('falls back to the leading token for unknown slash-delimited agents', () => {
expect(parseClientTool('myagent/1.0.0')).toBe('myagent');
});
Expand Down
8 changes: 8 additions & 0 deletions docs/developer/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,17 @@ request used:
|---|---|
| `X-Session-ID` header | anything speaking the gateway's own contract; wins whenever present |
| `session-id` / `thread-id` headers (the `session_id` / `conversation_id` spellings too) | Codex CLI, which stamps its run on every request |
| `x-session-affinity` / `x-opencode-session` headers | OpenCode and its Kilo Code fork, which send the affinity header beside `X-Session-ID` on any provider they do not recognise as their own, and `x-opencode-session` on one they do |
| `metadata.session_id` or `client_metadata.session_id` in the request body | a client that labels the session where it labels everything else; Codex uses `client_metadata` |
| `metadata.user_id` in the request body | Claude Code, which packs the run into `user_<hash>_account_<uuid>_session_<uuid>` |

An OpenCode or Kilo build older than the one that restored those headers
(`sst/opencode#43188`, 2026-08-18) sends no session header at all on its newer
runner, and nothing else on the wire names the session — the gateway records
none rather than inferring one from a field that was not meant to carry it.
Upgrading is the fix; on the older runner a `chat.headers` plugin can set
`X-Session-ID` from the session id it is handed.

The admin console's Recent Requests view shows the session under each row's
client, and clicking it filters the list to that one conversation — an exact
match against the indexed column, so it stays a lookup rather than a scan.
Expand Down
125 changes: 93 additions & 32 deletions docs/developer/locale/zh_CN/LC_MESSAGES/database.po
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgid ""
msgstr ""
"Project-Id-Version: HybridInference \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-09-06 00:24+0000\n"
"POT-Creation-Date: 2026-09-06 11:51+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: zh_CN\n"
Expand Down Expand Up @@ -536,6 +536,19 @@ msgstr "`session-id` / `thread-id` 头(`session_id` / `conversation_id` 这两
msgid "Codex CLI, which stamps its run on every request"
msgstr "Codex CLI,它在每个请求上都盖上本次运行的标识"

#: ../developer/database.md
msgid "`x-session-affinity` / `x-opencode-session` headers"
msgstr "`x-session-affinity` / `x-opencode-session` 头"

#: ../developer/database.md
msgid ""
"OpenCode and its Kilo Code fork, which send the affinity header beside `X"
"-Session-ID` on any provider they do not recognise as their own, and `x"
"-opencode-session` on one they do"
msgstr ""
"OpenCode 及其 Kilo Code 分支:对任何它们不认作自家的提供方,会在 `X-Session-ID` "
"旁边一并发出这个亲和性头;对认作自家的那个,则改发 `x-opencode-session`"

#: ../developer/database.md
msgid "`metadata.session_id` or `client_metadata.session_id` in the request body"
msgstr "请求体里的 `metadata.session_id` 或 `client_metadata.session_id`"
Expand All @@ -556,15 +569,32 @@ msgid ""
"`user_<hash>_account_<uuid>_session_<uuid>`"
msgstr "Claude Code,它把本次运行打包进 `user_<hash>_account_<uuid>_session_<uuid>`"

#: ../developer/database.md:134
#: ../developer/database.md:135
msgid ""
"An OpenCode or Kilo build older than the one that restored those headers "
"(`sst/opencode#43188`, 2026-08-18) sends no session header at all on its "
"newer runner, and nothing else on the wire names the session — the "
"gateway records none rather than inferring one from a field that was not "
"meant to carry it. Upgrading is the fix; on the older runner a "
"`chat.headers` plugin can set `X-Session-ID` from the session id it is "
"handed."
msgstr ""
"比恢复这些头的那个版本(`sst/opencode#43188`,2026-08-18)更旧的 OpenCode 或 Kilo "
"构建,在新 runner 上根本不发任何会话头,而链路上也没有别的东西能指明这次会话——这时"
"网关宁可什么都不记,也不从一个本就不是用来承载会话的字段里去推断。升级是正解;在旧 "
"runner 上,可以用一个 `chat.headers` 插件,把它拿到的会话 id 设成 `X-Session-ID`。"

#: ../developer/database.md:142
msgid ""
"The admin console's Recent Requests view shows the session under each "
"row's client, and clicking it filters the list to that one conversation —"
" an exact match against the indexed column, so it stays a lookup rather "
"than a scan."
msgstr "管理控制台的 Recent Requests 视图会把会话显示在每一行的客户端下方,点一下就把列表过滤成那一段对话——这里走的是对索引列的精确匹配,所以它始终是一次查找,而不是一次扫描。"
msgstr ""
"管理控制台的 Recent Requests "
"视图会把会话显示在每一行的客户端下方,点一下就把列表过滤成那一段对话——这里走的是对索引列的精确匹配,所以它始终是一次查找,而不是一次扫描。"

#: ../developer/database.md:138
#: ../developer/database.md:146
msgid ""
"`metadata.session_id_source` on the same row names which of those it came"
" from, so a value read out of a composite id is never mistaken for one a "
Expand All @@ -581,25 +611,25 @@ msgstr ""
"128 个字符、或者带控制字符的声明会被丢弃,而不是记录下来。和上面那些派生列一样,无论 `DB_STORE_FULL_CONTENT` "
"是否打开,会话都会被记录:它是客户端贴在请求上的标签,而不是对话内容的一部分。"

#: ../developer/database.md:147
#: ../developer/database.md:155
msgid "Backup"
msgstr "备份"

#: ../developer/database.md:149
#: ../developer/database.md:157
msgid "`pg_dump` in custom format, straight out of the container:"
msgstr "直接从容器里做 custom 格式的 `pg_dump`:"

#: ../developer/database.md:156
#: ../developer/database.md:164
msgid ""
"Do not add `-t` to `docker exec`: allocating a TTY corrupts the binary "
"stream."
msgstr "不要给 `docker exec` 加 `-t`:分配 TTY 会破坏二进制流。"

#: ../developer/database.md:158
#: ../developer/database.md:166
msgid "Restore into an existing, running database:"
msgstr "恢复到一个已存在且正在运行的数据库:"

#: ../developer/database.md:165
#: ../developer/database.md:173
msgid ""
"Stop the backend (`make down`, or `docker stop hybridinference-backend`) "
"before restoring over a live database, and remember that the backup "
Expand All @@ -610,19 +640,19 @@ msgstr ""
"backend`),并且记住:备份里含有哈希后的 API key,以及——只要 `DB_STORE_FULL_CONTENT` "
"曾经打开过——用户的 prompt 内容。请按相应的密级保管。"

#: ../developer/database.md:170
#: ../developer/database.md:178
msgid "Reset"
msgstr "重置"

#: ../developer/database.md:172
#: ../developer/database.md:180
msgid ""
"Deleting the database is more awkward than it looks, because the Postgres"
" volume is declared `external:` in `deploy/docker/docker-compose.yml`:"
msgstr ""
"删掉数据库比看上去别扭,因为 Postgres 的卷在 `deploy/docker/docker-compose.yml` 里声明为 "
"`external:`:"

#: ../developer/database.md:182
#: ../developer/database.md:190
msgid ""
"**`docker compose down --volumes` does not delete an external volume.** "
"Nor does `make down`, which passes no `--volumes` at all. A reset that "
Expand All @@ -632,7 +662,7 @@ msgstr ""
"**`docker compose down --volumes` 不会删除外部卷**。`make down` 也不会,它压根没传 "
"`--volumes`。依赖这两者中任何一个的「重置」,都会静默地把每一行数据原样留在那里。要按名字删除这个卷:"

#: ../developer/database.md:192
#: ../developer/database.md:200
msgid ""
"`make up` depends on the `docker-volumes` target, which recreates the "
"named volume if it is missing, so the stack comes back on an empty "
Expand All @@ -643,7 +673,7 @@ msgstr ""
"target,具名卷缺失时它会重新创建,于是整套栈在一个空数据库上起来,由启动时的初始化逻辑重建表结构。只要还有一丝想把数据找回来的可能,就先做一份"
" dump。"

#: ../developer/database.md:197
#: ../developer/database.md:205
msgid ""
"The runnable example (`make demo-reset DISTRIBUTION=example`) uses its "
"own, example-scoped volume and cannot delete "
Expand All @@ -652,11 +682,11 @@ msgstr ""
"那个可运行的示例(`make demo-reset DISTRIBUTION=example`)用的是它自己的、限定在 example "
"范围内的卷,删不掉 `hybridinference_postgres_data`。"

#: ../developer/database.md:200
#: ../developer/database.md:208
msgid "Inspecting the database"
msgstr "查看数据库"

#: ../developer/database.md:206
#: ../developer/database.md:214
msgid ""
"Useful starting points: `\\dt` for the table list and `\\d api_logs` for "
"the request log's columns. `make ps` shows the published bindings — "
Expand All @@ -666,11 +696,11 @@ msgstr ""
"几个好用的起点:`\\dt` 看表清单,`\\d api_logs` 看请求日志的列。`make ps` 会列出发布出来的绑定——Postgres"
" 和 pgAdmin 都应该显示成 `127.0.0.1:...`;不是这样就说明数据库在这台机器之外也在监听。"

#: ../developer/database.md:211
#: ../developer/database.md:219
msgid "Optional: pgAdmin"
msgstr "可选:pgAdmin"

#: ../developer/database.md:213
#: ../developer/database.md:221
msgid ""
"The Compose stack ships a pgAdmin service for people who prefer a GUI. It"
" is **profile-gated** — nothing starts it unless the `admin` profile is "
Expand All @@ -679,15 +709,15 @@ msgstr ""
"Compose 栈里带了一个 pgAdmin 服务,给偏好图形界面的人用。它由 **profile 把关**——不点名 `admin` "
"profile 就没有任何东西会启动它——而且它完全可选;上面的 `psql` 什么都能做。"

#: ../developer/database.md:217
#: ../developer/database.md:225
msgid "Starting and restarting it"
msgstr "启动与重启"

#: ../developer/database.md:219
#: ../developer/database.md:227
msgid "The profile must be on *every* Compose command, not just the first:"
msgstr "这个 profile 必须出现在*每一条* Compose 命令上,不只是第一条:"

#: ../developer/database.md:226
#: ../developer/database.md:234
msgid ""
"Omitting it does not fail loudly, it just does the wrong thing in both "
"directions: a plain `make up` starts every other service and skips "
Expand All @@ -699,15 +729,15 @@ msgstr ""
"profile 的 `make down` 会把周围的一切都删掉,却留着 pgAdmin 容器在跑(然后 Compose "
"报告网络仍在使用中)。重启的两半都要带上 `COMPOSE_PROFILES=admin`。"

#: ../developer/database.md:232
#: ../developer/database.md:240
msgid "Authentication"
msgstr "认证"

#: ../developer/database.md:234
#: ../developer/database.md:242
msgid "Two independent gates, and by default neither is pgAdmin's own login:"
msgstr "两道互相独立的关卡,而且默认情况下没有一道是 pgAdmin 自己的登录:"

#: ../developer/database.md:236
#: ../developer/database.md:244
msgid ""
"`PGADMIN_CONFIG_SERVER_MODE` defaults to `False`, which serves pgAdmin "
"with **no login of its own**. Set it to `True` in `.env` and pgAdmin asks"
Expand All @@ -718,7 +748,7 @@ msgstr ""
"`.env` 里把它设为 `True`,pgAdmin 就会要 `PGADMIN_EMAIL` / "
"`PGADMIN_PASSWORD`(这两个自身的默认值是 `admin@local.dev` / `admin`——启用之前先改掉)。"

#: ../developer/database.md:240
#: ../developer/database.md:248
#, python-brace-format
msgid ""
"Reached through the console at `/pgadmin/`, the request is gated on an "
Expand All @@ -732,7 +762,7 @@ msgstr ""
"handler(`apps/frontend/src/app/pgadmin/[[...path]]/route.ts`)按管理员会话把关,它会请后端校验调用方。直接走它发布的端口(`127.0.0.1:${PGADMIN_PORT:-5050}`,只监听回环)进去时,这道关卡不生效——请用"
" SSH 隧道:"

#: ../developer/database.md:251
#: ../developer/database.md:259
msgid ""
"pgAdmin's *master password* prompt never appears: "
"`PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED` is pinned to `\"False\"` in the"
Expand All @@ -742,19 +772,19 @@ msgstr ""
"password)提示永远不会出现:`PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED` 在 Compose "
"文件里被钉死为 `\"False\"`,而且没有任何环境变量能改它。"

#: ../developer/database.md:255
#: ../developer/database.md:263
msgid "Registering the database"
msgstr "注册数据库"

#: ../developer/database.md:257
#: ../developer/database.md:265
msgid "`Servers` → right-click → `Register` → `Server`."
msgstr "`Servers` → 右键 → `Register` → `Server`。"

#: ../developer/database.md:258
#: ../developer/database.md:266
msgid "**General**: any name."
msgstr "**General**:名字随便起。"

#: ../developer/database.md:259
#: ../developer/database.md:267
msgid ""
"**Connection**: host `postgres`, port `5432`, maintenance database "
"`DB_NAME`, username `DB_USER`, password `DB_PASSWORD` — the container-"
Expand All @@ -764,11 +794,11 @@ msgstr ""
"`DB_NAME`,username 填 `DB_USER`,password 填 "
"`DB_PASSWORD`——都用容器内部的值,不是宿主机那侧的端口映射。"

#: ../developer/database.md:263
#: ../developer/database.md:271
msgid "Resetting pgAdmin"
msgstr "重置 pgAdmin"

#: ../developer/database.md:265
#: ../developer/database.md:273
msgid ""
"Its saved connections live in `hybridinference_pgadmin_data`, an ordinary"
" project-local volume (not external, unlike the Postgres one):"
Expand All @@ -779,3 +809,34 @@ msgstr ""
#~ msgid "`session_id` / `conversation_id` headers"
#~ msgstr "`session_id` / `conversation_id` 头"

#~ msgid "`prompt_cache_key` in the request body"
#~ msgstr "请求体里的 `prompt_cache_key`"

#~ msgid ""
#~ "OpenCode and Kilo Code, which set "
#~ "OpenAI's cache-routing hint to their "
#~ "session id. Read last and forwarded "
#~ "upstream untouched — see below"
#~ msgstr "OpenCode 和 Kilo Code,它们把 OpenAI 的缓存路由提示设成了自己的会话 id。最后才读,并且原样转发给上游——见下文"

#~ msgid ""
#~ "`prompt_cache_key` is the one source the"
#~ " gateway does not treat as a "
#~ "declaration. The API only asks for "
#~ "a value that groups similar prompts, "
#~ "so a client is free to send "
#~ "one that is constant, and a "
#~ "constant would collapse everything behind "
#~ "it into one apparent session; it "
#~ "therefore loses to every other source."
#~ " It is also the only one that"
#~ " is *not* stripped before the request"
#~ " goes upstream, because it is a "
#~ "real provider parameter that raises the"
#~ " cache hit rate — the gateway "
#~ "reads a label out of it without"
#~ " taking it away."
#~ msgstr ""
#~ "`prompt_cache_key` 是网关唯一不当作「声明」看待的来源。这个 API "
#~ "只要求传一个能把相似提示词归到一起的值,所以客户端完全可以传一个常量,而常量会把它背后的所有请求塌缩成同一个看似的会话;因此它排在其他所有来源之后。它也是唯一在请求发往上游之前*不*被摘掉的一个,因为它是提供方真正会用到的参数,能提高缓存命中率——网关只是从中读出一个标签,并不把它拿走。"

Loading
Loading