svc-infra 0.1.622__py3-none-any.whl → 0.1.624__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of svc-infra might be problematic. Click here for more details.

Files changed (35) hide show
  1. svc_infra/cli/cmds/docs/docs_cmds.py +104 -140
  2. svc_infra/docs/acceptance-matrix.md +71 -0
  3. svc_infra/docs/acceptance.md +44 -0
  4. svc_infra/docs/adr/0002-background-jobs-and-scheduling.md +40 -0
  5. svc_infra/docs/adr/0003-webhooks-framework.md +24 -0
  6. svc_infra/docs/adr/0004-tenancy-model.md +42 -0
  7. svc_infra/docs/adr/0005-data-lifecycle.md +86 -0
  8. svc_infra/docs/adr/0006-ops-slos-and-metrics.md +47 -0
  9. svc_infra/docs/adr/0007-docs-and-sdks.md +83 -0
  10. svc_infra/docs/adr/0008-billing-primitives.md +109 -0
  11. svc_infra/docs/adr/0009-acceptance-harness.md +40 -0
  12. svc_infra/docs/api.md +59 -0
  13. svc_infra/docs/auth.md +11 -0
  14. svc_infra/docs/cache.md +18 -0
  15. svc_infra/docs/cli.md +74 -0
  16. svc_infra/docs/contributing.md +34 -0
  17. svc_infra/docs/data-lifecycle.md +52 -0
  18. svc_infra/docs/database.md +14 -0
  19. svc_infra/docs/docs-and-sdks.md +62 -0
  20. svc_infra/docs/environment.md +114 -0
  21. svc_infra/docs/getting-started.md +63 -0
  22. svc_infra/docs/idempotency.md +111 -0
  23. svc_infra/docs/jobs.md +67 -0
  24. svc_infra/docs/observability.md +16 -0
  25. svc_infra/docs/ops.md +33 -0
  26. svc_infra/docs/rate-limiting.md +121 -0
  27. svc_infra/docs/repo-review.md +48 -0
  28. svc_infra/docs/security.md +155 -0
  29. svc_infra/docs/tenancy.md +35 -0
  30. svc_infra/docs/webhooks.md +112 -0
  31. svc_infra/mcp/svc_infra_mcp.py +21 -1
  32. {svc_infra-0.1.622.dist-info → svc_infra-0.1.624.dist-info}/METADATA +26 -17
  33. {svc_infra-0.1.622.dist-info → svc_infra-0.1.624.dist-info}/RECORD +35 -6
  34. {svc_infra-0.1.622.dist-info → svc_infra-0.1.624.dist-info}/WHEEL +0 -0
  35. {svc_infra-0.1.622.dist-info → svc_infra-0.1.624.dist-info}/entry_points.txt +0 -0
@@ -1,9 +1,8 @@
1
1
  from __future__ import annotations
2
2
 
3
- import importlib.util
4
3
  import os
5
- import sys
6
- from importlib.metadata import PackageNotFoundError, distribution
4
+ from importlib.resources import as_file
5
+ from importlib.resources import files as pkg_files
7
6
  from pathlib import Path
8
7
  from typing import Dict, List
9
8
 
@@ -13,153 +12,88 @@ from typer.core import TyperGroup
13
12
 
14
13
  from svc_infra.app.root import resolve_project_root
15
14
 
16
- # ---------- small helpers ----------
17
-
18
15
 
19
16
  def _norm(name: str) -> str:
20
17
  return name.strip().lower().replace(" ", "-").replace("_", "-")
21
18
 
22
19
 
23
- def _md_topics_in(dirpath: Path) -> Dict[str, Path]:
20
+ def _discover_fs_topics(docs_dir: Path) -> Dict[str, Path]:
24
21
  topics: Dict[str, Path] = {}
25
- if dirpath.exists() and dirpath.is_dir():
26
- for p in sorted(dirpath.glob("*.md")):
22
+ if docs_dir.exists() and docs_dir.is_dir():
23
+ for p in sorted(docs_dir.glob("*.md")):
27
24
  if p.is_file():
28
25
  topics[_norm(p.stem)] = p
29
26
  return topics
30
27
 
31
28
 
32
- # ---------- where could docs live after install? ----------
29
+ def _discover_pkg_topics() -> Dict[str, Path]:
30
+ """
31
+ Discover docs shipped inside the installed package at svc_infra/docs/*,
32
+ using importlib.resources so this works for wheels, sdists, and zipped wheels.
33
+ """
34
+ topics: Dict[str, Path] = {}
35
+ try:
36
+ docs_root = pkg_files("svc_infra").joinpath("docs")
37
+ # docs_root is a Traversable; it may be inside a zip. Iterate safely.
38
+ for entry in docs_root.iterdir():
39
+ if entry.name.endswith(".md"):
40
+ # materialize to a real tempfile path if needed
41
+ with as_file(entry) as concrete:
42
+ p = Path(concrete)
43
+ if p.exists() and p.is_file():
44
+ topics[_norm(p.stem)] = p
45
+ except Exception:
46
+ # If the package has no docs directory, just return empty.
47
+ pass
48
+ return topics
33
49
 
34
50
 
35
- def _candidate_docs_dirs(ctx: click.Context) -> List[Path]:
51
+ def _resolve_docs_dir(ctx: click.Context) -> Path | None:
36
52
  """
37
- Return likely directories that contain the shipped docs (*.md), ordered by priority.
38
- Covers:
39
- 1) explicit --docs-dir / env var
40
- 2) in-repo (editable install): <repo>/docs relative to src/svc_infra
41
- 3) wheel installs: <site-packages>/docs
42
- 4) wheel .data area: <site-packages>/{name}-{ver}.data/**/docs
53
+ Optional override precedence:
54
+ 1) --docs-dir CLI option
55
+ 2) SVC_INFRA_DOCS_DIR env var
56
+ 3) *Only when working inside the svc-infra repo itself*: repo-root /docs
43
57
  """
44
- out: List[Path] = []
45
-
46
- # 1) explicit override (--docs-dir or env)
47
- # Walk up parent contexts to see Typer's group option.
48
- cur: click.Context | None = ctx
49
- while cur is not None:
50
- docs_dir_opt = (cur.params or {}).get("docs_dir")
58
+ # 1) CLI option on this or parent contexts
59
+ current: click.Context | None = ctx
60
+ while current is not None:
61
+ docs_dir_opt = (current.params or {}).get("docs_dir")
51
62
  if docs_dir_opt:
52
- p = docs_dir_opt if isinstance(docs_dir_opt, Path) else Path(docs_dir_opt)
53
- p = p.expanduser()
54
- if p.exists():
55
- out.append(p)
56
- return out # explicit override wins
57
- cur = cur.parent
63
+ path = docs_dir_opt if isinstance(docs_dir_opt, Path) else Path(docs_dir_opt)
64
+ path = path.expanduser()
65
+ if path.exists():
66
+ return path
67
+ current = current.parent
58
68
 
69
+ # 2) Env var
59
70
  env_dir = os.getenv("SVC_INFRA_DOCS_DIR")
60
71
  if env_dir:
61
72
  p = Path(env_dir).expanduser()
62
73
  if p.exists():
63
- out.append(p)
64
- return out # explicit override wins
65
-
66
- # locate installed package dir: .../site-packages/svc_infra
67
- pkg_dir: Path | None = None
68
- spec = importlib.util.find_spec("svc_infra")
69
- if spec and spec.submodule_search_locations:
70
- pkg_dir = Path(next(iter(spec.submodule_search_locations)))
71
-
72
- # 2) in-repo editable install: src/svc_infra -> ../../docs
73
- if pkg_dir:
74
- repo_root_docs = pkg_dir.parent.parent / "docs"
75
- if repo_root_docs.exists():
76
- out.append(repo_root_docs)
77
-
78
- # 3) wheel installs often end up with a top-level site-packages/docs
79
- top_level_docs = pkg_dir.parent / "docs"
80
- if top_level_docs.exists():
81
- out.append(top_level_docs)
82
-
83
- # 4) wheel .data layout: <site-packages>/{dist-name}-{version}.data/**/docs
84
- # This catches Poetry's include=docs/**/* paths installed by pip.
85
- # We compute sibling candidates off site-packages base.
86
- site_pkgs: Path | None = pkg_dir.parent if pkg_dir else None
87
- dist = None
88
- for name in ("svc-infra", "svc_infra"):
89
- try:
90
- dist = distribution(name)
91
- break
92
- except PackageNotFoundError:
93
- dist = None
94
-
95
- if site_pkgs and dist is not None:
96
- # normalized dist name (hyphen/underscore forms both happen in practice)
97
- dist_name = dist.metadata.get("Name", "svc-infra")
98
- dist_ver = dist.version
99
- data_candidates = [
100
- site_pkgs / f"{dist_name}-{dist_ver}.data",
101
- site_pkgs / f"{dist_name.replace('-', '_')}-{dist_ver}.data",
102
- site_pkgs / f"{dist_name.replace('_', '-')}-{dist_ver}.data",
103
- ]
104
- for data_dir in data_candidates:
105
- if not data_dir.exists():
106
- continue
107
- # common wheel data subfolders
108
- for sub in ("purelib", "platlib", "data"):
109
- d = data_dir / sub / "docs"
110
- if d.exists():
111
- out.append(d)
112
- # fallback: search shallowly for any docs/ folder inside .data
113
- for root, dirs, _files in os.walk(data_dir):
114
- root_path = Path(root)
115
- # limit depth (cheap)
116
- if len(root_path.parts) - len(data_dir.parts) > 3:
117
- dirs[:] = []
118
- continue
119
- if root_path.name == "docs":
120
- out.append(root_path)
121
-
122
- # 5) extremely defensive: scan sys.path entries that look like site-/dist-packages for top-level docs/
123
- for entry in sys.path:
124
- if not entry or ("site-packages" not in entry and "dist-packages" not in entry):
125
- continue
126
- p = Path(entry) / "docs"
127
- if p.exists():
128
- out.append(p)
129
-
130
- # de-dup while preserving order
131
- seen = set()
132
- uniq: List[Path] = []
133
- for p in out:
134
- if p.exists():
135
- key = str(p.resolve())
136
- if key not in seen:
137
- seen.add(key)
138
- uniq.append(p)
139
- return uniq
140
-
141
-
142
- def _discover_topics(ctx: click.Context) -> Dict[str, Path]:
143
- topics: Dict[str, Path] = {}
144
- for d in _candidate_docs_dirs(ctx):
145
- found = _md_topics_in(d)
146
- # do not override earlier (higher-priority) sources
147
- for k, v in found.items():
148
- topics.setdefault(k, v)
149
- if topics:
150
- # one dir with content is enough for most setups
151
- # (comment out this break if you *want* deep merging)
152
- break
153
- return topics
74
+ return p
154
75
 
76
+ # 3) In-repo convenience (so `svc-infra docs` works inside this repo)
77
+ try:
78
+ root = resolve_project_root()
79
+ proj_docs = root / "docs"
80
+ if proj_docs.exists():
81
+ return proj_docs
82
+ except Exception:
83
+ pass
155
84
 
156
- # ---------- Typer group ----------
85
+ return None
157
86
 
158
87
 
159
88
  class DocsGroup(TyperGroup):
160
89
  def list_commands(self, ctx: click.Context) -> List[str]:
161
- topics = _discover_topics(ctx)
162
- return sorted(topics.keys())
90
+ names: List[str] = list(super().list_commands(ctx) or [])
91
+ dir_to_use = _resolve_docs_dir(ctx)
92
+ fs = _discover_fs_topics(dir_to_use) if dir_to_use else {}
93
+ pkg = _discover_pkg_topics()
94
+ names.extend(fs.keys())
95
+ names.extend([k for k in pkg.keys() if k not in fs])
96
+ return sorted(set(names))
163
97
 
164
98
  def get_command(self, ctx: click.Context, name: str) -> click.Command | None:
165
99
  cmd = super().get_command(ctx, name)
@@ -167,15 +101,27 @@ class DocsGroup(TyperGroup):
167
101
  return cmd
168
102
 
169
103
  key = _norm(name)
170
- topics = _discover_topics(ctx)
171
- if key in topics:
172
- file_path = topics[key]
104
+
105
+ dir_to_use = _resolve_docs_dir(ctx)
106
+ fs = _discover_fs_topics(dir_to_use) if dir_to_use else {}
107
+ if key in fs:
108
+ file_path = fs[key]
173
109
 
174
110
  @click.command(name=name)
175
- def _show() -> None:
111
+ def _show_fs() -> None:
176
112
  click.echo(file_path.read_text(encoding="utf-8", errors="replace"))
177
113
 
178
- return _show
114
+ return _show_fs
115
+
116
+ pkg = _discover_pkg_topics()
117
+ if key in pkg:
118
+ file_path = pkg[key]
119
+
120
+ @click.command(name=name)
121
+ def _show_pkg() -> None:
122
+ click.echo(file_path.read_text(encoding="utf-8", errors="replace"))
123
+
124
+ return _show_pkg
179
125
 
180
126
  return None
181
127
 
@@ -194,31 +140,49 @@ def register(app: typer.Typer) -> None:
194
140
  ) -> None:
195
141
  if topic:
196
142
  key = _norm(topic)
197
- topics = _discover_topics(click.get_current_context())
198
- if key in topics:
199
- typer.echo(topics[key].read_text(encoding="utf-8", errors="replace"))
143
+ ctx = click.get_current_context()
144
+ dir_to_use = _resolve_docs_dir(ctx)
145
+ fs = _discover_fs_topics(dir_to_use) if dir_to_use else {}
146
+ if key in fs:
147
+ typer.echo(fs[key].read_text(encoding="utf-8", errors="replace"))
148
+ raise typer.Exit(code=0)
149
+ pkg = _discover_pkg_topics()
150
+ if key in pkg:
151
+ typer.echo(pkg[key].read_text(encoding="utf-8", errors="replace"))
200
152
  raise typer.Exit(code=0)
201
153
  raise typer.BadParameter(f"Unknown topic: {topic}")
202
154
 
203
155
  @docs_app.command("list", help="List available documentation topics")
204
156
  def list_topics() -> None:
205
157
  ctx = click.get_current_context()
206
- root = resolve_project_root()
207
- topics = _discover_topics(ctx)
158
+ dir_to_use = _resolve_docs_dir(ctx)
159
+ fs = _discover_fs_topics(dir_to_use) if dir_to_use else {}
160
+ pkg = _discover_pkg_topics()
208
161
 
209
- for name, path in topics.items():
162
+ def _print(name: str, path: Path) -> None:
210
163
  try:
211
- rel = path.relative_to(root)
212
- typer.echo(f"{name}\t{rel}")
213
- except Exception:
214
164
  typer.echo(f"{name}\t{path}")
165
+ except Exception:
166
+ typer.echo(name)
167
+
168
+ for name, path in fs.items():
169
+ _print(name, path)
170
+ for name, path in pkg.items():
171
+ if name not in fs:
172
+ _print(name, path)
215
173
 
216
174
  @docs_app.command("show", help="Show docs for a topic (alternative to dynamic subcommand)")
217
175
  def show(topic: str) -> None:
218
176
  key = _norm(topic)
219
- topics = _discover_topics(click.get_current_context())
220
- if key in topics:
221
- typer.echo(topics[key].read_text(encoding="utf-8", errors="replace"))
177
+ ctx = click.get_current_context()
178
+ dir_to_use = _resolve_docs_dir(ctx)
179
+ fs = _discover_fs_topics(dir_to_use) if dir_to_use else {}
180
+ if key in fs:
181
+ typer.echo(fs[key].read_text(encoding="utf-8", errors="replace"))
182
+ return
183
+ pkg = _discover_pkg_topics()
184
+ if key in pkg:
185
+ typer.echo(pkg[key].read_text(encoding="utf-8", errors="replace"))
222
186
  return
223
187
  raise typer.BadParameter(f"Unknown topic: {topic}")
224
188
 
@@ -0,0 +1,71 @@
1
+ # Acceptance Matrix (A-IDs)
2
+
3
+ This document maps Acceptance scenarios (A-IDs) to endpoints, CLIs, fixtures, and seed data. Use it to drive the CI promotion gate and local `make accept` runs.
4
+
5
+ ## A0. Harness
6
+ - Stack: docker-compose.test.yml (api, db, redis)
7
+ - Makefile targets: accept, compose_up, wait, seed, down
8
+ - Tests bootstrap: tests/acceptance/conftest.py (BASE_URL), _auth.py, _seed.py, _http.py
9
+
10
+ ## A1. Security & Auth
11
+ - A1-01 Register → Verify → Login → /auth/me
12
+ - Endpoints: POST /auth/register, POST /auth/verify, POST /auth/login, GET /auth/me
13
+ - Fixtures: admin, user
14
+ - A1-02 Password policy & breach check
15
+ - Endpoints: POST /auth/register
16
+ - A1-03 Lockout escalation and cooldown
17
+ - Endpoints: POST /auth/login
18
+ - A1-04 RBAC/ABAC enforced
19
+ - Endpoints: GET /admin/*, resource GET with owner guard
20
+ - A1-05 Session list & revoke
21
+ - Endpoints: GET/DELETE /auth/sessions
22
+ - A1-06 API keys lifecycle
23
+ - Endpoints: POST/GET/DELETE /auth/api-keys, usage via Authorization header
24
+ - A1-07 MFA lifecycle
25
+ - Endpoints: /auth/mfa/*
26
+
27
+ ## A2. Rate Limiting
28
+ - A2-01 Global limit → 429 with Retry-After
29
+ - A2-02 Per-route & tenant override honored
30
+ - A2-03 Window reset
31
+
32
+ ## A3. Idempotency & Concurrency
33
+ - A3-01 Same Idempotency-Key → identical 2xx
34
+ - A3-02 Conflicting payload + same key → 409
35
+ - A3-03 Optimistic lock mismatch → 409; success increments version
36
+
37
+ ## A4. Jobs & Scheduling
38
+ - A4-01 Custom job consumed
39
+ - A4-02 Backoff & DLQ
40
+ - A4-03 Cron tick observed
41
+
42
+ ## A5. Webhooks
43
+ - A5-01 Producer → delivery (HMAC verified)
44
+ - A5-02 Retry stops on success
45
+ - A5-03 Secret rotation window accepts old+new
46
+
47
+ ## A6. Tenancy
48
+ - A6-01 tenant_id injected on create; list scoped
49
+ - A6-02 Cross-tenant → 404/403
50
+ - A6-03 Per-tenant quotas enforced
51
+
52
+ ## A7. Data Lifecycle
53
+ - A7-01 Soft delete hides; undelete restores
54
+ - A7-02 GDPR erasure steps with audit
55
+ - A7-03 Retention purge soft→hard
56
+ - A7-04 Backup verification healthy
57
+
58
+ ## A8. SLOs & Ops
59
+ - A8-01 Metrics http_server_* and db_pool_* present
60
+ - A8-02 Maintenance mode 503; circuit breaker trips/recover
61
+ - A8-03 Liveness/readiness under DB up/down
62
+
63
+ ## A9. OpenAPI & Error Contracts
64
+ - A9-01 /openapi.json valid; examples present
65
+ - A9-02 Problem+JSON conforms
66
+ - A9-03 Spectral + API Doctor pass
67
+
68
+ ## A10. CLI & DX
69
+ - A10-01 DB migrate/rollback/seed
70
+ - A10-02 Jobs runner consumes a sample job
71
+ - A10-03 SDK smoke-import and /ping
@@ -0,0 +1,44 @@
1
+ # Pre-Deploy Acceptance (Promotion Gate)
2
+
3
+ This guide describes the acceptance harness that runs post-build against an ephemeral stack. Artifacts are promoted only if acceptance checks pass.
4
+
5
+ ## Stack
6
+ - docker-compose.test.yml: api (uvicorn serving tests.acceptance.app), optional db/redis (via profiles), and a tester container to run pytest inside
7
+ - Makefile targets: accept, compose_up, wait, seed, down
8
+ - Health probes: /healthz (liveness), /readyz (readiness), /startupz (startup)
9
+
10
+ ## Workflow
11
+ 1. Build image
12
+ 2. docker compose up -d (test stack)
13
+ 3. CLI DB checks & seed: run `sql setup-and-migrate`, `sql current`, `sql downgrade -1`, `sql upgrade head` against an ephemeral SQLite DB, then call `sql seed tests.acceptance._seed:acceptance_seed` (no-op by default)
14
+ 4. Run pytest inside tester: docker compose run --rm tester (Makefile wires this)
15
+ 5. OpenAPI lint & API Doctor
16
+ 6. Teardown
17
+
18
+ ## Supply-chain & Matrix (v1 scope)
19
+ - SBOM: generate and upload as artifact; image scan (Trivy/Grype) with severity gate.
20
+ - Provenance: sign/attest images (cosign/SLSA) on best-effort basis.
21
+ - Backend matrix: run acceptance against two stacks via COMPOSE_PROFILES:
22
+ 1) in-memory stores (default), 2) Redis + Postgres (COMPOSE_PROFILES=pg-redis).
23
+
24
+ ## Additional Acceptance Checks (fast wins)
25
+ - Headers/CORS: assert HSTS, X-Content-Type-Options, Referrer-Policy, X-Frame-Options/SameSite; OPTIONS preflight behavior.
26
+ - Resilience: restart DB/Redis during request; expect breaker trip and recovery.
27
+ - DR drill: restore a tiny SQL dump then run smoke.
28
+ - OpenAPI invariants: no orphan routes; servers block correctness for versions; 100% examples for public JSON; stable operationIds; reject /auth/{id} path via lint rule.
29
+ - CLI contracts: `svc-infra --help` and key subcommands exit 0 and print expected flags.
30
+
31
+ ## Local usage
32
+ - make accept (runs the full flow locally)
33
+ - make down (tears down the stack)
34
+ - To run tests manually: docker compose run --rm tester
35
+ - To target a different backend: COMPOSE_PROFILES=pg-redis make accept
36
+
37
+ ## Files
38
+ - tests/acceptance/conftest.py: BASE_URL, httpx client, fixtures
39
+ - tests/acceptance/_auth.py: login/register helpers
40
+ - tests/acceptance/_seed.py: seed users/tenants/api keys
41
+ - tests/acceptance/_http.py: HTTP helpers
42
+
43
+ ## Scenarios
44
+ See docs/acceptance-matrix.md for A-IDs and mapping to endpoints.
@@ -0,0 +1,40 @@
1
+ # ADR 0002: Background Jobs & Scheduling
2
+
3
+ Date: 2025-10-15
4
+
5
+ Status: Accepted
6
+
7
+ ## Context
8
+ We need production-grade background job processing and simple scheduling with a one-call setup. The library already includes in-memory queue/scheduler for tests/local. We need a production backend and a minimal runner.
9
+
10
+ ## Decision
11
+ - JobQueue protocol defines enqueue/reserve/ack/fail with retry and exponential backoff (base seconds * attempts). Jobs have: id, name, payload, available_at, attempts, max_attempts, backoff_seconds, last_error.
12
+ - Backends:
13
+ - InMemoryJobQueue for tests/local.
14
+ - RedisJobQueue for production using Redis primitives with visibility timeout and atomic operations.
15
+ - Scheduler:
16
+ - InMemoryScheduler providing interval-based scheduling via next_run_at. Cron parsing is out of scope initially; a simple YAML loader can be added later.
17
+ - Runner:
18
+ - A CLI loop `svc-infra jobs run` will tick the scheduler and process jobs in a loop with small sleep/backoff.
19
+ - Configuration:
20
+ - One-call `easy_jobs()` returns (queue, scheduler). Picks backend via `JOBS_DRIVER` env (memory|redis). Redis URL via `REDIS_URL`.
21
+
22
+ ## Alternatives Considered
23
+ - Using RQ/Huey/Celery: heavier dependency and less control over API ergonomic goals; we prefer thin primitives aligned with svc-infra patterns.
24
+ - SQL-backed queue first: we will consider later; Redis is sufficient for v1.
25
+
26
+ ## Consequences
27
+ - Enables outbox/webhook processors on a reliable queue.
28
+ - Minimal cognitive load: consistent APIs, ENV-driven.
29
+ - Future work: SQL queue, cron YAML loader, metrics, concurrency controls.
30
+
31
+ ## Redis Data Model (initial)
32
+ - List `jobs:ready` holds ready job IDs; a ZSET `jobs:delayed` with score=available_at keeps delayed jobs; a HASH per job `job:{id}` stores fields.
33
+ - Reserve uses RPOPLPUSH from `jobs:ready` to `jobs:processing` or BRPOPLPUSH with timeout; sets `visible_at` on job as now+vt and increments `attempts`.
34
+ - Ack removes job from `jobs:processing` and deletes `job:{id}`.
35
+ - Fail increments attempts and computes next available_at = now + backoff_seconds * attempts; moves job to delayed ZSET.
36
+ - A housekeeping step periodically moves due jobs from delayed ZSET to ready list. Reserve also checks ZSET for due jobs opportunistically.
37
+
38
+ ## Testing Strategy
39
+ - Unit tests cover enqueue/reserve/ack/fail, visibility timeout behavior, and DLQ after max_attempts.
40
+ - Runner tests cover one iteration loop processing.
@@ -0,0 +1,24 @@
1
+ # ADR 0003: Webhooks Framework
2
+
3
+ Date: 2025-10-15
4
+
5
+ Status: Accepted
6
+
7
+ ## Context
8
+ Services need a consistent way to publish domain events to external consumers via webhooks, verify inbound signatures, and handle retries with backoff. We already have an outbox pattern, a job queue, and a webhook delivery worker.
9
+
10
+ ## Decision
11
+ - Event Schema: minimal fields {topic, payload, version, created_at}. Versioning included to evolve payloads.
12
+ - Signing: HMAC-SHA256 over canonical JSON payload; header `X-Signature` carries hex digest. Future: include timestamp and v1 signature header variant.
13
+ - Outbox → Job Queue: Producer writes events to Outbox; outbox tick enqueues delivery jobs; worker performs HTTP POST with signature.
14
+ - Subscriptions: In-memory subscription store maps topic → {url, secret}. Persistence deferred.
15
+ - Verification: Provide helper for verifying incoming webhook requests by recomputing the HMAC.
16
+ - Retry: Already handled by JobQueue backoff; DLQ after max attempts.
17
+
18
+ ## Consequences
19
+ - Clear boundary: producers don't call HTTP directly; they publish to Outbox.
20
+ - Deterministic signing & verification across producer/consumer.
21
+ - Extensibility: timestamped signed headers, secret rotation, persisted subscriptions are future extensions.
22
+
23
+ ## Testing
24
+ - Unit tests for verification helper and end-to-end publish→outbox→queue→delivery using in-memory components and a fake HTTP client.
@@ -0,0 +1,42 @@
1
+ # ADR-0004: Tenancy Model and Enforcement
2
+
3
+ Date: 2025-10-15
4
+
5
+ Status: Proposed
6
+
7
+ ## Context
8
+
9
+ The framework needs a consistent, ergonomic multi-tenant story across modules (API scaffolding, SQL/Mongo persistence, auth/security, payments, jobs, webhooks). Existing patterns already reference `tenant_id` in many places (payments models and service, audit/session models, SQL/Mongo scaffolds). However, enforcement and app ergonomics were not unified.
10
+
11
+ ## Decision
12
+
13
+ Adopt a default "soft-tenant" isolation model via a `tenant_id` column and centralized enforcement primitives:
14
+
15
+ - Resolution: `resolve_tenant_id` and `require_tenant_id` FastAPI dependencies in `api.fastapi.tenancy.context`. Resolution order: override hook → identity (user/api_key) → `X-Tenant-Id` header → `request.state.tenant_id`.
16
+ - Enforcement in SQL: `TenantSqlService(SqlService)` that scopes list/get/update/delete/search/count with a `where` clause and injects `tenant_id` on create when the model supports it. Repository methods accept optional `where` filters.
17
+ - Router ergonomics: `make_tenant_crud_router_plus_sql` which requires `TenantId` and uses `TenantSqlService` under the hood. This keeps route code simple while enforcing scoping.
18
+ - Extensibility: `set_tenant_resolver` hook to override resolution logic per app; `tenant_field` parameter to support custom column names. Future: schema-per-tenant or db-per-tenant via alternate repository/service implementations.
19
+
20
+ ## Alternatives considered
21
+
22
+ 1) Enforce tenancy at the ORM layer (SQLAlchemy events/session) – rejected for clarity and testability; we prefer explicit service/dep composition.
23
+ 2) Global middleware that rewrites queries – rejected due to SQLAlchemy complexity and opacity.
24
+ 3) Only rely on developers to remember filters – rejected due to footguns.
25
+
26
+ ## Consequences
27
+
28
+ - Clear default behavior with escape hatches. Minimal changes for consumers using CRUD builders and SqlService.
29
+ - Requires models to include an optional or required `tenant_id` column for scoping.
30
+ - Non-SQL stores should add equivalent wrappers; Mongo scaffolds already include `tenant_id` fields and can mirror these patterns later.
31
+
32
+ ## Implementation Notes
33
+
34
+ - New modules: `api.fastapi.tenancy.context`, `db.sql.tenant`. Repository updated to accept `where` filters.
35
+ - CRUD router extended with `make_tenant_crud_router_plus_sql` to require `TenantId`.
36
+ - Tests added: `tests/tenancy/*` for resolution and service scoping.
37
+
38
+ ## Open Items
39
+
40
+ - Per-tenant quotas & rate limit overrides (tie into rate limit dependency/middleware via a resolver that returns per-tenant config).
41
+ - Export tenant CLI (dump/import data for a specific tenant).
42
+ - Docs: isolation guidance (column vs schema vs db), migration guidance.
@@ -0,0 +1,86 @@
1
+ # ADR 0005: Data Lifecycle — Soft Delete, Retention, Erasure, Backups
2
+
3
+ Date: 2025-10-16
4
+ Status: Accepted
5
+
6
+ ## Context
7
+ We need a coherent Data Lifecycle story in svc-infra that covers:
8
+ - Migrations & fixtures: simple way to run DB setup/migrations and load reference data.
9
+ - Soft delete conventions: consistent filtering and model scaffolding support.
10
+ - Retention policies: periodic purging of expired records per model/table.
11
+ - GDPR/PII erasure: queued workflow to scrub user-related data while preserving legal audit.
12
+ - Backups/PITR verification: a job that exercises restore checks or at least validates backup health signals.
13
+
14
+ Existing building blocks:
15
+ - Migrations CLI with end-to-end "setup-and-migrate" and new `sql seed` command for executing a user-specified seed callable.
16
+ - Code: `src/svc_infra/cli/cmds/db/sql/alembic_cmds.py` (cmd_setup_and_migrate, cmd_seed)
17
+ - Soft delete support in repository and scaffold:
18
+ - Repo filtering: `src/svc_infra/db/sql/repository.py` (soft_delete flags, `deleted_at` timestamp, optional active flag)
19
+ - Model scaffolding: `src/svc_infra/db/sql/scaffold.py` (optional `deleted_at` field)
20
+ - Easy-setup helper to coordinate lifecycle bits:
21
+ - `src/svc_infra/data/add.py` provides a startup hook to auto-migrate and optional callbacks for fixtures, retention jobs, and an erasure job.
22
+
23
+ Gaps:
24
+ - No standardized fixture loader contract beyond the callback surface.
25
+ - No built-in retention policy registry or purge execution job.
26
+ - No opinionated GDPR erasure workflow and audit trail.
27
+ - No backup/PITR verification job implementation.
28
+
29
+ ## Decision
30
+ Introduce minimal, composable primitives that keep svc-infra flexible while providing a clear path to production-grade lifecycle.
31
+
32
+ 1) Fixture Loader Contract
33
+ - Provide a simple callable signature for deterministic, idempotent fixture loading: `Callable[[], None | Awaitable[None]]`.
34
+ - Document best practices: UPSERT by natural keys, avoid random IDs, guard on existing rows.
35
+ - Expose via `add_data_lifecycle(on_load_fixtures=...)` (already available); add docs and tests.
36
+
37
+ 2) Retention Policy Registry
38
+ - Define a registry API that allows services to register per-resource retention rules.
39
+ - Basic shape:
40
+ - `RetentionPolicy(name: str, model: type, where: list[Any] | None, older_than_days: int, soft_delete_field: str = "deleted_at")`
41
+ - A purge function computes a cutoff timestamp and issues DELETE or marks soft-delete fields.
42
+ - Execution model: a periodic job (via jobs scheduler) calls `run_retention_purge(registry)`.
43
+ - Keep SQL-only first; room for NoSQL extensions later.
44
+
45
+ 3) GDPR Erasure Workflow
46
+ - Provide a single callable entrypoint `erase_principal(principal_id: str) -> None | Awaitable[None]`.
47
+ - Default strategy: enqueue a job that runs a configurable erasure plan composed of steps (delete/soft-delete/overwrite) across tables.
48
+ - Add an audit log entry per erasure request with outcome and timestamp (reuse `security.audit` helpers if feasible).
49
+ - Keep the plan provider pluggable so apps specify which tables/columns participate.
50
+
51
+ 4) Backup/PITR Verification Job
52
+ - Define an interface `verify_backups() -> HealthReport` with a minimal default implementation that:
53
+ - Queries the backup system or driver for last successful backup timestamp and retention window.
54
+ - Emits metrics/logs and returns a structured status.
55
+ - Defer full "restore drill" capability; provide extension hook only.
56
+
57
+ ## Interfaces
58
+ - Registry
59
+ - `register_retention(policy: RetentionPolicy) -> None`
60
+ - `run_retention_purge(session_factory, policies: list[RetentionPolicy]) -> PurgeReport`
61
+ - Erasure
62
+ - `erase_principal(principal_id: str, plan: ErasurePlan, session_factory) -> ErasureReport`
63
+ - Fixtures
64
+ - `load_fixtures()` as provided by caller via `add_data_lifecycle`.
65
+ - Backup
66
+ - `verify_backups() -> BackupHealthReport`
67
+
68
+ ## Alternatives Considered
69
+ - Heavy-weight DSL for retention and erasure: rejected for now; keep APIs Pythonic and pluggable.
70
+ - Trigger-level soft delete enforcement: skipped to avoid provider lock-in; enforced at repository and query layer.
71
+ - Full restore drill automation: out of scope for v1; introduce later behind provider integrations.
72
+
73
+ ## Consequences
74
+ - Minimal surface that doesn't over-constrain adopters; provides default patterns and contracts.
75
+ - Requires additional test scaffolds and example docs to demonstrate usage.
76
+ - SQL-focused initial implementation; other backends can plug via similar interfaces.
77
+
78
+ ## Rollout & Testing
79
+ - Add unit/integration tests for fixture loader, retention purge logic, and erasure workflow skeleton.
80
+ - Provide docs in `docs/database.md` with examples and operational guidance.
81
+
82
+ ## References
83
+ - `src/svc_infra/db/sql/repository.py` soft-delete handling
84
+ - `src/svc_infra/db/sql/scaffold.py` deleted_at field scaffolding
85
+ - `src/svc_infra/data/add.py` data lifecycle helper
86
+ - `src/svc_infra/cli/cmds/db/sql/alembic_cmds.py` migrations & seed