Open
fix:worksapce.usage接口5分钟定时请求刷新ASpod老化时间问题 #485
fix:worksapce.usage接口5分钟定时请求刷新ASpod老化时间问题 #485
Open
hjh0606Add on 1 day ago
hjh0606
hjh0606Collaborators
1 day ago

Paired: GitHub #699 ↔ GitCode !485

What type of PR is this?

/kind

Self-checklist:(请自检,在[ ]内打上x,我们将检视你的完成情况,否则会导致pr无法合入)

Linked Closing Issues:

likedislike
Merge blocked
atomgit-bot
atomgit-bot
1 day ago Comments:

变更摘要

此 PR 修复企业版 workspace.usage 接口在 5 分钟定时轮询时每次都转发到 AS 的问题:原实现会触发 Runtime 的 route/touch,刷新空闲 AS Pod 的老化计时(pod_ttl),导致 Pod 无法按预期回收。改动后企业版在非强制刷新时优先读取 Gateway 侧 workspace_quota_usage 缓存并直接组装配额快照返回,仅当 force_refresh 为真或缓存未命中时才继续 forward 到 AS;同时将 force_refresh 参数解析抽取为独立函数,并新增对应的单元测试。

主要改动

  • 新增缓存读取能力: 在 quota.py 中新增 get_cached_used_bytes,按 user_id/group_id/bot_id 从 workspace_quota_usage 仓储读取 used_bytes,在仓储未装配、无记录、读取异常或字段非法(非整数、负数、布尔值)时返回 None;并在 workspace 能力包的 __init__.py 中导出该函数。
  • 企业版 usage 走缓存优先: workspace.usage 处理逻辑在非本地工作区场景下,若未请求强制刷新且缓存命中,则调用 resolve_quota_snapshot / snapshot_to_dict 组装结果并通过 send_response 直接返回,不再调用 _forward_to_agent,从而避免干扰空闲 Pod 的回收计时。
  • 统一 force_refresh 解析: 抽出 _force_refresh_requested,统一支持布尔、整数 1 及 "1"/"true"/"yes"/"on" 等字符串形式,并让 _local_usage_bytes 复用该函数,替代原有内联判断。
  • 身份变量完整传递: _usage 中 _bind_trusted_identity 的返回值由忽略 agent_id 改为保留为 aid,供缓存命中分支构造配额快照与响应时使用。
  • 新增单元测试: 新增 tests/unit_tests/enterprise/test_workspace_usage_cache_read.py,覆盖 _force_refresh_requested 多种入参、get_cached_used_bytes 的命中/未命中/非法值,以及企业版缓存命中时不转发 AS 的行为。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
1 day ago Comments:

代码审查

审查完成,各文件结论:

  • agent_gateway/channel_manager/web/web_method_register/workspace.py:_force_refresh_requested 提取后语义与旧实现等价(is True or == 1 + 字符串白名单一致),_local_usage_bytes 行为未变;身份仍经 _bind_trusted_identity 取受信值,缓存键不可被客户端篡改,无越权读取风险。发现 1 项 P3(payload 组装逻辑重复)。
  • agent_gateway/enterprise/capabilities/workspace/__init__.py:导出与 __all__ 同步、顺序与既有风格一致,无问题。
  • agent_gateway/enterprise/capabilities/workspace/quota.py:新增读取函数键与建表唯一键、upsert_usage_cache 一致,异常一律降级为 None(回落 forward,无误报成功);发现 1 项 P2(无 reported_at 新鲜度/TTL 校验)。
  • tests/unit_tests/enterprise/test_workspace_usage_cache_read.py:发现 2 项 P3(handler 取键兜底可能挑错 handler;force_refresh / 缓存未命中两条 forward 分支无回归用例)。

汇总:P0 0 项,P1 0 项,P2 1 项,P3 3 项。

整体风险判断:改动方向正确(把 5 分钟轮询从 forward 改为读 Gateway 缓存,避免 route/touch 干扰 Pod 老化),未引入安全或数据一致性问题;主要遗留风险是缓存没有新鲜度上限,用量条在 Agent 侧文件变更后可能长期显示过期值(含误显示 block 导致前端隐藏上传入口),建议合入前补上基于 reported_at 的 TTL 与对应 force_refresh/未命中用例。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
Incorrect?
atomgit-bot
atomgit-bot
1 day ago Comments:

AtomGit AI 助手使用指南

在 PR 评论中使用命令即可触发。支持在普通评论和代码行评论中使用。

功能 命令 说明
代码审查 /ai review 检查代码质量、潜在问题、安全风险
PR 摘要 /ai summary 生成 PR 变更内容的结构化摘要
代码解释 /ai explain 解释代码变更的逻辑和意图
自由提问 @atomgit-bot <问题> 询问关于本 PR 的任何问题
帮助 /ai help 显示此帮助信息

免责声明

AI 助手可能存在误判,请结合自身判断。可以对评论点 👍 或 👎 帮助我们改进。

likedislike
Incorrect?
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: sig/sig-agent-gateway
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

欢迎来到 openJiuwen 社区

Hey @hanjiahui , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。

联系指引

有疑问可以联系 SIG: sig-agent-gateway ,
维护者是: @YiyangShao, @mingo007 ,
审核者是: @d-0771, @gcw_hx9prznZ, @yechenzhen, @yukuanj .

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: openJiuwen-cla/yes
openjiuwen-syncopenjiuwen-syncCollaborators
1 day ago Modify description of pull request
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

The pipeline(pipeline number:485) is running. Please wait a moment...

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-running
atomgit-bot
atomgit-bot1 day agoStart code review2
agent_gateway/enterprise/capabilities/workspace/quota.py
@@ -287,0 +321,4 @@
321+ raw = existing.get("used_bytes")
322+ if isinstance(raw, bool) or not isinstance(raw, int) or raw < 0:
323+ return None
324+ return raw
atomgit-bot
atomgit-bot1 day agoComments:

🟡 Medium Priority

变更行:quota.py 新增 get_cached_used_bytes(319-324 行)只校验 used_bytes 类型/非负,完全忽略行内已有的 reported_at,也没有任何 TTL 上限;workspace.py 655-675 行据此直接返回。

影响链路:改动前企业版每次 workspace.usage 都会 forward 到 AS 取实时值;改动后只要 workspace_quota_usage 里有行就永久返回该行。而该表仅由 Gateway 侧 workspace.tree(611)、entries.delete(764/776)、_usage_payload_from_used(534) 写入,workspace.usage 轮询本身不再刷新(缓存命中分支跳过了 upsert,也跳过 forward)。

失败模式:Agent 在 Pod 内自行写入/清理工作区文件(唯一不经过 Gateway 的路径)后,缓存既不更新也不过期,前端 5 分钟轮询会一直拿到旧值:①旧值偏高(例如已清理但缓存仍是接近上限)→ 响应 status="block"、percent≈100,前端按 block 状态会隐藏/阻断上传入口,用户实际空间充裕却无法上传,直到手动 force_refresh;②旧值偏低 → 接近配额告警/block 完全不提示,用户在写入时被 AS 侧硬拦。该偏差在没有 TTL 的情况下无上限,属于本改动引入的功能性回退(原实现每次都拿实时值)。

建议:在 get_cached_used_bytes 中增加基于 reported_at 的新鲜度上限(例如模块常量 _CACHE_MAX_AGE_SECONDS,可取 300~900 秒并允许环境变量覆盖):解析 existing.get("reported_at"),兼容 naive datetime(视为 UTC),若 (_utc_now() - reported_at).total_seconds() > _CACHE_MAX_AGE_SECONDS 则返回 None,让调用方回落 forward,从而把"读缓存"限定为"仅读较新的缓存",过期后仍能刷新一次真实用量。

likedislike
Incorrect?
hjh0606
hjh0606Collaborators
1 day ago Comments:

符合设计预期,非问题

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Removed label: ci-running
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:
任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ❌FAILED 点此跳转
ST测试 ✅SUCCESS 点此跳转
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 ❌FAILED N/A
pr size检查 ✅SUCCESS N/A
likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-failed
5 event messages are collapsed here。 See more
hjh0606hjh0606Collaborators
1 day ago Resolved final issue
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

CLA 签署成功

hanjiahui, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

The pipeline(pipeline number:485) is running. Please wait a moment...

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Removed label: ci-failed
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-running
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Removed label: ci-running
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:
任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 ✅SUCCESS 点此跳转
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 ✅SUCCESS N/A
pr size检查 ✅SUCCESS N/A
likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-successful