perf(skills): reduce common-path context loading

This commit is contained in:
Dennis
2026-07-30 15:17:48 +08:00
parent 9aa76ea748
commit 158e7ec701
24 changed files with 1543 additions and 157 deletions
+61 -15
View File
@@ -1,13 +1,15 @@
#!/usr/bin/env python3
"""Generate shortcut sections for DWS skills.
"""Generate shortcut discovery sections for DWS skills.
The skill should teach agents which high-level shortcut entries are available
in the public catalog and Runtime Schema. Leaf Schema publishes the Agent
contract, while leaf `--help` remains the source of truth for accepted flags.
without forcing large product catalogs into every common task. Leaf Schema
publishes the Agent contract, while leaf `--help` remains the source of truth
for accepted flags.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
@@ -47,6 +49,13 @@ MONO_END = "<!-- VISIBLE_SHORTCUTS_OVERVIEW_END -->"
PRODUCT_START = "<!-- VISIBLE_SHORTCUTS_START -->"
PRODUCT_END = "<!-- VISIBLE_SHORTCUTS_END -->"
# Large, high-frequency product skills should route known intents directly and
# keep their full shortcut inventory in Runtime Catalog/Schema. Add services
# here only after verifying that the product skill has its own reviewed routing
# section and intent table; compacting a sparse skill without an alternative
# route would make its shortcuts harder to discover.
COMPACT_PRODUCT_SERVICES = {"chat"}
def md_escape(value: Any) -> str:
text = str(value or "")
@@ -90,21 +99,24 @@ def mono_overview(items: list[dict[str, Any]]) -> str:
path = SERVICE_TO_SKILL.get(service)
skill = "—"
if path:
skill = next((part for part in path.parts if part.startswith("dingtalk-")), path.parent.name)
rows.append(f"| `{md_escape(service)}` | {count} | `{md_escape(skill)}` | `dws shortcut list --service {md_escape(service)} --format json` |")
skill = next((part for part in reversed(path.parts) if part.startswith("dingtalk-")), path.parent.name)
rows.append(f"| `{md_escape(service)}` | {count} | `{md_escape(skill)}` |")
body = "\n".join(rows)
return f"""{MONO_START}
## Shortcut 总览
下面统计当前公开 catalog 中的 shortcut。mono 模式不展开 200+ 行明细,避免 skill 过重;需要执行时先按产品路由,再用 `dws shortcut list --service <service> --format json` 读取参数、约束、风险和示例,最后用 `dws <service> +<shortcut> --help` 核对当前 Cobra flags。multi 模式的各产品 skill 会展开该产品的 shortcut 表。
下面只统计当前公开 catalog 中的 shortcut,不展开完整明细。已知意图应先按产品 Skill、意图表或任务 reference 选择唯一命令;命令已选中时直接执行,只在参数或安全语义不确定时读取 leaf Schema,在当前 Cobra flags 不确定时读取 leaf Help。仅当现有路由和 reference 都无法定位低频能力时,才用 `dws shortcut list --service <service> --format json` 做最后回退;不要为已知高频意图加载完整产品 Catalog。
| 服务 | shortcut 数 | multi skill | 发现命令 |
|---|---:|---|---|
| 服务 | shortcut 数 | multi skill |
|---|---:|---|
{body}
{MONO_END}"""
def product_section(service: str, rows: list[dict[str, Any]]) -> str:
if service in COMPACT_PRODUCT_SERVICES:
return compact_product_section(service, rows)
table = []
for item in rows:
table.append(
@@ -114,7 +126,7 @@ def product_section(service: str, rows: list[dict[str, Any]]) -> str:
return f"""{PRODUCT_START}
## Shortcuts(无专用脚本/recipe 时优先)
以下 shortcut 同时进入公开 catalog 与 Runtime Schema。先按本 skill 的意图表、脚本和 recipe 路由:存在精确覆盖该场景的专用脚本/recipe 时按其执行;否则用户意图命中时,shortcut 优先于手写原子命令。用 leaf Schema(例如 `dws schema --cli-path "{service} +<shortcut>" --format json`)读取 Agent 选择、参数、约束、风险和确认语义;用 `dws shortcut list --service {service} --format json` 批量发现;最后以 `dws {service} <shortcut> --help` 核对当前 Cobra flags。
以下 shortcut 同时进入公开 catalog 与 Runtime Schema。先按本 skill 的意图表、脚本和 recipe 路由:存在精确覆盖该场景的专用脚本/recipe 时按其执行;否则用户意图命中时,shortcut 优先于手写原子命令。命令已选中时直接执行;只在参数或安全语义不确定时读取 leaf Schema(例如 `dws schema --cli-path "{service} +<shortcut>" --format json`),在当前 Cobra flags 不确定时读取 `dws {service} <shortcut> --help`。仅当现有路由和 reference 都无法定位低频能力时,才用 `dws shortcut list --service {service} --format json` 批量发现。
| Shortcut | 风险 | 适用场景 |
|---|---|---|
@@ -122,17 +134,38 @@ def product_section(service: str, rows: list[dict[str, Any]]) -> str:
{PRODUCT_END}"""
def update_mono(items: list[dict[str, Any]]) -> None:
def compact_product_section(service: str, rows: list[dict[str, Any]]) -> str:
return f"""{PRODUCT_START}
## Shortcut 发现(按需)
`{md_escape(service)}` 当前有 {len(rows)} 条公开 shortcut,完整清单保留在 Runtime Catalog 与 Schema,不在高频产品根 Skill 中重复展开。已知意图直接使用下方的优先路由、意图表或任务 reference;命令已选中时直接执行,只在参数/安全语义不确定时读取 leaf Schema,在当前 Cobra flags 不确定时读取 leaf Help。
仅当现有路由和 reference 都无法定位低频能力时,才执行 `dws shortcut list --service {md_escape(service)} --format json` 做最后回退;不要为已知高频意图加载完整 Shortcut Catalog 或产品级 Schema。
{PRODUCT_END}"""
def apply_update(path: Path, text: str, updated: str, check: bool) -> bool:
if updated == text:
return False
if check:
print(f"generated skill drift: {path.relative_to(ROOT)}", file=sys.stderr)
else:
path.write_text(updated, encoding="utf-8")
return True
def update_mono(items: list[dict[str, Any]], check: bool) -> list[Path]:
text = MONO_SKILL.read_text(encoding="utf-8")
block = mono_overview(items)
updated = replace_block(text, MONO_START, MONO_END, block, "## 产品总览")
MONO_SKILL.write_text(updated, encoding="utf-8")
return [MONO_SKILL] if apply_update(MONO_SKILL, text, updated, check) else []
def update_product_skills(items: list[dict[str, Any]]) -> None:
def update_product_skills(items: list[dict[str, Any]], check: bool) -> list[Path]:
by_service: dict[str, list[dict[str, Any]]] = defaultdict(list)
for item in items:
by_service[item["service"]].append(item)
changed = []
for service, path in SERVICE_TO_SKILL.items():
if service not in by_service:
continue
@@ -142,13 +175,26 @@ def update_product_skills(items: list[dict[str, Any]]) -> None:
block = product_section(service, by_service[service])
anchor = "## 概念地图" if service == "devapp" else "## 意图表"
updated = replace_block(text, PRODUCT_START, PRODUCT_END, block, anchor)
path.write_text(updated, encoding="utf-8")
if apply_update(path, text, updated, check):
changed.append(path)
return changed
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
"--check",
action="store_true",
help="verify generated skill sections are current without rewriting files",
)
args = parser.parse_args()
items = collect_visible()
update_mono(items)
update_product_skills(items)
changed = update_mono(items, args.check)
changed.extend(update_product_skills(items, args.check))
if args.check and changed:
print("run: python3 scripts/gen_skill_shortcut_sections.py", file=sys.stderr)
return 1
print(f"visible_shortcuts={len(items)} services={len(set(item['service'] for item in items))}")
return 0
+41
View File
@@ -0,0 +1,41 @@
#!/bin/sh
set -eu
ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)"
cd "$ROOT"
python3 scripts/gen_skill_shortcut_sections.py --check
chat_skill="skills/multi/dingtalk-chat/SKILL.md"
mono_skill="skills/mono/SKILL.md"
chat_max_bytes=14000
chat_bytes="$(wc -c < "$chat_skill" | tr -d ' ')"
if [ "$chat_bytes" -gt "$chat_max_bytes" ]; then
printf '%s\n' \
"skill context budget exceeded: $chat_skill is ${chat_bytes} bytes (max ${chat_max_bytes})" >&2
exit 1
fi
shortcut_rows="$(
awk '
/<!-- VISIBLE_SHORTCUTS_START -->/ { in_block = 1; next }
/<!-- VISIBLE_SHORTCUTS_END -->/ { in_block = 0 }
in_block && /^\| `dws chat \+/ { count++ }
END { print count + 0 }
' "$chat_skill"
)"
if [ "$shortcut_rows" -ne 0 ]; then
printf '%s\n' \
"skill context budget exceeded: $chat_skill re-expanded $shortcut_rows shortcut rows" >&2
exit 1
fi
if grep -Fq "充分阅读产品参考文件" "$mono_skill"; then
printf '%s\n' \
"skill context budget regression: $mono_skill requires full product-reference loading" >&2
exit 1
fi
printf '%s\n' \
"skill context budget: ok (chat_bytes=$chat_bytes max=$chat_max_bytes shortcut_rows=$shortcut_rows)"