mirror of
https://github.com/hanzoai/framework.git
synced 2026-11-23 05:50:47 +00:00
Turns the agentic-marketing content loop from scaffolding into a working edge pair, then closes every finding from the adversarial review. Edges (wired at Mount, fail-closed until configured): - Generator: zen5 copy via deps.AI.ChatCompletion (metered Bill.Gate/MeterUsage) + studio assets via the ComfyUI Qwen-Image-Edit-2511 graph. - Distributor: hanzoai/social Public API fan-out, per-brand key custodied in KMS. errNotConfigured until a brand connects a key — no key, no fan-out. Hardening: - Publish TOCTOU interlock: a per-item, store-backed lease (framework fw_locks, the ONE cross-process coordination primitive) serializes concurrent publishes of the same item across drivers and pods; non-idempotent fan-out runs once. - source_media SSRF gate: the URL validator fails closed on unparseable/backslash input. - external_ids is server-managed: the before_save guard rejects a client write; Publish records it under the lease as a trusted server write. Adversarial regression suite pins each guarantee from the outside (raw framework PUT, direct ops, concurrency): red_adversarial / red_rereview / red_final / red_lease_final, plus blue_hardening and per-file unit tests. One open item recorded as a HARD GATE in clients/content/LLM.md: lease TTL-preemption re-opens the double-post window for a brand with ~15+ slow channels (fan-out (N+1)x20s > 5m TTL, external_ids recorded only at fan-out end). ZERO exposure until a brand connects a social key; the fix (lease heartbeat/renew preferred) MUST land in the same change that connects the first real key. Co-authored-by: Hanzo Dev <[email protected]>
117 lines
3.8 KiB
Go
117 lines
3.8 KiB
Go
package framework
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// lock_test.go proves the store lease primitive at the level Publish depends on:
|
|
// exactly one holder at a time, a LIVE lease is never stolen, an EXPIRED lease is
|
|
// reclaimed, release is holder-scoped, and different (org,key) never contend.
|
|
|
|
func TestLock_MutualExclusion(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
const org, key = "acme", "content.publish\x00SocialPost\x00POST-1"
|
|
|
|
ok, err := s.acquireLock(ctx, org, key, "holderA", time.Minute)
|
|
if err != nil || !ok {
|
|
t.Fatalf("first acquire must win: ok=%v err=%v", ok, err)
|
|
}
|
|
// A second, different holder must LOSE while A's lease is live.
|
|
if ok, err := s.acquireLock(ctx, org, key, "holderB", time.Minute); err != nil || ok {
|
|
t.Fatalf("second acquire on a live lease must lose: ok=%v err=%v", ok, err)
|
|
}
|
|
// A holder for a DIFFERENT key is independent.
|
|
if ok, err := s.acquireLock(ctx, org, key+"-other", "holderB", time.Minute); err != nil || !ok {
|
|
t.Fatalf("different key must acquire independently: ok=%v err=%v", ok, err)
|
|
}
|
|
// A holder in a DIFFERENT org is independent (tenant isolation).
|
|
if ok, err := s.acquireLock(ctx, "other-org", key, "holderB", time.Minute); err != nil || !ok {
|
|
t.Fatalf("different org must acquire independently: ok=%v err=%v", ok, err)
|
|
}
|
|
}
|
|
|
|
func TestLock_HolderScopedRelease(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
const org, key = "acme", "k"
|
|
|
|
if ok, _ := s.acquireLock(ctx, org, key, "holderA", time.Minute); !ok {
|
|
t.Fatal("acquire A")
|
|
}
|
|
// A NON-holder release must not free A's lease.
|
|
if err := s.releaseLock(ctx, org, key, "holderB"); err != nil {
|
|
t.Fatalf("release: %v", err)
|
|
}
|
|
if ok, _ := s.acquireLock(ctx, org, key, "holderC", time.Minute); ok {
|
|
t.Fatal("a non-holder release must NOT free a live lease")
|
|
}
|
|
// The real holder releases; now the key is free.
|
|
if err := s.releaseLock(ctx, org, key, "holderA"); err != nil {
|
|
t.Fatalf("release A: %v", err)
|
|
}
|
|
if ok, _ := s.acquireLock(ctx, org, key, "holderC", time.Minute); !ok {
|
|
t.Fatal("after the holder released, the key must be acquirable")
|
|
}
|
|
}
|
|
|
|
func TestLock_ExpiredLeaseReclaimed(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
const org, key = "acme", "k"
|
|
|
|
// Acquire with an already-lapsed TTL: the row exists but is expired.
|
|
if ok, _ := s.acquireLock(ctx, org, key, "crashed", -time.Second); !ok {
|
|
t.Fatal("acquire with negative ttl still writes the (expired) row")
|
|
}
|
|
// A fresh acquirer reclaims the expired lease (a crashed holder never wedges).
|
|
if ok, err := s.acquireLock(ctx, org, key, "recoverer", time.Minute); err != nil || !ok {
|
|
t.Fatalf("expired lease must be reclaimable: ok=%v err=%v", ok, err)
|
|
}
|
|
// The stale (crashed) holder's release must NOT free the recoverer's live lease.
|
|
if err := s.releaseLock(ctx, org, key, "crashed"); err != nil {
|
|
t.Fatalf("release: %v", err)
|
|
}
|
|
if ok, _ := s.acquireLock(ctx, org, key, "other", time.Minute); ok {
|
|
t.Fatal("the recoverer's live lease must survive the stale holder's release")
|
|
}
|
|
}
|
|
|
|
// TestLock_ConcurrentAcquireExactlyOne fires N goroutines at one key; exactly one wins.
|
|
func TestLock_ConcurrentAcquireExactlyOne(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
const org, key = "acme", "k"
|
|
|
|
const n = 32
|
|
var wins int64
|
|
var wg sync.WaitGroup
|
|
start := make(chan struct{})
|
|
for i := 0; i < n; i++ {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
<-start
|
|
ok, err := s.acquireLock(ctx, org, key, holderID(i), time.Minute)
|
|
if err != nil {
|
|
t.Errorf("acquire: %v", err)
|
|
return
|
|
}
|
|
if ok {
|
|
atomic.AddInt64(&wins, 1)
|
|
}
|
|
}(i)
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
if wins != 1 {
|
|
t.Fatalf("exactly one goroutine must win the lease, got %d", wins)
|
|
}
|
|
}
|
|
|
|
func holderID(i int) string { return "h" + string(rune('A'+i%26)) + string(rune('0'+i/26)) }
|