Skip to content

[Bug] project_memory_extract 后台调用的 token 用量未写入 model_usage,本地用量统计永久少计 #1391

Open

Description

@SKYhuangjing

环境

  • ZCode Desktop 3.17.2(macOS arm64)
  • 问题类别:计费 / 配额 · Billing / Quota(用量统计准确性)
  • 框架:不涉及框架(桌面端项目记忆功能)

现象

querySource = project_memory_extract(项目记忆提取)产生的模型调用不写入 ~/.zcode/cli/db/db.sqlite 的 model_usage 表——全史 0 行。其他 querySource(main_turn / workflow_child / subagent / compact / session_title / goal_summary_title / target_completion_verification)均正常落库,唯独这个后台功能不落。

影响

任何依赖本地用量库的统计(包括第三方用量采集工具)都会永久少计这部分消耗。实测 2026-11-04 单日(一个重负载长会话):

  • 日志中 135 次 model.sdk.generate.completed 且 querySource=project_memory_extract;
  • 其中 51 次与网关侧账单按时间戳+token 逐条精确对上,合计 18,662,182 tokens 未入账(当日该会话未入账总量约 20.7M);
  • 另有部分调用直连 open.bigmodel.cn(不经网关),意味着这部分消耗连网关账单都没有——除 ZCode 自身外无任何地方可对账。

证据

  1. SELECT COUNT(*) FROM model_usage WHERE query_source='project_memory_extract' → 0(全史)
  2. 日志 ~/.zcode/cli/log/zcode-2026-11-04.jsonl 事件样例(usage 字段在日志层被脱敏为 "[Redacted]",外部无法从日志恢复数字):
{
  "timestamp": "2026-11-03T22:05:19.768Z",
  "event": "model.sdk.generate.completed",
  "module": "adapters.model",
  "sessionId": "sess_…",
  "turnId": "turn_…",
  "context": {
    "baseURL": "https://open.bigmodel.cn/api/coding/paas/v4",
    "modelId": "GLM-5.3-Flash",
    "querySource": "project_memory_extract",
    "requestId": "…",
    "finishReason": "stop",
    "usage": { "inputTokens": "[Redacted]", "outputTokens": "[Redacted]", "totalTokens": "[Redacted]", "…": "[Redacted]" }
  }
}
  1. 对照实验:同一 turn 内的 main_turn 调用(12 次)全部落库且与网关账单逐条一致;紧接着的 project_memory_extract 调用(冷启动 cache=0、逐步预热的分块特征)网关有、库无。

期望

project_memory_extract(建议同时排查其他未落库的 querySource)与普通调用一样写入 model_usage(表结构现成,四分项字段齐全)。修复后,读取该表的本地用量统计无需任何改动即可自动覆盖。

提交前确认

  • 已搜索现有 issue,未见重复
  • 已阅读 CONTRIBUTING.md

Activity

  1. github-actions commented on Nov 4, 2026

    @github-actions

    👋 感谢你的反馈,我们已经收到。

    • 维护者看到后会尽快回复你。
    • 状态保持为 status: 待评估,你可以随时补充信息。
    • 信息不全时我们会打上 needs: 更多信息 标签并 @ 你。

    👋 Thanks — we've received your issue.

    • A maintainer will get back to you as soon as we can.
    • Status stays at status: 待评估 (Triage); feel free to add context.
    • If we need more details, we'll add needs: 更多信息 and ping you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions