Files
oikos/internal/core/ports/events.go
dtoro 9e3783734e
Some checks failed
ci / build-test (push) Has been cancelled
ci / docker-build (push) Has been cancelled
feat: Phase 3b — EntityService, postgres EntityRepository, converged mutations
Problem: entity mutations (create/update/state) existed as three drifted
copies — HTTP CreateEntity/PatchEntity, MCP create_entity/
update_entity_attributes/set_entity_state — each with its own inline
SQL, its own validation subset (MCP validated lifecycle states, HTTP
did not; HTTP patched attributes without regenerating derived checks,
MCP did; MCP wrote no audit trail), the exact drift ADR 0016's first
vertical slice exists to collapse.

Change:
- internal/core/ports: DerivedCheck, Idempotency (adapter-owned request
  hash + cached-body renderer so the replay record commits in the
  create's transaction), IdempotentResponse + GetIdempotent read,
  AuditEntry gains Method/Path/CorrelationID, Event gains
  CorrelationID; EntityUpdateInput carries ExpectedVersion +
  RederiveChecks (derivation for updates runs repo-side: the graph
  host fallback reads relationships through the open tx).
- internal/adapters/postgres/repositories.go: EntityRepo (Create/
  Update/SetState/reads/idempotency) preserving the load-bearing
  check-then-act invariants in-tx: version WHERE-clause, declared
  transitions + preconditions (ValidateTransition), duplicate-slug
  mapping, audit/event/writeCheck all inside one BEGIN…COMMIT.
  OntologyRepo: TTL-cached OntologyStore.
- internal/core/app/entities.go: EntityService — ontology validation
  (type exists, concrete, state declared — the stricter MCP rule now
  governs both surfaces), default-state resolution, id generation,
  derivation for creates, audit/event construction, idempotency
  pass-through.
- httpapi CreateEntity/PatchEntity rewired to the service; PATCH now
  regenerates derived checks (the A2 parity gap). MCP create/update/
  set-state tools call the same service — and now write audit + event
  rows like the HTTP surface always did.
- Integration-test seed paths fixed for the adapters/postgres package
  depth (../../../seeds).

Pre-existing failures documented: TestAPIEndToEnd (entity_types 60 vs
59; 501-endpoint now 200), TestClientLifecycleEndToEnd, TestPhase3*
rows — verified failing identically at ec11956 (scratch approval-
notifier commit test drift), unrelated to this change. All mutation
integration tests (create/patch/idempotency/audit/regeneration) pass.

Verification: make test-db (postgres + mcp green, httpapi green except
the pre-existing set), full non-DB suite (19 pkgs), golangci on new
packages — 0 issues.
2026-08-15 23:38:21 +02:00

52 lines
1.6 KiB
Go

package ports
import (
"context"
"time"
"github.com/dtoro/oikos/internal/core/domain"
)
// Event is a domain occurrence worth publishing (SSE + events table). The
// events adapter maps it to the persistence/SSE shapes.
type Event struct {
Type string // e.g. "execution.completed", "health.changed"
Severity string // "info", "warning", "critical"
Source string // "api", "mcp", "scheduler", "webhook"
EntityID domain.UUID
CorrelationID string
Data map[string]any
Ts time.Time
}
// EventPublisher fans events out to subscribers and persists them. The
// events adapter owns the SSE broker, the events table, and the dedicated
// LISTEN/NOTIFY connection.
type EventPublisher interface {
Publish(ctx context.Context, event Event) error
}
// AuditEntry is an append-only audit-log record. Method/Path carry the
// surface context ("POST", "/api/v1/entities" for REST; "TOOL",
// "create_entity" for MCP).
type AuditEntry struct {
ActorType string // "agent", "operator", "system"
ActorLabel string
Action string
EntityID domain.UUID
Method string
Path string
CorrelationID string
Details map[string]any
Ts time.Time
}
// AuditRepository appends audit records and events. Entries are usually
// carried inside other repositories' input structs so they commit in the
// same transaction (§3.6 of the plan); the standalone methods serve
// read-path actions that audit without another aggregate write.
type AuditRepository interface {
AppendAudit(ctx context.Context, entries []AuditEntry) error
AppendEvent(ctx context.Context, events []Event) error
}