( tools: Tools, messages: Message[], scanContext?: DeferredToolsDeltaScanContext, )
| 646 | * wrong. |
| 647 | */ |
| 648 | export function getDeferredToolsDelta( |
| 649 | tools: Tools, |
| 650 | messages: Message[], |
| 651 | scanContext?: DeferredToolsDeltaScanContext, |
| 652 | ): DeferredToolsDelta | null { |
| 653 | const announced = new Set<string>() |
| 654 | let attachmentCount = 0 |
| 655 | let dtdCount = 0 |
| 656 | const attachmentTypesSeen = new Set<string>() |
| 657 | for (const msg of messages) { |
| 658 | if (msg.type !== 'attachment') continue |
| 659 | attachmentCount++ |
| 660 | attachmentTypesSeen.add(msg.attachment.type) |
| 661 | if (msg.attachment.type !== 'deferred_tools_delta') continue |
| 662 | dtdCount++ |
| 663 | for (const n of msg.attachment.addedNames) announced.add(n) |
| 664 | for (const n of msg.attachment.removedNames) announced.delete(n) |
| 665 | } |
| 666 | |
| 667 | const deferred: Tool[] = tools.filter(isDeferredTool) |
| 668 | const deferredNames = new Set(deferred.map(t => t.name)) |
| 669 | const poolNames = new Set(tools.map(t => t.name)) |
| 670 | |
| 671 | const added = deferred.filter(t => !announced.has(t.name)) |
| 672 | const removed: string[] = [] |
| 673 | for (const n of announced) { |
| 674 | if (deferredNames.has(n)) continue |
| 675 | if (!poolNames.has(n)) removed.push(n) |
| 676 | // else: undeferred — silent |
| 677 | } |
| 678 | |
| 679 | if (added.length === 0 && removed.length === 0) return null |
| 680 | |
| 681 | // Diagnostic for the inc-4747 scan-finds-nothing bug. Round-1 fields |
| 682 | // (messagesLength/attachmentCount/dtdCount from #23167) showed 45.6% of |
| 683 | // events have attachments-but-no-DTD, but those numbers are confounded: |
| 684 | // subagent first-fires and compact-path scans have EXPECTED prior=0 and |
| 685 | // dominate the stat. callSite/querySource/attachmentTypesSeen split the |
| 686 | // buckets so the real main-thread cross-turn failure is isolable in BQ. |
| 687 | logEvent('ncode_deferred_tools_pool_change', { |
| 688 | addedCount: added.length, |
| 689 | removedCount: removed.length, |
| 690 | priorAnnouncedCount: announced.size, |
| 691 | messagesLength: messages.length, |
| 692 | attachmentCount, |
| 693 | dtdCount, |
| 694 | callSite: (scanContext?.callSite ?? |
| 695 | 'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 696 | querySource: (scanContext?.querySource ?? |
| 697 | 'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 698 | attachmentTypesSeen: [...attachmentTypesSeen] |
| 699 | .sort() |
| 700 | .join(',') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 701 | }) |
| 702 | |
| 703 | return { |
| 704 | addedNames: added.map(t => t.name).sort(), |
| 705 | addedLines: added.map(formatDeferredToolLine).sort(), |
no test coverage detected