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