(
index: BugPathIndex,
observations: BugPathObservations,
target: BugPathNode,
options: BugPathOptions,
)
| 474 | * the two differ. |
| 475 | */ |
| 476 | export async function bugPaths( |
| 477 | index: BugPathIndex, |
| 478 | observations: BugPathObservations, |
| 479 | target: BugPathNode, |
| 480 | options: BugPathOptions, |
| 481 | ): Promise<BugPathsDto> { |
| 482 | const depth = clamp(options.depth, BUG_PATH_DEFAULT_DEPTH, BUG_PATH_MAX_DEPTH); |
| 483 | const chainCap = clamp(options.chains, BUG_PATH_MAX_CHAINS, BUG_PATH_MAX_CHAINS); |
| 484 | const flowCap = clamp(options.maxFlows, BUG_PATH_MAX_FLOWS, BUG_PATH_MAX_FLOWS); |
| 485 | const project = options.project; |
| 486 | |
| 487 | const walked = await staticChains(index, project, target, depth, chainCap); |
| 488 | const expected = chainEdges(walked.chains); |
| 489 | |
| 490 | // ---------------------------------------------------- observations ------ |
| 491 | const observedEdges = new Map<string, { from: BugPathNode; to: BugPathNode; observed: ObservedRecord }>(); |
| 492 | const observedPaths: BugPathNode[][] = []; |
| 493 | |
| 494 | const remember = (from: BugPathNode, to: BugPathNode, observed: ObservedRecord): void => { |
| 495 | const key = edgeKey(from, to); |
| 496 | if (!observedEdges.has(key)) { |
| 497 | observedEdges.set(key, { from, to, observed }); |
| 498 | } |
| 499 | }; |
| 500 | |
| 501 | if (target.qualifiedName !== undefined) { |
| 502 | const asked = new Set<string>(); |
| 503 | for (const chain of walked.chains) { |
| 504 | /* |
| 505 | * From the outside in, until one end answers. |
| 506 | * |
| 507 | * The trace resolves its endpoints against the callable graph, and a |
| 508 | * chain can start at something that is not one: file-level code that |
| 509 | * calls `main` is a Module node, the index records the call, and |
| 510 | * `/api/trace` answers "source is not an indexed callable". Stopping |
| 511 | * at the head would therefore lose every observation on a chain that |
| 512 | * begins outside a function, which is most chains of most programs. |
| 513 | * So the next hop is asked, and the next, until an answer comes back |
| 514 | * reachable. Each source is asked once across all chains. |
| 515 | */ |
| 516 | for (let at = 0; at + 1 < chain.length; at++) { |
| 517 | const from = chain[at]; |
| 518 | if (from.qualifiedName === undefined || asked.has(from.qualifiedName)) { |
| 519 | continue; |
| 520 | } |
| 521 | asked.add(from.qualifiedName); |
| 522 | let answer: TraceAnswer; |
| 523 | try { |
| 524 | answer = await observations.trace(project, from.qualifiedName, target.qualifiedName); |
| 525 | } catch { |
| 526 | continue; |
| 527 | } |
| 528 | if (!answer.reachable) { |
| 529 | continue; |
| 530 | } |
| 531 | for (let hop = 1; hop < answer.path.length; hop++) { |
| 532 | const step = answer.path[hop]; |
| 533 | if (step.observed !== undefined) { |
no test coverage detected