Repository navigation
Conversation
## 变更摘要
- 改前:Anthropic/Claude 槽位仅按 5h/7d 配额利用率与并发席位调度,缺乏按自然日(可配 IANA 时区)限制单 Anthropic 账号新建主会话数的硬闸门;无法按账号聚合每日新建会话计数、跨进程重启恢复、同步阻断同账号兄弟槽或在面板按分档与单槽 3 态覆盖管理。
- 改后:
- 存储与配置:新增 SQLite 增量迁移 `075_vm_daily_session_creations.sql` 与 PostgreSQL 18 迁移 `014_vm_daily_session_creations.sql`、双库仓储 `src/lib/db/repos/daily-session-gate-repo.mjs`(PG 走非阻塞 `pgPool`)、以及 `migrate-sqlite-to-pg.mjs` / `seed-mock-sqlite.mjs` 对接;在 `routing.quota.daily_session_gate` 增加开关(默认关闭)、`reset_timezone`(默认 `UTC`,支持任意合法 IANA 时区)与 6 档 `tier_limits`(`default`/`pro`/`max_5x`/`max_20x`/`team`/`enterprise`),支持单槽 `quota_override.daily_session_limit`(`null` 跟随分档 / `0` 不限 / `1..100000` 上限)。
- 核心闸门与调度:`src/lib/pool/daily-session-gate.mjs` 实现按 `account_uuid`(无 UUID 回退 `vm_id`)的日历日切换、DST、时区热切换、预占/幂等提交/预提交释放与并发重复会话零泄漏清理;接入 M3 Rust 调度器运行时(`scheduler-runtime.mjs` / `failover-runner.mjs` / `pool-scheduler.mjs`,含同账号兄弟槽 `new_sessions_blocked` 遥测同步与 `explainDispatch` 一致性)、Agent Sessions(`handle-agent-sessions.mjs`,按租户隔离 `kernelSessionId` 建键且延迟 sticky 绑定至提交点)以及 `src/server.mjs`(含 `activateStandby` 回填与备份恢复重绑)。
- 错误码与状态映射:`errors.mjs`、`handle-protocol.mjs` 与 `error-class.mjs` 端到端保留 HTTP 429 `daily_session_limit` + `Retry-After`(归类 `rate_limit` 并计入 `SLA_OK_ERROR_CODES`);`schedule-state.mjs` 映射 `restricted` / `new_session` / `今日新会话上限`;更新 `docs/PANEL_API.md`。
- 前端面板:在「设置 → 配额」新增每日新会话闸门卡片与 6 档弹窗、修复 `TimezonePicker` 自定义 IANA 时区入口;在 VM 调度区抽出 `vm-scheduling-model.ts` 支持 3 态 `daily_session_limit` 覆盖并在详情状态板/列表展示实时用量、在途数与重置时间。
- 未改 / 延后:不限制 Codex 槽位、续接已提交会话、子代理请求、探针与管理员 `pinVmId`;未修改 `scheduler/` Rust 源码(树哈希保持 `cac6c91fea2c1f4f5a798bd3a4c6d5d37a55f445`);未修改 `CHANGELOG.md`。
## 验收记录(Run)
- 环境与基线:WSL2 Linux + Windows `git.exe`,基于 `origin/dev` `7870fa8d52c7dcc4f2af6701a45f23f6a6a37248` rebase 后在独立工作树 `/mnt/x/vm2api-wt-anthropic-daily-session-gate` 完成全套复验(已解决 `test/unit/routing-runtime-load.test.mjs` 导入块冲突并按 Prettier 规范格式化 `web/src/features/vm/vm-list-table.test.tsx`)。
- 已跑命令与结果:
- `find src scripts -name '*.mjs' -print0 | xargs -0 -n1 node --check`:PASS(exit 0)
- `./node_modules/.bin/biome format`(29 个变更/新增后端 `.mjs` 文件,check-only):PASS(Checked 29 files, 0 diffs)
- `KIN_SCHEDULER_BIN=/mnt/x/vm2api-wt-gemini-format-parity/bin/kin-scheduler npm run test:ci`:PASS(13/13 isolated files, exit 0)
- `KIN_SCHEDULER_BIN=/mnt/x/vm2api-wt-gemini-format-parity/bin/kin-scheduler node --test test/unit/{routing-runtime-load,errors-map,error-class,vm-quota-override,scheduler-m3-runtime,rust-kernel-health,failover-runner,pool-scheduler,availability,sqlite-to-pg-migration,scheduler-m0-migrations,bg-migration-guard,database}.test.mjs`:PASS(395 pass / 0 fail / 2 skipped live-PG)
- `node --test test/unit/schedule-state.test.mjs`:11 pass / 1 fail(全部新增 `daily_session_limit` 断言通过;唯一失败项为 `origin/dev` 基线既有 `crag_prompt_replace_empty_unsupported, license_invalid`)
- `pnpm -C web exec prettier --check`(19 个变更/新增前端文件):PASS(exit 0)
- `pnpm -C web test:ci`:PASS(9/9 files, 99/99 tests)
- `pnpm -C web exec vitest run src/features/vm/proxy-timezone-copy.test.tsx src/features/settings/settings-routing-audit.test.tsx src/features/settings/index.test.tsx src/lib/vm-status.test.ts src/features/vm/vm-list-table.test.tsx src/features/vm/status-today.test.tsx src/features/pools/pools.test.ts`:PASS(7/7 files, 129/129 tests)
- `pnpm -C web typecheck`:PASS(`tsc -b`, exit 0)
- `pnpm -C web build`:PASS(`tsc -b && vite build`, 4523 modules, built in 15.52s, exit 0)
## 回退与手破验证(Broken by hand)
- 后端 `src/lib/pool/daily-session-gate.mjs` 关闭上限阻塞判断 -> `test/unit/vm-quota-override.test.mjs` FAIL(`true !== false`,10/11)-> 恢复原实现后 PASS(11/11)
- 后端 `src/lib/scheduler/scheduler-runtime.mjs` 跳过 `commitLease` 内 `dailyGate.commitReservation` -> `test/unit/scheduler-m3-runtime.test.mjs` FAIL(`0 !== 1`,12/13)-> 恢复原实现后 PASS(13/13)
- 后端 `src/lib/protocol/handle-agent-sessions.mjs` 跳过 `if (gate)` 每日会话过滤 -> `test/unit/rust-kernel-health.test.mjs` FAIL(`0 !== 1`,26/27)-> 恢复原实现后 PASS(27/27)
- 后端 `src/lib/core/errors.mjs` 禁用 `inboundCode === ErrorCode.DAILY_SESSION_LIMIT` 分支 -> `test/unit/errors-map.test.mjs` FAIL(`'upstream_rate_limit' !== 'daily_session_limit'`,40/41)-> 恢复原实现后 PASS(41/41)
- 后端 `src/lib/protocol/handle-agent-sessions.mjs` 将 `boundCommitted` 弱化为 `Boolean(boundVm)` -> `test/unit/rust-kernel-health.test.mjs` FAIL(重试漏计数 `0 !== 1`,27/28)-> 恢复原实现后 PASS(28/28)
- 前端 `web/src/features/settings/routing-draft.ts` 将 `dailySessions` 待应用检测改为比较整个 `quota` 对象 -> `web/src/features/settings/settings-routing-audit.test.tsx` FAIL(`P3-02` 期望 `dailySessions: false` 实得 `true`,19/20)-> 恢复原实现后 PASS(20/20)
- 前端 `web/src/features/vm/vm-scheduling.tsx` 在 `patchOf` 跳过 `kind === 'int'` 且值为 `0` -> `web/src/features/vm/vm-list-table.test.tsx` FAIL(期望 `{ quota_override: { daily_session_limit: 0 } }` 实得 `{}`,28/29)-> 恢复原实现后 PASS(29/29)
- 前端 `web/src/features/vm/create-options.ts` 回退 `resolveTimezonePickerState` 为旧逻辑 `showCustom = custom && !preset` -> `web/src/features/vm/proxy-timezone-copy.test.tsx` FAIL(`'UTC'` 切换自定义返回 `showCustom: false`,1/3)-> 恢复原实现后 PASS(3/3)
## 真实使用(Real use)
- 未执行真实线上 `ph24` 流量或真实浏览器交互点击实测:本次按沙箱与本地提交要求执行,未连接生产环境;后端通过真实 Rust `kin-scheduler` 二进制(`cac6c91fea2c1f4f5a798bd3a4c6d5d37a55f445`)在单测中拉起真实调度进程跑通预占/提交/释放/遥测链路,前端通过 Vitest SSR、状态钩子测试及 Vite 生产包构建验收。
## 未运行项(Not run)
- 真实 PostgreSQL 18 集成测试:未配置 `VM2API_TEST_PG_URL`,2 个 live-PG 用例自动跳过(PG 异步仓储与 SQLite→PG 迁移表映射已通过单测与 mock 覆盖)。
- 全量 556 个后端单测文件、真实浏览器 E2E 点击流程与 ARM64 镜像/生产发布:本次仅按用户指令完成本地原子提交,未推送远端、未开 PR、未部署。
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: 3e06daa5-0c11-42b5-8da3-fb67f292c7ae) |
评审结果(Codex 只读评审合并
|
…ssert test(pg): stop pinning migration 012 as the last PG migration in pg-log-writer (unblocks #2441)
…rted a schedule-state row ## 变更摘要 - 改前:`src/lib/pool/upstream-error-policy.mjs` 会发出两个调度原因 `license_invalid`(单槽 resync + 退避重试后仍答 `license_invalid`、尚未提交,换槽重试且不冷却)和 `crag_prompt_replace_empty_unsupported`(旧 CRAG 不支持空 prompt replace,换槽重试且不冷却),但 `src/lib/pool/schedule-state.mjs` 的原因表里没有这两行。`test/unit/schedule-state.test.mjs` 的“every emitted reason has exactly one row in the table”因此在 `origin/dev` 基线上一直失败(`add these reasons to schedule-state.mjs: crag_prompt_replace_empty_unsupported, license_invalid`)。该文件不在 `test:ci` 固定集合里,平时没人发现;任何改到它的 PR(例如 dofastted#2441)CI 都会因它变红。 - 改后:两个原因归入 `STATELESS`(“不改变槽位状态”:它们都经 `continueWithoutCooldown` 发出,语义是换槽重试而不是冷却或摘除),并加进测试里已有的“不改变槽位状态”断言列表。 - 未改 / 延后:不改 `upstream-error-policy.mjs` 的发出逻辑,不改任何其它原因的归类,不动面板文案。 ## 验收记录(Run) - 本机(Node 24)`schedule-state` 12/12(修复前 11/12);Node 22.23.5 容器 + 真实 `kin-scheduler` 跑引用 `schedule-state` 的 5 个测试文件(`schedule-state`、`codex-slot-pool`、`crag-zombie-guard`、`organization-disabled-probe`、`scheduler-fastpath`)76/76。`biome format` 无需修改。 ## 回退与手破验证(Broken by hand) - 3 处手破(唯一锚点,还原后 sha256 一致):整行去掉、只去掉 `license_invalid`、只去掉 `crag_prompt_replace_empty_unsupported`,各让 `schedule-state` 变红。 ## 真实使用(Real use) - 不适用:只补原因表的归类,没有真实槽位或流量;没有 `ph24`。 ## 未运行项(Not run) - 面板上这两个原因的展示文案没有看(它们归入“不改变槽位状态”,面板不为它们显示状态);`npm run test:ci` 由 GitHub `ci` 检查覆盖。
…og-writer ## 变更摘要 - 改前:`test/unit/pg-log-writer.test.mjs` 的“PG migrations 011 and 012 …”用例断言 `applyPgMigrations()` 返回的最后一个迁移是 `012_optimize_usage_logs_and_attempts_indexes.sql`。`292743b1f`(2026-11-30)新增 `013_codex_slot_runtime.sql` 时没有同步它,用例自 11-30 起在 `origin/dev` 基线上就是红的(实得 `013_…`);它用 mock 池、不需要真实 PG 就会红,但文件不在 `test:ci` 固定集合里,所以一直没人发现。新增迁移的 PR(例如 dofastted#2441 的 `014_vm_daily_session_creations.sql`)每次都会让它多红一次。 - 改后:用例改为用迁移目录里按文件名排序的最后一个文件作为期望,并断言 011 与 012 都被应用且 012 在 011 之后;以后再增加迁移不必回头改它。只改这一个测试文件,没有改任何迁移或生产代码。 - 未改 / 延后:其它仍在基线上红的用例(例如 `scoped-intercept-session`、`scheduler-m0-migrations`、`issue-1776-active-usage-reconcile`、`kernel-switch-recovery`)不在本次范围。 ## 验收记录(Run) - 本机(Node 24)`pg-log-writer` 17 个用例 16 通过、0 失败(1 个依赖真实 PG 的用例被跳过);修复前该用例失败(实得 `013_codex_slot_runtime.sql`)。`biome format` 无需修改。 ## 回退与手破验证(Broken by hand) - 3 处手破(还原后 sha256 一致):往迁移目录临时加一个 `999_zz_tmp_probe.sql` 后用例仍绿(不再钉死 012);把断言改回 `mig012` 在存在新迁移文件时变红;把期望改成错误文件名变红。 ## 真实使用(Real use) - 不适用:只改测试,没有真实 PG;没有 `ph24`。 ## 未运行项(Not run) - 真实 PG18 上的迁移应用不在本测试范围;`npm run test:ci` 由 GitHub `ci` 检查覆盖。
…d merge origin/dev
## 变更摘要
- 改前:
- 合并前分支基于旧 `dev`(迁移号 `075` / `014` 与最新 `origin/dev` `60f797b3e` 撞号,且 8 个文件存在合并冲突)。
- Major 1:`DailySessionGate` 仅依赖实例内 Map 计数,双实例共享真实 SQLite/PG 库时可超发。
- Major 2:在途预占默认 180s 直接过期、跨午夜重建索引丢失前一日在途预占、`commitReservation()` 不复核上限。
- Major 3:PG 时区热切换后同步返回且请求入口未等 `whenReady()`,存在放行空窗。
- Major 4:PG 写入失败仅交给可选 `onError`(生产未传)且内存先行去重导致静默丢账;hydrate 失败后以空账本继续服务。
- Major 5:`hasCommittedSession()` 仅查当日内存账本,跨日续接会话在当日触顶后被 429 拒绝。
- Major 6:Agent Sessions 路径存在重复选槽与 `acquireManagedKey()` 异常泄漏预占风险(且最新 `dev` 已统一收口至 `schedulerRuntime`)。
- Major 7:`#isClaudeQuotaGuardBlocking()` 传入 `null` 快照导致永远返回 `false`,释放每日预占会误清 5h/7d `continuing` 保护。
- Major 8:`timezone.mjs` `zonedWallToMs` 两轮换算在 `America/Santiago` 2026-09-06 跳过午夜时落回前一天 23:00、`reset_at` 指向过去。
- Major 9:默认关闭(`enabled: false`)仍执行启动 hydrate、`commitReservation()` 仍写库、正整数单槽覆盖仍生效。
- Major 10 + Cursor HIGH:`shouldGateSchedulerLease()` 凭客户端传入的 `parent_session_id` 盲目豁免新主会话;`isEphemeralProbe: true` 在全槽每日触顶时被 Rust `new_sessions_blocked` 误拒;Agent Sessions 管理员 pin 豁免与快速流式提交/释放竞态未闭环。
- 改后:
- 合并 `origin/dev`(`60f797b3e`),将迁移顺延至 SQLite `076_vm_daily_session_creations.sql` 与 PG `015_vm_daily_session_creations.sql`,新增跨日会话索引与共享预占表 `vm_daily_session_reservations`,同步更新 `scripts/migrate-sqlite-to-pg.mjs` 与 `scripts/seed-mock-sqlite.mjs`。
- Major 1:`DailySessionGateRepo` 实现 SQLite `BEGIN IMMEDIATE` 与 PG `pg_advisory_xact_lock` 跨进程原子预占/提交与共享计数,双实例共享库不再超发。
- Major 2:活跃在途预占绑定请求生命周期 + 心跳续期(不因 180s 误删),跨午夜重建保留在途预占,`commitReservation()` / `commitReservationAsync()` 提交前复核上限。
- Major 3:PG 时区热切换/启用期间标记 `hydrating`,异步入口等待 `whenReady()`、同步入口 fail-closed,消除空窗。
- Major 4:生产 `server.mjs` 显式注入 `onError` 与 `onHydrated`;PG 写入失败不静默丢账并回滚乐观去重以支持重试;hydrate 失败置 fail-closed 并自动重试恢复;移除备份恢复空 `catch {}`。
- Major 5:`hasCommittedSession()` / `hasCommittedSessionAsync()` 与 `findCommittedAccountsForSessionAsync()` 覆盖跨日历史账本;`scheduler-proto` 新增向后兼容可选字段 `continuing_node_keys` 与 `admission.rs` 配合,跨日续接会话即使在 Rust sticky 过期/重启后仍可放行。
- Major 6 + Agent Sessions 统一收口:`handle-agent-sessions.mjs` 沿用最新 `dev` 的 `schedulerRuntime` 单一真值路径(`acquireManagedKey()` 先于 `rt.acquireNode()` 执行,无预占泄漏;`onHeaders` 仅 2xx/3xx 提交租约并等待 `commitLeasePromise` 避免快速流式响应下的提交/释放竞态)。
- Major 7:`#isClaudeQuotaGuardBlocking()` 改为读取真实调度快照/VM 与 `accountQuota` 账本,释放每日预占不再覆盖 5h/7d `continuing` 保护。
- Major 8:`timezone.mjs` `zonedWallToMs()` 修复春调跳空(含 `America/Santiago` 2026-09-06 跳过午夜)及秋调重小时(取首次出现),保证当天起点落在当日且 `reset_at` 严格在未来。
- Major 9:`enabled: false` 为全局总开关,关闭时单槽正整数覆盖不激活、跳过启动/切主/恢复 hydrate、`tryReserveNewSession`/`commitReservation` 严格 no-op 零写库。
- Major 10 + Cursor HIGH:移除 `shouldGateSchedulerLease()` 与 `#dailySessionBlockedStatusesFromExplain()` 中对未校验 `parent_session_id` 的盲目豁免(仅豁免 Rust 校验过的 `subagent_follow_parent`);Rust `admission.rs` 对 `is_ephemeral_probe` 免除 `new_sessions_blocked` 拦截;Agent Sessions `x-kin-pin-vm` 管理员诊断 pin 自动豁免。
- 未改 / 延后:无。
## 验收记录(Run)
- 环境与基线:WSL2 Debian + Windows 宿主,合并最新 `origin/dev` `60f797b3e` 后于独立工作树 `X:/vm2api-wt-anthropic-daily-session-gate` 验证。
- 已跑命令与结果:
- `find src scripts -name "*.mjs" -print0 | xargs -0 -n1 node --check`: PASS(全量语法通过)
- `npx biome check`(17 个改动 Node/测试文件)+ `pnpm -C web exec prettier --write`(2 个改动 Web 测试文件): PASS(0 error)
- `cd scheduler && RUSTUP_TOOLCHAIN=1.86.0 cargo fmt --check && cargo clippy --workspace --all-targets -- -D warnings && cargo test --workspace --locked && cd .. && npm run build:scheduler`: PASS(Rust 141 passed, 0 failed;`bin/kin-scheduler` 构建成功;已清理 `/mnt/x/.cargo-build/pr2441`)
- `node --test test/unit/vm-quota-override.test.mjs test/unit/timezone.test.mjs test/unit/scheduler-m3-runtime.test.mjs test/unit/rust-kernel-health.test.mjs test/unit/pool-scheduler.test.mjs test/unit/schedule-state.test.mjs test/unit/error-class.test.mjs test/unit/errors-map.test.mjs test/unit/routing-runtime-load.test.mjs test/unit/bg-migration-guard.test.mjs test/unit/scheduler-m0-migrations.test.mjs test/unit/sqlite-to-pg-migration.test.mjs test/unit/pg-log-writer.test.mjs`: PASS(262 passed, 0 failed, 3 skipped [PG_URL 未配置])
- `npm run test:ci`: PASS(13/13 隔离套件全部通过,0 failed)
- `pnpm -C web test:ci`: PASS(9/9 文件,99/99 passed)
- `pnpm -C web exec vitest run src/features/pools/pools.test.ts src/features/settings/settings-routing-audit.test.tsx src/features/vm/proxy-timezone-copy.test.tsx src/features/vm/status-today.test.tsx src/features/vm/vm-list-table.test.tsx src/lib/vm-status.test.ts`: PASS(6/6 文件,131/131 passed)
- `pnpm -C web typecheck && pnpm -C web build`: PASS(`tsc -b` + `vite build` 4523 modules)
## 回退与手破验证(Broken by hand)
- 注入禁用 `tryReserveAtomic` 跨进程 SQLite 事务路径 -> `DailySessionGate cross-process SQLite & PG cap enforcement prevents over-issuance across two instances` FAIL(`AssertionError: true !== false`)-> 恢复后 PASS
- 注入恢复 `shouldGateSchedulerLease` 中的 `if (req?.parent_session_id) return false` -> `DailySessionGate cross-day resume & shouldGateSchedulerLease parent_session_id security guard` FAIL(`forged parent_session_id on fresh placement must still be gated: false !== true`)-> 恢复后 PASS
- 回退 `timezone.mjs` `zonedWallToMs` 为原两轮换算 -> `zonedDayStartMs & zonedWallToMs handle America/Santiago 2026-09-06 skipped-midnight DST cleanly` FAIL(`1788663600000 !== 1788667200000`)-> 恢复后 PASS
- 注入 `#isClaudeQuotaGuardBlocking` 返回 `false`(复现旧 `null` 上下文)-> `M3 runtime + DailySessionGate: releasing daily reservation preserves active 5h/7d quota guard continuing block, and cross-day resume succeeds after restart` FAIL(`releasing daily reservation must NOT clear new_sessions_blocked when 5h/7d quota guard is in continuing mode: true !== false`)-> 恢复后 PASS
- 禁用 `#prepareDailySessionContinuingHints` 跨日提示 -> `handleAgentSessions + real kin-scheduler + DailySessionGate: pre-header release, cross-day resume, admin pin exemption, acquireManagedKey throw safety, and tenant isolation` FAIL(`429 !== 200`)-> 恢复后 PASS
## 真实使用(Real use)
- 通过真实编译产出的 `bin/kin-scheduler` 二进制与真实 `node:sqlite` 库在 `scheduler-m3-runtime.test.mjs` 与 `rust-kernel-health.test.mjs` 中完成端到端实测(含 `POST /v1/agent/sessions`、跨日续接、管理员 `x-kin-pin-vm`、独立 `isEphemeralProbe`、伪造 `parentSessionId` 拒绝、并发抢占切换)。未连接线上 `ph24` 生产环境。
## 未运行项(Not run)
- 真实 PostgreSQL 18 实例联调测试(3 条依赖 `PG_URL` 环境变量的迁移/仓储用例自动 skip;本地工作区未启动 PG18 服务,PG 路径已通过模拟事务连接池与 SQL 契约单测验证)。
- 全量 553 个 `npm run test:unit` 文件及 `test:e2e`(已完整覆盖 `test:ci` 13 套件 + 13 个直接关联单测套件 + Rust 141 项 + Web 15 个测试文件 230 项)。
- 线上 `ph24` 生产实测(按安全红线不操作生产机)。
|
已合并最新 变更摘要
验收记录(Run)
回退与手破验证(Broken by hand)
真实使用(Real use)
未运行项(Not run)
|
There was a problem hiding this comment.
Stale comment
Agentic security review of the Anthropic daily session gate found one remaining medium-severity issue: short one-shot Messages requests classified as ephemeral probes can still land on
new_sessions_blockedClaude nodes and skip the daily cap.The previous parent-session skip (
req.parent_session_id) is addressed on this head.Sent by Cursor Security Agent: Security Reviewer
| continue; | ||
| } | ||
| if st.new_sessions_blocked && !continuing { | ||
| if st.new_sessions_blocked && !continuing_known_session && !req.is_ephemeral_probe { |
There was a problem hiding this comment.
🔒 Agentic Security Review
Severity: MEDIUM
Admission now skips new_sessions_blocked when req.is_ephemeral_probe is true, even for a brand-new primary placement. That flag is derived from isShortProbeRequest / skipSessionSeat (single user turn, no tools, trimmed text 1–64 chars), not from an operator-only health path. JS shouldGateSchedulerLease also no-ops on the same flag, so these requests never reserve the daily ledger.
Impact: After the daily cap or 5h/7d continuing-only guard has blocked an Anthropic account, an authenticated caller can keep sending short one-shot prompts onto those VMs and burn remaining shared-account quota that was reserved for existing conversations.
Reviewed by Cursor Security Reviewer for commit 914b617. Configure here.
…SQLite 077 / PG 018
| continue; | ||
| } | ||
| if st.new_sessions_blocked && !continuing { | ||
| if st.new_sessions_blocked && !continuing_known_session && !req.is_ephemeral_probe { |
There was a problem hiding this comment.
🔒 Agentic Security Review
Severity: MEDIUM
Admission now skips new_sessions_blocked when req.is_ephemeral_probe is true, even for a brand-new primary placement. That flag is derived from isShortProbeRequest / skipSessionSeat (single user turn, no tools, trimmed text 1–64 chars), not from an operator-only health path. JS shouldGateSchedulerLease also no-ops on the same flag, so these requests never reserve the daily ledger.
Impact: After the daily cap or 5h/7d continuing-only guard has blocked an Anthropic account, an authenticated caller can keep sending short one-shot prompts onto those VMs and burn remaining shared-account quota that was reserved for existing conversations.
Reviewed by Cursor Security Reviewer for commit 558bebf. Configure here.




变更摘要
075_vm_daily_session_creations.sql与 PostgreSQL 18 迁移014_vm_daily_session_creations.sql、双库仓储src/lib/db/repos/daily-session-gate-repo.mjs(PG 走非阻塞pgPool)、以及migrate-sqlite-to-pg.mjs/seed-mock-sqlite.mjs对接;在routing.quota.daily_session_gate增加开关(默认关闭)、reset_timezone(默认UTC,支持任意合法 IANA 时区)与 6 档tier_limits(default/pro/max_5x/max_20x/team/enterprise),支持单槽quota_override.daily_session_limit(null跟随分档 /0不限 /1..100000上限)。src/lib/pool/daily-session-gate.mjs实现按account_uuid(无 UUID 回退vm_id)的日历日切换、DST、时区热切换、预占/幂等提交/预提交释放与并发重复会话零泄漏清理;接入 M3 Rust 调度器运行时(scheduler-runtime.mjs/failover-runner.mjs/pool-scheduler.mjs,含同账号兄弟槽new_sessions_blocked遥测同步与explainDispatch一致性)、Agent Sessions(handle-agent-sessions.mjs,按租户隔离kernelSessionId建键且延迟 sticky 绑定至提交点)以及src/server.mjs(含activateStandby回填与备份恢复重绑)。errors.mjs、handle-protocol.mjs与error-class.mjs端到端保留 HTTP 429daily_session_limit+Retry-After(归类rate_limit并计入SLA_OK_ERROR_CODES);schedule-state.mjs映射restricted/new_session/今日新会话上限;更新docs/PANEL_API.md。TimezonePicker自定义 IANA 时区入口;在 VM 调度区抽出vm-scheduling-model.ts支持 3 态daily_session_limit覆盖并在详情状态板/列表展示实时用量、在途数与重置时间。pinVmId;未修改scheduler/Rust 源码(树哈希保持cac6c91fea2c1f4f5a798bd3a4c6d5d37a55f445);未修改CHANGELOG.md。验收记录(Run)
90c7aff67(基于origin/dev7870fa8d52c7dcc4f2af6701a45f23f6a6a37248rebase 后在独立工作树<worktree>完成全套复验,已解决test/unit/routing-runtime-load.test.mjs导入块冲突并按 Prettier 规范格式化web/src/features/vm/vm-list-table.test.tsx)。find src scripts -name '*.mjs' -print0 | xargs -0 -n1 node --check:PASS(exit 0)./node_modules/.bin/biome format(29 个变更/新增后端.mjs文件,check-only):PASS(Checked 29 files, 0 diffs)KIN_SCHEDULER_BIN=<local bin/kin-scheduler> npm run test:ci:PASS(13/13 isolated files, exit 0)KIN_SCHEDULER_BIN=<local bin/kin-scheduler> node --test test/unit/{routing-runtime-load,errors-map,error-class,vm-quota-override,scheduler-m3-runtime,rust-kernel-health,failover-runner,pool-scheduler,availability,sqlite-to-pg-migration,scheduler-m0-migrations,bg-migration-guard,database}.test.mjs:PASS(395 pass / 0 fail / 2 skipped live-PG)node --test test/unit/schedule-state.test.mjs:11 pass / 1 fail(全部新增daily_session_limit断言通过;唯一失败项为origin/dev基线既有crag_prompt_replace_empty_unsupported, license_invalid)pnpm -C web exec prettier --check(19 个变更/新增前端文件):PASS(exit 0)pnpm -C web test:ci:PASS(9/9 files, 99/99 tests)pnpm -C web exec vitest run src/features/vm/proxy-timezone-copy.test.tsx src/features/settings/settings-routing-audit.test.tsx src/features/settings/index.test.tsx src/lib/vm-status.test.ts src/features/vm/vm-list-table.test.tsx src/features/vm/status-today.test.tsx src/features/pools/pools.test.ts:PASS(7/7 files, 129/129 tests)pnpm -C web typecheck:PASS(tsc -b, exit 0)pnpm -C web build:PASS(tsc -b && vite build, 4523 modules, built in 15.52s, exit 0)回退与手破验证(Broken by hand)
src/lib/pool/daily-session-gate.mjs关闭上限阻塞判断 ->test/unit/vm-quota-override.test.mjsFAIL(true !== false,10/11)-> 恢复原实现后 PASS(11/11)src/lib/scheduler/scheduler-runtime.mjs跳过commitLease内dailyGate.commitReservation->test/unit/scheduler-m3-runtime.test.mjsFAIL(0 !== 1,12/13)-> 恢复原实现后 PASS(13/13)src/lib/protocol/handle-agent-sessions.mjs跳过if (gate)每日会话过滤 ->test/unit/rust-kernel-health.test.mjsFAIL(0 !== 1,26/27)-> 恢复原实现后 PASS(27/27)src/lib/core/errors.mjs禁用inboundCode === ErrorCode.DAILY_SESSION_LIMIT分支 ->test/unit/errors-map.test.mjsFAIL('upstream_rate_limit' !== 'daily_session_limit',40/41)-> 恢复原实现后 PASS(41/41)src/lib/protocol/handle-agent-sessions.mjs将boundCommitted弱化为Boolean(boundVm)->test/unit/rust-kernel-health.test.mjsFAIL(重试漏计数0 !== 1,27/28)-> 恢复原实现后 PASS(28/28)web/src/features/settings/routing-draft.ts将dailySessions待应用检测改为比较整个quota对象 ->web/src/features/settings/settings-routing-audit.test.tsxFAIL(P3-02期望dailySessions: false实得true,19/20)-> 恢复原实现后 PASS(20/20)web/src/features/vm/vm-scheduling.tsx在patchOf跳过kind === 'int'且值为0->web/src/features/vm/vm-list-table.test.tsxFAIL(期望{ quota_override: { daily_session_limit: 0 } }实得{},28/29)-> 恢复原实现后 PASS(29/29)web/src/features/vm/create-options.ts回退resolveTimezonePickerState为旧逻辑showCustom = custom && !preset->web/src/features/vm/proxy-timezone-copy.test.tsxFAIL('UTC'切换自定义返回showCustom: false,1/3)-> 恢复原实现后 PASS(3/3)真实使用(Real use)
kin-scheduler二进制(cac6c91fea2c1f4f5a798bd3a4c6d5d37a55f445)在单测中拉起真实调度进程跑通预占/提交/释放/遥测链路,前端通过 Vitest SSR、状态钩子测试及 Vite 生产包构建验收。未运行项(Not run)
VM2API_TEST_PG_URL,2 个 live-PG 用例自动跳过(PG 异步仓储与 SQLite→PG 迁移表映射已通过单测与 mock 覆盖)。Note
High Risk
Touches session admission, scheduler leases, and persistent quota accounting on the critical Anthropic request path; misconfiguration or hydration/race bugs could incorrectly block new sessions or over-admit past caps.
Overview
Adds a configurable daily limit on new Anthropic/Claude primary sessions per account (tiered global caps, IANA reset timezone, optional per-VM
quota_override.daily_session_limit), backed by new SQLite/PG tables and a cross-process reserve → commit → release ledger.Routing & panel:
GET/PUT /routingexposesquota.daily_session_gate; VM list/detail returnsdaily_sessionsruntime stats and surfacesdaily_session_limitasrestricted/new_sessionwith reset time. Config reload pushes scheduler telemetry.Admission path: After Rust picks a node, JS reserves a daily slot on the lease; blocked accounts get HTTP 429
daily_session_limitwithRetry-After. Continuing sessions (prior-day commits viacontinuing_node_keys), subagents, probes, and admin pin bypass the cap. Rust admission also exemptscontinuing_node_keysand ephemeral probes fromnew_sessions_blocked.Scheduler sync: Telemetry sets
new_sessions_blockedon sibling VMs sharing an account when the cap is hit.Also:
zonedWallToMsDST gap handling for correct day boundaries; migration/seed scripts include the new tables.Reviewed by Cursor Bugbot for commit 558bebf. Configure here.