Ticket contract for automated reporters: dedup_key, source, links, reopen window (#123) #124

Merged
jared merged 1 commits from dedup-key-contract into development 2026-11-25 20:24:37 -05:00
Owner

Closes #123. This is Tinker's side of the ticket contract from the thor#1 planning session: each reporter (gandalf, hwmonDaemon, thor) decides what "the same problem" is, and Tinker enforces it.

What's in it

1. dedup_key on create_ticket_api.php

  • The format is scope:subject:kind, e.g. host:compute-storage-01:oom.
  • When a key is sent, the hash is sha256("key:" + key) and the title is ignored. An invalid key gets a 400.
  • Keyless callers hash exactly as before. I compared old and new code over every title on prod (230 titles, plus serial / check_code variants): 1,380 payloads, 0 differences.
  • Deviation from the issue: the issue's format says [a-z0-9_.:-], but its own example cluster:ceph:BLUESTORE_SLOW_OP_ALERT is uppercase, and so are Ceph's health codes. So keys allow A-Z and are case-sensitive, and the column is ascii_bin so lookups match the hash. Thor and Gandalf should send keys exactly as they want them stored.

2. Lookup by key: GET api/tickets_api.php?dedup_key=K

  • Returns the newest ticket with that key, in any status, plus recurrences: the earlier tickets in its chain.
  • The key's visibility rules apply.
  • The list view also gets ?source=.

3. Link API: new POST api/ticket_link_api.php {ticket_id, other_ticket_id, relation[, action: "unlink"]}, read_write scope.

  • It goes through the new DependencyService::link() / unlink(), which MCP link_tickets / unlink_tickets now use too (moved out of the MCP class, same behaviour).
  • Audit rows record the key name.
  • Status codes: 404 for a missing or hidden ticket, 409 for a duplicate or circular link, 400 for a bad relation.

4. Creator attribution: a new tickets.source column holds the API key name.

  • The dashboard's Created By column and the ticket page show it in place of "System": "thor (API)".
  • The sidebar has a new Source filter.
  • MCP results include source, and search_tickets takes a source filter.
  • Comments the create API writes now name the actual reporter. They were hard-coded as "hwmonDaemon", even when Gandalf caused them.
  • Comments linked to the system user now display their stored author.

5. Reopen window, for every reporter, keyed or not. When a report matches a Closed ticket:

  • Closed under AUTO_REOPEN_WINDOW_DAYS (default 7) ago: same as today. Reopen it if the Workflow Designer allows an unattended reopen, otherwise recurrence_noted.
  • Closed longer ago, new_recurrence:
    • A new ticket takes over the dedup slot. hash stays UNIQUE, and the old ticket gives up its hash but keeps its key.
    • The new ticket records recurrence_of and is linked relates_to the old one.
    • It gets a history comment: "Recurrence of #X ... seen N times in the last 7 days and M in the last 30".
    • The response includes previous_ticket_id.

6. Backfills, per app

  • scripts/backfill_dedup_keys.php --map=file.json [--dry-run]: the app supplies {ticket_id: dedup_key}. It detects conflicts, re-keys the hash, and writes one audit row per ticket.
  • scripts/backfill_ticket_source.php [--dry-run]: fills source on old API tickets where it's certain.
    • Box layout means hwmonDaemon. Gandalf's own footer means gandalf.
    • Against prod (read-only): about 137 hwmonDaemon, 9 gandalf, and the rest left unset.

Migration 008_dedup_key_source.sql: adds dedup_key, source and recurrence_of. All are nullable, so the currently deployed code keeps working.

⚠️ Decision needed: the 7-day reopen doesn't happen on prod today

On prod, the Workflow Designer's Closed → Open needs admin + comment, and so does Closed → In Progress. The create API respects the workflow, so an automated report never reopens a closed ticket on prod. It only adds a "recurrence noted, left Closed" comment, which is what happened on 993441773. This PR keeps that, so within the 7 days prod gives recurrence_noted, not reopened.

To get the contract's reopen, either relax Closed → Open in the Workflow Designer for unattended use, or tell me to let automated reports reopen regardless of the workflow. The tests below cover both configurations.

Screenshots (local, synthetic data; development on the left, this PR on the right)

Source filter in the sidebar:
sidebar

Created By shows the reporter instead of "System":
table

Filtered to source=thor (development ignores the parameter):
filtered

Ticket page, Created By:
ticket

A new_recurrence history comment, now credited to thor instead of System:
comment

The negative "-17860s ago" times are an artifact of the local DB's timezone, not this PR. The capture fails on page or console errors, and there were none.

Verified locally (throwaway tinker_ui DB)

End-to-end over HTTP: 42/42 with prod's workflow, and 42/42 with an unattended Closed → Open.

  • Create and dedupe: a keyed create dedupes, and an escalation comment by thor names thor. An invalid key is a 400. Keyless creates keep the legacy hash.
  • Reopen window:
    • Closed 2 days ago: reopened or noted, depending on the workflow.
    • Closed 10 days ago: new_recurrence, the old hash released, the link and the history counts (2 in 7 days, 3 in 30) correct, and the next report dedups onto the new ticket.
    • Keyless reports get the same new_recurrence.
    • A Closed ticket with no closed_at falls back to updated_at.
  • Concurrency: 8 concurrent identical creates produce 1 ticket.
  • Uppercase keys: accepted and case-sensitive.
  • Key lookup:
    • A hit returns the newest ticket plus recurrences; a miss is a 404; a bad key is a 400.
    • A confidential match is a 404 for a public-only key and visible to a see-all key.
    • The ?source= filter works.
  • Link API:
    • Link works; a duplicate, an inverse duplicate or a circular link is a 409.
    • Unlink works from the other side; unlinking again is a 404.
    • A hidden ticket is a 404 for a public-only key and works for a see-all key.
    • Status codes: a read key is a 403, a bad relation is a 400, a self link is a 400, a missing ticket is a 404 and GET is a 405.
    • The audit row records api_key.

Everything else:

  • MCP link_tickets / unlink_tickets after the move: 11/11, with the same messages as before.
  • Backfill scripts:
    • Dry runs and real runs.
    • backfill_dedup_keys reports invalid keys, missing tickets and conflicts, and marks Closed tickets.
    • A keyed report after the backfill lands on the adopted ticket.
    • Re-runs are no-ops.
  • Migration 008 applies with the current migrate.php, and the new columns' collations match prod.
  • Lint: phpcs is clean, and eslint reports no errors.

Rollout (beta and prod share one DB, and deploys don't run migrations)

  1. Before merging, apply 008 once on CT132. Otherwise beta's dashboard query for the Source filter fails. It's additive and safe for the code currently on prod.
  2. Merge to development, then run backfill_ticket_source.php --dry-run and the real run.
  3. NPM: add a location = /api/ticket_link_api.php exemption, like the other Bearer endpoints, on both t. and beta.t. Gandalf and hwmonDaemon post to CT132 directly, so this only matters for callers going through the proxy.
  4. Key backfills happen per app, when it starts sending keys. Thor's 4 Ceph tickets come later with its mapping file.

Behaviour change on deploy, with no app changes: reports on tickets closed more than 7 days ago get a new linked ticket instead of recurrence_noted on the old one.

Not changed

  • The description refresh on dedupe (out of scope per the issue).
  • Ticket 906739396's overwritten description.
  • ticket_comment_api.php doesn't check ticket visibility, unlike the new link API. I noticed it and left it.

🤖 Generated with Claude Code

Closes #123. This is Tinker's side of the ticket contract from the thor#1 planning session: each reporter (gandalf, hwmonDaemon, thor) decides what "the same problem" is, and Tinker enforces it. ## What's in it **1. `dedup_key` on `create_ticket_api.php`** - The format is `scope:subject:kind`, e.g. `host:compute-storage-01:oom`. - When a key is sent, the hash is `sha256("key:" + key)` and the title is ignored. An invalid key gets a 400. - **Keyless callers hash exactly as before.** I compared old and new code over every title on prod (230 titles, plus `serial` / `check_code` variants): 1,380 payloads, 0 differences. - **Deviation from the issue:** the issue's format says `[a-z0-9_.:-]`, but its own example `cluster:ceph:BLUESTORE_SLOW_OP_ALERT` is uppercase, and so are Ceph's health codes. So keys allow `A-Z` and are **case-sensitive**, and the column is `ascii_bin` so lookups match the hash. Thor and Gandalf should send keys exactly as they want them stored. **2. Lookup by key:** `GET api/tickets_api.php?dedup_key=K` - Returns the newest ticket with that key, in any status, plus `recurrences`: the earlier tickets in its chain. - The key's visibility rules apply. - The list view also gets `?source=`. **3. Link API:** new `POST api/ticket_link_api.php {ticket_id, other_ticket_id, relation[, action: "unlink"]}`, `read_write` scope. - It goes through the new `DependencyService::link()` / `unlink()`, which MCP `link_tickets` / `unlink_tickets` now use too (moved out of the MCP class, same behaviour). - Audit rows record the key name. - **Status codes:** 404 for a missing or hidden ticket, 409 for a duplicate or circular link, 400 for a bad relation. **4. Creator attribution:** a new `tickets.source` column holds the API key name. - The dashboard's Created By column and the ticket page show it in place of "System": "thor (API)". - The sidebar has a new **Source** filter. - MCP results include `source`, and `search_tickets` takes a `source` filter. - Comments the create API writes now name the actual reporter. They were hard-coded as "hwmonDaemon", even when Gandalf caused them. - Comments linked to the `system` user now display their stored author. **5. Reopen window, for every reporter, keyed or not.** When a report matches a Closed ticket: - **Closed under `AUTO_REOPEN_WINDOW_DAYS` (default 7) ago:** same as today. Reopen it if the Workflow Designer allows an unattended reopen, otherwise `recurrence_noted`. - **Closed longer ago, `new_recurrence`:** - A new ticket takes over the dedup slot. `hash` stays UNIQUE, and the old ticket gives up its hash but keeps its key. - The new ticket records `recurrence_of` and is linked `relates_to` the old one. - It gets a history comment: "Recurrence of #X ... seen N times in the last 7 days and M in the last 30". - The response includes `previous_ticket_id`. **6. Backfills, per app** - `scripts/backfill_dedup_keys.php --map=file.json [--dry-run]`: the app supplies `{ticket_id: dedup_key}`. It detects conflicts, re-keys the hash, and writes one audit row per ticket. - `scripts/backfill_ticket_source.php [--dry-run]`: fills `source` on old API tickets where it's certain. - Box layout means hwmonDaemon. Gandalf's own footer means gandalf. - Against prod (read-only): about 137 hwmonDaemon, 9 gandalf, and the rest left unset. **Migration `008_dedup_key_source.sql`:** adds `dedup_key`, `source` and `recurrence_of`. All are nullable, so the currently deployed code keeps working. ## ⚠️ Decision needed: the 7-day reopen doesn't happen on prod today On prod, the Workflow Designer's **Closed → Open needs admin + comment**, and so does Closed → In Progress. The create API respects the workflow, so an automated report never reopens a closed ticket on prod. It only adds a "recurrence noted, left Closed" comment, which is what happened on 993441773. This PR keeps that, so within the 7 days prod gives `recurrence_noted`, not `reopened`. To get the contract's reopen, either relax Closed → Open in the Workflow Designer for unattended use, or tell me to let automated reports reopen regardless of the workflow. The tests below cover both configurations. ## Screenshots (local, synthetic data; development on the left, this PR on the right) Source filter in the sidebar: ![sidebar](https://code.lotusguild.org/LotusGuild/tinker_tickets/raw/branch/pr-124-screenshots/01-filter-sidebar.png) Created By shows the reporter instead of "System": ![table](https://code.lotusguild.org/LotusGuild/tinker_tickets/raw/branch/pr-124-screenshots/02-tickets-created-by.png) Filtered to `source=thor` (development ignores the parameter): ![filtered](https://code.lotusguild.org/LotusGuild/tinker_tickets/raw/branch/pr-124-screenshots/03-filter-source-thor.png) Ticket page, Created By: ![ticket](https://code.lotusguild.org/LotusGuild/tinker_tickets/raw/branch/pr-124-screenshots/04-ticket-created-by.png) A `new_recurrence` history comment, now credited to thor instead of System: ![comment](https://code.lotusguild.org/LotusGuild/tinker_tickets/raw/branch/pr-124-screenshots/05-recurrence-comment.png) The negative "-17860s ago" times are an artifact of the local DB's timezone, not this PR. The capture fails on page or console errors, and there were none. ## Verified locally (throwaway `tinker_ui` DB) **End-to-end over HTTP: 42/42 with prod's workflow, and 42/42 with an unattended Closed → Open.** - **Create and dedupe:** a keyed create dedupes, and an escalation comment by thor names thor. An invalid key is a 400. Keyless creates keep the legacy hash. - **Reopen window:** - Closed 2 days ago: reopened or noted, depending on the workflow. - Closed 10 days ago: `new_recurrence`, the old hash released, the link and the history counts (2 in 7 days, 3 in 30) correct, and the next report dedups onto the new ticket. - Keyless reports get the same `new_recurrence`. - A Closed ticket with no `closed_at` falls back to `updated_at`. - **Concurrency:** 8 concurrent identical creates produce 1 ticket. - **Uppercase keys:** accepted and case-sensitive. - **Key lookup:** - A hit returns the newest ticket plus `recurrences`; a miss is a 404; a bad key is a 400. - A confidential match is a 404 for a public-only key and visible to a see-all key. - The `?source=` filter works. - **Link API:** - Link works; a duplicate, an inverse duplicate or a circular link is a 409. - Unlink works from the other side; unlinking again is a 404. - A hidden ticket is a 404 for a public-only key and works for a see-all key. - Status codes: a read key is a 403, a bad relation is a 400, a self link is a 400, a missing ticket is a 404 and GET is a 405. - The audit row records `api_key`. **Everything else:** - **MCP `link_tickets` / `unlink_tickets` after the move:** 11/11, with the same messages as before. - **Backfill scripts:** - Dry runs and real runs. - `backfill_dedup_keys` reports invalid keys, missing tickets and conflicts, and marks Closed tickets. - A keyed report after the backfill lands on the adopted ticket. - Re-runs are no-ops. - **Migration 008** applies with the current `migrate.php`, and the new columns' collations match prod. - **Lint:** phpcs is clean, and eslint reports no errors. ## Rollout (beta and prod share one DB, and deploys don't run migrations) 1. **Before merging,** apply 008 once on CT132. Otherwise beta's dashboard query for the Source filter fails. It's additive and safe for the code currently on prod. 2. Merge to `development`, then run `backfill_ticket_source.php --dry-run` and the real run. 3. **NPM:** add a `location = /api/ticket_link_api.php` exemption, like the other Bearer endpoints, on both t. and beta.t. Gandalf and hwmonDaemon post to CT132 directly, so this only matters for callers going through the proxy. 4. Key backfills happen per app, when it starts sending keys. Thor's 4 Ceph tickets come later with its mapping file. **Behaviour change on deploy, with no app changes:** reports on tickets closed more than 7 days ago get a new linked ticket instead of `recurrence_noted` on the old one. ## Not changed - The description refresh on dedupe (out of scope per the issue). - Ticket 906739396's overwritten description. - `ticket_comment_api.php` doesn't check ticket visibility, unlike the new link API. I noticed it and left it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jared added 1 commit 2026-11-25 20:22:46 -05:00
Ticket contract for automated reporters: dedup_key, source, links, reopen window (#123)
Lint / JS (eslint) (push) Successful in 14s
Lint / PHP (phpcs PSR-12) (push) Successful in 44s
Lint / PHP requirements (version + extensions) (push) Successful in 42s
Lint / Deploy (push) Skipped
Lint / Notify on failure (push) Skipped
Lint / PHP (phpcs PSR-12) (pull_request) Successful in 1m15s
Lint / JS (eslint) (pull_request) Successful in 34s
Lint / PHP requirements (version + extensions) (pull_request) Successful in 42s
Lint / Deploy (pull_request) Skipped
Lint / Notify on failure (pull_request) Skipped
Security / PHP Security (semgrep) (push) Successful in 4m13s
Security / PHP Security (semgrep) (pull_request) Successful in 3m25s
7061c40d4d
From the thor#1 plan: each reporter (gandalf, hwmonDaemon, thor) decides
what "the same problem" is, and Tinker enforces it.

- dedup_key on create_ticket_api.php (scope:subject:kind, A-Za-z0-9_.:-,
  case-sensitive). The hash becomes sha256("key:" + key) and the title is
  not looked at; an invalid key is a 400. Keyless callers hash exactly as
  before (checked over every prod title).
- Recurrence window for every reporter, keyed or not: a match on a ticket
  closed under AUTO_REOPEN_WINDOW_DAYS (7) ago reopens it as before
  (subject to the Workflow Designer); an older one gets a new ticket that
  takes over the dedup slot (tickets.hash stays UNIQUE, the old ticket
  gives it up), carries the key, records recurrence_of, is linked
  relates_to, and gets a history comment. action: new_recurrence.
- GET api/tickets_api.php?dedup_key= returns the newest ticket with that
  key plus its recurrence chain; ?source= filters the list.
- New api/ticket_link_api.php (link/unlink, read_write) through
  DependencyService::link()/unlink(), now shared with MCP
  link_tickets/unlink_tickets; audit rows record the key name.
- tickets.source records the API key name. Shown as the creator instead of
  "System" on the dashboard and ticket page, a Source filter in the
  sidebar, source in MCP results and a search_tickets filter. Comments the
  create API writes name the reporter instead of a hard-coded
  "hwmonDaemon", and system-linked comments display that name.
- Migration 008 (dedup_key, source, recurrence_of). Scripts:
  backfill_ticket_source.php (pre-#123 tickets, where certain) and
  backfill_dedup_keys.php (mapping file supplied by the app).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared merged commit c4dfbecb3b into development 2026-11-25 20:24:37 -05:00
Sign in to join this conversation.