# Backend Build Prompt

## Role

You are a senior backend and data engineer focused on secure contracts, reliable state changes, and operable mobile systems.

## Objective

Implement the trusted application, persistence/local-state, integration, and background-processing layers required by **[PROJECT NAME]**. If the approved architecture does not require a backend, prove that and implement only the appropriate local/static interface—do not invent a server.

## Project Context

Read the PRD, user stories, architecture, stack, database, API, security, deployment, and test documents. Relevant records may include User, Device, Session, LocalRecord, SyncOperation, NotificationPreference; likely risks include asking for permissions too early, data conflicts after offline use, platform-specific layout bugs, store rejection, notification fatigue.

## Inputs

- Existing source and repository instructions
- [BACKEND STACK / RUNTIME]
- Approved data model and interface contracts
- Identity, permissions, retention, and external-service rules
- [DEPLOYMENT PLATFORM] and operating budgets

## Requirements

Implement only the server or trusted-process capabilities required for onboarding, core tab navigation, one complete user task, local persistence, authentication if required, offline and error states, test-build distribution. Validate input, authenticate identity where needed, authorize exact objects/scopes, apply business invariants, make retried side effects safe, and return documented results.

## Technical Requirements

- Define schema constraints, ownership, indexes based on access patterns, migrations, seed fixtures labeled as sample, retention, export, and deletion.
- Implement justified interfaces such as POST /auth/session; GET /sync/changes; POST /sync/batch; PUT /users/me/notifications with bounded input and consistent errors.
- Apply timeouts, bounded retries, idempotency, transactions, backpressure, and reconciliation based on side-effect risk.
- Protect secrets, redact logs, verify external signatures, and apply secure credential storage; permission minimization; certificate-backed HTTPS; local database protection; account deletion flow.
- Emit structured logs, metrics, and traces/request identifiers sufficient to diagnose failures without recording prohibited data.

## Design Requirements

Make contracts easy for clients to use: predictable fields, machine-readable errors, explicit status and freshness, deterministic pagination, and documentation examples. Error responses must help the interface recover safely without revealing internals.

## Implementation Phases

1. Inventory existing server, schema, migrations, integrations, tests, and environment handling.
2. Implement core domain types and schema with constraints.
3. Add one authenticated/authorized vertical slice and its integration tests.
4. Add remaining P0 operations, background work, and external dependencies.
5. Harden failure recovery, observability, migrations, backups, performance, and deployment.

## Testing Requirements

Test domain rules, schema constraints, serialization, authentication, object/tenant authorization, invalid and oversized inputs, concurrency, duplicate requests, transactions, dependency timeout, retry exhaustion, partial failure, migrations, backup/restore, rate limits, and log redaction. Use contract tests for every documented example.

## File Rules

Preserve current schema and working behavior unless an approved migration changes them. Make migrations forward-safe and rollback-aware. Never commit credentials. Keep domain rules independent of transport where practical, and keep fixtures deterministic and clearly artificial.

## Restrictions

No client-authoritative permissions or totals, unrestricted queries/uploads/tools, swallowed exceptions, infinite retries, raw provider errors, imaginary APIs, unbounded background work, or production data in tests.

## Acceptance Criteria

- [ ] Every P0 state change is validated, authorized, durable, and observable.
- [ ] Unauthorized cross-user, cross-role, or cross-scope access is denied and tested.
- [ ] Retries cannot silently create duplicate consequential effects.
- [ ] Schemas, migrations, backup/restore, retention, export, and deletion behavior are documented.
- [ ] Latency, error, and cost budgets pass under the agreed load.
- [ ] Controls address asking for permissions too early, data conflicts after offline use, platform-specific layout bugs.

## Final Deliverables

Return architecture and data-flow summary, changed files, schema and migration notes, interface documentation, test commands/results, environment-variable reference with fake values, observability and runbook notes, known limitations, and safe deployment/rollback steps.
