From 99f96b066d8c74441847dab47380f71a33d2cd84 Mon Sep 17 00:00:00 2001 From: yowucai Date: Fri, 20 Nov 2026 02:02:51 +0800 Subject: [PATCH] feat(schedule): report per-execution token usage and dashboard totals --- design.md | 6 +- docs/api/openapi.yaml | 25 +++++++ docs/api/orchestration.md | 4 +- internal/domain/schedule_execution.go | 64 ++++++++++++++++++ internal/httpapi/assistant.go | 3 + internal/httpapi/assistant_schedule.go | 42 ++++++++---- internal/httpapi/assistant_schedule_test.go | 61 +++++++++++++++++ internal/httpapi/assistant_tasks.go | 3 + internal/httpapi/server.go | 2 +- internal/repo/models.go | 10 +++ .../repo/schedule_job_execution_usage_test.go | 65 +++++++++++++++++++ internal/repo/schedule_job_executions.go | 28 ++++++++ internal/schedule/execution_session_test.go | 4 +- internal/schedule/service.go | 27 ++++++-- migrations/034_schedule_job_executions.sql | 8 +++ .../039_schedule_execution_legacy_stats.sql | 62 ++++++++++++++++++ web/app/schedule-executions.js | 32 ++++++--- web/sync.css | 12 +++- webtest/tests/schedule-executions.test.mjs | 35 ++++++++-- 19 files changed, 453 insertions(+), 40 deletions(-) create mode 100644 internal/repo/schedule_job_execution_usage_test.go create mode 100644 migrations/039_schedule_execution_legacy_stats.sql diff --git a/design.md b/design.md index f33cee368..b2739e08e 100644 --- a/design.md +++ b/design.md @@ -180,11 +180,11 @@ - AI 定时任务页通过「本项目任务 / 共享任务市场」SlidingTabs 切换,市场沿用 MCP 市场卡片与搜索,展示来源、版本、推荐调度及本项目订阅数量;允许继续添加不同参数或调度的订阅。独立任务可发布不可变共享版本。项目订阅显示来源与固定版本,在原任务弹窗填写业务参数,保留独立调度、模型与 Bot 通知。有新版的订阅入口使用淡紫强调色;版本选择、提示词与参数对比直接展开在同一个任务编辑器内,参数随目标版本更新,点击「保存并升级」生效。市场详情保持独立只读窗口。编辑保存成功自动关闭弹窗并刷新列表,失败保留输入;迟到的保存响应不得关闭新打开的编辑器。弹窗沿用 Harness 令牌与关闭交互,窄屏改为单列。 - AI 定时任务页的最后一个标签是「执行记录」(`#aiScheduleExecPanel`,`web/app/schedule-executions.js`),按当前项目列出提示词任务和 SOP 触发的每次执行,数据来自 `GET /api/schedule-job-executions/dashboard`:筛选、计数、分页和概览都在服务端完成,前端每次只取当前一页,数据量再大结果也不会因截断而失真。标签上的数字是该项目保留的记录总数,最近 7 天有失败时改为红色,没有记录时隐藏。面板由三块组成,块间距 16px: - - **执行概览卡**:标题「执行概览(最近 7 天)」,按任务筛选时标题带上任务名。4 张指标卡依次为执行次数(紫 `layers`)、成功率(绿 `check`)、失败次数(红 `close`,数值红字)、平均耗时(蓝 `priority-bolt`)。图标放在 36px 圆角方块里,底色是对应色 12%。每张卡下方写「较上周 ±N%」(成功率写 pp)并带 `arrow-up` / `arrow-down`,变好绿色、变差红色,失败次数和耗时下降算变好;上周没有数据时写「上周无数据」。「最近 7 天」指包含当前小时在内的最近 7×24 小时,「上周」指再往前 7×24 小时,按执行开始的小时归入。已结束的执行来自服务端按小时累计的统计(保留 30 天),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数。概览不画趋势图,概览只随任务筛选变化。 + - **执行概览卡**:标题「执行概览(最近 7 天)」,按任务筛选时标题带上任务名。4 张指标卡依次为执行次数(紫 `layers`)、成功率(绿 `check`)、平均耗时(蓝 `priority-bolt`)、Token 总消耗(紫 `layers`)。图标放在 36px 圆角方块里,底色是对应色 12%。成功率下方显示「成功 N 次 · 失败 N 次」,仅失败大于 0 时标红,不再单独占卡。执行次数和平均耗时下方写「较上周 ±N%」并带 `arrow-up` / `arrow-down`,变好绿色、变差红色,耗时下降算变好;上周没有数据时写「上周无数据」。Token 卡下方显示已上报执行数,仅统计最近 7 天本次 AI 调用的消耗(含重试、不含下游 SOP),未上报不当作零消耗。「最近 7 天」指包含当前小时在内的最近 7×24 小时,「上周」指再往前 7×24 小时,按执行开始的小时归入。已结束的执行来自服务端按小时累计的统计(保留 30 天),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数。概览不画趋势图,概览只随任务筛选变化。 - **筛选栏**:依次为搜索框(匹配任务名、任务 ID、Trace ID、错误、AI 回复、派发需求)、时间(全部时间 / 今天 / 最近 7 天 / 最近 30 天)、任务、状态,每项左侧有 Harness 图标(`search` / `calendar` / `layers` / `filter`),最右是紫字「重置」(`refresh`,没有筛选时禁用)。改任何筛选都回到第 1 页并重新向服务端请求;搜索框在停止输入 250ms 后才请求。 - - **执行记录卡**:标题「执行记录(共 N 条)」,下面是表格,列为展开箭头、状态、任务名称、触发时间、耗时(表头 `info` 提示口径)、Trace ID、操作。状态用 Status Badge(进行中蓝色呼吸点、成功绿色 `check`、失败红色 `alert`、待对齐基线琥珀色 `clock`)。任务名称格第一行是类型标签加任务名,第二行单行省略显示摘要:错误(红)、AI 回复,或 SOP 的「候选 N · 启动 N · 需求名」。触发时间格式为 `YYYY/MM/DD HH:mm:ss`。Trace ID 列是带描边的小按钮,显示末 8 位加 `copy`,点击复制排障信息,失败行用红字红描边。操作列每行固定两个位置,右对齐,「⋯」在整列对齐:左边是主操作位(最小宽 88px),AI 执行显示「打开会话」(`preview-expand`,新标签页打开),SOP 触发显示「查看运行」(打开第一个运行);没有可打开的内容时这个位置不消失,改为灰字「无会话」/「准备中」/「未派发」,悬停说明原因。失败的执行只要已经创建了会话(例如运行中途服务重启),同样可以打开会话。执行记录比会话留得久,会话已被清理(超出保留会话数或保留期限,或被手动删除)时,这个位置显示灰字「会话已清理」,不再给出打不开的链接,AI 回复仍可在详情里查看;是否已清理以列表接口返回的 `sessionGone` 为准。右边是「⋯」菜单(复制排障信息、只看该任务、编辑任务,复用任务行菜单的组件和键盘行为)。点击箭头或行主体展开详情:触发方式、开始 / 结束时间、耗时、完整 Trace ID、SOP 的候选 / 启动,以及完整错误或 AI 回复(最高 220px,超出滚动)和全部派发运行。表格底部是分页:左侧「每页 10 / 20 / 50 条」,右侧上一页、页码(首页、末页和当前页前后各一页,中间用「…」)、「共 N 页」、下一页,当前页紫底白字。翻页和改每页条数也向服务端请求;记录变少导致当前页超出末页时,改为显示末页。最底部脚注说明列表保留每个任务最近 200 次执行,概览按全部执行统计。 + - **执行记录卡**:标题「执行记录(共 N 条)」,下面是表格,列为展开箭头、状态、任务名称、触发时间、耗时(表头 `info` 提示口径)、Token、Trace ID、操作。Token 显示本次总量,未上报显示「未上报」、进行中显示「待统计」,展开详情提供模型与输入、输出、缓存读写明细。状态用 Status Badge(进行中蓝色呼吸点、成功绿色 `check`、失败红色 `alert`、待对齐基线琥珀色 `clock`)。任务名称格第一行是类型标签加任务名,第二行单行省略显示摘要:错误(红)、AI 回复,或 SOP 的「候选 N · 启动 N · 需求名」。触发时间格式为 `YYYY/MM/DD HH:mm:ss`。Trace ID 列是带描边的小按钮,显示末 8 位加 `copy`,点击复制排障信息,失败行用红字红描边。操作列每行固定两个位置,右对齐,「⋯」在整列对齐:左边是主操作位(最小宽 88px),AI 执行显示「打开会话」(`preview-expand`,新标签页打开),SOP 触发显示「查看运行」(打开第一个运行);没有可打开的内容时这个位置不消失,改为灰字「无会话」/「准备中」/「未派发」,悬停说明原因。失败的执行只要已经创建了会话(例如运行中途服务重启),同样可以打开会话。执行记录比会话留得久,会话已被清理(超出保留会话数或保留期限,或被手动删除)时,这个位置显示灰字「会话已清理」,不再给出打不开的链接,AI 回复仍可在详情里查看;是否已清理以列表接口返回的 `sessionGone` 为准。右边是「⋯」菜单(复制排障信息、只看该任务、编辑任务,复用任务行菜单的组件和键盘行为)。点击箭头或行主体展开详情:触发方式、开始 / 结束时间、耗时、完整 Trace ID、SOP 的候选 / 启动,以及完整错误或 AI 回复(最高 220px,超出滚动)和全部派发运行。表格底部是分页:左侧「每页 10 / 20 / 50 条」,右侧上一页、页码(首页、末页和当前页前后各一页,中间用「…」)、「共 N 页」、下一页,当前页紫底白字。翻页和改每页条数也向服务端请求;记录变少导致当前页超出末页时,改为显示末页。最底部脚注说明列表保留每个任务最近 200 次执行,概览按全部执行统计。 - 任务列表每行的「⋯」菜单里有「执行记录」,点击后切到这个标签,按该任务筛选并清空其他筛选。AI 任务行上不放「复制 Trace」,排障信息在执行记录里复制。复制内容是一段排障信息:首行「定时任务执行排障信息」,第二行 `Trace ID: <32 位 ID>`(与服务端调度日志里的 `trace=` 字段一致),之后是任务、类型、项目、触发方式、状态、开始时间、耗时、错误或 AI 回复摘要、派发运行、页面和复制时间,空字段省略;可直接贴给 AI 助手,助手用 `schedule_job_executions_list` 按 traceId 查询。 - - 窄屏(≤860px)时指标改为 2×2,表格隐藏触发时间、耗时和 Trace ID 三列,这些信息在展开详情里仍能看到。 + - 窄屏(≤860px)时指标改为 2×2,表格隐藏触发时间、耗时、Token 和 Trace ID 四列,这些信息在展开详情里仍能看到。 - AI 定时任务列表行常驻启停开关(`.hui-switch`,`role="switch"`,旁边文字为「已启用 / 已停用」)、「立即运行」和一枚「⋯」按钮(Harness `more-horizontal`,`aria-haspopup="menu"`)。开关调用 `POST /api/schedule-jobs/{id}/enabled`,只改启停,不动提示词、执行 Key 和会话;请求期间禁用,失败时回到原状态并提示;停用的任务名改为次要色。编辑弹窗不再显示「启用」,保存时沿用行上的状态,新任务默认启用;触发配置弹窗仍保留「启用」。另外只有订阅有新版时,行上才会多一枚淡紫「有新版 vN」。「⋯」菜单依次为编辑、执行记录、发布共享(订阅为「查看版本」)、分隔线、删除(红字)。菜单 `position: fixed`,在触发器下方 6px 右对齐,放不下时翻到上方;打开时聚焦第一项,方向键 / Home / End 移动,Esc 关闭并把焦点还给触发器,点击外部、Tab、滚动或缩放窗口时关闭;列表轮询重绘时保持菜单打开。点击行主体仍打开编辑。 - AI 定时任务弹窗不让选执行身份:任务用保存人的 Key 执行(新建时就是创建人,其他成员编辑保存后改用他的 Key,不会借用别人的 Key),选的模型决定用哪个 CLI 的 Key。保存人没有个人 CodeBuddy Key 时改用平台共享 Key,模型只能选管理员允许的那几个(选 CLI 默认时用共享 Key 的默认模型);两者都没有,或选了 Codex / Claude Code 但没配对应 Key,保存会被拒绝,并提示去个人配置填 Key 或联系管理员配置共享 Key。弹窗说明和模型提示写明这个规则。模型下方是「会话」(每次新开会话 / 始终在同一个会话里继续),固定会话已有会话时,提示文字后带「打开当前会话 ↗」。选「每次新开会话」时下面多一项「保留会话数」(最近 10 / 50 / 100 个、不限,默认 50),超过后每次执行结束时删除最早的会话,执行记录仍保留;删除会话后,它的消息引用的截图、上传文件和回复文件(存放在发送人的附件目录里)由后台清理:删除请求只记一个待清理标记,后台在删除后约 10 秒(之后每 30 分钟兜底一次)批量检查一遍引用再删,仍被其他会话(例如分支会话)、进行中的回复或空间消息引用的文件保留;这对所有会话的删除都生效,不只是定时任务;选「始终在同一个会话里继续」时这一项隐藏。列表行不写用谁的 Key,固定会话的任务带「同一会话」。 diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml index 4d993a599..74754a3fa 100644 --- a/docs/api/openapi.yaml +++ b/docs/api/openapi.yaml @@ -5415,6 +5415,21 @@ components: failed: { type: integer } running: { type: integer } avgMs: { type: integer, format: int64 } + metered: + type: integer + description: Finished executions that reported usage; missing usage is not counted as zero. + tokens: + type: object + description: Sum of each execution's reported total (or input plus output); cache details are not added again. + properties: + total: { type: integer, format: int64 } + input: { type: integer, format: int64 } + output: { type: integer, format: int64 } + cacheRead: { type: integer, format: int64 } + cacheWrite: { type: integer, format: int64 } + avgTokens: { type: integer, format: int64, description: Average tokens over metered executions only. } + priced: { type: integer, description: Executions whose cost could be estimated. } + costCny: { type: number, format: double, description: Sum of estimated costs in CNY. } ScheduleJobExecution: type: object description: One execution of a schedule job. Each job keeps its latest 200 executions. @@ -5447,6 +5462,16 @@ components: type: integer assistantMessage: type: string + model: + type: string + description: Model used by this execution's AI generation. + usage: + $ref: "#/components/schemas/TokenUsage" + description: Optional usage for this execution including retries and usage before failure, excluding prior turns and downstream SOPs. Absence means unreported. + costCny: + type: number + format: double + description: Optional estimated cost in CNY, only present when pricing is known. sessionId: type: string description: Studio conversation an AI execution ran in. diff --git a/docs/api/orchestration.md b/docs/api/orchestration.md index 21afc3f58..fc126c35f 100644 --- a/docs/api/orchestration.md +++ b/docs/api/orchestration.md @@ -414,7 +414,9 @@ curl -u "$AGENTFLOW_USER:$AGENTFLOW_PASS" \ - `overview.current` / `overview.previous`:最近 7×24 小时(含当前小时)与再往前 7×24 小时的 `total`、`finished`、`success`、`failed`、`running`、`avgMs`,只随 `jobId` 变化。已结束的执行来自按小时累计的统计表 `schedule_job_execution_stats`(保留 30 天,删除任务时一并删除),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数; - `records`:该项目保留的记录总数;`recentFailed`:该项目最近 7 天的失败次数(标签角标用)。 -缺 `projectId` 或 `since` 格式不对返回 400;`jobId` 属于无权访问的项目返回 403。统计表从部署后开始累计,部署前的执行不计入概览。 +缺 `projectId` 或 `since` 格式不对返回 400;`jobId` 属于无权访问的项目返回 403。统计表从部署后开始累计。升级时可备份并停止平台后,手动执行 `migrations/039_schedule_execution_legacy_stats.sql`,将仍保留的旧执行补入缺失的小时桶;脚本不覆盖已有桶,发现部分重叠时拒绝回补,不重建已清理的记录,也不推算旧 Token。 + +AI 执行记录的可选 `model`、`usage`、`costCny` 保存本次 generation 的消耗与费用估计,包含重试及失败前已上报的调用,不含同一固定会话的前次执行或下游 SOP。`usage` 缺失表示未上报。`overview.current` / `overview.previous` 另外返回 `metered`、`tokens`(`total`、`input`、`output`、`cacheRead`、`cacheWrite`)、`avgTokens`、`priced`、`costCny`。`tokens.total` 优先使用模型上报总量,否则为输入加输出;缓存明细不再相加。`avgTokens` 仅对已上报执行取平均,`priced` 仅计入可估价执行。 ```bash curl -u "$AGENTFLOW_USER:$AGENTFLOW_PASS" \ diff --git a/internal/domain/schedule_execution.go b/internal/domain/schedule_execution.go index 4498f7b7e..b03a78d67 100644 --- a/internal/domain/schedule_execution.go +++ b/internal/domain/schedule_execution.go @@ -32,6 +32,51 @@ type ScheduleJobExecution struct { StartedAt time.Time `json:"startedAt"` FinishedAt *time.Time `json:"finishedAt,omitempty"` DurationMs int64 `json:"durationMs,omitempty"` + // Model and Usage cover this execution's generation, including retries + // and usage reported before failure, but not prior turns in a fixed session. + // Usage.CostUSD/CostStatus are the analytics estimate; CostCNY is set only + // when that estimate is known. Unreported executions and SOP triggers have + // no usage. + Model string `json:"model,omitempty"` + Usage *TokenUsage `json:"usage,omitempty"` + CostCNY *float64 `json:"costCny,omitempty"` +} + +// RecordUsage stores the prompt's model and usage on the execution, priced +// like conversation turns in analytics. Empty usage is not recorded. +func (e *ScheduleJobExecution) RecordUsage(model string, usage *TokenUsage) { + if usage == nil || usageTotalTokens(*usage) == 0 && usage.CostUSD == 0 { + return + } + priced := PriceScheduleExecutionUsage(model, *usage) + e.Model, e.Usage, e.CostCNY = strings.TrimSpace(model), &priced, nil + if priced.CostStatus == "estimated" { + cny := priced.CostUSD * AnalyticsUSDToCNY + e.CostCNY = &cny + } +} + +// PriceScheduleExecutionUsage prices an AI prompt's usage the way analytics +// prices conversation turns: GPT models report input including caches. +func PriceScheduleExecutionUsage(model string, usage TokenUsage) TokenUsage { + inclusive := strings.HasPrefix(strings.ToLower(strings.TrimSpace(model)), "gpt-") + return AnalyticsPricedUsage(model, usage, inclusive) +} + +// usageTotalTokens is the billed total, falling back to input + output. +func usageTotalTokens(u TokenUsage) int64 { + if u.TotalTokens > 0 { + return u.TotalTokens + } + return u.InputTokens + u.OutputTokens +} + +// ExecutionTotalTokens is the billed total of an execution's usage. +func (e ScheduleJobExecution) ExecutionTotalTokens() int64 { + if e.Usage == nil { + return 0 + } + return usageTotalTokens(*e.Usage) } // ScheduleJobExecutionRun links an execution to the SOP run it started. @@ -80,6 +125,25 @@ type ScheduleExecutionWindow struct { Failed int `json:"failed"` Running int `json:"running"` AvgMs int64 `json:"avgMs"` + // Metered counts the finished executions that recorded token usage; + // AvgTokens averages Tokens.Total over them, so runs that failed before + // reaching a model do not pull the average down. Priced counts those + // whose cost could be estimated; CostCNY sums their estimates. + Metered int `json:"metered"` + Tokens ScheduleExecutionTokens `json:"tokens"` + AvgTokens int64 `json:"avgTokens"` + Priced int `json:"priced"` + CostCNY float64 `json:"costCny"` +} + +// ScheduleExecutionTokens sums token usage. Total is the billed total; the +// four parts follow the providers' own split and need not add up to it. +type ScheduleExecutionTokens struct { + Total int64 `json:"total"` + Input int64 `json:"input"` + Output int64 `json:"output"` + CacheRead int64 `json:"cacheRead"` + CacheWrite int64 `json:"cacheWrite"` } // ScheduleSessionFixed is ScheduleJob.SessionMode for an AI job that keeps diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 7ac913058..501edc0ed 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -429,6 +429,9 @@ func (h *assistantHandlers) chat(c *gin.Context) { if replyModel == "" { replyModel = assistantDefaultModel } + if report, ok := modelCtx.Value(scheduledUsageReportKey{}).(scheduledUsageReport); ok && report.GenerationID == assistantControlScopeFrom(modelCtx).GenerationID && report.Report != nil { + defer func() { report.Report(replyModel, turnUsage.snapshot()) }() + } generate := h.generate if generate == nil { resume := false diff --git a/internal/httpapi/assistant_schedule.go b/internal/httpapi/assistant_schedule.go index 287a013ae..acde23c7e 100644 --- a/internal/httpapi/assistant_schedule.go +++ b/internal/httpapi/assistant_schedule.go @@ -8,8 +8,16 @@ import ( "agentflow/internal/domain" "agentflow/internal/repo" + "agentflow/internal/schedule" ) +type scheduledUsageReportKey struct{} + +type scheduledUsageReport struct { + GenerationID string + Report func(string, *domain.TokenUsage) +} + // scheduledSessionPolicy carries how a schedule job's AI run uses Studio // conversations; legacy assistant tasks run without one. type scheduledSessionPolicy struct { @@ -21,7 +29,8 @@ type scheduledSessionPolicy struct { KeepHistory bool // Opened, when set, learns the conversation as soon as it is created, // before the prompt finishes. - Opened func(sessionID string) + Opened func(sessionID string) + ReportUsage func(string, *domain.TokenUsage) } type scheduledSessionPolicyKey struct{} @@ -49,26 +58,31 @@ func (h *assistantHandlers) runScheduleJobPrompt(job domain.ScheduleJob) (messag // runScheduleJobPromptReporting is runScheduleJobPrompt that also tells // opened about the conversation the moment it is created. func (h *assistantHandlers) runScheduleJobPromptReporting(job domain.ScheduleJob, opened func(sessionID string)) (message, sessionID string, err error) { + result, err := h.runScheduleJobPromptResult(job, opened) + return result.Message, result.SessionID, err +} + +func (h *assistantHandlers) runScheduleJobPromptResult(job domain.ScheduleJob, opened func(sessionID string)) (result schedule.AssistantRunResult, err error) { if h == nil || h.repo == nil { - return "", "", fmt.Errorf("未连接数据源") + return result, fmt.Errorf("未连接数据源") } prompt := strings.TrimSpace(job.AssistantPrompt) if job.SharedTaskID != "" { version, verr := h.repo.SharedScheduleVersion(job.SharedTaskID, job.SharedTaskVersion) if verr != nil { - return "", "", fmt.Errorf("读取订阅版本失败:%w", verr) + return result, fmt.Errorf("读取订阅版本失败:%w", verr) } prompt, _, err = version.Render(job.ParameterValues) if err != nil { - return "", "", err + return result, err } } if prompt == "" { - return "", "", nil + return result, nil } user, shared, err := h.scheduleJobAssistantUser(job, job.AssistantModel) if err != nil { - return "", "", err + return result, err } model := strings.TrimSpace(job.AssistantModel) if shared != nil { @@ -76,7 +90,7 @@ func (h *assistantHandlers) runScheduleJobPromptReporting(job domain.ScheduleJob // allows, and never silently switches to another one. allowed, ok := assistantSponsoredModelFor(shared, scheduleSharedRequestedModel(model)) if !ok { - return "", "", fmt.Errorf("定时任务使用平台共享 Key 执行,只能用平台管理员允许的模型(%s),任务指定的 %s 不在其中,本次没有运行;请在定时任务里改选允许的模型,或在个人配置中填写自己的 Key", strings.Join(shared, "、"), model) + return result, fmt.Errorf("定时任务使用平台共享 Key 执行,只能用平台管理员允许的模型(%s),任务指定的 %s 不在其中,本次没有运行;请在定时任务里改选允许的模型,或在个人配置中填写自己的 Key", strings.Join(shared, "、"), model) } model = allowed } @@ -89,23 +103,27 @@ func (h *assistantHandlers) runScheduleJobPromptReporting(job domain.ScheduleJob } ctx := withAssistantExecMode(context.Background(), assistantExecModeForApproval(AssistantApprovalAuto)) ctx = withAssistantSessionOrigin(ctx, domain.AssistantSourceScheduled, job.ID) - policy := scheduledSessionPolicy{KeepHistory: true, Opened: opened} + policy := scheduledSessionPolicy{KeepHistory: true, Opened: opened, ReportUsage: func(model string, usage *domain.TokenUsage) { + result.Model, result.Usage = model, usage + }} if job.UsesFixedSession() { policy.Continue = strings.TrimSpace(job.FixedSessionID) } ctx = withScheduledSessionPolicy(ctx, policy) err = h.executeAssistantTask(ctx, user, &task) h.pruneScheduledTaskSessions(job) + result.Message, result.SessionID = task.LastMessage, task.SessionID if err != nil { if msg := strings.TrimSpace(task.LastError); msg != "" { - return task.LastMessage, task.SessionID, fmt.Errorf("%s", msg) + return result, fmt.Errorf("%s", msg) } if errors.Is(err, context.Canceled) { - return task.LastMessage, task.SessionID, fmt.Errorf("生成已停止") + return result, fmt.Errorf("生成已停止") } - return task.LastMessage, task.SessionID, err + return result, err } - return firstNonEmpty(task.NotificationResult, task.LastMessage), task.SessionID, nil + result.Message = firstNonEmpty(task.NotificationResult, task.LastMessage) + return result, nil } // scheduleJobAssistantUser picks the saved run-as member, or the project or diff --git a/internal/httpapi/assistant_schedule_test.go b/internal/httpapi/assistant_schedule_test.go index 121efb182..6f029020a 100644 --- a/internal/httpapi/assistant_schedule_test.go +++ b/internal/httpapi/assistant_schedule_test.go @@ -3,6 +3,7 @@ package httpapi import ( "context" "errors" + "os" "path/filepath" "strings" "testing" @@ -12,6 +13,7 @@ import ( "agentflow/internal/engine" "agentflow/internal/repo" "agentflow/internal/runtime" + "agentflow/internal/schedule" "agentflow/internal/tracker" ) @@ -112,3 +114,62 @@ func TestScheduledAssistantAutoWritesWithoutConfirmation(t *testing.T) { }) } } + +func TestScheduledUsageStaysPerExecution(t *testing.T) { + h, user := workbenchFixture(t) + bin := filepath.Join(t.TempDir(), "codebuddy") + t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin) + job := domain.ScheduleJob{ID: "usage-job", ProjectID: "workbench", OwnerUserID: user.ID, RunAsUserID: user.ID, Name: "巡检", SkipDispatch: true, AssistantPrompt: "检查文件", Enabled: true, SessionMode: domain.ScheduleSessionFixed} + if err := h.repo.PutScheduleJob(job); err != nil { + t.Fatal(err) + } + svc := schedule.New(h.repo, tracker.NewService(), nil) + svc.SetAssistantSessionPrep(h.runScheduleJobPromptResult) + var sessionID, firstID string + for _, tc := range []struct { + name, message string + input int64 + failed bool + unreported bool + }{ + {name: "first", message: "检查结果正常,没有问题。", input: 10}, + {name: "fixed_second", message: "检查结果正常,没有问题。", input: 10}, + {name: "failed_retries", message: "我先查看一下文件。", input: 30, failed: true}, + {name: "unreported", message: "检查结果正常,没有问题。", unreported: true}, + } { + t.Run(tc.name, func(t *testing.T) { + usage := `,"usage":{"input_tokens":10,"output_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":3}` + if tc.unreported { + usage = "" + } + script := "#!/bin/sh\necho '{\"type\":\"result\",\"result\":\"" + tc.message + "\"" + usage + "}'\n" + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + out, err := svc.RunNow(job.ID) + if (err != nil) != tc.failed { + t.Fatalf("outcome=%+v err=%v", out, err) + } + e, ok := h.repo.ScheduleJobExecution(domain.ScheduleJobExecutionID(out.LastTraceID)) + if !ok || e.SessionID == "" || (!tc.unreported && e.Model == "") { + t.Fatalf("execution=%+v", e) + } + if sessionID == "" { + sessionID, firstID = e.SessionID, e.ID + } else if e.SessionID != sessionID { + t.Fatalf("fixed session changed: %s != %s", e.SessionID, sessionID) + } + if tc.unreported { + if e.Usage != nil { + t.Fatalf("unexpected usage=%+v", e.Usage) + } + } else if e.Usage == nil || e.Usage.InputTokens != tc.input || e.Usage.OutputTokens != tc.input/2 || e.Usage.CacheReadTokens != tc.input*10 || e.ExecutionTotalTokens() != tc.input*3/2 { + t.Fatalf("usage=%+v", e.Usage) + } + }) + } + first, _ := h.repo.ScheduleJobExecution(firstID) + if first.Usage == nil || first.Usage.InputTokens != 10 { + t.Fatalf("earlier usage changed: %+v", first) + } +} diff --git a/internal/httpapi/assistant_tasks.go b/internal/httpapi/assistant_tasks.go index a90eb6f59..ae4378fe3 100644 --- a/internal/httpapi/assistant_tasks.go +++ b/internal/httpapi/assistant_tasks.go @@ -296,6 +296,9 @@ func (h *assistantHandlers) executeAssistantTask(ctx context.Context, user domai } // Like a chat turn, a scheduled run has no time limit: it ends when the // CLI finishes or someone stops it from the conversation page. + if policy.ReportUsage != nil { + ctx = context.WithValue(ctx, scheduledUsageReportKey{}, scheduledUsageReport{GenerationID: g.ID, Report: policy.ReportUsage}) + } h.executeGeneration(ctx, req, g, domainUserToAuth(user)) if policy.KeepHistory { // Schedule jobs keep every run's conversation for their execution diff --git a/internal/httpapi/server.go b/internal/httpapi/server.go index 99b7bb3b8..f5f2f6c51 100644 --- a/internal/httpapi/server.go +++ b/internal/httpapi/server.go @@ -650,7 +650,7 @@ func newWithNotifications(s *repo.Store, e *engine.Engine, syncSvc *tracker.Serv d.api = g if d.schedule != nil { d.schedule.SetAssistantPrep(asst.prepareScheduleJob) - d.schedule.SetAssistantSessionPrep(asst.runScheduleJobPromptReporting) + d.schedule.SetAssistantSessionPrep(asst.runScheduleJobPromptResult) } static, _ := fs.Sub(webassets.Files, ".") g.NoRoute(func(c *gin.Context) { diff --git a/internal/repo/models.go b/internal/repo/models.go index 538418d69..1354bb0a1 100644 --- a/internal/repo/models.go +++ b/internal/repo/models.go @@ -265,6 +265,16 @@ type scheduleJobExecutionStatRow struct { Failed int `gorm:"column:failed;not null;default:0"` Timed int `gorm:"column:timed;not null;default:0"` DurationMs int64 `gorm:"column:duration_ms;not null;default:0"` + // Token usage of the AI prompts, priced when each execution finished + // (the hour mixes models, so it cannot be priced afterwards). + Metered int `gorm:"column:metered;not null;default:0"` + TokensTotal int64 `gorm:"column:tokens_total;not null;default:0"` + TokensInput int64 `gorm:"column:tokens_input;not null;default:0"` + TokensOutput int64 `gorm:"column:tokens_output;not null;default:0"` + TokensCacheRead int64 `gorm:"column:tokens_cache_read;not null;default:0"` + TokensCacheWrite int64 `gorm:"column:tokens_cache_write;not null;default:0"` + Priced int `gorm:"column:priced;not null;default:0"` + CostUSD float64 `gorm:"column:cost_usd;not null;default:0"` } func (scheduleJobExecutionStatRow) TableName() string { return "schedule_job_execution_stats" } diff --git a/internal/repo/schedule_job_execution_usage_test.go b/internal/repo/schedule_job_execution_usage_test.go new file mode 100644 index 000000000..77a39fbca --- /dev/null +++ b/internal/repo/schedule_job_execution_usage_test.go @@ -0,0 +1,65 @@ +package repo + +import ( + "fmt" + "testing" + "time" + + "agentflow/internal/domain" +) + +func TestScheduleJobExecutionUsageOverview(t *testing.T) { + for _, backend := range []string{"sqlite", "mysql"} { + t.Run(backend, func(t *testing.T) { + st := openExecutionTestStore(t, backend) + jobID := fmt.Sprintf("usage-overview-%d", time.Now().UnixNano()) + if err := st.PutScheduleJob(domain.ScheduleJob{ID: jobID, ProjectID: "platform", Name: "AI", SkipDispatch: true, AssistantPrompt: "inspect"}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = st.DeleteScheduleJob(jobID) }) + now := time.Now().UTC().Truncate(time.Hour).Add(30 * time.Minute) + put := func(trace string, started time.Time, status string, usage *domain.TokenUsage) { + t.Helper() + e := domain.ScheduleJobExecution{JobID: jobID, ProjectID: "platform", TraceID: jobID + trace, Status: "running", StartedAt: started} + if err := st.PutScheduleJobExecution(e); err != nil { + t.Fatal(err) + } + e.Status, e.DurationMs = status, 1000 + e.RecordUsage("unpriced-test-model", usage) + for range 2 { + if err := st.PutScheduleJobExecution(e); err != nil { + t.Fatal(err) + } + } + } + put("success", now.Add(-2*time.Hour), "success", &domain.TokenUsage{InputTokens: 10, OutputTokens: 5, TotalTokens: 20, CacheReadTokens: 100, CacheWriteTokens: 3}) + put("failed", now.Add(-time.Hour), "failed", &domain.TokenUsage{InputTokens: 20, OutputTokens: 10, CacheReadTokens: 200, CacheWriteTokens: 6}) + put("unreported", now.Add(-time.Hour), "success", nil) + put("previous", now.Add(-200*time.Hour), "success", &domain.TokenUsage{InputTokens: 4, OutputTokens: 2, TotalTokens: 6}) + if err := st.PutScheduleJobExecution(domain.ScheduleJobExecution{JobID: jobID, ProjectID: "platform", TraceID: jobID + "running", Status: "running", StartedAt: now}); err != nil { + t.Fatal(err) + } + current, previous := st.ScheduleJobExecutionOverview([]string{jobID}, now) + wantCurrent := domain.ScheduleExecutionWindow{ + Total: 4, Finished: 3, Success: 2, Failed: 1, Running: 1, AvgMs: 1000, + Metered: 2, AvgTokens: 25, + Tokens: domain.ScheduleExecutionTokens{Total: 50, Input: 30, Output: 15, CacheRead: 300, CacheWrite: 9}, + } + wantPrevious := domain.ScheduleExecutionWindow{ + Total: 1, Finished: 1, Success: 1, AvgMs: 1000, Metered: 1, AvgTokens: 6, + Tokens: domain.ScheduleExecutionTokens{Total: 6, Input: 4, Output: 2}, + } + if current != wantCurrent || previous != wantPrevious { + t.Fatalf("overview=%+v / %+v, want=%+v / %+v", current, previous, wantCurrent, wantPrevious) + } + if err := st.DB.Where("job_id = ?", jobID).Delete(&scheduleJobExecutionRow{}).Error; err != nil { + t.Fatal(err) + } + current, _ = st.ScheduleJobExecutionOverview([]string{jobID}, now) + wantCurrent.Total, wantCurrent.Running = 3, 0 + if current != wantCurrent { + t.Fatalf("retention changed usage overview: %+v", current) + } + }) + } +} diff --git a/internal/repo/schedule_job_executions.go b/internal/repo/schedule_job_executions.go index 28426a321..efe902362 100644 --- a/internal/repo/schedule_job_executions.go +++ b/internal/repo/schedule_job_executions.go @@ -2,6 +2,7 @@ package repo import ( "encoding/json" + "math" "strings" "time" @@ -95,6 +96,8 @@ func countScheduleJobExecution(tx *gorm.DB, e domain.ScheduleJobExecution) error if _, err := insertIgnore(tx, "schedule_job_execution_stats", map[string]any{ "job_id": e.JobID, "bucket_hour": hour, "project_id": e.ProjectID, "finished": 0, "success": 0, "failed": 0, "timed": 0, "duration_ms": 0, + "metered": 0, "tokens_total": 0, "tokens_input": 0, "tokens_output": 0, + "tokens_cache_read": 0, "tokens_cache_write": 0, "priced": 0, "cost_usd": 0, }); err != nil { return err } @@ -109,6 +112,18 @@ func countScheduleJobExecution(tx *gorm.DB, e domain.ScheduleJobExecution) error updates["timed"] = gorm.Expr("timed + 1") updates["duration_ms"] = gorm.Expr("duration_ms + ?", e.DurationMs) } + if u := e.Usage; u != nil { + updates["metered"] = gorm.Expr("metered + 1") + updates["tokens_total"] = gorm.Expr("tokens_total + ?", e.ExecutionTotalTokens()) + updates["tokens_input"] = gorm.Expr("tokens_input + ?", u.InputTokens) + updates["tokens_output"] = gorm.Expr("tokens_output + ?", u.OutputTokens) + updates["tokens_cache_read"] = gorm.Expr("tokens_cache_read + ?", u.CacheReadTokens) + updates["tokens_cache_write"] = gorm.Expr("tokens_cache_write + ?", u.CacheWriteTokens) + if e.CostCNY != nil { + updates["priced"] = gorm.Expr("priced + 1") + updates["cost_usd"] = gorm.Expr("cost_usd + ?", u.CostUSD) + } + } return tx.Model(&scheduleJobExecutionStatRow{}).Where("job_id = ? AND bucket_hour = ?", e.JobID, hour).Updates(updates).Error } @@ -133,6 +148,7 @@ func (r *CatalogRepo) ScheduleJobExecutionOverview(jobIDs []string, now time.Tim return ¤t } durations := map[*domain.ScheduleExecutionWindow][2]int64{} + costs := map[*domain.ScheduleExecutionWindow]float64{} var stats []scheduleJobExecutionStatRow if err := r.DB.Where("job_id IN ? AND bucket_hour >= ?", jobIDs, previousFrom).Find(&stats).Error; err == nil { for _, s := range stats { @@ -145,6 +161,14 @@ func (r *CatalogRepo) ScheduleJobExecutionOverview(jobIDs []string, now time.Tim w.Failed += s.Failed d := durations[w] durations[w] = [2]int64{d[0] + s.DurationMs, d[1] + int64(s.Timed)} + w.Metered += s.Metered + w.Tokens.Total += s.TokensTotal + w.Tokens.Input += s.TokensInput + w.Tokens.Output += s.TokensOutput + w.Tokens.CacheRead += s.TokensCacheRead + w.Tokens.CacheWrite += s.TokensCacheWrite + w.Priced += s.Priced + costs[w] += s.CostUSD } } var running []*time.Time @@ -164,6 +188,10 @@ func (r *CatalogRepo) ScheduleJobExecutionOverview(jobIDs []string, now time.Tim if d := durations[w]; d[1] > 0 { w.AvgMs = d[0] / d[1] } + if w.Metered > 0 { + w.AvgTokens = w.Tokens.Total / int64(w.Metered) + } + w.CostCNY = math.Round(costs[w]*domain.AnalyticsUSDToCNY*100) / 100 } return } diff --git a/internal/schedule/execution_session_test.go b/internal/schedule/execution_session_test.go index ab864a369..585f0e3cc 100644 --- a/internal/schedule/execution_session_test.go +++ b/internal/schedule/execution_session_test.go @@ -24,11 +24,11 @@ func TestInterruptedExecutionKeepsItsConversation(t *testing.T) { } svc := New(st, tracker.NewService(), nil) opened, release, done := make(chan struct{}), make(chan struct{}), make(chan struct{}) - svc.SetAssistantSessionPrep(func(job domain.ScheduleJob, report func(string)) (string, string, error) { + svc.SetAssistantSessionPrep(func(job domain.ScheduleJob, report func(string)) (AssistantRunResult, error) { report("chat-live") close(opened) <-release - return "", "chat-live", nil + return AssistantRunResult{SessionID: "chat-live"}, nil }) claimed, err := svc.claim("job") if err != nil { diff --git a/internal/schedule/service.go b/internal/schedule/service.go index b57383aed..fc902df09 100644 --- a/internal/schedule/service.go +++ b/internal/schedule/service.go @@ -39,9 +39,20 @@ type BaselineGuard func(job domain.ScheduleJob, issues []domain.Issue) (blocked type AssistantPrep func(job domain.ScheduleJob) (message string, err error) // AssistantSessionPrep is an AssistantPrep that also reports the Studio -// conversation the prompt ran in (empty when none was opened). It calls -// opened as soon as the conversation exists, before the prompt finishes. -type AssistantSessionPrep func(job domain.ScheduleJob, opened func(sessionID string)) (message, sessionID string, err error) +// conversation the prompt ran in (empty when none was opened) and what the +// prompt consumed. It calls opened as soon as the conversation exists, +// before the prompt finishes. +type AssistantSessionPrep func(job domain.ScheduleJob, opened func(sessionID string)) (AssistantRunResult, error) + +// AssistantRunResult is the outcome of one AI prompt run. Usage sums the +// replies the run added (nil when it reached no model); Model is the model +// that produced them. +type AssistantRunResult struct { + Message string + SessionID string + Model string + Usage *domain.TokenUsage +} type Service struct { repo *repo.Store @@ -297,10 +308,14 @@ func (s *Service) executeJob(job domain.ScheduleJob, triggerOrigin string, exec return s.saveResult(result), err } if (s.assistant != nil || s.assistantRun != nil) && (strings.TrimSpace(job.AssistantPrompt) != "" || job.SharedTaskID != "") { - msg, sessionID, prepErr := s.runAssistant(job, exec) + run, prepErr := s.runAssistant(job, exec) + msg, sessionID := run.Message, run.SessionID if sessionID == "" && exec != nil { sessionID = exec.SessionID } + if exec != nil { + exec.RecordUsage(run.Model, run.Usage) + } job.LastSessionID = sessionID if sessionID != "" && job.UsesFixedSession() { job.FixedSessionID = sessionID @@ -379,12 +394,12 @@ func (s *Service) executeJob(job domain.ScheduleJob, triggerOrigin string, exec return finish(job, nil) } -func (s *Service) runAssistant(job domain.ScheduleJob, exec *domain.ScheduleJobExecution) (string, string, error) { +func (s *Service) runAssistant(job domain.ScheduleJob, exec *domain.ScheduleJobExecution) (AssistantRunResult, error) { if s.assistantRun != nil { return s.assistantRun(job, func(sessionID string) { s.recordOpenedSession(job, exec, sessionID) }) } msg, err := s.assistant(job) - return msg, "", err + return AssistantRunResult{Message: msg}, err } // recordOpenedSession stores the conversation while the prompt still runs, diff --git a/migrations/034_schedule_job_executions.sql b/migrations/034_schedule_job_executions.sql index d6d5ae2af..469d0daea 100644 --- a/migrations/034_schedule_job_executions.sql +++ b/migrations/034_schedule_job_executions.sql @@ -31,6 +31,14 @@ CREATE TABLE IF NOT EXISTS schedule_job_execution_stats ( failed BIGINT NOT NULL DEFAULT 0, timed BIGINT NOT NULL DEFAULT 0, duration_ms BIGINT NOT NULL DEFAULT 0, + metered BIGINT NOT NULL DEFAULT 0, + tokens_total BIGINT NOT NULL DEFAULT 0, + tokens_input BIGINT NOT NULL DEFAULT 0, + tokens_output BIGINT NOT NULL DEFAULT 0, + tokens_cache_read BIGINT NOT NULL DEFAULT 0, + tokens_cache_write BIGINT NOT NULL DEFAULT 0, + priced BIGINT NOT NULL DEFAULT 0, + cost_usd DOUBLE NOT NULL DEFAULT 0, PRIMARY KEY (job_id, bucket_hour), INDEX idx_schedule_job_execution_stats_project (project_id) ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/migrations/039_schedule_execution_legacy_stats.sql b/migrations/039_schedule_execution_legacy_stats.sql new file mode 100644 index 000000000..3c00df98d --- /dev/null +++ b/migrations/039_schedule_execution_legacy_stats.sql @@ -0,0 +1,62 @@ +-- MySQL 8.0.16+; execute manually with the platform stopped and a database backup. +-- Requires the current schedule_job_execution_stats columns (additive AutoMigrate). +-- Backfill only missing hourly buckets from retained, finished legacy executions. +-- Existing buckets (including records removed by retention) are never overwritten. +-- Reject partial existing buckets and missing buckets with usage: those require +-- execution-level reconciliation, not an additive or estimated backfill. +-- Unreported legacy Token usage stays unknown; no Token usage is reconstructed. +-- Stop on the first SQL error; do not execute with mysql --force. + +SET @legacy_stats_from = UTC_TIMESTAMP() - INTERVAL 30 DAY; +START TRANSACTION; + +CREATE TEMPORARY TABLE legacy_schedule_stats AS +SELECT + job_id, + MAX(project_id) AS project_id, + CAST(DATE_FORMAT(started_at, '%Y-%m-%d %H:00:00') AS DATETIME) AS bucket_hour, + COUNT(*) AS finished, + SUM(status = 'success') AS success, + SUM(status = 'failed') AS failed, + SUM(COALESCE(CAST(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.durationMs')) AS SIGNED), 0) > 0) AS timed, + SUM(GREATEST(COALESCE(CAST(JSON_UNQUOTE(JSON_EXTRACT(payload, '$.durationMs')) AS SIGNED), 0), 0)) AS duration_ms, + SUM(COALESCE(JSON_TYPE(JSON_EXTRACT(payload, '$.usage')) = 'OBJECT', 0)) AS with_usage +FROM schedule_job_executions +WHERE started_at >= @legacy_stats_from + AND COALESCE(status, '') NOT IN ('', 'running') +GROUP BY job_id, bucket_hour; + +CREATE TEMPORARY TABLE legacy_schedule_stats_guard ( + passed TINYINT NOT NULL CHECK (passed = 1) +); +INSERT INTO legacy_schedule_stats_guard (passed) +SELECT IF(COUNT(*) = 0, 1, 0) +FROM legacy_schedule_stats r +LEFT JOIN schedule_job_execution_stats s + ON s.job_id = r.job_id AND s.bucket_hour = r.bucket_hour +WHERE (s.job_id IS NOT NULL AND s.finished < r.finished) + OR (s.job_id IS NULL AND r.with_usage > 0); + +CREATE TEMPORARY TABLE legacy_schedule_stats_missing AS +SELECT r.* +FROM legacy_schedule_stats r +LEFT JOIN schedule_job_execution_stats s + ON s.job_id = r.job_id AND s.bucket_hour = r.bucket_hour +WHERE s.job_id IS NULL + AND EXISTS (SELECT 1 FROM legacy_schedule_stats_guard WHERE passed = 1); + +INSERT INTO schedule_job_execution_stats ( + job_id, bucket_hour, project_id, finished, success, failed, timed, duration_ms, + metered, tokens_total, tokens_input, tokens_output, tokens_cache_read, + tokens_cache_write, priced, cost_usd +) +SELECT + job_id, bucket_hour, project_id, finished, success, failed, + timed, duration_ms, 0, 0, 0, 0, 0, 0, 0, 0 +FROM legacy_schedule_stats_missing; + +SELECT ROW_COUNT() AS restored_hour_buckets; +DROP TEMPORARY TABLE legacy_schedule_stats_missing; +DROP TEMPORARY TABLE legacy_schedule_stats_guard; +DROP TEMPORARY TABLE legacy_schedule_stats; +COMMIT; diff --git a/web/app/schedule-executions.js b/web/app/schedule-executions.js index 723f366d5..12173c9c6 100644 --- a/web/app/schedule-executions.js +++ b/web/app/schedule-executions.js @@ -48,6 +48,22 @@ function scheduleExecDurationLabel(ms){ return `${Math.floor(s/3600)} 小时 ${Math.floor(s%3600/60)} 分`; } function scheduleExecShortTrace(trace){return String(trace||'').slice(-8)} +function scheduleExecTokenNumber(value){const n=Number(value);return Number.isFinite(n)&&n>0?Math.floor(n):0} +function scheduleExecTokenLabel(value){return scheduleExecTokenNumber(value).toLocaleString('zh-CN')} +function scheduleExecUsageTotal(e){return e?.usage?scheduleExecTokenNumber(e.usage.totalTokens)||scheduleExecTokenNumber(e.usage.inputTokens)+scheduleExecTokenNumber(e.usage.outputTokens):null} +function scheduleExecTokenCellHTML(e){ + const total=scheduleExecUsageTotal(e); + if(total===null)return `${e.jobKind==='assistant'?(e.status==='running'?'待统计':'未上报'):'—'}`; + const u=e.usage; + const parts=[`输入 ${scheduleExecTokenLabel(u.inputTokens)}`,`输出 ${scheduleExecTokenLabel(u.outputTokens)}`,`缓存读 ${scheduleExecTokenLabel(u.cacheReadTokens)}`,`缓存写 ${scheduleExecTokenLabel(u.cacheWriteTokens)}`]; + return `${scheduleExecTokenLabel(total)}`; +} +function scheduleExecUsageDetailHTML(e){ + if(!e.usage)return ''; + const u=e.usage; + const field=(label,value)=>`
${label}
${scheduleExecTokenLabel(value)}
`; + return `
模型
${escapeHTML(e.model||'未上报')}
${field('Token 总量',scheduleExecUsageTotal(e))}${field('输入 Token',u.inputTokens)}${field('输出 Token',u.outputTokens)}${field('缓存读 Token',u.cacheReadTokens)}${field('缓存写 Token',u.cacheWriteTokens)}`; +} function scheduleExecTimeLabel(value){ const d=knownTime(value);if(!d)return '—'; const p=n=>String(n).padStart(2,'0'); @@ -57,7 +73,7 @@ function scheduleExecDayStart(ms){const d=new Date(ms);d.setHours(0,0,0,0);retur // The server counts windows; this only turns one into the four metrics. function scheduleExecWindow(w){ const finished=Number(w?.finished)||0,success=Number(w?.success)||0; - return {total:Number(w?.total)||0,success,failed:Number(w?.failed)||0,running:Number(w?.running)||0,rate:finished?Math.round(success/finished*100):null,avgMs:Number(w?.avgMs)||0}; + return {total:Number(w?.total)||0,success,failed:Number(w?.failed)||0,running:Number(w?.running)||0,rate:finished?Math.round(success/finished*100):null,avgMs:Number(w?.avgMs)||0,metered:Number(w?.metered)||0,tokens:scheduleExecTokenNumber(w?.tokens?.total)}; } // "today" starts at local midnight; 7d/30d count back from now. function scheduleExecSince(range,now=scheduleExecNow()){ @@ -119,9 +135,9 @@ function scheduleExecMetricsHTML(overview){ const c=scheduleExecWindow(overview?.current),p=scheduleExecWindow(overview?.previous); const cell=(tone,icon,label,value,delta)=>`
${label}${value}${delta}
`; return cell('accent','layers','执行次数',c.total,c.total||p.total?scheduleExecDeltaHTML(c.total,p.total):'最近 7 天无执行') - +cell('success','check','成功率',c.rate===null?'—':`${c.rate}%`,scheduleExecDeltaHTML(c.rate,p.rate,{unit:'pp'})) - +cell('error','close','失败次数',c.failed,c.total||p.total?scheduleExecDeltaHTML(c.failed,p.failed,{lowerIsBetter:true}):'—') - +cell('info','priority-bolt','平均耗时',c.avgMs?scheduleExecDurationLabel(c.avgMs):'—',c.avgMs&&p.avgMs?scheduleExecDeltaHTML(Math.round(c.avgMs),Math.round(p.avgMs),{lowerIsBetter:true}):'已结束的执行'); + +cell('success','check','成功率',c.rate===null?'—':`${c.rate}%`,`成功 ${c.success} 次0?' class="has-failures"':''}>失败 ${c.failed} 次`) + +cell('info','priority-bolt','平均耗时',c.avgMs?scheduleExecDurationLabel(c.avgMs):'—',c.avgMs&&p.avgMs?scheduleExecDeltaHTML(Math.round(c.avgMs),Math.round(p.avgMs),{lowerIsBetter:true}):'已结束的执行') + +cell('accent','layers','Token 总消耗',c.metered?scheduleExecTokenLabel(c.tokens):'—',`${c.metered} 次已上报 · 最近 7 天`); } // Same link shape as issueConversationURL: the Studio frame opens chatSession. function scheduleExecConversationURL(e){ @@ -156,7 +172,7 @@ function scheduleExecDetailHTML(e,detailId){ const runs=(e.runs||[]).filter(r=>r.runId); const runLinks=runs.length?`
${runs.map(r=>``).join('')}
`:''; const finished=knownTime(e.finishedAt); - return `
${field('触发方式',escapeHTML(scheduleExecTriggerLabel(e.triggerOrigin)))}${field('开始时间',escapeHTML(scheduleExecTimeLabel(e.startedAt)))}${field('结束时间',finished?escapeHTML(scheduleExecTimeLabel(e.finishedAt)):'')}${field('耗时',e.status==='running'?'进行中':escapeHTML(scheduleExecDurationLabel(e.durationMs)))}${field('Trace ID',e.traceId?`${escapeHTML(e.traceId)}`:'')}${assistant?'':field('候选 / 启动',`${Number(e.pulled)||0} / ${Number(e.dispatched)||0}`)}
${error?`
错误
${escapeHTML(error)}
`:''}${!error&&assistant&&e.assistantMessage?`
AI 回复
${escapeHTML(e.assistantMessage)}
`:''}${runLinks?`
派发运行${runLinks}
`:''}
`; + return `
${field('触发方式',escapeHTML(scheduleExecTriggerLabel(e.triggerOrigin)))}${field('开始时间',escapeHTML(scheduleExecTimeLabel(e.startedAt)))}${field('结束时间',finished?escapeHTML(scheduleExecTimeLabel(e.finishedAt)):'')}${field('耗时',e.status==='running'?'进行中':escapeHTML(scheduleExecDurationLabel(e.durationMs)))}${field('Trace ID',e.traceId?`${escapeHTML(e.traceId)}`:'')}${assistant?'':field('候选 / 启动',`${Number(e.pulled)||0} / ${Number(e.dispatched)||0}`)}${scheduleExecUsageDetailHTML(e)}
${error?`
错误
${escapeHTML(error)}
`:''}${!error&&assistant&&e.assistantMessage?`
AI 回复
${escapeHTML(e.assistantMessage)}
`:''}${runLinks?`
派发运行${runLinks}
`:''}
`; } // Every row has the same two action slots, so ⋯ lines up down the column: // the primary slot says why there is nothing to open instead of vanishing. @@ -175,10 +191,10 @@ function scheduleExecRowHTML(e){ const assistant=e.jobKind==='assistant',open=scheduleExecExpanded.has(e.id); const detailId=`sched-exec-detail-${String(e.id).replace(/[^A-Za-z0-9_-]/g,'')}`; const trace=e.traceId?``:'—'; - return `${scheduleExecBadgeHTML(e)}${assistant?'AI':'触发'}${escapeHTML(e.jobName||e.jobId)}${scheduleExecSummaryLine(e)}${escapeHTML(scheduleExecTimeLabel(e.startedAt))}${e.status==='running'?'进行中':escapeHTML(scheduleExecDurationLabel(e.durationMs))}${trace}
${scheduleExecPrimaryActionHTML(e)}${scheduleExecRowMenuHTML(e)||''}
${scheduleExecDetailHTML(e,detailId)}`; + return `${scheduleExecBadgeHTML(e)}${assistant?'AI':'触发'}${escapeHTML(e.jobName||e.jobId)}${scheduleExecSummaryLine(e)}${escapeHTML(scheduleExecTimeLabel(e.startedAt))}${e.status==='running'?'进行中':escapeHTML(scheduleExecDurationLabel(e.durationMs))}${scheduleExecTokenCellHTML(e)}${trace}
${scheduleExecPrimaryActionHTML(e)}${scheduleExecRowMenuHTML(e)||''}
${scheduleExecDetailHTML(e,detailId)}`; } function scheduleExecTableHTML(rows){ - return `${rows.map(scheduleExecRowHTML).join('')}
状态任务名称触发时间耗时${scheduleExecIcon('info',12)}Trace ID操作
`; + return `${rows.map(scheduleExecRowHTML).join('')}
状态任务名称触发时间耗时${scheduleExecIcon('info',12)}Token${scheduleExecIcon('info',12)}Trace ID操作
`; } // Page buttons: first, last, and the current page ±1, with gaps as "…". function scheduleExecPageList(page,pages){ @@ -251,7 +267,7 @@ function renderScheduleExecutions(){ list.querySelector('[data-reset-exec]')?.addEventListener('click',resetScheduleExecFilters); bindScheduleExecList(list); bindScheduleExecPager(pager); - $('#scheduleExecFootnote').textContent=empty?'':'列表保留每个任务最近 200 次执行;概览按全部执行统计,不受保留条数限制。'; + $('#scheduleExecFootnote').textContent=empty?'':'列表保留每个任务最近 200 次执行;概览按全部执行统计,不受保留条数限制。Token 只计本次 AI 调用(含重试),不含下游 SOP;缓存明细不重复计入总量。'; window.AgentFlowHarnessIcons?.hydrate(panel); if(focusKey){ const host=focusKey==='execPage'?pager:list; diff --git a/web/sync.css b/web/sync.css index c4e2e7962..8cff75f80 100644 --- a/web/sync.css +++ b/web/sync.css @@ -175,7 +175,7 @@ #aiSchedulesView .sched-exec-card-title .muted { font-weight:400;font-size:13px;color:var(--hui-secondary); } #aiSchedulesView .sched-exec-th-hint { display:inline-flex;align-items:center;gap:3px;color:inherit;cursor:help; } #aiSchedulesView .sched-exec-th-hint > .harness-icon { color:var(--hui-muted); } -/* 概览:4 张指标卡 */ +/* 执行概览 */ #aiSchedulesView .sched-exec-overview-main { min-width:0;padding-bottom:16px; } #aiSchedulesView .sched-exec-metrics { display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;padding:0 20px; } #aiSchedulesView .sched-exec-metric { display:flex;gap:12px;align-items:flex-start;min-width:0;padding:14px;border:1px solid var(--hui-border-soft);border-radius:12px;background:var(--hui-surface); } @@ -189,6 +189,9 @@ #aiSchedulesView .sched-exec-metric-body > b { font:600 22px/1.2 var(--hui-sans);font-variant-numeric:tabular-nums;color:var(--hui-text);white-space:nowrap; } #aiSchedulesView .sched-exec-metric[data-tone="error"] .sched-exec-metric-body > b { color:var(--hui-error); } #aiSchedulesView .sched-exec-delta { display:inline-flex;align-items:center;gap:2px;color:var(--hui-muted);font-size:12px;white-space:nowrap; } +#aiSchedulesView .sched-exec-outcomes { display:flex;flex-wrap:wrap;gap:2px 12px;color:var(--hui-muted);font-size:12px;font-variant-numeric:tabular-nums; } +#aiSchedulesView .sched-exec-outcomes > span { white-space:nowrap; } +#aiSchedulesView .sched-exec-outcomes .has-failures { color:var(--hui-error); } #aiSchedulesView .sched-exec-delta b { font-weight:500;font-variant-numeric:tabular-nums; } #aiSchedulesView .sched-exec-delta[data-good="true"] :is(b, .harness-icon) { color:var(--hui-success); } #aiSchedulesView .sched-exec-delta[data-good="false"] :is(b, .harness-icon) { color:var(--hui-error); } @@ -211,7 +214,10 @@ #aiSchedulesView .sched-exec-table th:nth-child(2) { width:104px; } #aiSchedulesView .sched-exec-table th:nth-child(4) { width:156px; } #aiSchedulesView .sched-exec-table th:nth-child(5) { width:92px; } -#aiSchedulesView .sched-exec-table th:nth-child(6) { width:116px; } +#aiSchedulesView .sched-exec-table th:nth-child(6) { width:104px; } +#aiSchedulesView .sched-exec-table th:nth-child(7) { width:116px; } +#aiSchedulesView .sched-exec-tokens { color:var(--hui-text);font-variant-numeric:tabular-nums;white-space:nowrap; } +#aiSchedulesView .sched-exec-tokens .muted { color:var(--hui-muted); } #aiSchedulesView .sched-exec-table th:last-child { width:150px;padding-right:16px; } #aiSchedulesView .sched-exec-table td { padding:12px 10px;vertical-align:middle;border-top:1px solid var(--hui-border-soft); } #aiSchedulesView .sched-exec-row { cursor:pointer;transition:background .15s var(--hui-ease-flow); } @@ -276,7 +282,7 @@ #aiSchedulesView .sched-exec-footnote:empty { display:none; } @media(max-width:860px) { #aiSchedulesView .sched-exec-metrics { grid-template-columns:repeat(2,minmax(0,1fr)); } - #aiSchedulesView .sched-exec-table :is(th, td):is(:nth-child(4), :nth-child(5), :nth-child(6)) { display:none; } + #aiSchedulesView .sched-exec-table :is(th, td):is(:nth-child(4), :nth-child(5), :nth-child(6), :nth-child(7)) { display:none; } #aiSchedulesView .sched-exec-detail > td { padding-left:16px; } #aiSchedulesView .sched-exec-search { max-width:none; } #aiSchedulesView .sched-exec-reset { margin-left:0; } diff --git a/webtest/tests/schedule-executions.test.mjs b/webtest/tests/schedule-executions.test.mjs index 20a0566c0..3ad265416 100644 --- a/webtest/tests/schedule-executions.test.mjs +++ b/webtest/tests/schedule-executions.test.mjs @@ -122,7 +122,10 @@ test('execution dashboard lists history with metrics, filters and copyable trace const metrics = app.document.querySelector('#scheduleExecMetrics').textContent; assert.match(metrics, /执行次数\s*2/); assert.match(metrics, /成功率\s*50%/); - assert.match(metrics, /失败次数\s*1/); + assert.equal(app.document.querySelectorAll('#scheduleExecMetrics .sched-exec-metric').length, 4); + const successCard = app.document.querySelectorAll('#scheduleExecMetrics .sched-exec-metric')[1]; + assert.match(successCard.textContent, /成功率\s*50%\s*成功 1 次\s*失败 1 次/); + assert.match(successCard.querySelector('.has-failures').textContent, /失败 1 次/); assert.match(rows[1].textContent, /候选 3 · 启动 1 · 登录页改版/); assert.match(rows[1].textContent, /1 分 30 秒/); @@ -249,9 +252,9 @@ test('the overview comes from the server and the table filters and pages there', const metric = (tone) => doc.querySelector(`#scheduleExecMetrics [data-tone="${tone}"]`); assert.match(metric('accent').textContent, /执行次数\s*12\s*较上周 \+50%/); - assert.match(metric('success').textContent, /成功率\s*83%\s*较上周 \+33pp/); - assert.match(metric('error').textContent, /失败次数\s*2\s*较上周 −50%/); - assert.equal(metric('error').querySelector('.sched-exec-delta').dataset.good, 'true', 'fewer failures read as good'); + assert.match(metric('success').textContent, /成功率\s*83%\s*成功 10 次\s*失败 2 次/); + assert.equal(metric('error'), null, 'failure counts share the success-rate card'); + assert.match(metric('success').querySelector('.has-failures').textContent, /失败 2 次/); assert.match(metric('info').textContent, /平均耗时\s*1 分 0 秒\s*较上周 −50%/); assert.equal(doc.querySelector('#scheduleExecTrend'), null, 'the overview has no trend chart'); assert.match(doc.querySelector('#scheduleExecFootnote').textContent, /不受保留条数限制/); @@ -357,3 +360,27 @@ test('execution dashboard reports empty, error and duration states', async (t) = assert.equal(app.evaluate(`scheduleExecDurationLabel(7380000)`), '2 小时 3 分'); assert.equal(app.evaluate(`scheduleExecTriggerLabel('manual_run')`), '手动运行'); }); + +test('token metrics use server windows and execution rows expose per-run usage', async (t) => { + const app = await loadApp(); + t.after(app.close); + const overview = { current: { total: 200, finished: 200, success: 190, failed: 10, metered: 100, tokens: { total: 150000 }, avgTokens: 1500 } }; + const html = app.evaluate(`scheduleExecMetricsHTML(${JSON.stringify(overview)})`); + assert.match(html, /Token 总消耗.*150,000.*100 次已上报/s); + assert.equal((html.match(/class="sched-exec-metric"/g) || []).length, 4); + assert.match(html, /执行次数.*成功率.*95%.*成功 190 次.*失败 10 次.*平均耗时.*Token 总消耗/s); + assert.doesNotMatch(html, /平均 Token|失败次数/); + const noFailures = app.evaluate(`scheduleExecMetricsHTML({current:{total:2,finished:2,success:2,failed:0}})`); + assert.match(noFailures, /成功率.*100%.*成功 2 次.*失败 0 次/s); + assert.doesNotMatch(noFailures, /has-failures/); + const e = { id: 'usage', jobKind: 'assistant', status: 'success', model: '', usage: { inputTokens: 1000, outputTokens: 200, totalTokens: 1500, cacheReadTokens: 800, cacheWriteTokens: 50 } }; + assert.match(app.evaluate(`scheduleExecTokenCellHTML(${JSON.stringify(e)})`), />1,500