perf(expand): prefetch descendant grants to eliminate per-principal SQL queries #863
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.42) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 328 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.347
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #863"
general
weight: 0.35
arreyder
Summary
ListGrantsForEntitlementSQL queries inrunActionwith a single bulk pre-fetch into an in-memory mapProblem
Profile evidence from be-temporal-sync prod (1h aggregate):
dotc1z.listGrantsGeneric → ListGrantsForEntitlement → database/sql.Rows.Next → modernc.org/sqlite._sqlite3VdbeExecThe inner loop in
runAction(previously lines 278-350) calledListGrantsForEntitlementwith a specificPrincipalIdfor each source grant. Even though the compound index(entitlement_id, principal_resource_type_id, principal_resource_id)makes each a point lookup returning 0-1 rows, the per-query overhead in pure-Go SQLite (~0.1-1ms per call) compounds to 38 minutes at 180K source grants.Fix
Pre-fetch ALL grants on the descendant entitlement into
map[principalKey][]*v2.Grantbefore entering the source grants loop. Each source grant then does a map lookup (O(1)) instead of a SQL query.The pre-fetch is per-
runActioncall, not cached across actions. This ensures correctness in diamond graphs: if action A→C creates grants that action B→C must see, the B→C call does a fresh pre-fetch that includes the writes from A→C.Memory tradeoff
For the worst case (descendant with 180K grants × ~2KB each = ~360MB peak), this is within budget for worker pods (32GB RAM). Most entitlements are much smaller.
Test plan
go test ./pkg/sync/expand/...— all pass (including diamond graph, multi-level, mixed directness, cycle tests)go test ./pkg/sync/...— all passgo test -short ./pkg/dotc1z/...— all passgo test -bench=BenchmarkExpand ./pkg/sync/expand/...— 70% improvementBenchmark
🤖 Generated with Claude Code