ai_co_discuss 0.6.6 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/discuss-host/references/room.md +2 -0
- package/.agents/skills/discuss-observer/SKILL.md +15 -0
- package/.agents/skills/workflow-creator/SKILL.md +3 -0
- package/README.md +5 -1
- package/README.zh-CN.md +5 -1
- package/backend/analytics.py +10 -3
- package/backend/analytics_rooms.py +134 -0
- package/backend/free_room.py +253 -47
- package/backend/identity.py +1 -1
- package/backend/message_queue.py +1 -1
- package/backend/moss_delegation.py +84 -5
- package/backend/moss_window_messages.py +154 -1
- package/backend/observer.py +216 -0
- package/backend/schema.sql +52 -1
- package/backend/server.py +518 -50
- package/backend/session_room.py +6 -1
- package/docs/en/guide/analytics.md +4 -0
- package/docs/en/guide/cli.md +8 -0
- package/docs/en/guide/first-run.md +2 -0
- package/docs/en/guide/members.md +41 -0
- package/docs/en/guide/rooms.md +1 -1
- package/docs/en/guide/scheduling.md +1 -0
- package/docs/en/guide/steward.md +17 -7
- package/docs/en/guide/workflows.md +10 -5
- package/docs/en/guide/working.md +1 -1
- package/docs/guide/analytics.md +4 -0
- package/docs/guide/cli.md +8 -0
- package/docs/guide/first-run.md +2 -0
- package/docs/guide/members.md +41 -0
- package/docs/guide/rooms.md +1 -1
- package/docs/guide/scheduling.md +1 -0
- package/docs/guide/steward.md +15 -7
- package/docs/guide/workflows.md +10 -5
- package/docs/guide/working.md +1 -1
- package/frontend/locales/en/analytics.json +25 -0
- package/frontend/locales/en/app.json +7 -3
- package/frontend/locales/en/backend.json +51 -0
- package/frontend/locales/en/discussion.json +14 -1
- package/frontend/locales/en/room.json +47 -1
- package/frontend/locales/en/settings.json +3 -1
- package/frontend/locales/en/steward.json +2 -0
- package/package.json +2 -1
- package/runner/adapters/antigravity_cli.mjs +2 -1
- package/runner/adapters/api.mjs +61 -22
- package/runner/adapters/claude.mjs +26 -7
- package/runner/adapters/codebuddy.mjs +389 -0
- package/runner/adapters/codebuddy_edit_hook.mjs +58 -0
- package/runner/adapters/codex_cli.mjs +33 -3
- package/runner/adapters/deepseek_harness.mjs +104 -0
- package/runner/adapters/grok_cli.mjs +290 -0
- package/runner/adapters/index.mjs +52 -3
- package/runner/adapters/profiles.mjs +75 -0
- package/runner/analytics.mjs +10 -0
- package/runner/channel_bot.mjs +1 -1
- package/runner/channel_onebot.mjs +108 -11
- package/runner/channels.mjs +120 -19
- package/runner/codiscuss.mjs +18 -8
- package/runner/discuss.py +62 -12
- package/runner/discussion_launch.mjs +173 -51
- package/runner/discussion_runner.mjs +3 -1
- package/runner/discussion_service.mjs +26 -8
- package/runner/discussion_setup.mjs +48 -33
- package/runner/discussion_skills.mjs +1 -1
- package/runner/expert_prompts.mjs +9 -1
- package/runner/experts.mjs +14 -2
- package/runner/free_runner.mjs +284 -57
- package/runner/host_permissions.mjs +3 -0
- package/runner/i18n_en.mjs +91 -2
- package/runner/moss_windows.mjs +388 -97
- package/runner/pinyin.mjs +49 -0
- package/runner/project_files.mjs +1 -48
- package/runner/qq_faces.mjs +5 -0
- package/runner/scheduler.mjs +21 -4
- package/runner/steward.mjs +9 -5
- package/runner/workflows.mjs +25 -9
- package/web/admin.js +1 -1
- package/web/app.js +59 -20
- package/web/backup.js +1 -1
- package/web/guest.js +1 -1
- package/web/i18n.js +4 -3
- package/web/icons/codebuddy.svg +5 -0
- package/web/modern.css +1 -1
- package/web/modern.js +615 -212
- package/workflows/decision-council/SKILL.md +1 -0
- package/workflows/story-serial/SKILL.md +1 -0
- package/workflows/trpg-classic/SKILL.md +1 -0
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
|
|
27
27
|
## 派活
|
|
28
28
|
|
|
29
|
+
如果 `room_roster` 有 `observer: true`,高风险方案在派实现前先点名咨询旁观席,不把它当实现或正式评审成员。高风险信号包括新增持久状态、改协议或权限、跨模块改动,或在已有入口之外另起一条实现路径。看到旁观意见,在下一次回答里说明采纳、后置或驳回及理由。
|
|
30
|
+
|
|
29
31
|
默认做法:在回答里用 `--to` 点名、写清分工,其余参数按需加;完整写法查 `message post --help-host`(普通 `--help` 只列成员能用的)。
|
|
30
32
|
|
|
31
33
|
- 需要他人时加 `--to 显示名`(可重复,不能点名自己),写明各自负责的文件或模块;不需要他人时不加,本次即结束。派活正文要能独立看懂:直接写要做的事、材料编号与完成标准,不写“同上”“见前文”。不写策略档位名、流程代号和内部卡号,这些只有你看得懂,成员和用户看不懂。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: discuss-observer
|
|
3
|
+
description: 在 AI CoDiscuss 旁观席收到 observe 请求时,按只读权限核对方案或批次改动,仅交有证据的风险意见或显式结束自动旁观。普通评审和实现任务不用本技能。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 旁观席
|
|
7
|
+
|
|
8
|
+
同时遵守 [discuss-participant](../discuss-participant/SKILL.md) 的身份、读取和提交规则。本次只判断有没有值得报告的风险,不接实现、不派活、不读私聊。
|
|
9
|
+
|
|
10
|
+
- **被点名**:读本次请求绑定的原消息及必要的用户决定。只回答被咨询的问题。
|
|
11
|
+
- **自动旁观**:按任务 `requests[].observation_sources` 的交回消息编号读本批交付,用执行编号核对执行详情和报告的文件改动。按需看文件或差异,不对报告以外的改动做全量评审。共享目录里不能确定改动是谁的,标“归属不足”,不归咎成员。
|
|
12
|
+
|
|
13
|
+
发现值得报告的风险时,只提交一条旁观意见,最多两项。每项写清 **证据位置 → 违背的用户目标或已确认决定 → 后果 → 最小替代**。最小替代只针对已证实的问题,不另提优化建议或新抽象。已经提过且主持人已回应的风险,只有相关代码或决定发生实质变化时才再提。
|
|
14
|
+
|
|
15
|
+
本次执行只包含自动请求(`created_by: "system"`)且没有值得报告的发现时,用 `codiscuss message post --no-finding` 结束,不发正文。只要有用户或主持人点名的请求,就必须用普通回答交回,没发现或证据不足时一句即可。两种方式都绑定本次执行,不靠最后一段文字自动收录。
|
|
@@ -168,6 +168,7 @@ project_access: write
|
|
|
168
168
|
|
|
169
169
|
- **工作流 ID**(文件夹名 = SKILL.md 的 `name`):小写英文和数字,可用单个连字符连接(不在首尾),1—40 位。`metadata` 下的 `label`(显示名)最多 32 字;早期写在顶层的 `label` 仍可读取,保存时移到 `metadata` 下。
|
|
170
170
|
- **展示元数据**(均可选,不换行,只给人看和分享,**不会发给主持人**):`metadata.author`(作者,最多 64 字)、`metadata.source`(出处,最多 200 字)。不要把作者、出处等背景信息塞进 `description`。
|
|
171
|
+
- **分类**:只在主工作流 `SKILL.md` 的 `metadata` 下写可选的 `category`。新建或改类时用 `development`(开发)、`entertainment`(娱乐)、`office`(办公)、`business`(商业)、`design-2d`(二维设计)、`design-3d`(三维设计)、`video`(剪辑)、`writing`(创作)或 `other`(其他)。缺失或未知的旧值显示为“其他”,不改类时保留原值。
|
|
171
172
|
- **`description`** 最多 500 字:写清做什么、什么时候用。主持人只凭它判断一条请求该不该按这个工作流做,写含糊了,该用时会不用、不该用时会套用。
|
|
172
173
|
- **卡片**:最多 30 张。`name` 是卡片 ID,等于文件名,只用小写英文、数字和下划线,最多 16 位。`platform` 是平台 ID:`Claude`、`Codex`、`Antigravity`、`DeepSeek`、`Qwen`、`Grok`、`OpenAI`、`Custom`(网页显示的 Gemini 写 `Antigravity`,自定义 API 写 `Custom`),须是本机已配置的软件,或已配置的模型 API(官方平台可按已保存的 API Key 加入;Custom 须先保存完整的本机 API 配置)。成员名由程序拼成 `平台-卡片ID`。
|
|
173
174
|
- **卡片可选字段**:`label`、`description`、`project_access`(read / write / auto / full)、`model`、`effort`。生图不是卡片字段:房间添加了生图卡片后所有成员都能画,卡片提示词写清谁负责出图即可。不用的字段直接省略;模型和强度省略即用本机默认值。接入方式、可执行文件、密钥不写进卡片,由各自电脑的配置决定。各角色挑哪个平台和模型,见 [给卡片挑平台和模型](references/model-selection.md)。
|
|
@@ -186,6 +187,8 @@ project_access: write
|
|
|
186
187
|
|
|
187
188
|
在房间外,`workflow-check`、`workflow-save`、`expert-export` 和 `expert-import` 支持 `--scope project|global` 与 `--project <项目目录>`。检查、读取修订号和保存应选同一归属;项目写入要求目录可写。
|
|
188
189
|
|
|
190
|
+
从命令行改分类时,编辑主 `SKILL.md` 的 `metadata.category`,用 `workflow-check --dir` 预检,再用 `workflow-save --dir --revision <修订号>` 保存。不改分类时不替换已有分类值。
|
|
191
|
+
|
|
189
192
|
不要直接写入全局或项目的 `workflows/`:那样会跳过校验和写入锁,格式错的文件会让启用它的房间读不到工作流。命令失败时按报错修正,不要绕过。
|
|
190
193
|
|
|
191
194
|
### 草稿格式
|
package/README.md
CHANGED
|
@@ -87,6 +87,8 @@ The [User Guide](docs/en/guide/README.md) covers each of these in full.
|
|
|
87
87
|
|
|
88
88
|
### Working together
|
|
89
89
|
|
|
90
|
+
- **Observer seat** — Assign a read-only member to flag evidence-backed risks in plans and completed batches with file changes. [Observer seat](docs/en/guide/members.md#observer-seat)
|
|
91
|
+
|
|
90
92
|
- **Delegate by mention** — `@` a participating member to start a side task. Automatically connected members can work alongside the main task when idle and queue requests when busy. Manual members check for requests themselves. The host normally handles unaddressed messages; smart scheduling can route some directly. [Working in a room](docs/en/guide/working.md)
|
|
91
93
|
- **Questions for you**: when a member needs your decision it asks in the room, and an answer card appears above the input box. [Working in a room](docs/en/guide/working.md)
|
|
92
94
|
- **Structured discussions** — Set a goal and limits on rounds or time. Members cannot read each other’s opening answers during the independent round. The host can schedule follow-up questions if time and rounds remain. [Discussions](docs/en/guide/discussion.md)
|
|
@@ -109,7 +111,7 @@ The [User Guide](docs/en/guide/README.md) covers each of these in full.
|
|
|
109
111
|
|
|
110
112
|
- **Projects and records** — Browse project files, keep attachments, branch a room, or export and import it across sites. Destroying a room clears local room records and sessions created exclusively for it. Existing and shared sessions are retained, and provider-side records follow the provider’s policy. Eligible idle rooms can be set to expire. [Rooms](docs/en/guide/rooms.md)
|
|
111
113
|
- **Context and usage limits** — See the most recent available context reading and the usage windows or balance your account reports. Common windows are five hours and seven days, and the usage table can refresh supported CLI accounts on demand. [Members](docs/en/guide/members.md#see-context-and-usage-limits)
|
|
112
|
-
- **Cross-room analytics**:
|
|
114
|
+
- **Cross-room analytics**: compare model usage and delivery outcomes within each room, open runs that need attention, and generate a share image locally. [Analytics](docs/en/guide/analytics.md)
|
|
113
115
|
- **Deploy as website**: publish a page or built folder a member made to a public address for 7 days, renewable, after you confirm what goes out. [Working in a room](docs/en/guide/working.md#8-deploy-a-page-as-a-website)
|
|
114
116
|
- **MOSS (formerly Platform steward)**: check which rooms need you, get help with CoDiscuss, or request platform tasks in a dedicated conversation. [MOSS](docs/en/guide/steward.md)
|
|
115
117
|
- **Hosted accounts** — A Windows administrator can create an isolated account in the admin panel. Configure a remote entry point to let its user sign in from a browser outside your network. [Admin panel and hosted accounts](docs/en/guide/admin.md)
|
|
@@ -120,6 +122,8 @@ The [User Guide](docs/en/guide/README.md) covers each of these in full.
|
|
|
120
122
|
| --- | --- | --- |
|
|
121
123
|
| **Auto-wake** | Claude Code CLI · Codex App / CLI · Antigravity (Gemini) App / CLI | Can serve as host and resume supported sessions. Delivery to an open window depends on the tool. |
|
|
122
124
|
| **Model API** | OpenAI · Claude · Gemini · DeepSeek · Qwen · Grok · OpenAI-compatible | [Pi](https://github.com/earendil-works/pi) runs on the connected computer and sends requests to the selected provider. Configuration and permissions determine its tools: web search requires setup, and unrestricted access allows general commands. Each member has its own session and can host. |
|
|
125
|
+
| **CLI preview** | Grok Build CLI 2.x | [Grok Build CLI](docs/en/guide/members.md#grok-build-cli-preview) uses the software's own sign-in. Native headless operation was tested with version 2.0.9 on Windows. |
|
|
126
|
+
| **CLI** | DeepSeek Harness | [DeepSeek Harness](docs/en/guide/members.md#deepseek-harness) uses the native headless CLI with full access and its own model credentials. Delivery, stop, and resume were tested with a live model on Windows 0.2.2-rc.3. |
|
|
123
127
|
| **Manual** | Any local AI that can run `codiscuss` (e.g. Cursor CLI) | Reads and writes with its own member credential; see [Manual connection](docs/en/guide/members.md#manual-connection) |
|
|
124
128
|
|
|
125
129
|
Web chat products such as ChatGPT, Claude or Gemini on the web cannot join as members.
|
package/README.zh-CN.md
CHANGED
|
@@ -87,6 +87,8 @@ codiscuss setup
|
|
|
87
87
|
|
|
88
88
|
### 一起干活
|
|
89
89
|
|
|
90
|
+
- **旁观席**:房主可指定一位只读成员,在方案咨询和有文件改动的批次交回时报告有证据的风险。[旁观席](docs/guide/members.md#旁观席)
|
|
91
|
+
|
|
90
92
|
- **点名分工**:直接 `@` 在场成员即可开支线。自动接入的成员空闲时可以和主线并行,忙时排队;手动成员需查收请求。未点名的消息默认交给主持人,智能调度可直接分派部分请求。 [在房间里干活](docs/guide/working.md)
|
|
91
93
|
- **向你提问**:需要你拍板时,成员在房间里提问,输入框上方出现答题卡。[在房间里干活](docs/guide/working.md)
|
|
92
94
|
- **结构化讨论**:设定目标和轮次或时长上限。程序限制成员读取彼此的首轮回答;有剩余轮次和时间时,主持人可安排相互质询。 [讨论](docs/guide/discussion.md)
|
|
@@ -109,7 +111,7 @@ codiscuss setup
|
|
|
109
111
|
|
|
110
112
|
- **房间记录与项目**:浏览项目文件,保存附件,分支或跨站点导入导出。销毁会清理本机记录及该房间新建的专用会话,原有和共用会话保留;服务商端记录按其政策处理。符合条件的空闲房间可设置自动销毁。 [房间目录与建房](docs/guide/rooms.md)
|
|
111
113
|
- **上下文与额度**:查看最近读到的上下文用量,以及账号提供的额度窗口或余额;常见窗口为 5 小时和 7 天,支持的 CLI 账号可从额度表按需刷新。 [查看上下文与额度](docs/guide/members.md#查看上下文与额度)
|
|
112
|
-
-
|
|
114
|
+
- **全房间统计**:按房间查看各模型的用量与交付表现,点开需要处理的运行,在本机导出分享图。[统计](docs/guide/analytics.md)
|
|
113
115
|
- **部署为网站**:把成员做出的网页或构建好的目录发布到公网地址,确认上传内容后上线,有效 7 天、可续期。[在房间里干活](docs/guide/working.md#8-把网页部署成网站)
|
|
114
116
|
- **MOSS(原“平台主理人”)**:在专用对话里查看哪些房间等你处理、询问共议用法,或办理平台事务。[MOSS](docs/guide/steward.md)
|
|
115
117
|
- **托管账号**:Windows 管理员可通过管理面板创建隔离账号。配置托管外网入口后,对方可从外部浏览器登录使用。 [管理面板与托管账号](docs/guide/admin.md)
|
|
@@ -120,6 +122,8 @@ codiscuss setup
|
|
|
120
122
|
| --- | --- | --- |
|
|
121
123
|
| **自动唤醒** | Claude Code CLI · Codex App / CLI · Antigravity(Gemini)App / CLI | 可担任主持人;支持的已有会话可续接,原窗口行为因软件而异 |
|
|
122
124
|
| **模型 API** | OpenAI · Claude · Gemini · DeepSeek · Qwen · Grok · OpenAI 兼容服务 | [Pi](https://github.com/earendil-works/pi) 在接入电脑上运行,请求发给所选服务商。可用工具由配置和权限决定;配置后可搜索网页,全放开档可运行通用命令。成员有独立会话,可担任主持人。 |
|
|
125
|
+
| **CLI 预览** | Grok Build CLI 2.x | [Grok Build CLI](docs/guide/members.md#grok-build-cli-预览) 使用原软件登录,原生无头运行已用 Windows 2.0.9 实测。 |
|
|
126
|
+
| **CLI** | DeepSeek Harness | [DeepSeek Harness](docs/guide/members.md#deepseek-harness) 通过原生无头 CLI 接入,使用全放开及 Harness 自己的模型凭据。Windows 0.2.2-rc.3 已用真实模型验证投递、停止和续接。 |
|
|
123
127
|
| **手动接入** | 任何可运行 `codiscuss` 的本地 AI(如 Cursor CLI) | 凭自己的成员凭证读写,见[手动接入](docs/guide/members.md#手动接入) |
|
|
124
128
|
|
|
125
129
|
网页版 ChatGPT、Claude、Gemini 不能作为成员接入。
|
package/backend/analytics.py
CHANGED
|
@@ -11,6 +11,7 @@ from time import monotonic
|
|
|
11
11
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
12
12
|
|
|
13
13
|
from .analytics_dispatch import dispatch_quality
|
|
14
|
+
from .analytics_rooms import TOKEN_WEIGHTS, room_performance
|
|
14
15
|
from .analytics_live import live_snapshot
|
|
15
16
|
from .analytics_scheduler import scheduler_quality
|
|
16
17
|
from .moss_results import DELEGATED_SENDS
|
|
@@ -157,7 +158,9 @@ SELECT r.id,r.room_id,r.message_id,r.chain_id,r.created_at,r.status,mbr.name,mbr
|
|
|
157
158
|
(m.author='用户' AND NOT EXISTS(SELECT 1 FROM message_relays l WHERE l.message_id=m.id)
|
|
158
159
|
AND NOT EXISTS(SELECT 1 FROM internal_task_messages i WHERE i.message_id=m.id)) AS initiated,
|
|
159
160
|
CAST(strftime('%s',m.created_at) AS INTEGER) AS sent,CAST(strftime('%s',a.created_at) AS INTEGER) AS answered,
|
|
160
|
-
|
|
161
|
+
r.execution_id, prior.request_id IS NOT NULL AS had_retry,
|
|
162
|
+
coalesce(e.model,(SELECT old.model FROM retried_requests rr JOIN member_executions old ON old.id=rr.execution_id
|
|
163
|
+
WHERE rr.request_id=r.id ORDER BY rr.execution_id LIMIT 1),'') AS delivery_model, coalesce(w.kind,aw.kind) AS withdrawal,e.result,c.reason,coalesce(prior.dispatched,e.created_at) AS dispatched
|
|
161
164
|
FROM member_requests r LEFT JOIN prior ON prior.request_id=r.id JOIN messages m ON m.id=r.message_id
|
|
162
165
|
JOIN room_directory d ON d.room_id=r.room_id AND d.system_kind=''
|
|
163
166
|
JOIN room_members mbr ON mbr.id=r.member_id LEFT JOIN member_labels l ON l.room_id=r.room_id AND l.name=mbr.name
|
|
@@ -717,12 +720,14 @@ def snapshot(connection, params, cache, now):
|
|
|
717
720
|
if params.get("range") == "all" and "from" not in params and "to" not in params:
|
|
718
721
|
first = connection.execute("""SELECT MIN(r.created_at) FROM member_requests r
|
|
719
722
|
JOIN room_directory d ON d.room_id=r.room_id AND d.system_kind=''""").fetchone()[0]
|
|
723
|
+
first_run = connection.execute("""SELECT min(e.created_at) FROM member_executions e
|
|
724
|
+
JOIN room_directory d ON d.room_id=e.room_id AND d.system_kind=''""").fetchone()[0]
|
|
720
725
|
first_wait = connection.execute("""SELECT MIN(w.created_at) FROM room_waits w
|
|
721
726
|
JOIN room_directory d ON d.room_id=w.room_id AND d.system_kind=''""").fetchone()[0]
|
|
722
727
|
sealed_first = connection.execute("SELECT MIN(day) FROM analytics_sealed_days").fetchone()[0]
|
|
723
728
|
first_decision = connection.execute("""SELECT MIN(unixepoch(s.created_at)) FROM scheduler_decisions s
|
|
724
729
|
JOIN room_directory d ON d.room_id=s.room_id AND d.system_kind=''""").fetchone()[0]
|
|
725
|
-
candidates = [int(stamp) for stamp in (first, first_wait, first_decision, first_share_activity(connection)) if stamp]
|
|
730
|
+
candidates = [int(stamp) for stamp in (first, first_run, first_wait, first_decision, first_share_activity(connection)) if stamp]
|
|
726
731
|
if sealed_first:
|
|
727
732
|
candidates.append(int(datetime.combine(date.fromisoformat(sealed_first), time.min, timezone.utc).timestamp()))
|
|
728
733
|
earliest = min(candidates) if candidates else None
|
|
@@ -762,13 +767,15 @@ def snapshot(connection, params, cache, now):
|
|
|
762
767
|
scope = {"mode": mode, "rooms": len(rooms), "sealed_rooms": retained, "included_rooms": len(rooms) + retained,
|
|
763
768
|
"includes_archived": True, "sealed_day_tz": "UTC", "sealed_counts": SEALED_COUNTS,
|
|
764
769
|
"sealed_distributions": SEALED_DISTRIBUTIONS,
|
|
765
|
-
"current_only": ["active_seconds", "folders", "rooms", "peak", "concurrency", "issues", "queue", "affected_requests", "members", "models", "workflows", "insights", "scheduler", "share_totals"]}
|
|
770
|
+
"current_only": ["active_seconds", "folders", "rooms", "room_performance", "peak", "concurrency", "issues", "queue", "affected_requests", "members", "models", "workflows", "insights", "scheduler", "share_totals"]}
|
|
766
771
|
scheduler = scheduler_quality(connection, start, end, distribution)
|
|
767
772
|
result = dict(range=dates, scope=scope,
|
|
768
773
|
share_totals=share_totals(connection, attempts, start, end),
|
|
769
774
|
overview=overview(facts, start, end, sealed), previous=overview(facts, previous, start, prior),
|
|
770
775
|
time={**time_stats(connection, requests, executions, (start, end), now, sealed), **activity},
|
|
771
776
|
members=members, models=models, workflows=workflow_stats(connection, attempts),
|
|
777
|
+
token_weights=TOKEN_WEIGHTS,
|
|
778
|
+
room_performance=room_performance(connection, requests, (earlier_attempts, attempts), (previous, start, end)),
|
|
772
779
|
scheduler=scheduler,
|
|
773
780
|
issues=attention_issues(connection, attempts, earlier_attempts, revived, (previous, start, end, now), scheduler["summary"]),
|
|
774
781
|
insights=[] if compact else insights(connection, members, start, end))
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Per-room model usage and delivery metrics for the current and comparison periods."""
|
|
2
|
+
from bisect import bisect_right
|
|
3
|
+
from collections import Counter, defaultdict
|
|
4
|
+
|
|
5
|
+
from .free_room import OWNER_INTERRUPT_RESULT, OWNER_STOP_RESULT
|
|
6
|
+
|
|
7
|
+
TOKEN_WEIGHTS = {"input": 1, "cache_read": 0.1, "cache_write": 1.25, "output": 5}
|
|
8
|
+
STOP_RESULTS = {OWNER_INTERRUPT_RESULT, OWNER_STOP_RESULT}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _bucket():
|
|
12
|
+
return dict(runs=0, covered=0, seconds=0, tokens=0, waste=0, stopped=0, ended=0,
|
|
13
|
+
first=0, user_stop_requests=0, raw=Counter(), failures=Counter(), deliveries=[])
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _ratio(part, total):
|
|
17
|
+
return part / total if total else None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _run(bucket, run, raw):
|
|
21
|
+
seconds = max(0, run["finished_at"] - run["created_at"])
|
|
22
|
+
tokens = sum(raw[key] * weight for key, weight in TOKEN_WEIGHTS.items()) if raw else 0
|
|
23
|
+
bucket["runs"] += 1
|
|
24
|
+
bucket["covered"] += raw is not None
|
|
25
|
+
bucket["seconds"] += seconds
|
|
26
|
+
bucket["tokens"] += tokens
|
|
27
|
+
if raw:
|
|
28
|
+
bucket["raw"].update({key: raw[key] for key in TOKEN_WEIGHTS})
|
|
29
|
+
stopped = run["status"] == "cancelled" and run["result"] in STOP_RESULTS
|
|
30
|
+
if run["status"] in ("failed", "cancelled"):
|
|
31
|
+
bucket["waste"] += tokens
|
|
32
|
+
if run["status"] == "failed":
|
|
33
|
+
bucket["failures"][run["failure_class"] or "unknown"] += 1
|
|
34
|
+
if stopped:
|
|
35
|
+
bucket["stopped"] += 1
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _request(bucket, row):
|
|
39
|
+
bucket["ended"] += 1
|
|
40
|
+
bucket["user_stop_requests"] += row["reason"] == "user_stop"
|
|
41
|
+
if row["status"] == "completed" and row["answered"] and not row["withdrawal"]:
|
|
42
|
+
bucket["first"] += not row["had_retry"]
|
|
43
|
+
if row["answered"] >= row["created_at"]:
|
|
44
|
+
bucket["deliveries"].append(row["answered"] - row["created_at"])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _finish(bucket):
|
|
48
|
+
ordered = sorted(bucket.pop("deliveries"))
|
|
49
|
+
for name, fraction in (("p50", .5), ("p90", .9)):
|
|
50
|
+
bucket[name] = ordered[round((len(ordered) - 1) * fraction)] if ordered else None
|
|
51
|
+
bucket.update(coverage=_ratio(bucket["covered"], bucket["runs"]),
|
|
52
|
+
first_rate=_ratio(bucket["first"], bucket["ended"]),
|
|
53
|
+
intervention=_ratio(bucket["stopped"], bucket["runs"]),
|
|
54
|
+
waste_rate=_ratio(bucket["waste"], bucket["tokens"]))
|
|
55
|
+
return bucket
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _period_groups(requests, runs, usage):
|
|
59
|
+
groups = defaultdict(lambda: defaultdict(_bucket))
|
|
60
|
+
for run in runs:
|
|
61
|
+
room = groups[run["room_id"]]
|
|
62
|
+
for bucket in (room[None], room[run["model"]]):
|
|
63
|
+
_run(bucket, run, usage.get(run["id"]))
|
|
64
|
+
for row in requests:
|
|
65
|
+
if row["status"] not in ("completed", "failed", "cancelled"):
|
|
66
|
+
continue
|
|
67
|
+
if not row["execution_id"] and not row["had_retry"]:
|
|
68
|
+
continue
|
|
69
|
+
room = groups[row["room_id"]]
|
|
70
|
+
for bucket in (room[None], room[row["delivery_model"]]):
|
|
71
|
+
_request(bucket, row)
|
|
72
|
+
return groups
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _host_usage(connection, runs, usage):
|
|
76
|
+
"""Attribute runs by their creation second, including runs without a request anchor."""
|
|
77
|
+
current = dict(connection.execute("SELECT room_id,name FROM room_members WHERE role='host'"))
|
|
78
|
+
times, names = defaultdict(list), defaultdict(list)
|
|
79
|
+
changes = connection.execute("""SELECT e.room_id,e.at,m.name,json_extract(e.data,'$.role')='host' AS took
|
|
80
|
+
FROM room_events e JOIN room_members m ON m.id=json_extract(e.data,'$.member_id')
|
|
81
|
+
WHERE e.kind IN ('member.joined','member.changed') AND json_valid(e.data)
|
|
82
|
+
AND (json_extract(e.data,'$.role')='host') <> (json_extract(e.data,'$.from_role') IS 'host')
|
|
83
|
+
ORDER BY e.room_id,e.at,e.id""")
|
|
84
|
+
for room, stamp, name, took in changes:
|
|
85
|
+
if not names[room]:
|
|
86
|
+
# A first recorded departure identifies the host before the event log began.
|
|
87
|
+
names[room].append(name if not took else None)
|
|
88
|
+
if not took and names[room][-1] != name:
|
|
89
|
+
continue
|
|
90
|
+
times[room].append(stamp)
|
|
91
|
+
names[room].append(name if took else None)
|
|
92
|
+
totals = defaultdict(_bucket)
|
|
93
|
+
for run in runs:
|
|
94
|
+
room = run["room_id"]
|
|
95
|
+
host = current.get(room)
|
|
96
|
+
if room in names:
|
|
97
|
+
index = bisect_right(times[room], run["created_at"] * 1000 + 999)
|
|
98
|
+
host = names[room][index]
|
|
99
|
+
if host == run["name"]:
|
|
100
|
+
_run(totals[room], run, usage.get(run["id"]))
|
|
101
|
+
return totals
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def room_performance(connection, requests, periods, bounds):
|
|
105
|
+
"""Return existing active rooms, using creation time for each execution and request cohort."""
|
|
106
|
+
previous, start, end = bounds
|
|
107
|
+
usage = {r["execution_id"]: r for r in connection.execute("""SELECT u.* FROM execution_usage u
|
|
108
|
+
JOIN member_executions e ON e.id=u.execution_id WHERE e.created_at>=? AND e.created_at<?""", (previous, end))}
|
|
109
|
+
before, current = [_period_groups((r for r in requests if left <= r["created_at"] < right), runs, usage)
|
|
110
|
+
for runs, left, right in zip(periods, bounds, bounds[1:])]
|
|
111
|
+
hosts = _host_usage(connection, periods[1], usage)
|
|
112
|
+
rows = []
|
|
113
|
+
for room, title, folder in connection.execute("""SELECT r.id,r.title,f.name FROM rooms r
|
|
114
|
+
LEFT JOIN room_folder_assignments a ON a.room_id=r.id LEFT JOIN topic_folders f ON f.id=a.folder_id"""):
|
|
115
|
+
if room not in current:
|
|
116
|
+
continue
|
|
117
|
+
models = current[room]
|
|
118
|
+
total = _finish(models.pop(None))
|
|
119
|
+
total.update(room_id=room, title=title, folder=folder, models=[])
|
|
120
|
+
old = _finish(before[room][None]) if room in before else {}
|
|
121
|
+
total["previous"] = {key: old.get(key) for key in ("first_rate", "intervention", "waste_rate")}
|
|
122
|
+
for model, bucket in models.items():
|
|
123
|
+
item = _finish(bucket)
|
|
124
|
+
token_share = _ratio(item["tokens"], total["tokens"])
|
|
125
|
+
waste_share = _ratio(item["waste"], total["waste"])
|
|
126
|
+
item.update(model=model, token_share=token_share, time_share=_ratio(item["seconds"], total["seconds"]),
|
|
127
|
+
waste_skew=_ratio(waste_share, token_share) if waste_share is not None else None)
|
|
128
|
+
total["models"].append(item)
|
|
129
|
+
total["models"].sort(key=lambda model: (-model["tokens"], model["model"]))
|
|
130
|
+
host = hosts[room]
|
|
131
|
+
total["host"] = dict(runs=host["runs"], token_share=_ratio(host["tokens"], total["tokens"]),
|
|
132
|
+
time_share=_ratio(host["seconds"], total["seconds"]))
|
|
133
|
+
rows.append(total)
|
|
134
|
+
return sorted(rows, key=lambda row: (-row["tokens"], row["room_id"]))
|