feat(library): 死信按精确指纹解除,保留上游记录
build / build (push) Successful in 1m26s

上游 task_dead_letters 没有解决标记,purge/DELETE 会销毁"失败过"的唯一证据。
治理侧新增解除记录(迁移 073):绑定实时行逐字节指纹、重放引用、源侧观测摘要与
操作人;行被改过、重放后又出现新死信、源侧未结清都不命中,封存照旧阻断。
指纹先压紧空白再摘要,避免 PG to_jsonb::text 与 json.Marshal 的空白差异。

试过:go test ./internal/{librarydeadletter,libraryagent,httpapi,releaseadapter/v071,
platform/postgres},make verify 15 项全绿。
This commit is contained in:
姚章浩
2026-10-29 06:55:13 +08:00
parent 958292d1ff
commit c2402947a9
22 changed files with 1267 additions and 23 deletions
+21
View File
@@ -63,6 +63,9 @@ type request struct {
Action string `json:"action"`
Scope string `json:"scope,omitempty"`
Job string `json:"job,omitempty"`
// Released 只允许精确死信指纹(sha256:<64 hex>)。它不改变固定命令形态:
// 还能传的只有"这些确切的行已由治理平台审计解除",CLI 会逐字节复核。
Released []string `json:"released,omitempty"`
}
type response struct {
@@ -76,6 +79,11 @@ type response struct {
var jobPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
// 死信解除只能按精确指纹传入,不接受通配、实例或任务类型级解除。
var fingerprintPattern = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
const maxReleasedDeadLetters = 64
// Agent serves capture requests. Only one operation runs at a time: a library
// capture owns the writer group, so overlapping requests would race the window.
type Agent struct {
@@ -214,6 +222,19 @@ func (a *Agent) execute(ctx context.Context, req request) response {
if req.Job != "" {
args = append(args, "-job", req.Job)
}
if len(req.Released) > 0 {
if len(req.Released) > maxReleasedDeadLetters {
out.Error = "too many released dead-letter fingerprints"
return out
}
for _, fingerprint := range req.Released {
if !fingerprintPattern.MatchString(fingerprint) {
out.Error = "released dead letters must be sha256:<64 hex> fingerprints"
return out
}
}
args = append(args, "-released-dead-letters", strings.Join(req.Released, ","))
}
default:
out.Error = "unsupported action"
return out
+29
View File
@@ -380,3 +380,32 @@ func TestAgentPinsTheHostSideObjectEndpoint(t *testing.T) {
}
}
}
// 死信解除只能按精确指纹透传:形状不对就拒绝,且固定命令其余部分不受影响。
func TestAgentForwardsOnlyExactDeadLetterFingerprints(t *testing.T) {
agent, socket, calls := testAgent(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() { _ = agent.Serve(ctx) }()
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if conn, err := net.Dial("unix", socket); err == nil {
conn.Close()
break
}
time.Sleep(10 * time.Millisecond)
}
fingerprint := "sha256:" + strings.Repeat("a", 64)
ok := roundTrip(t, socket, `{"action":"capture","scope":"internal","job":"j1","released":["`+fingerprint+`"]}`)
if !ok.OK {
t.Fatalf("exact fingerprint refused: %s", ok.Error)
}
last := calls()[len(calls())-1]
if !strings.Contains(last, "-released-dead-letters "+fingerprint) {
t.Fatalf("fingerprint not forwarded: %s", last)
}
bad := roundTrip(t, socket, `{"action":"capture","scope":"internal","job":"j2","released":["*"]}`)
if bad.OK || !strings.Contains(bad.Error, "fingerprints") {
t.Fatalf("wildcard release accepted: %+v", bad)
}
}
+13 -4
View File
@@ -29,20 +29,29 @@ type Result struct {
// Capture asks the agent to seal one library scope. The job id makes the call
// idempotent: repeating it returns the already sealed candidate.
func Capture(ctx context.Context, config ClientConfig, scope, job string) (Result, error) {
return call(ctx, config, map[string]string{"action": "capture", "scope": scope, "job": job})
return CaptureReleased(ctx, config, scope, job, nil)
}
// CaptureReleased 在 Capture 之上带上治理平台已审计的精确死信解除指纹。
func CaptureReleased(ctx context.Context, config ClientConfig, scope, job string, released []string) (Result, error) {
request := map[string]any{"action": "capture", "scope": scope, "job": job}
if len(released) > 0 {
request["released"] = released
}
return call(ctx, config, request)
}
// Status probes the agent (source wiring, writer group, archive usage).
func Status(ctx context.Context, config ClientConfig) (Result, error) {
return call(ctx, config, map[string]string{"action": "status"})
return call(ctx, config, map[string]any{"action": "status"})
}
// Recover restores writers from an interrupted freeze journal.
func Recover(ctx context.Context, config ClientConfig) (Result, error) {
return call(ctx, config, map[string]string{"action": "recover"})
return call(ctx, config, map[string]any{"action": "recover"})
}
func call(ctx context.Context, config ClientConfig, request map[string]string) (Result, error) {
func call(ctx context.Context, config ClientConfig, request map[string]any) (Result, error) {
if config.Socket == "" {
return Result{}, errors.New("library agent socket is not configured")
}