Files
HomeAgent/scripts/migrate-plugins-to-org.sh
JianFeeeee 81a0f5f598 feat(security): 插件按工具声明内容策略 + 目录名统一
## 一、插件声明层(用户裁定)

> 「敏感词策略也应该是插件声明啊,每个插件声明自己轮次以及自己工具的
>   敏感词策略」

SecurityProfile 新增两个字段:

  ToolSensitiveWords map[string][]string  // key=工具名模式(尾 *),value=词表
  DetectInjection    bool                 // 开启内核内置注入迹象识别

语义是**追加不覆盖**:整轮 SensitiveWords 仍然生效。
取宽不取窄的理由:当前处置是 B 档(标记+警示,不阻断),
多标一条只是多一句警示,漏标则是安全缺口。

## 二、修掉我自己造的「莲藕式分层」

前一版把 *security.Round 从 runInputTask 一路搬到工具链
(prepareInputTask(evt, round) → TaskFrame.SecRound →
executeToolCallOutcomeWithRound),并在 rtchild 里另补一条。

那是把**分层策略实现成跨层传参**:每多一条工具执行路径就要求调用方
多传一次句柄。profileActivator 的设计注释早就写明了正路:

> 工具调用发生在 LLM 决定之后,此时插件已不在调用栈上。
> 唯一可靠的传递方式是**内核在输入进来时记住**,在该轮的工具判定里使用。

改为 security 自取:

  Runtime.ScanContentForAgent(agentID, isChild, ins, text)
    → ForAgent(agentID, isChild)   // per-agent 视图,天然不串档
    → EffectiveProfile()           // session > longtime
    → 合并 全局词表 + 档位词表 + 工具词表 + DetectInjection

core 只递交**事实**(我是谁 / 哪个工具 / 什么文本),不认识 Round。
与 CheckToolForAgent 同一套身份推断,不引入第二个入口语义。
TaskFrame.SecRound / executeToolCallOutcomeWithRound 全部撤除。

## 三、目录名统一

third_party/homeagent-sdk → third_party/hologramagentsdk

module 与远端仓早已是 hologramagentsdk,只剩物理目录是遗留名。
同步改了 go.mod replace、构建/打包/部署脚本、5 处测试里的分段路径。
注意:这里改的是**主仓 vendored 的 SDK 源码路径**,example/ 由 SDK
独立仓自己跟踪,主仓不涉及。

## 判据

TestToolSensitiveWordsAppendOnlyForThatTool   词表只对目标工具生效 + 追加语义
TestToolSensitiveWordsUseAgentScope           security 按 agent scope 自取生效
(变异验证:删掉 ForAgent 查询 → 红)

## 验证

	go build ./...                 → 0
	go vet ./...                   → 0
	go test ./... -count=1 -race   → 62 ok / 0 FAIL / 0 DATA RACE
	gofmt -l internal/             → 干净
2026-11-03 09:51:11 +08:00

180 lines
5.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# 把 SDK 仓 example/ 下的示例插件迁移到 GitHub 组织下成为**独立仓库**。
#
# 用法:
# scripts/migrate-plugins-to-org.sh [--dry-run] [--only name1,name2]
#
# 环境:
# GH_ORG_TOKEN fine-grained PAT,Resource owner = 目标组织,
# 带 Repository permissions: Administration(Write) 或
# Repository creation(Write) + Contents(Write)
# GH_ORG 目标组织名(默认 HologramAgent)
#
# 为什么这么做:
# 插件散落在 SDK 仓 example/ 下有三个问题:
# ① 插件版本与 SDK 版本绑死(一次 SDK 发版把所有插件都带上)
# ② 插件作者不能单独为自己的插件开发/发版(都在别人的仓里)
# ③ plugin_update 需要「仓库地址 → 拉源码 → 编译」,而
# example/ 是别人的仓库,不是插件自己的仓库
#
# 纪律:
# 1. 默认 --dry-run:只打印会做什么,不碰 GitHub。
# 2. 产物不入库:dist/ build/ rawfile/ node_modules/ *.hmap 全部排除
# (实测源码 3MB,产物 1.9G —— 后者进了仓库就再也删不干净)
# 3. 测试与 go.sum 保留:它们是仓库的一部分,不是产物。
set -euo pipefail
DRY=0
ONLY=""
for arg in "$@"; do
case "$arg" in
--dry-run) DRY=1 ;;
--only)
shift
ONLY="${1:-}"
;;
*)
echo "未知参数: $arg" >&2
exit 2
;;
esac
shift || true
done
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
EXAMPLES="$ROOT/third_party/hologramagentsdk/example"
GH_ORG="${GH_ORG:-HologramAgent}"
TOKEN="${GH_ORG_TOKEN:-}"
if [ "$DRY" -eq 0 ] && [ -z "$TOKEN" ]; then
echo "ERROR: 非 dry-run 需要 GH_ORG_TOKEN" >&2
exit 2
fi
api() {
# $1=method $2=path $3=payload(optional)
local method="$1" path="$2" payload="${3:-}"
if [ -n "$payload" ]; then
curl -sS -X "$method" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com${path}" -d "$payload"
else
curl -sS -X "$method" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com${path}"
fi
}
created=0
pushed=0
skipped=0
for dir in "$EXAMPLES"/*/; do
name="$(basename "$dir")"
if [ -n "$ONLY" ]; then
case ",$ONLY," in *",$name,"*) ;; *) continue ;; esac
fi
# 入口判据:Go 插件是 plugin.go,Lua 插件是 main.lua(与 build-examples.sh 同)
if [ ! -f "$dir/plugin.go" ] && [ ! -f "$dir/main.lua" ]; then
continue
fi
desc=$(python3 -c "import json,sys;print(json.load(open('$dir/plg.json')).get('description',''))" 2>/dev/null || echo "")
ver=$(python3 -c "import json,sys;print(json.load(open('$dir/plg.json')).get('version',''))" 2>/dev/null || echo "")
echo ""
echo "── $name (v$ver) ──"
echo " desc: ${desc:0:60}"
if [ "$DRY" -eq 1 ]; then
echo " [dry-run] 将创建仓库 $GH_ORG/$name 并推送源码(排除 dist/build/rawfile/node_modules)"
continue
fi
# ① 创建仓库(已存在则跳过 —— 幂等,重跑安全)
code=$(curl -s -o /tmp/ghresp -w "%{http_code}" -X POST \
-H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" \
https://api.github.com/orgs/$GH_ORG/repos \
-d "{\"name\":\"$name\",\"private\":false,\"description\":\"$desc\",\"has_issues\":true}")
if [ "$code" = "201" ]; then
echo " ✓ 仓库已创建"
created=$((created + 1))
elif [ "$code" = "422" ]; then
echo " · 仓库已存在(跳过创建)"
skipped=$((skipped + 1))
else
echo " ✗ 创建失败 HTTP=$code: $(head -c 200 /tmp/ghresp)"
continue
fi
# ② 准备干净的源码副本
#
# 用 tar 管道而不是 rsync/cp -r:rsync 在最小化系统里常常没有
# (本机实测就是),而 cp -r 无法精确排除多类产物。
# --exclude 用**不带前导斜杠**的形态,匹配任意层级同名目录。
work=$(mktemp -d)
mkdir -p "$work/$name"
(cd "$dir" && tar -cf - \
--exclude='dist' --exclude='build' --exclude='rawfile' \
--exclude='node_modules' --exclude='*.hmap' --exclude='.git' \
.) | (cd "$work/$name" && tar -xf -)
# plg.json 里补 repository 字段,让装好的插件能反过来找到自己的仓库
# (plugin_update 靠它拉源码;不填的话插件永远无法自动更新)
python3 - "$work/$name/plg.json" "https://github.com/$GH_ORG/$name" <<'PY'
import json, sys
path, repo = sys.argv[1], sys.argv[2]
d = json.load(open(path))
d["repo"] = repo
json.dump(d, open(path, "w"), indent=2, ensure_ascii=False)
open(path, "a").write("\n")
PY
# 每个独立仓库都要有自己的 README 与 LICENSE 指引
cat >"$work/$name/README.md" <<EOF
# $name
$desc
HologramAgent 插件,随 SDK \`${ver:-?}\` 的示例插件拆分而来。
## 构建
\`\`\`bash
hmapdev build
\`\`\`
产物 \`dist/${name}_bundle.hmap\` 经 \`plugin_install\` 安装。
## 自动更新
本仓库地址登记在 \`plg.json\` 的 \`repo\` 字段,安装后随产物写入
\`plugin.json\` 的 \`repository\` 字段 —— 内核的 \`plugin_update\` 靠它
从本仓库拉最新源码并重编安装。
EOF
# ③ 推送
(
cd "$work/$name"
git init -q
git checkout -q -b main
git add -A
git -c user.name="$GH_ORG" -c user.email="$GH_ORG@users.noreply.github.com" \
commit -q -m "feat: 从 SDK example/ 迁移为独立仓库
保留全部源码与测试(dist/build/rawfile/node_modules 等产物不入库)。
plg.json 补 repo 字段,使 plugin_update 能定位本仓库。"
git remote add origin "https://x-access-token:$TOKEN@github.com/$GH_ORG/$name.git"
git push -q origin main 2>&1 | tail -1 || echo " ✗ 推送失败(见上)"
)
echo " ✓ 已推送"
pushed=$((pushed + 1))
rm -rf "$work"
done
echo ""
echo "=== 完成 ==="
echo " 创建仓库: $created / 已存在: $skipped / 推送成功: $pushed"