Repository navigation
fix(kanban): auto-derive babysit idempotency key in create_task to stop duplicate pr-babysitter tickets #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0496bc7
c5935ae
5925bd7
57b2553
194f11e
7286eb9
43b4416
9774a21
3d0a7a9
3a354b6
7ba776d
29619a0
c284348
a258610
2045882
b7964df
d822940
4e28ada
8494550
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2385,6 +2385,83 @@ def _canonical_assignee(assignee: Optional[str]) -> Optional[str]: | |
| return normalize_profile_name(assignee) | ||
|
|
||
|
|
||
| def _slug_from_git_remote(workspace_path: Optional[str]) -> Optional[str]: | ||
| """Resolve a checkout/worktree path to its ``owner/repo`` GitHub slug via the | ||
| git ``origin`` remote. Returns ``None`` when the path is empty, not a git | ||
| repo, or the remote URL doesn't parse. Mirrors ``_slug_from_worktree`` in | ||
| ``~/.hermes/scripts/babysit-pr-detector.py``. | ||
| """ | ||
| if not workspace_path or not os.path.isdir(workspace_path): | ||
| return None | ||
|
exiao marked this conversation as resolved.
Resolved
|
||
| try: | ||
| p = subprocess.run( | ||
| ["git", "-C", workspace_path, "remote", "get-url", "origin"], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=10, | ||
| ) | ||
| except (subprocess.SubprocessError, OSError): | ||
| return None | ||
| if p.returncode != 0: | ||
| return None | ||
| m = re.search(r"[:/]([^/:]+/[^/]+?)(?:\.git)?/?$", p.stdout.strip()) | ||
|
exiao marked this conversation as resolved.
Resolved
|
||
| return m.group(1) if m else None | ||
|
Comment on lines
+2450
to
+2455
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For worktrees whose Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2045882: _slug_from_git_remote now allows optional userinfo before github.com, so |
||
|
|
||
|
|
||
| def _derive_babysit_idempotency_key( | ||
| title: Optional[str], | ||
| body: Optional[str], | ||
| workspace_path: Optional[str], | ||
| ) -> Optional[str]: | ||
| """Derive a canonical ``babysit:<owner>/<repo>#<pr>`` idempotency key for a | ||
| pr-babysitter task, so EVERY creation path (the detector cron, the | ||
| ``kanban_create`` tool, sibling handoffs) dedups against the same PR — not | ||
| just the detector that already sets its own key. | ||
|
|
||
| Extraction precedence (most reliable first), per the patch note: | ||
| * PR number: first ``#(\\d+)`` in ``title``, else a | ||
| ``github.com/.../pull/(\\d+)`` url in ``title``/``body``. | ||
| * Repo slug: a ``github.com/<owner>/<repo>/pull/...`` url in title/body | ||
| (most authoritative), else the workspace git remote resolved to | ||
| ``owner/repo``. | ||
|
|
||
| Returns the key only when BOTH a slug and a PR number resolve; otherwise | ||
| ``None`` — a wrong key is worse than none (PR numbers collide across repos, | ||
| so a repo-less key would wrongly cross-dedup #70 in one repo against #70 in | ||
| another). When this returns ``None`` the caller leaves the key unset and | ||
| behavior is identical to today (no dedup, task still creates). | ||
| """ | ||
| title = title or "" | ||
| body = body or "" | ||
|
|
||
| # PR number + repo slug from a github pull-request URL anywhere in the | ||
| # title/body (authoritative for both pieces when present). | ||
| url_pr: Optional[int] = None | ||
| url_slug: Optional[str] = None | ||
| um = re.search( | ||
| r"github\.com/([^/\s]+/[^/\s]+)/pull/(\d+)", | ||
| f"{title}\n{body}", | ||
| ) | ||
| if um: | ||
| url_slug = um.group(1) | ||
| url_pr = int(um.group(2)) | ||
|
|
||
| # PR number: prefer the bare ``#<n>`` in the title (the detector's title | ||
| # convention), fall back to the pull-URL number. | ||
| tm = re.search(r"#(\d+)", title) | ||
| pr = int(tm.group(1)) if tm else url_pr | ||
|
exiao marked this conversation as resolved.
Resolved
|
||
| if pr is None: | ||
| return None | ||
|
|
||
| # Repo slug: the pull URL is the most authoritative; otherwise resolve the | ||
| # workspace git remote. | ||
| slug = url_slug or _slug_from_git_remote(workspace_path) | ||
|
exiao marked this conversation as resolved.
Resolved
|
||
| if not slug: | ||
| return None | ||
|
|
||
| return f"babysit:{slug}#{pr}" | ||
|
exiao marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| # Bare placeholder titles that carry no spec on their own. A task whose title is | ||
| # one of these (or the literal "<assignee> task" / the assignee name itself) AND | ||
| # whose body is empty has no work in it — it must never reach a worker lane in | ||
|
|
@@ -2621,23 +2698,6 @@ def create_task( | |
| ) | ||
| skills_list = cleaned | ||
|
|
||
| # Idempotency check — return the existing task instead of creating a | ||
| # duplicate. Done BEFORE entering write_txn to keep the fast path fast | ||
| # and to avoid holding a write lock during the lookup. Race is | ||
| # acceptable: two concurrent creators with the same key might both | ||
| # insert, at which point both rows exist but the next lookup stabilises. | ||
| if idempotency_key: | ||
| row = conn.execute( | ||
| "SELECT id FROM tasks WHERE idempotency_key = ? " | ||
| "AND status != 'archived' " | ||
| "ORDER BY created_at DESC LIMIT 1", | ||
| (idempotency_key,), | ||
| ).fetchone() | ||
| if row: | ||
| return row["id"] | ||
|
|
||
| now = int(time.time()) | ||
|
|
||
| # Resolve workspace_path from board-level default_workdir when the | ||
| # caller did not specify one explicitly. Board defaults represent | ||
| # persistent project checkouts, so only persistent workspace kinds may | ||
|
|
@@ -2647,6 +2707,14 @@ def create_task( | |
| # task would point cleanup at the user's source tree (#28818). The | ||
| # containment guard in ``_cleanup_workspace`` is the safety rail, but | ||
| # we also stop the bad state from being created in the first place. | ||
| # | ||
| # This runs BEFORE the babysit-key derivation below so that a | ||
| # pr-babysitter task relying on a board ``default_workdir`` (the common | ||
| # ``workspace_kind='worktree'`` + board-default create path) has its | ||
| # repo path resolved in time to derive the canonical babysit key from | ||
| # the git remote — otherwise ``workspace_path`` is still ``None`` at | ||
| # derive time, the key is left unset, and two creates for the same PR | ||
| # insert two non-idempotent rows. | ||
| if ( | ||
| workspace_path is None | ||
| and project_repo is None | ||
|
|
@@ -2665,6 +2733,36 @@ def create_task( | |
| else: | ||
| workspace_path = str(board_default) | ||
|
|
||
| # Auto-derive a canonical babysit idempotency key so EVERY pr-babysitter | ||
| # creation path dedups against the same PR, not just the detector cron that | ||
| # sets its own key. Done AFTER _canonical_assignee (so a pr-babysitter alias | ||
| # is still caught) and only when the caller passed no explicit key — when the | ||
| # key can't be derived it stays None and behavior is identical to today. | ||
| # ``workspace_path or project_repo`` feeds the git-remote slug fallback both | ||
| # the board-default-resolved path (above) and a project-linked task's primary | ||
| # repo, so the key resolves for every persistent-workspace babysitter create. | ||
| if idempotency_key is None and assignee == "pr-babysitter": | ||
|
exiao marked this conversation as resolved.
Resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When operators rely on Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both fixed in 4e28ada: (1) body PR #n now outranks a bare title #n (moved above title_bare, same as the body-URL rule); (2) the derive guard now uses the effective assignee (explicit or kanban.default_assignee) so default-assigned babysitter cards get keyed at create time. Red/green tests for both. |
||
| idempotency_key = _derive_babysit_idempotency_key( | ||
| title, body, workspace_path or project_repo | ||
| ) | ||
|
|
||
| # Idempotency check — return the existing task instead of creating a | ||
| # duplicate. Done BEFORE entering write_txn to keep the fast path fast | ||
| # and to avoid holding a write lock during the lookup. Race is | ||
| # acceptable: two concurrent creators with the same key might both | ||
| # insert, at which point both rows exist but the next lookup stabilises. | ||
| if idempotency_key: | ||
| row = conn.execute( | ||
| "SELECT id FROM tasks WHERE idempotency_key = ? " | ||
| "AND status != 'archived' " | ||
| "ORDER BY created_at DESC LIMIT 1", | ||
| (idempotency_key,), | ||
|
exiao marked this conversation as resolved.
Resolved
Comment on lines
+2889
to
+2892
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For boards that already contain pr-babysitter rows created before this canonicalization, the incoming key is now lowercased but the lookup still compares Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 3a354b6: added a one-shot canonicalization pass in |
||
| ).fetchone() | ||
| if row: | ||
| return row["id"] | ||
|
|
||
| now = int(time.time()) | ||
|
|
||
| # Retry once on the extremely unlikely id collision. | ||
| for attempt in range(2): | ||
| task_id = _new_task_id() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.