Files
archie/internal/infrastructure/eventbus/nats/dedup_test.go
T
sam 8a799cd8cd feat(forge): observable webhook receiver + dedup regression proof; decide adversarial review design
archie-core-7d5u.5 (idempotency and poll/webhook parity): the dedup
mechanism (TaskEnvelope.IdempotencyKey + PublishUnique + JetStream
Nats-Msg-Id dedup) and the shared dispatch predicate were already fully
wired by 7d5u.4. What was missing: proof the dedup actually holds, and
observability for a wedged receiver.

- internal/infrastructure/eventbus/nats/dedup_test.go: regression test
  proving PublishUnique suppresses a duplicate publish on the same
  WorkQueuePolicy stream ARCHIE_TASKS uses in production.
- internal/forge/webhook/receiver.go: Receiver.Status() tracks
  authenticated deliveries separately from successful publishes; a GET
  to the receiver's listen address (GitHub only ever POSTs) now returns
  it as JSON, so a wedged/non-delivering receiver is observable instead
  of indistinguishable from an idle repo.

Poll/webhook parity (disabling webhook still polls) needed no code
change: Cycle()/Run() never reference cfg.Forge.Intake anywhere in
internal/daemon, only setupForgeWebhook gates the webhook HTTP server.

archie-core-h019.1 (design: adversarial self-review stage): one-page
decision at docs/prds/adversarial-self-review.md. Reviewer runs in a
second workspace pointed at a .git-stripped snapshot of the reviewed
commit, never the implementer's worktree, so isolation is a property of
what's mounted rather than a prompt instruction. Findings contract is
workflow.ReviewFinding, owned by internal/domain/workflow. A surviving
error-level finding sets StatusParked, not WaitingHuman -- the epic's
informal wording named two different states in the actual machine; this
resolves which.

Both changes reviewed by a fresh subagent with no memory of the
implementation, per CLAUDE.md's adversarial-verification requirement;
zero blocking findings, two minor coverage gaps closed.
2026-08-22 19:45:31 +10:00

67 lines
2.5 KiB
Go

package nats
import (
"errors"
"testing"
"github.com/nats-io/nats-server/v2/server"
natstest "github.com/nats-io/nats-server/v2/test"
"github.com/samcharles93/archie-core/internal/eventbus"
)
// TestPublishUniqueSuppressesDuplicateKeyOnWorkQueueStream is the regression
// proof behind archie-core-7d5u.5 (idempotency and poll/webhook parity): a
// forge issue discovered independently by the poller and the webhook
// receiver produces two TaskEnvelopes with the identical
// workintake.TaskEnvelope.IdempotencyKey(), published to the SAME
// WorkQueuePolicy stream ARCHIE_TASKS uses in production (Config.Retention
// left unset, per TestWithDefaultsRetention). A second PublishUnique call
// with the same key inside the dedup window must be silently suppressed by
// the server, so a single consumer only ever observes one message -- never
// two tasks for one issue.
func TestPublishUniqueSuppressesDuplicateKeyOnWorkQueueStream(t *testing.T) {
srv := natstest.RunServer(&server.Options{Port: -1, JetStream: true, StoreDir: t.TempDir()})
t.Cleanup(srv.Shutdown)
c, err := Connect(t.Context(), Config{
URL: srv.ClientURL(),
StreamName: "TASKS_DEDUP_TEST",
Subjects: []string{"tasks.dedup.test.>"},
FilterSubject: "tasks.dedup.test.>",
// Retention left unset: defaults to WorkQueuePolicy, the same policy
// ARCHIE_TASKS uses in production for task distribution.
}, discardLogger())
if err != nil {
t.Fatalf("Connect = %v", err)
}
t.Cleanup(c.Close)
const key = "archie:owner/repo/42"
// Poll-discovered publish, then webhook-discovered publish of the same
// issue moments later -- both carry the identical idempotency key.
if err := c.PublishUnique(t.Context(), "tasks.dedup.test.issue", key, []byte("poll")); err != nil {
t.Fatalf("PublishUnique (poll) = %v", err)
}
if err := c.PublishUnique(t.Context(), "tasks.dedup.test.issue", key, []byte("webhook")); err != nil {
t.Fatalf("PublishUnique (webhook) = %v", err)
}
msg, err := c.Fetch(t.Context())
if err != nil {
t.Fatalf("Fetch (first) = %v", err)
}
if err := msg.Ack(); err != nil {
t.Fatalf("Ack = %v", err)
}
// A second delivery for the same issue would be a double-dispatched
// task. There must be none queued.
if _, err := c.Fetch(t.Context()); err == nil {
t.Fatal("Fetch (second) succeeded, want eventbus.ErrNoMessage -- duplicate was not suppressed")
} else if !errors.Is(err, eventbus.ErrNoMessage) {
t.Fatalf("Fetch (second) = %v, want eventbus.ErrNoMessage", err)
}
}