Skip to content

[M2.2b1] Make command and audit persistence atomic - #41

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2b1-transactional-audit
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2b1-transactional-audit

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

M2.2b1, part of #21. Depends on #40; base feat/m2.2a-identity-primitives.

What changed and why

Add a request-local Prisma unit of work and make the audit repository participate in the current command transaction. Identity commands can now roll back their state change when audit persistence fails. Nested commands share their caller's transaction; concurrent requests remain isolated. Existing repositories retain their behavior until they explicitly adopt this unit of work.

Validation

  • Real PostgreSQL integration test rejects an audit insert with a database constraint and verifies that the command write is rolled back.
  • Concurrent transaction test commits one command and rolls back another, including nested audit writes, and verifies the async context is cleared afterwards.
  • All six API integration tests pass; workspace lint, type checks and TypeScript tests pass.
  • The unchanged Go/protobuf contracts passed in [M2.2a] Add bounded authentication security primitives #40.

Security considerations

No endpoint, role, credential or external delivery is added. Transaction callbacks must await their work and allow failures to propagate; password hashing and external I/O remain outside transactions. The synchronous audit subscriber is the only current event consumer; future external consumers must use an outbox rather than emit side effects inside a database transaction.

Checklist

  • M2.2b1 acceptance met and checked in PLAN; M2.2 remains open
  • Lint, type checks, tests and PostgreSQL integration pass
  • Failure and concurrency tests added
  • No shell invocation or new secret exposure path
  • Audit participates in the command transaction
  • Architecture documentation updated

Base automatically changed from feat/m2.2a-identity-primitives to main November 5, 2026 12:10
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2b1-transactional-audit branch from f467fd5 to b791e52 Compare November 5, 2026 12:10
@samuelmbabhazi
samuelmbabhazi merged commit 7282249 into main Nov 5, 2026
9 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.2b1-transactional-audit branch November 5, 2026 12:11
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
## Task

M2.2b2, part of #21. Depends on #41; base
`feat/m2.2b1-transactional-audit`.

## What changed and why

Add PostgreSQL users, browser sessions and single-use identity
credentials. `citext` enforces case-insensitive email uniqueness;
foreign keys bind credentials to their user. Database constraints
prevent unknown account states/purposes, active unverified accounts,
negative security versions, plaintext-shaped credential hashes and
lifetimes beyond the specification. Indexes support credential lookup,
revocation and expiry cleanup.

No account endpoint is exposed yet. The next PR adds repositories and
command behavior on this schema.

## Validation

- Eleven integration tests pass against PostgreSQL 17, including five
identity schema tests.
- Identity tests independently cover duplicate emails, invalid account
transitions, invalid token hashes/purposes/lifetimes, competing token
consumers over separate connections, persistence after reconnect,
session lifetime limits and credential cascades.
- Migration application is repeated to verify the test migration runner
remains idempotent.
- Prisma client regenerated; workspace lint and type checks pass.

## Security considerations

Only token hashes are stored. Session and identity credentials carry the
user's security version so later commands can invalidate stale
credentials on password or privilege changes. Runtime database mutations
arrive in the following PR and will use the transaction/audit mechanism
from #41. The migration adds tables without altering existing fleet or
audit records.

## Checklist

- [x] M2.2b2 acceptance met and checked in PLAN; M2.2 remains open
- [x] Lint, types and integration tests pass
- [x] Invalid-input and concurrency cases covered on real PostgreSQL
- [x] No new shell invocation or plaintext credential persistence
- [x] No runtime command introduced; audit will be enforced by command
handlers
- [x] Data model follows SPEC 4 and identity limits from SPEC 8.1
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
## Task

M2.2b3, part of #21. Base: `feat/m2.2b2-identity-schema`; follows the
identity schema PR.

## What changed and why

Add the identity storage port, redacting domain records and Prisma
adapter. Per-user row locks serialize sensitive mutations. Password
changes increment the security version and revoke sessions and reset
credentials; single-use token claims use conditional updates. Session
activity advances monotonically across overlapping requests and cannot
revive expired, revoked, disabled-owner or stale-version sessions.

Commands can share the transaction/audit boundary from #41. Driver
errors from command transactions are replaced with an opaque
availability error to keep bound data out of HTTP logs. A reusable
integration fixture owns its PostgreSQL container, temporary KEK, Nest
application and cleanup.

## Validation

- Workspace lint, type checks and TypeScript tests pass.
- Seventeen PostgreSQL integration tests pass, including six repository
tests covering concurrent mutations, consumed-token rollback with audit
rollback, duplicate signup transactions, expired/future-created
credentials, monotonic activity and security-version invalidation.
- Existing schema, fleet and audit integration suites pass unchanged.

## Security considerations

No HTTP endpoint is exposed yet. Application handlers must perform
mutations and publish their events inside `transaction` or
`withUserLock`; this PR supplies and tests that boundary. Password
hashes returned by repositories are wrapped in `Secret`. Session
activity is authentication telemetry, not a separate domain command. No
new shell invocation or dynamic SQL identifier is introduced.

## Checklist

- [x] M2.2b persistence acceptance met and checked; M2.2 account flows
remain open
- [x] Lint, types and tests pass
- [x] Real PostgreSQL concurrency and failure cases covered
- [x] No new secret exposure path
- [x] Transaction and audit rollback verified
- [x] Threat model updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant