Commit 80b195f
feat: bot multi-mode platform with restricted mode and admin diagnostics (#129)
* feat: add bot slash command router and multi-mode platform specs
建立 Bot 多模式平台基礎架構:
- 新增斜線指令路由框架(commands.py),支援指令註冊、別名、權限檢查
- 將 /reset 指令遷移到 CommandRouter,Line/Telegram 共用
- 新增 bot_usage_tracking 資料表 migration(rate limiter 用)
- 新增 BOT_UNBOUND_USER_POLICY 等 6 個環境變數設定
- 新增 OpenSpec change:proposal、design、specs、tasks 完整規劃
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: implement identity router for unbound user routing (Group 3)
- Add services/bot/identity_router.py with route_unbound() and handle_restricted_mode()
- Modify linebot_router.py access control to use identity router for user_not_bound
- Modify telegram handler access control to use identity router
- Binding code verification remains prioritized before identity routing
- 16 unit tests for route_unbound, get_unbound_policy, and handle_restricted_mode
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: add bot-restricted and bot-debug agent definitions (Group 4)
- Add BOT_RESTRICTED_PROMPT for unbound user restricted mode
- Add BOT_DEBUG_PROMPT for admin diagnostic mode with debug-skill scripts
- Add DEFAULT_BOT_MODE_AGENTS config (bot-restricted, bot-debug)
- Refactor ensure_default_linebot_agents() into shared _ensure_agents() helper
- Agents auto-created on startup, won't overwrite existing customizations
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: implement rate limiter for unbound users (Group 5)
- Add services/bot/rate_limiter.py with check_rate_limit() and record_usage()
- Integrate rate limit check at restricted mode entry point
- Record usage after successful AI processing
- PostgreSQL UPSERT for hourly/daily counters
- 9 unit tests all passing
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: add debug-skill with 5 diagnostic scripts (Group 6)
- SKILL.md defining debug-skill with run_skill_script tool
- check-server-logs: journalctl with lines/keyword params
- check-ai-logs: query ai_logs table with limit/errors_only params
- check-nginx-logs: docker logs with lines/type (access/error) params
- check-db-status: connections, table sizes, database size
- check-system-health: comprehensive health check with status report
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: implement /debug admin diagnostic command (Group 7)
- Register /debug command with aliases /診斷 /diag (admin-only, private-only)
- Handler uses bot-debug Agent with BOT_DEBUG_MODEL
- Default prompt triggers check-system-health when no problem description
- 10 unit tests: handler logic + routing permissions
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: add is_public flag to knowledge base and library folder access control (Group 8)
知識庫新增 is_public 欄位支援公開存取控制:
- models 新增 is_public 布林欄位(預設 false)
- search_knowledge 新增 public_only 過濾(未綁定用戶自動啟用)
- MCP search_knowledge 工具根據 ctos_user_id 自動設定 public_only
- 前端知識庫編輯器新增「公開」勾選框
- list_library_folders 支援 LIBRARY_PUBLIC_FOLDERS 環境變數過濾
- 修復相關測試(rate limiter mock、CommandRouter 初始化、agent 數量調整)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: add integration tests for bot multi-mode platform (Group 9)
23 個整合測試覆蓋:
- reject 策略回歸測試(Line/Telegram 個人/群組)
- restricted 策略受限模式完整流程
- 知識庫 public_only 過濾邏輯驗證
- /debug 指令權限控制(管理員/非管理員/群組/未綁定)
- rate limiter 超限阻斷與 key 格式
- 跨功能整合(config、models、指令別名)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* chore: archive bot-multi-mode-platform and sync specs
歸檔已完成的 bot-multi-mode-platform change,並同步 delta specs:
- 新增 3 個 capability specs: bot-identity-router, bot-rate-limiter, bot-slash-commands
- 更新 bot-platform spec: 新增 binding_status 欄位
- 更新 line-bot spec: 策略式未綁定用戶存取控制
- 更新 knowledge-base spec: is_public 公開存取旗標、圖書館公開資料夾
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: resolve critical bugs found in code review
修復 code review 發現的重要問題:
- parse_ai_response 呼叫方式錯誤(傳入 ClaudeResponse 而非 .message,回傳 tuple 被當 dict)
- 未綁定用戶永遠被 _check_library_permission 拒絕(跳過完整權限檢查改用公開資料夾路徑)
- _sanitize_path_segment 破壞多層路徑(改為分段清理後重組)
- call_claude 在受限模式無 try/except(新增錯誤處理)
- /debug 例外訊息洩漏內部細節(改用通用錯誤訊息)
- 移除未使用的 record_usage import
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: harden bot multi-mode platform security and robustness
- Rate limiter: atomic check-and-increment in single transaction (TOCTOU fix),
add cleanup_old_tracking() for data retention, fail-open on DB errors
- Line router: intercept slash commands before restricted mode AI processing,
fallback to push_text when reply_token expires during long AI calls
- Commands: guard against missing handler (was non-async lambda default)
- Debug scripts: sanitize error messages (no str(e) leak), validate int inputs
with range clamping, whitelist nginx log_type, strict boolean for errors_only
- SKILL.md: set requires_app to admin (was null/unrestricted)
- Config: validate bot_rate_limit_hourly <= bot_rate_limit_daily
- Update all tests for new atomic rate limiter API (708 tests pass)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add restricted mode routing coverage for linebot_router
Cover lines 279-341: slash command interception, AI processing,
reply_token expiry fallback, push_text fallback, error handling.
Raises overall coverage from 84.33% to 87.83%.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: address code review findings (Gemini + self-review)
- Remove redundant index from migration 009 (UNIQUE already implies it)
- Log warning instead of silent pass on push_text failure
- Move `import re` to file top in identity_router and command_handlers
- Remove unused imports (get_tools_for_user, get_tool_routing_for_user)
- Add slash command interception to Telegram restricted mode
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add Telegram restricted mode routing tests for coverage
Cover slash command interception, AI processing, error handling,
and silent routing in restricted mode to maintain 85% coverage.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: address code review H1-H4, M1-M4 findings
- H1: Rate limiter rollback on denial (avoid inflating counters)
- H3: Remove dead code in Telegram restricted mode command parsing
- H4: Rename reset_conversation param to platform_user_id (cross-platform)
- M1: Telegram restricted mode now saves message and passes message_uuid
- M2: Schedule cleanup_old_bot_tracking (daily at 04:30)
- M4: Remove redundant group check in _handle_reset
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: ensure Telegram _ensure_bot_user/_ensure_bot_group return str
asyncpg returns UUID objects from row["id"], but downstream code
(CommandContext, rate_limiter, identity_router) expects str.
Add str() conversion at the source to match Line-side behavior.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: remove dead code from reset command migration
- Remove is_reset_command block from process_message_with_ai (now
handled by CommandRouter in handle_text_message)
- Remove unused RESET_COMMANDS constant from Telegram handler
- Remove unused check_rate_limit backward-compat wrapper
- Fix misleading SQL comment in check-ai-logs.py
- Remove related obsolete tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: add reply fallback, deduplicate user query, and add /debug AI log
- H1: Command dispatch reply falls back to push_text when reply_token
expires (e.g., /debug taking 3+ minutes)
- M2: /debug command now records AI logs via log_linebot_ai_call with
context_type "bot-debug"; make message_uuid param accept None
- M3: Merge duplicate get_line_user_record queries in handle_text_message
into a single call before command parse, reused for both paths
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: extract get_command_user_context shared helper
Extract the "bot_user_id → ctos_user_id + is_admin" query pattern
into a shared function in bot/commands.py, replacing the inline SQL
in Telegram handler. Addresses Gemini review suggestion to deduplicate
user context lookup across platforms.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 0f131ae commit 80b195f
50 files changed
Lines changed: 4864 additions & 201 deletions
File tree
- backend
- migrations/versions
- src/ching_tech_os
- api
- models
- services
- bot_line
- bot_telegram
- bot
- mcp
- skills/debug-skill
- scripts
- tests
- frontend/js
- openspec
- changes/archive/2026-02-26-bot-multi-mode-platform
- specs
- bot-identity-router
- bot-platform
- bot-rate-limiter
- bot-slash-commands
- knowledge-base
- line-bot
- specs
- bot-identity-router
- bot-platform
- bot-rate-limiter
- bot-slash-commands
- knowledge-base
- line-bot
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
| |||
259 | 261 | | |
260 | 262 | | |
261 | 263 | | |
262 | | - | |
263 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
264 | 308 | | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
273 | 325 | | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
274 | 336 | | |
275 | | - | |
276 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
277 | 343 | | |
278 | 344 | | |
279 | 345 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
153 | 185 | | |
154 | 186 | | |
155 | 187 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
120 | 124 | | |
121 | 125 | | |
122 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
96 | 98 | | |
97 | 99 | | |
98 | 100 | | |
| 101 | + | |
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
| |||
116 | 119 | | |
117 | 120 | | |
118 | 121 | | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
| 183 | + | |
179 | 184 | | |
180 | 185 | | |
181 | 186 | | |
| |||
Lines changed: 139 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
0 commit comments