fix(library): 期望执行器身份随 capture 请求送达并在执行前核对
build / build (push) Successful in 1m27s

原来 worker 先 health 再 capture,两次拨号之间 socket 可能被换成另一个实例的
执行器:错的 agent 已经把写入者停了、快照也出了,事后 postcheck 只能拒绝登记,
副作用无法撤销。

现在 capture 请求自带期望身份(治理侧配置生成:project 必填,source_app /
archive_root 配了就绑定)与覆盖三项的稳定指纹;接收方在操作互斥内、执行任何源
操作之前比对,缺字段、指纹与字段不一致、project/source_app 不符都直接拒绝,
**错的执行器一次 capture 都不跑**。封存结果回显该指纹,worker 事后复核按它对照,
postcheck 只作为证据防线。恢复路径不要求期望(它只把写入者恢复回来)。

负例是确定性的:两个真实 agent 各占一个 socket,把调用方指向错的那个,断言它的
capture 调用为 0 且仍能回答 health;同一份期望打到对的 socket 则只跑一次。
This commit is contained in:
姚章浩
2026-10-29 13:23:56 +08:00
parent 5d42cba6c6
commit 6ed06357f9
12 changed files with 382 additions and 161 deletions
+21 -4
View File
@@ -66,6 +66,9 @@ type request struct {
// Released 只允许精确死信指纹(sha256:<64 hex>)。它不改变固定命令形态:
// 还能传的只有"这些确切的行已由治理平台审计解除",CLI 会逐字节复核。
Released []string `json:"released,omitempty"`
// Expect 是调用方对执行器身份的期望。capture 必须在操作互斥内、任何源操作
// 之前比对它;缺失即拒绝(fail closed),不靠"事后核对"兜底。
Expect *Expectation `json:"expect,omitempty"`
}
type response struct {
@@ -255,6 +258,16 @@ func (a *Agent) execute(ctx context.Context, req request) response {
out.Error = "invalid job id"
return out
}
if req.Expect == nil {
out.Error = "capture 请求没有携带期望执行器身份,拒绝执行"
return out
}
// 此时已持有操作互斥(a.mu):从"比对身份"到"真正跑子进程"之间没有别的
// 请求能插进来,也不会被另一个 agent 抢走——这是零副作用的前提。
if err := a.config.matchExpectation(*req.Expect); err != nil {
out.Error = err.Error()
return out
}
args = []string{"capture", "-scope", req.Scope}
if req.Job != "" {
args = append(args, "-job", req.Job)
@@ -317,19 +330,23 @@ func (a *Agent) execute(ctx context.Context, req request) response {
}
// 封存结果必须带上"这次是谁执行的":调用方据此复核,防止心跳与执行之间
// socket 被换成另一个 agent(真正的封错库事故就是这样发生的)。
if req.Action == "capture" {
out.Payload = a.attachIdentity(out.Payload)
if req.Action == "capture" && req.Expect != nil {
out.Payload = a.attachIdentity(out.Payload, *req.Expect)
}
return out
}
// attachIdentity 把执行器身份并进封存结果(保留 CLI 原有的字段,如 snapshot_id)。
func (a *Agent) attachIdentity(payload json.RawMessage) json.RawMessage {
// expectation_fingerprint 是"本执行器实际核对过的那份期望"的指纹:调用方据此确认
// 执行时看到的期望与它发出的一致,而不是被替换后重新生成的。
func (a *Agent) attachIdentity(payload json.RawMessage, expectation Expectation) json.RawMessage {
var document map[string]any
if err := json.Unmarshal(payload, &document); err != nil {
return payload
}
document["agent_identity"] = a.config.identity()
identity := a.config.identity()
identity["expectation_fingerprint"] = expectation.Fingerprint
document["agent_identity"] = identity
encoded, err := json.Marshal(document)
if err != nil {
return payload