Skip to main content

Architecture

labor-performance is a hexagonal (ports and adapters) Go service (ADR 0001). Dependencies point inward only: domain depends on nothing, application depends on domain, adapters depend on application and domain. The analytical read model (internal/analytics) is isolated from the OLTP code in both directions. internal/architecture enforces all of this in CI (Testing).

Package layout​

PackageRole
internal/domain/standardLaborStandard aggregate: TaskType → ExpectedSeconds (> 0) plus an optional TravelComponentSeconds (0 ≤ travel ≤ expected), an optional HandlingUnit (EACH, CASE, PALLET) with, for CASE, an optional TeamLift (ADR 0035), and an effective range.
internal/domain/performanceTaskPerformance aggregate: one scored completion, with EfficiencyPct = standard / actual × 100, or nil when either side is ≤ 0. Also the pure ClassifyTrend (the recent mean of up to 10 tasks against the baseline, ±5 percentage points, needs at least 3 scored tasks) and DetectCoachingFlag (last 3 scored tasks all below 85%).
internal/domain/idlenessIdlePeriod aggregate (gap between consecutive completions, capped) and UtilizationPct.
internal/domain/sharedTaskType (PICK, PACK, SLAM), HandlingUnit (EACH, CASE, PALLET), AssociateId, StandardId, the domain events LaborStandardDefined, LaborStandardRevised and TaskPerformanceRecorded, and shared errors.
internal/application/portsOutbound ports: StandardRepo, PerformanceRepo, IdlePeriodRepo, ProcessedEvents, EventPublisher, UnitOfWork, Clock, StandardMetrics.
internal/application/usecasesDefineStandard, GetStandard, RecordTaskPerformance, GetAssociateScorecard, GetTaskTypePerformance, GetUtilization (Use cases).
internal/analytics/reportAnalytical read model (rollup rows, Build, normalisation to UNCLASSIFIED). It imports nothing from the OLTP layers.
internal/adapters/inbound/httpchi routers for the OLTP API (NewRouter) and the reports API (NewReportsRouter), DTOs, RFC 7807 mapping, the Idempotency-Key middleware, /readyz, the request logger and CORS.
internal/adapters/inbound/kafkaConsumer (warehouse.fulfillment.events → RecordTaskPerformance, with DLQ) and AnalyticsConsumer (warehouse.labor-performance.analytics → projection).
internal/adapters/inbound/mcpMCP tools, resource template and prompt for cmd/mcp.
internal/adapters/kafka/cloudevents, internal/adapters/kafka/otelkafkaCloudEvents New/Decode, topic and type constants, and W3C trace propagation over Kafka headers.
internal/adapters/outbound/postgrespgx repositories, RunMigrations, UnitOfWork, OutboxPublisher, OutboxRelay, Sweeper, and the outbox lag gauge.
internal/adapters/outbound/analyticsstoreAnalytical writer (PostgresProjection, ConsumedEventsRepo, UnitOfWork), the read-only PostgresReport, and an in-memory store.
internal/adapters/outbound/kafkaAnalyticsPublisher, IntegrationPublisher, RelaySink, FanOutPublisher and the writer settings.
internal/adapters/outbound/eventsLog publisher, a test double / debugging aid (no binary wires it).
internal/adapters/outbound/memoryIn-memory repositories, test doubles only (never imported by a cmd/** binary: ADR 0034 sensor).
internal/adapters/outbound/systemThe wall clock the binaries inject.
internal/adapters/outbound/telemetryOTel setup, the labor_performance.standards.defined counter and the trace-aware slog handler.
internal/adapters/outbound/bootretryBoot-time retry for migrations and the DB ping (ADR 0027).
internal/pgtxThe one transaction-in-context mechanism, shared by the idempotency middleware and the Postgres UnitOfWork.

Binaries​

BinaryRolePort(s)ReadsWrites
cmd/laborOLTP composition root: REST API, TaskCompleted consumer, outbox relay, housekeeping sweeper, outbox-lag gaugeHTTP_ADDR :8080OLTP DB, warehouse.fulfillment.eventsOLTP DB, warehouse.fulfillment.events.dlq, warehouse.labor-performance.analytics, warehouse.labor-performance.events
cmd/labor-projectorAnalytics writer: consumes this service's own analytics topic into the rollup and owns the analytical schemaADMIN_ADDR :8091 (/healthz only)warehouse.labor-performance.analyticsAnalytical DB
cmd/labor-reportsAnalytics reader: serves the report and its freshness over a read-only poolHTTP_ADDR :8092Analytical DB (read-only)nothing
cmd/mcpMCP server over Streamable HTTP, exposing the four read use cases as tools (ADR 0009)MCP_ADDR :8090 (/ MCP endpoint, /healthz)OLTP DBnothing (it applies OLTP migrations at boot)

The binaries share one image (Dockerfile), and the Helm chart runs each one as its own Deployment (Runbook). The OLTP and analytical databases never share a connection: report load cannot contend with the transactional path that ingests TaskCompleted.

Component diagram​

Source: cmd/labor/main.go, cmd/mcp/main.go, cmd/labor-projector/main.go, cmd/labor-reports/main.go.

cmd/mcp builds its own instances of the read use cases over its own OLTP pool. The diagram shows them as one application/usecases box because the code is the same.

Data stores​

StoreOwnerTablesPool
OLTP Postgres (DATABASE_URL, through PgBouncer in the fleet)cmd/labor writes, cmd/mcp reads, and both migratelabor_standards, task_performances, processed_events, idle_periods, outbox_events, idempotency_keysMaxConns 10, statement_timeout 5s
Analytical Postgres (ANALYTICS_DATABASE_URL, direct)cmd/labor-projector writes and migrates, cmd/labor-reports readslabor_performance_rollup, analytics_processed_events, analytics_consumed_eventswriter 5 / 10s, reader 5 / 15s, read-only

Locally, docker-compose.yml provides both databases and a Kafka broker: OLTP Postgres 16 on localhost:5435 (labor/labor) and analytical Postgres 16 on localhost:5436 (labor_analytics/labor_analytics). The ER diagram is in Entity relationship.

Write path: one TaskCompleted​

Source: internal/adapters/inbound/kafka/consumer.go, internal/application/usecases/record_task_performance.go, internal/adapters/outbound/postgres/outbox_relay.go.

POST /standards follows the same shape. With a database, the idempotency middleware opens the transaction, inserts the idempotency_keys row, and passes the transaction down through internal/pgtx, so the standard rows, the outbox row and the cached response commit together.

Cross-cutting decisions​

  • CloudEvents 1.0 structured mode on every Kafka message (ADR 0021).
  • Transactional outbox (ADR 0010), key-aware Hash balancer (ADR 0018), and RequireAll acks with a 10ms batch (ADR 0029).
  • RFC 7807 for every REST error (ADR 0025).
  • No authentication on REST, reports or MCP (ADR 0012).
  • OTel traces and metrics over OTLP, with no /metrics endpoint (ADR 0008, Observability).