Commit eb4c4f3
Closes task #88 per PM @不穷 dispatch (msg=8f130f25). Implements task
#61 spec v1 § 2.4 P2-S1 + Planetegg P2-HIGH (msg=db7fb085 +
msg=1314ac59) + Singapore SRE diagnostic (Planetegg msg=4043adf4)
batch alias resolution.
Background
----------
``LineageGraphStoreWithAliasRedirect.expand_neighbors_n_hops`` is on
the ``GET /api/v2/collections/{id}/graphs`` and ``/graphs/hybrid``
read paths. Pre-fix, it called ``AliasMapRepository.resolve_canonical``
once per anchor name via ``asyncio.gather``, which checks out one
PG connection per name. Spec § 2.4 P2-S1 quantification:
* ``GET /graphs?max_nodes=1000`` → up to **2 × max_nodes = 2000**
seeds.
* ``GET /graphs/hybrid``: default 1000 / max 5000 seeds.
At those cardinalities the PG connection pool saturates, observed
in Singapore production (Planetegg msg=4043adf4 SRE diagnostic).
Changes
-------
* ``aperag/graph_curation/alias_map.py``: new
:meth:`AliasMapRepository.resolve_canonical_many` batch primitive.
Single SQL ``SELECT alias_name, canonical_name FROM
aperag_lineage_entity_alias WHERE collection_id=? AND alias_name
IN (...)`` reads all matching rows in one shot. Names absent from
the result set fall back to themselves (mirrors single-name
``resolve_canonical`` semantics). Empty / falsy names short-
circuit without an SQL lookup. Total connections checked out: **1**
per call regardless of seed count. Caller order is preserved on
the dict iteration order (insertion order semantics).
* ``aperag/indexing/alias_redirect_store.py``: rewrite
``LineageGraphStoreWithAliasRedirect.expand_neighbors_n_hops`` to
use the batch primitive. ``asyncio.gather`` per-name fan-out gone;
``import asyncio`` no longer needed at module top-level.
* Test stub ``_FakeAliasRepo`` in
``tests/unit_test/indexing/test_alias_redirect_store.py``: now
implements both ``resolve_canonical`` (single, used by
upsert/get/delete redirect paths) and ``resolve_canonical_many``
(batch, used by expand) + tracks call counts so tests can pin the
call-graph (i.e. expand path goes through batch primitive exactly
once).
Tests
-----
* ``tests/unit_test/graph_curation/test_alias_map.py`` (7 new):
- ``test_resolve_canonical_many_returns_self_for_unmapped_names``
- ``test_resolve_canonical_many_mixed_alias_and_canonical``
- ``test_resolve_canonical_many_dedupes_input``
- ``test_resolve_canonical_many_empty_input``
- ``test_resolve_canonical_many_handles_empty_string``
- ``test_resolve_canonical_many_per_collection_isolation``
- ``test_resolve_canonical_many_large_seed_cap`` (2000-name spec
quantification — pinned correctness at the spec-cap so a future
regression that re-introduces per-name fan-out either times out
or breaks the result shape).
* ``tests/unit_test/indexing/test_alias_redirect_store.py`` (2 new):
- ``test_expand_neighbors_uses_batch_alias_resolution`` —
call-graph gate: exactly 1 ``resolve_canonical_many`` call,
zero ``resolve_canonical`` calls, regardless of seed count. A
regression that re-introduces the gather pattern is caught
immediately.
- ``test_expand_neighbors_large_seed_cap_uses_single_batch_call``
— 2000-seed spec-cap pinned at the call-graph level.
Local: ``uv run pytest tests/unit_test/graph_curation/
tests/unit_test/indexing/test_alias_redirect_store.py`` →
**56 passed, 1 warning**.
Spec / scope alignment
----------------------
* task #61 spec v1 § 2.4 P2-S1 — batch resolve primitive ✅
* task #61 spec v1 § 2.4 P2-S2 — ``expand_neighbors_n_hops`` seed
cap test ✅
* Lesson #17 backend 收敛 contract — single primitive replaces N-
parallel fan-out at the same caller, no FE / API changes
required ✅
* Lesson #18 mechanical gate codification — call-graph assertion in
the redirect-store test is the mechanical gate (caught by CI on
any future regression that bypasses the batch primitive) ✅
Follow-ups (NOT in this PR)
---------------------------
* P3 cross-cutting concern: every ``LineageGraphStore`` consumer
that currently invokes the alias path per-name (e.g. some
GraphCurationService internals) should migrate to the batch
primitive — independent task gated on production data showing
the residual N-fan-out is a real bottleneck.
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 7d1adfa commit eb4c4f3
4 files changed
Lines changed: 341 additions & 14 deletions
File tree
- aperag
- graph_curation
- indexing
- tests/unit_test
- graph_curation
- indexing
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
| |||
92 | 99 | | |
93 | 100 | | |
94 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
95 | 182 | | |
96 | 183 | | |
97 | 184 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | 73 | | |
75 | 74 | | |
76 | 75 | | |
| |||
263 | 262 | | |
264 | 263 | | |
265 | 264 | | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
273 | 285 | | |
274 | 286 | | |
275 | | - | |
276 | | - | |
| 287 | + | |
| 288 | + | |
277 | 289 | | |
278 | 290 | | |
279 | | - | |
| 291 | + | |
| 292 | + | |
280 | 293 | | |
281 | 294 | | |
282 | 295 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
0 commit comments