| 608 | * wrong. |
| 609 | */ |
| 610 | export function getDeferredToolsDelta( |
| 611 | tools: Tools, |
| 612 | messages: Message[], |
| 613 | scanContext?: DeferredToolsDeltaScanContext, |
| 614 | ): DeferredToolsDelta | null { |
| 615 | const announced = new Set<string>() |
| 616 | let attachmentCount = 0 |
| 617 | let dtdCount = 0 |
| 618 | const attachmentTypesSeen = new Set<string>() |
| 619 | for (const msg of messages) { |
| 620 | if (msg.type !== 'attachment') continue |
| 621 | attachmentCount++ |
| 622 | attachmentTypesSeen.add(msg.attachment!.type) |
| 623 | if (msg.attachment!.type !== 'deferred_tools_delta') continue |
| 624 | dtdCount++ |
| 625 | for (const n of msg.attachment!.addedNames) announced.add(n) |
| 626 | for (const n of msg.attachment!.removedNames) announced.delete(n) |
| 627 | } |
| 628 | |
| 629 | const deferred: Tool[] = tools.filter(isDeferredTool) |
| 630 | const deferredNames = new Set(deferred.map(t => t.name)) |
| 631 | const poolNames = new Set(tools.map(t => t.name)) |
| 632 | |
| 633 | const added = deferred.filter(t => !announced.has(t.name)) |
| 634 | const removed: string[] = [] |
| 635 | for (const n of announced) { |
| 636 | if (deferredNames.has(n)) continue |
| 637 | if (!poolNames.has(n)) removed.push(n) |
| 638 | // else: undeferred — silent |
| 639 | } |
| 640 | |
| 641 | if (added.length === 0 && removed.length === 0) return null |
| 642 | |
| 643 | // Diagnostic for the inc-4747 scan-finds-nothing bug. Round-1 fields |
| 644 | // (messagesLength/attachmentCount/dtdCount from #23167) showed 45.6% of |
| 645 | // events have attachments-but-no-DTD, but those numbers are confounded: |
| 646 | // subagent first-fires and compact-path scans have EXPECTED prior=0 and |
| 647 | // dominate the stat. callSite/querySource/attachmentTypesSeen split the |
| 648 | // buckets so the real main-thread cross-turn failure is isolable in BQ. |
| 649 | logEvent('tengu_deferred_tools_pool_change', { |
| 650 | addedCount: added.length, |
| 651 | removedCount: removed.length, |
| 652 | priorAnnouncedCount: announced.size, |
| 653 | messagesLength: messages.length, |
| 654 | attachmentCount, |
| 655 | dtdCount, |
| 656 | callSite: (scanContext?.callSite ?? |
| 657 | 'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 658 | querySource: (scanContext?.querySource ?? |
| 659 | 'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 660 | attachmentTypesSeen: [...attachmentTypesSeen] |
| 661 | .sort() |
| 662 | .join(',') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 663 | }) |
| 664 | |
| 665 | return { |
| 666 | addedNames: added.map(t => t.name).sort(), |
| 667 | addedLines: added.map(formatDeferredToolLine).sort(), |