# Histogram Bug Discovery Guidelines

Use these instructions to find existing "Check expiry" bugs for histograms, or
to create a new tracking bug.

## The Discovery Query

**CRITICAL MANDATE:** Use the `mcp_Coding_internal_search` tool (Moma) for ALL
discovery and verification. **NEVER use `mcp_Buganizer_get_bugs` or `get_bugs`**
for this process, as they redact Chromium issues and return unrelated internal
bugs.

Use this exact pattern in Moma:
`"Check expiry of your histograms" "<HistogramName>" "<ExpiryDate>" status:open`

- **<HistogramName>**: The full name of the metric.
- **<ExpiryDate>**: The `expires_after` date from the XML (e.g., `2021-10-25`).

## Triage Rules (Oldest-ID-Wins)

Automated sweeps often create new bugs when old ones are ignored. However, the
oldest bug is frequently the one that remains open while newer duplicates are
closed.

1. **Extract IDs based on the Moma URL:** Look at the `URL:` provided in each
   Moma result to definitively determine the bug ID.
   - **Buganizer URLs** (`b.corp.google.com/issues/<ID>` or
     `issuetracker.google.com/issues/<ID>`): The `<ID>` is already a Buganizer
     ID. Use it directly.
   - **Monorail URLs** (`bugs.chromium.org/p/chromium/issues/detail?id=<ID>`):
     Use `curl -sL` to fetch this exact URL and extract the official migrated
     Buganizer ID from the redirect script (e.g.,
     `curl -sL "https://bugs.chromium.org/p/chromium/issues/detail?id=<ID>" | grep -o 'issues\.chromium\.org/[0-9]*'`).
     Use the extracted ID.
2. **Prioritize the lowest Buganizer ID:** After extracting or resolving the
   Buganizer IDs from all the Moma links, compare them and use the **lowest
   numeric ID** in the `Bug:` footer to ensure linking to the oldest tracker.
3. **Verify Status:** Only use the ID if the Moma snippet suggests it is
   `status:open` or actively tracking expiry. Do **not** use Buganizer tools
   (`render_issue`, etc.) to verify the bug, as they will fail on restricted
   Chromium issues. Rely entirely on the information from Moma and the `curl`
   redirect.

## Manual Triage (Fallback)

If no open bug is found, or if the Moma results only show closed/won't fix bugs,
use `ask_user` (`type='choice'`) to determine bug handling:

- `label`: "Generate New Bug", `description`: "Auto-create a Buganizer issue in
  the CodeHealth component"
- `label`: "Provide Existing Bug", `description`: "User will provide the
  existing Bug ID"
- `label`: "Skip Bug", `description`: "Do not include a Bug reference in the
  footer"

### Execution

- **Generate New Bug:** Use `run_shell_command` (`whoami`) for LDAP. Use
  `create_buganizer_issue` (componentId: "1456931", hotlistIds: ["8218789"],
  issueType: "INTERNAL_CLEANUP", priority: "P2", assignee: "<LDAP>"). Title:
  `[histogram-cleanup] Remove expired histogram: <HistogramName>`. Description
  MUST start with: *"Note: This bug was automatically generated by the Chrome
  Code Health AI."* followed by a statement that the histogram is being removed
  as it has expired and has no recording sites.
- **Provide Existing Bug:** Use `ask_user` (`type='text'`) with `question`:
  "What is the Buganizer ID?".
- **Skip Bug:** Proceed without a Bug ID (use "none").
