Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

100 lines
2.6 KiB
Go

package resolver
import (
"strings"
"github.com/zzet/gortex/internal/graph"
)
// sidekiqVia is the Meta["via"] tag the Ruby extractor stamps on a Sidekiq
// dispatch placeholder — a `Worker.perform_async(...)` the static graph
// cannot connect to the worker's `perform` because the job runs async.
const sidekiqVia = "sidekiq-dispatch"
// ResolveSidekiqCalls binds Sidekiq job dispatches to the worker's perform
// method: `EmailJob.perform_async(id)` → `EmailJob#perform`. Matching is
// namespace-aware — a `Workers::EmailJob.perform_async` reaches the
// `EmailJob` worker by its simple constant name. The include gate makes
// this precise, so edges land at the typed framework tier.
//
// Returns the number of placeholders landed on a worker perform.
func ResolveSidekiqCalls(g graph.Store) int {
if g == nil {
return 0
}
byFull := map[string][]*graph.Node{}
bySimple := map[string][]*graph.Node{}
for _, n := range nodesByKindsOrAll(g, graph.KindMethod, graph.KindFunction) {
if n == nil || n.Meta == nil {
continue
}
w, _ := n.Meta["sidekiq_worker"].(string)
if w == "" {
continue
}
byFull[w] = append(byFull[w], n)
bySimple[sidekiqSimpleName(w)] = append(bySimple[sidekiqSimpleName(w)], n)
}
if len(byFull) == 0 {
return 0
}
resolved := 0
var reindex []graph.EdgeReindex
for e := range g.EdgesByKind(graph.EdgeCalls) {
if e == nil || e.Meta == nil {
continue
}
if v, _ := e.Meta["via"].(string); v != sidekiqVia {
continue
}
worker, _ := e.Meta["sidekiq_worker"].(string)
if worker == "" {
continue
}
cands := byFull[worker]
if len(cands) == 0 {
cands = bySimple[sidekiqSimpleName(worker)]
}
target := pickStoreAction(g, e, sameBoundaryCandidates(g, e.From, cands))
want := "unresolved::*.perform"
if target != nil {
want = target.ID
}
if e.To == want {
if target != nil {
resolved++
}
continue
}
oldTo := e.To
e.To = want
if target != nil {
e.Origin = graph.OriginASTInferred
e.Confidence = ConfidenceTyped
e.ConfidenceLabel = graph.ConfidenceLabelFor(graph.EdgeCalls, ConfidenceTyped)
StampSynthesizedTyped(e, SynthSidekiq)
resolved++
} else {
e.Origin = graph.OriginASTInferred
e.Confidence = 0
e.ConfidenceLabel = ""
UnstampSynthesized(e)
}
reindex = append(reindex, graph.EdgeReindex{Edge: e, OldTo: oldTo})
}
if len(reindex) > 0 {
g.ReindexEdges(reindex)
}
return resolved
}
// sidekiqSimpleName returns the last `::` segment of a Ruby constant path.
func sidekiqSimpleName(s string) string {
if i := strings.LastIndex(s, "::"); i >= 0 {
return s[i+2:]
}
return s
}