JianFeeeee
82932821c0
feat(security): 每 agent 一个策略实例 —— sessionProfile / longtimeProfile 两层
## 用户裁定
> 每个 agent 一个,分为 sessionProfile 和 longtimeProfile
## 收敛前的缺陷
档位挂在 `Runtime.active`(**进程级单例**)上,多 agent 并发时串档:
主 agent 轮次 A:注入 qq.guest → active = "qq.guest"
驻留子 轮次 B:注入 qq.owner → active = "qq.owner" ← 覆盖 A
主 agent 轮次 A 继续执行工具 → 判定按 owner 走 ← 串档
这不是理论风险:驻留子 / rtchild 与主 agent **同进程并发**(plan.md 十一)。
## 切分原则:共享注册表 + per-agent 执行视图
**不能整份复制 Runtime** —— `profiles` / `principals` / `scopes` 是插件
声明出来的注册表,每个 agent 各持一份就会变成「插件往 A 声明了档位,
B 看不见」,那是最难查的一类缺陷。
共享(进程级 1 份) per-agent(N 份)
──────────────────────── ───────────────────
gate / policy / content sessionProfile ← 轮次级
profiles / principals longtimeProfile ← agent 生命期
scopes / eventPolicy
## 两层档位语义(用户裁定)
sessionProfile —— 属于**这一轮**。输入携带它,End() 即清。
严格 per-round:同一用户下一条消息要重新声明。
longtimeProfile —— 属于**这个 agent 的一段生命**。设置后跨轮保留,
直到显式改掉或 agent 销毁。
**优先级 session > longtime**,这个顺序本身是安全语义:
session 是"这一轮的事实",必须能盖住 long-time 默认值 ——
否则访客发一条消息也无法在长期为 owner 的 agent 上收窄权限。
反向(longtime 盖 session)= 提权:长期 owner 的 agent 上,
访客声明的 guest 被吞掉 ⇒ 访客拿到 owner 权限。
## 改动
① 新增 `internal/security/agentscope.go`:
rt.ForAgent(agentID, isChild) *AgentScope // 幂等
sc.BeginRound(RoundInput) // security 自己解析档位
sc.SetLongtimeProfile(name) / LongtimeProfile()
sc.EffectiveProfile() // session > longtime
sc.CheckTool(tool) // 全局基线 + 档位 + 主体策略
rt.ReleaseAgent(agentID) // 含 longtime 档一起清
② `Runtime` 新增 `scopesByAgent` 表;判定拆出 `checkToolWithProfile(p, profile, tool)`
与 `checkToolForWithProfile(...)` —— **档位由调用方显式传入**,
不再一律读全局 `r.active`(否则 per-agent 视图白做)。
③ `agent/core`:`securityScope()` 用 `sync.Once` 缓存本 agent 的视图;
`Stop()` 里 `ReleaseSecurityScope()`。
④ `Round.End()`:per-agent 路径只清 **session**,**保留** longtime;
旧进程级路径仍恢复进入前的值。
## 判据(3 个变异全部实测咬住)
| 判据 | 变异 |
|---|---|
| `TestAgentScopesDoNotShareSessionProfile` | ForAgent 退化为共享视图 ⇒ 红 |
| `TestLongtimeProfileSurvivesRounds` | 优先级写反 / longtime 不跨轮 ⇒ 红 |
| `TestReleaseAgentDropsLongtimeProfile` | ReleaseAgent 不删视图 ⇒ 红 |
| `TestLongtimeProfileRejectsUndeclared` | 未声明档位被接受 ⇒ 红 |
第一条同时钉:① 各自档位是自己的 ② **行为上**主 agent 仍被 guest 收窄、
子 agent 不受影响(只断言字符串相等会漏掉判定链没接上的情形)。
## 实测
- 真实并发探针:3 个 agent(guest / owner / guest)各 200 轮并发,
`-race` 干净,**零串档**
- build / vet / go test ./...(62 ok / 0 fail)/ -race 全绿
## 未变的约束
档位与主体策略**只能收窄**:`CheckToolFor` 内部先跑全局策略,
所以父的黑名单对子依然有效,子无法提权。
2026-10-31 07:24:03 +08:00
..
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-31 07:24:03 +08:00
2026-10-29 23:53:48 +08:00
2026-10-22 20:49:44 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-17 19:18:24 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-30 17:00:47 +08:00
2026-10-27 15:53:49 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-26 20:55:25 +08:00
2026-10-26 20:55:25 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-26 20:55:25 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-30 17:00:47 +08:00
2026-10-29 23:53:48 +08:00
2026-10-25 13:37:29 +08:00
2026-10-25 21:39:12 +08:00
2026-10-30 17:00:47 +08:00
2026-10-25 13:48:35 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-18 13:20:54 +08:00
2026-10-25 15:55:26 +08:00
2026-10-29 23:53:48 +08:00
2026-10-25 15:55:26 +08:00
2026-10-25 15:55:26 +08:00
2026-10-29 23:53:48 +08:00
2026-10-25 15:55:26 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-26 20:55:25 +08:00
2026-10-27 12:16:06 +08:00
2026-10-21 12:17:19 +08:00
2026-10-27 15:40:43 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 15:55:26 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 17:03:09 +08:00
2026-10-26 20:55:25 +08:00
2026-10-25 17:03:09 +08:00
2026-10-22 22:42:02 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-26 20:55:25 +08:00
2026-10-21 12:17:19 +08:00
2026-09-19 16:49:16 +08:00
2026-10-29 23:53:48 +08:00
2026-10-30 17:00:47 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-26 20:55:25 +08:00
2026-10-29 23:53:48 +08:00
2026-10-23 15:51:02 +08:00
2026-10-27 11:49:26 +08:00
2026-10-30 17:00:47 +08:00
2026-10-23 15:51:02 +08:00
2026-10-21 12:17:19 +08:00
2026-10-06 09:22:12 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-23 15:51:02 +08:00
2026-10-30 17:00:47 +08:00
2026-10-21 12:17:19 +08:00
2026-10-22 20:49:44 +08:00
2026-10-31 06:35:32 +08:00
2026-10-31 06:35:32 +08:00
2026-10-21 12:17:19 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 17:00:47 +08:00
2026-10-30 21:54:40 +08:00
2026-10-31 06:35:32 +08:00
2026-10-31 07:14:09 +08:00
2026-10-25 18:25:37 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-23 15:51:02 +08:00
2026-09-13 08:59:15 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-26 20:55:25 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-09-13 07:00:51 +08:00
2026-10-24 14:34:24 +08:00
2026-10-21 12:17:19 +08:00
2026-10-27 15:58:55 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-31 07:14:09 +08:00
2026-10-31 07:24:03 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-30 17:00:47 +08:00
2026-10-26 20:55:25 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 15:55:26 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 10:10:21 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-31 07:14:09 +08:00
2026-10-27 16:24:09 +08:00
2026-10-27 15:27:35 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-18 06:38:51 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-23 15:51:02 +08:00
2026-10-29 23:53:48 +08:00
2026-10-26 20:55:25 +08:00
2026-10-29 23:53:48 +08:00
2026-10-30 17:00:47 +08:00
2026-10-31 06:35:32 +08:00
2026-10-21 12:17:19 +08:00
2026-10-29 23:53:48 +08:00
2026-10-29 23:53:48 +08:00
2026-10-23 15:51:02 +08:00
2026-10-21 12:17:19 +08:00
2026-10-05 11:25:20 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 20:16:16 +08:00
2026-10-25 20:16:16 +08:00
2026-10-26 20:55:25 +08:00
2026-10-29 23:53:48 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-21 12:17:19 +08:00
2026-10-25 18:25:37 +08:00
2026-10-21 12:17:19 +08:00