GitNexus/gitnexus/bench/group-link-matching/measure.mjs
samuel mbabhazi 1f5ac81157
fix(group): match a manifest link's consumer only in the repo it names (#3881)
* fix(group): match a manifest link's consumer only in the repo it names

A group.yaml link adds a consumer contract to the exact and wildcard
passes, and those passes matched it on the contract id alone. Declaring
`gen-question → learning POST /import` in a group where user-service also
serves `POST /import` produced a second, undeclared cross-link from
gen-question to user-service.

The consumer contract a link creates now carries the repo the link names
(`meta.providerRepo`), and both passes skip providers outside it. In the
wildcard pass the filter runs before the thrift ambiguity check, so a
same-named service elsewhere in the group no longer blocks the declared
one. Consumers found in source still match across the whole group.

The field only steers matching. Sync drops it before writing the
registry, so contracts.json keeps its shape and a consumer whose provider
could not be read does not name that repo.

* fix(group): address review feedback on declared-repo matching (#3881)

- Track matched consumers as records in the exact and wildcard passes.
  Two consumers can share `repo::contractId` and still end differently
  now that a link consumer only matches the repo its link names, so the
  shared key hid the one that matched nothing from `unmatched`.
- Tests: the bookkeeping in both passes, a service-wildcard link through
  syncGroup, the strip on `unmatched`, the contracts handed to the
  bridge write, the exact pass on topic, lib, thrift and method-agnostic
  http contracts, and a declared repo that prefixes another repo's name.
- PIPELINE.md: say that the rule covers every link type and what still
  matches across the whole group.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(group): gate manifest-link matching on identity and linear scaling (#1188)

Build-free bench for the matching stage of syncGroup (provider index,
exact pass, wildcard pass, strip of the matching-only providerRepo) over
a synthetic group shaped like #1188: each link's route is served by four
repos, half the links have a source-extracted twin consumer, and every
fifth link names a repo that contributes nothing.

- fingerprint over every cross-link and unmatched contract at 8,000
  links. The matcher from before #1188 and the repo::contractId
  bookkeeping both move it; a missing strip fails a separate count.
- scaling ratio between 2,000 and 8,000 links, budget 1.8. Measured
  1.04-1.19; a sweep from 1,000 to 32,000 links has a log-log slope of
  1.13. An extra provider-index walk per consumer measures 2.0-2.3.

Wired into the benchmarks job. baselines.json also records two costs
that predate #1188 and are not gated: service-wildcard links and
method-agnostic http links each walk the provider index once per link.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-15 16:22:31 +00:00

182 lines
7 KiB
JavaScript

/**
* Build-free scaling + identity bench for group.yaml link matching (#1188).
*
* A `links:` entry creates a consumer contract that may only match providers
* in the repo the link names. This bench runs the matching stage of
* `syncGroup` (provider index, exact pass, wildcard pass, and the strip of the
* matching-only `meta.providerRepo`) over a synthetic group and gates two
* things:
*
* - `fingerprint`: every cross-link and every unmatched contract at the
* LARGE scale. It moves when a link consumer matches a repo its link does
* not name, when a consumer the passes could not place drops out of
* `unmatched`, or when `providerRepo` survives the strip.
* - `scaling_ratio`: (t_large / t_small) / (LARGE / SMALL). About 1.0 is
* linear in the number of links.
*
* Corpus, per link i (the #1188 shape, scaled):
* - an http link `svc-a -> svc-b POST::/import/i`, built by the real
* `ManifestExtractor`;
* - the same route served from source by four repos, the declared one first;
* - every 2nd link also has a source-extracted consumer in the consumer repo
* (it matches all four, which is the unrestricted case);
* - every 5th link names a repo that contributes nothing this run (its
* contracts are dropped, as `syncGroup` does for an unreadable repo), so
* the link consumer stays unmatched while its source twin, where present,
* matches.
* Plus a FIXED number of service-wildcard links over a gRPC provider set that
* grows with the corpus. The count is fixed on purpose: the wildcard pass
* walks the whole provider index once per wildcard consumer, so its cost is
* (wildcard links x provider keys) and only one of the two may scale here.
*
* Usage:
* node --import tsx bench/group-link-matching/measure.mjs
* node --import tsx bench/group-link-matching/measure.mjs --check
* node --import tsx bench/group-link-matching/measure.mjs --root <other checkout>/gitnexus
*/
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { minSample } from '../lib/identity-guard.mjs';
import { fingerprintIds, runBaselineCheck, runCountCheck } from '../lib/route-constant-guard.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const BASELINE_PATH = path.resolve(__dirname, 'baselines.json');
const SMALL = 2000;
const LARGE = 8000;
const REPS = 15;
const WARMUP = 5;
const PROVIDERS_PER_ROUTE = 4;
const WILDCARD_LINKS = 16;
// `--root` loads the matcher from another checkout, so the same corpus can be
// timed against a different revision.
const rootFlag = process.argv.indexOf('--root');
const ROOT =
rootFlag >= 0 ? path.resolve(process.argv[rootFlag + 1]) : path.resolve(__dirname, '../..');
const load = (rel) => import(pathToFileURL(path.join(ROOT, rel)).href);
const matching = await load('src/core/group/matching.ts');
const { ManifestExtractor } = await load('src/core/group/extractors/manifest-extractor.ts');
const { buildProviderIndex, runExactMatch, runWildcardMatch } = matching;
// Absent before #1188; the identity keeps `--root` usable on such a revision.
const strip = matching.withoutDeclaredProvider ?? ((contract) => contract);
const repoName = (n, repoCount) => `svc-${n % repoCount}`;
function sourceContract(contractId, role, repo, name) {
return {
contractId,
type: contractId.slice(0, contractId.indexOf('::')),
role,
symbolUid: `${role}:${repo}:${name}`,
symbolRef: { filePath: `src/${name}.ts`, name },
symbolName: name,
confidence: 0.9,
meta: {},
repo,
};
}
async function buildCorpus(linkCount) {
const repoCount = Math.max(16, linkCount / 8);
const links = [];
const source = [];
// `<repo>|<contractId>` of provider contracts a link's unread repo would have
// contributed, removed below.
const unread = new Set();
for (let i = 0; i < linkCount; i++) {
const consumerRepo = repoName(i, repoCount);
const declaredRepo = repoName(i + 1, repoCount);
const contractId = `http::POST::/import/${i}`;
const declaredIsUnread = i % 5 === 0;
links.push({
from: consumerRepo,
to: declaredRepo,
type: 'http',
contract: `POST::/import/${i}`,
role: 'consumer',
});
if (declaredIsUnread) unread.add(`${declaredRepo}|${contractId}`);
for (let k = declaredIsUnread ? 1 : 0; k < PROVIDERS_PER_ROUTE; k++) {
source.push(
sourceContract(contractId, 'provider', repoName(i + 1 + k, repoCount), `import${i}`),
);
}
if (i % 2 === 0) {
source.push(sourceContract(contractId, 'consumer', consumerRepo, `callImport${i}`));
}
}
for (let w = 0; w < WILDCARD_LINKS; w++) {
links.push({
from: repoName(w, repoCount),
to: repoName(w + 1, repoCount),
type: 'grpc',
contract: `pkg${w}.Svc${w}/*`,
role: 'consumer',
});
}
for (let j = 0; j < linkCount / 4; j++) {
const contractId = `grpc::pkg${j}.Svc${j}/Call`;
source.push(sourceContract(contractId, 'provider', repoName(j + 1, repoCount), `call${j}`));
source.push(sourceContract(contractId, 'provider', repoName(j + 2, repoCount), `call${j}`));
}
const manifest = await new ManifestExtractor().extractFromManifest(links);
const fromLinks = manifest.contracts.filter(
(c) => !(c.role === 'provider' && unread.has(`${c.repo}|${c.contractId}`)),
);
return [...source, ...fromLinks];
}
function match(contracts) {
const index = buildProviderIndex(contracts);
const exact = runExactMatch(contracts, index);
const wildcard = runWildcardMatch(exact.unmatched, index);
return {
crossLinks: [...exact.matched, ...wildcard.matched],
contracts: contracts.map(strip),
unmatched: wildcard.remaining.map(strip),
};
}
// Built outside the timed region: these strings grow with the corpus.
function identities(result) {
return [
...result.crossLinks.map(
(l) =>
`${l.matchType}|${l.from.repo}|${l.from.symbolUid}|${l.to.repo}|${l.to.symbolUid}|${l.contractId}`,
),
...result.unmatched.map((c) => `unmatched|${c.role}|${c.repo}|${c.contractId}|${c.symbolUid}`),
];
}
async function measure(linkCount) {
const contracts = await buildCorpus(linkCount);
const { last, ms } = minSample(() => match(contracts), WARMUP, REPS);
return {
links: linkCount,
contracts: contracts.length,
ms: Number(ms.toFixed(3)),
crossLinks: last.crossLinks.length,
unmatched: last.unmatched.length,
leakedProviderRepo: [...last.contracts, ...last.unmatched].filter(
(c) => 'providerRepo' in c.meta,
).length,
fingerprint: fingerprintIds(identities(last)),
};
}
const report = { small: await measure(SMALL), large: await measure(LARGE) };
report.scaling_ratio = Number((report.large.ms / report.small.ms / (LARGE / SMALL)).toFixed(3));
report.absolute_ms = report.large.ms;
report.fingerprint = report.large.fingerprint;
if (!process.argv.includes('--check')) {
console.log(JSON.stringify(report, null, 2));
process.exit(0);
}
runCountCheck(report, 'leakedProviderRepo', { small: 0, large: 0 });
runBaselineCheck(report, BASELINE_PATH);