---
title: Assumptions and gaps
description: What the Terraform expects from the application, what is still a placeholder, and what is not built yet.
sidebar:
  icon: triangle-alert
---

## Application changes

The Terraform expects four things from the application. The application now provides all of them:

- [x] **A health route.** `GET /healthz` returns 200 (`server/app/__main__.py`). The ALB target group checks it (`health_check_path`).
- [x] **An `/api` prefix.** The document routes sit on an `APIRouter(prefix="/api")` in `server/app/__main__.py`, which matches `api_path_pattern = "/api/*"`. `/healthz` and the WebSocket `/ws` stay at the root, because CloudFront and the ALB route them as their own paths.
- [x] **Same-origin URLs.** `client/src/config.ts` defaults the API base to `/api` and builds the WebSocket URL from the page origin, so an `https` page opens a `wss://` socket. `VITE_API_BASE` overrides the base for a build that targets another host. `client/vite.config.ts` proxies `/api` and `/ws` to the local server for development, and `client/src/App.tsx` and `client/src/Document.tsx` use these values.
- [x] **A production image.** `server/Dockerfile` ends in a `production` target, the default because it is the last stage. It runs one uvicorn process as a non-root user (uid 10001) with no `--reload`, and `--reload` stays in the separate `dev` target. `scripts/ci/build-server-image.sh` builds it for `linux/amd64`. The task definition in `infrastructure/modules/service/ecs.tf` sets `readonlyRootFilesystem = true` and mounts a writable `/tmp`.

## Blocker 1: the in-memory database

:::danger[The app is only correct with one task]
`server/app/internal/db.py` uses `sqlite:///:memory:`, so every task has its own database. A document saved through one task is missing on the others, and every restart or deploy loses all data.

Prod runs at least 3 tasks, so the application is not correct on this infrastructure until this is fixed. Until then, the only correct setting is one task, which gives up the multi-AZ availability.
:::

**The fix.** `server/README.md` forbids changes to `app/internal/`, so the fix goes outside it:

1. Add a database module outside `app/internal/`. It builds the SQLAlchemy engine from `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and `DB_PASSWORD`, with TLS required.
2. Make `__main__.py` import that module instead of `internal/db.py`.
3. Replace `create_all` and the seed inserts in `lifespan` with migrations. On a shared database, the fixed-ID seed insert fails as soon as a second task starts.

Aurora is provisioned in every environment for this fix, and the task definition already passes the `DB_*` variables.

## Placeholders

Replace these before the first plan:

| File | Setting | Current value |
|---|---|---|
| `envs/*/terraform.tfvars` | `domain_name` | `dev.app.example.com`, `staging.app.example.com`, `app.example.com` |
| `envs/*/terraform.tfvars` | `route53_zone_id` | `Z00000000PLACEHOLDER` |
| `envs/*/terraform.tfvars` | `alarm_email` | `null`, so no subscription |
| bootstrap `-var` | `github_repository` | `OWNER/REPO` |

`openai_model` is `gpt-6-luna` in every environment, because the application's own fallback (`gpt-3.5-turbo-1106`) is deprecated. The task definition always sets `OPENAI_MODEL`, so the fallback is never used.

## Environment settings

These are starting values, not measurements.

| Setting | dev | staging | prod |
|---|---|---|---|
| `min_tasks` / `max_tasks` | 1 / 2 | 2 / 4 | 3 / 12 |
| `single_nat_gateway` | true | true | false |
| `db_instance_count` | 1 | 2 | 2 |
| `db_backup_retention_days` | 1 | 7 | 35 |
| `db_deletion_protection` | false | true | true |
| `use_fargate_spot` | true | false | false |
| `log_retention_days` | 30 | 90 | 365 |
| `trace_sample_ratio` | 1.0 | 1.0 | 0.1 |
| `vpc_cidr` | `10.10.0.0/16` | `10.20.0.0/16` | `10.30.0.0/16` |

## Capacity

Production starts at 3 to 12 Fargate tasks of 0.5 vCPU and 1 GB. These are starting values, not load-test results.

**One socket, one call at a time**

Each open editor tab holds one WebSocket, and each 500 ms debounced edit sends one message. The handler is async and serial per connection, so each socket has at most one OpenAI call in flight. Later edits wait in a queue.

**Scale by adding tasks**

The `/document` and `/save` routes are sync and run in Starlette's thread pool. Each task runs one uvicorn process, so we scale by adding tasks.

**Weak scaling signals**

Autoscaling tracks CPU (60%) and ALB requests per target. Both are weak signals for this load: the work is mostly I/O wait, and the ALB counts a WebSocket only once, at the upgrade. A better signal is in-flight suggestions per task. The server now counts them (`suggestions.in_flight`, see [Signals](/signals#metrics)), but only locally: the AWS collector does not send it to CloudWatch, and autoscaling on it is not built.

**OpenAI is the first limit**

We expect the OpenAI quota (requests and tokens per minute) to be the first limit at 10,000 users, before Fargate, because every debounced edit sends the full document to OpenAI. This is not measured.

### Load-test method

1. **Isolate one task**

    Run one task behind the ALB in staging.

2. **Stub OpenAI**

    Open N WebSockets that each send a document edit every few seconds. Replace OpenAI with a stub that has a fixed latency, so the test measures our service and not OpenAI.

3. **Find the knee**

    Raise N until p95 latency or CPU passes the target. That N per task sets `min_tasks`, `max_tasks`, and the target for the in-flight-suggestions metric.

## Known gaps

**SOC 2 services not built**

Not built: CloudTrail, GuardDuty, AWS Config, Security Hub, IAM Identity Center, access logs for the ALB, CloudFront and WAF, and AWS Backup copies. There is no Content-Security-Policy header.

**Database password rotation**

RDS rotates the managed master password, but ECS injects secrets only when a task starts. Running tasks keep the old password until they are replaced.

**ALB ingress covers all of CloudFront**

The ALB admits the `com.amazonaws.global.cloudfront.origin-facing` prefix list. That list covers all of CloudFront, not only this distribution. AWS documents a tighter source, the `CloudFront-VPCOrigins-Service-SG` group, but that group exists only after the VPC origin is created.

**WAF body-size rule set to count**

`SizeRestrictions_BODY` in the common rule set only counts matches, because `POST /save` sends whole patents larger than 8 KB. The managed rules are configured but not tuned against real traffic.

**Broad Terraform roles**

The plan role has `ReadOnlyAccess` on the account, so it can read CloudWatch logs and S3 objects, including state. It cannot read secret values. The apply role has `AdministratorAccess`, and its only gate is the protected GitHub Environment.

**Deploy role scope**

The deploy role can invalidate any CloudFront distribution in its account. Its `ecs:RegisterTaskDefinition` permission is scoped to `task-definition/${name}-*`, following the IAM service authorization reference; this has not been tested against a real deploy.

**Shared certificate validation record**

The service and edge modules both manage the same ACM validation record (`allow_overwrite = true`). Destroying one module removes the record the other relies on.

**Smaller notes**

- The ECR repository uses the data module's KMS key.
- Dev runs on Fargate Spot.
- The application runs only in eu-west-2. us-east-1 holds just the CloudFront certificate and the WAF web ACL, because CloudFront requires them there.
