Skip to content

PRD 4173 M4 follow-up: enable/link race, forge health on linked repos, re-listed repos, autopilot D11 guard #17

Closed

Description

@MirceaTufanDev

Follow-up to M4 of PRD 4173 (prds/4173-external-issue-trackers.md), found in the M4 review at dfc26712 (PR #16). None of these blocks M4. Fix them before or alongside M5. No step touches .github/workflows/**.

1. Enable racing a link can write forge labels on a repo that ends up linked

api/internal/provisionsvc/provisionsvc.go, EnableRepo. The method reads the repo with GetRepoForUser and takes no lock. It then picks EnsureBoardColumnRows or EnsureBoardColumns from row.IssueTrackerConnectionID as it was at that read, and only after that runs SetRepoEnabledForUser (forge.sql), which does not re-check the link.

Sequence:

  1. A reads the unlinked repo.
  2. B's LinkRepo commits.
  3. A calls the forge's EnsureLabels.
  4. A's UPDATE enables a repo that is now linked.

The database still ends in a valid D13 state, but the forge got label writes that the linked path promises never happen. TestLinkRacingFirstEnableLiveDB misses this because it races the bare query, not EnableRepo.

Fix: smallest option first.

  • Make the enable UPDATE conditional on the link state that was read (e.g. AND issue_tracker_connection_id IS NOT DISTINCT FROM $n) and refuse or retry on zero rows,
  • or lock the repo row before choosing the column path.

Test: a live-DB test that races EnableRepo itself against LinkRepo. With a recording forge, assert that no EnsureLabels call happens when the link wins. Watch it go red on the current code, then green.

2. Forge health is not monitored on a linked repo

api/internal/poller/poller.go, syncRepo, the if linked { branch. For a linked repo, the per-repo sync report is only the YouTrack sync result. Every later code-host step only logs its errors.

What that causes:

  • When the forge PAT is revoked, forge.sync stays green, and MR watching and CI autofix fail silently.
  • When YouTrack has an outage, it shows up as a forge sync failure.
  • TestLinkedRepoHealthFollowsTheLinkedIssueSyncLiveDB pins today's behaviour.

Fix:

  • Report a code-host result for linked repos: a cheap probe, or the outcome of the code-host steps.
  • Report the issue tracker sync separately, under its own check name, with an action that points at the issue tracker connection.
  • Record the decision in the Decision Log.

Test: on a linked repo, a forge 401 turns forge.sync red with the forge action, and a YouTrack failure does not.

3. Removing and re-listing a used repo makes it linkable

DeleteRepoForUser (forge.sql) requires only enabled = false and no active runs; runs, issues and schedules cascade. The next membership listing runs UpsertRepo, which inserts a fresh row with awaiting_first_enable = true, so a repo used for months can be linked. The reverse also works: a linked repo can be removed and re-listed as a plain forge repo. Old agent/issue-<n> branches and MRs with Closes #<n> stay on the code host, and a linked ABC-<n> gets the same branch name.

Decide (smallest option that holds D13):

  • record this in the Decision Log as an accepted limit, after checking what the worker does when a branch name collides,
  • or make the "previously enabled" fact survive removal, keyed by forge connection and project id.

Test: whichever is chosen.

4. CreateAutopilotRun has no D11 guard of its own on a linked repo

api/internal/workersvc/service.go, CreateAutopilotRun → createRun. This path passes autoApprove=true with no scheduleApprovalKey, so the D11 reasons (permission_unknown) are never consulted. The only protection today is the poller skipping autopilot detection on linked repos (around poller.go:551).

Fix: in createRun, refuse a linked repo when autoApprove is set without scheduleApprovalKey, or apply capture.Reasons() there too.

Test: a unit test calling CreateAutopilotRun on a linked repo, watched red then green.

5. Small items

  • api/internal/issuetrackersvc/link.go: entityIDRE has no length cap, but the repos_issue_tracker_link_shape CHECK caps project and tag ids at 128 characters. A longer id from the issue tracker becomes a 500 (23514 mapped to Internal) instead of a 400. Cap it in the regex or the validation, and test it.
  • api/internal/issuetracker/youtrack_labels.go: ListLabels does not drop names that labelNameProblem refuses, while reads do. An account-owned tag with braces or control characters is listed, but ListIssues with it fails ErrInvalidLabelName. Drop such names in ListLabels, and test it.
  • ReadoptRunsFromSnapshot and FrozenReadoptRunsFromSnapshot have no issue_key_v1 clause, the same gap the existing ci_config_guard_v1 clause accepts. Record that boundary in the Decision Log. Change code only if it is cheap.

Wording

Write "issue tracker", never the bare word "tracker". No real hostnames, logins or tokens anywhere.

Disk and memory

Commit small and often. Do not run the full task gate:api or task test:api. Run go test -race -count=1 on the affected packages only (live-DB tests with -p 1 on postgres:17.10), and go clean -cache -testcache after each item. If lint is OOM-killed (exit 137), run GOGC=40 ../scripts/golangci-lint.sh v2.15.0 run --concurrency=2 ./..., then deadcode:api.

Evidence

  • task check:api.
  • go test -race -count=1 on the touched packages, with each new regression test watched red then green.
  • check:changelog-entry (one fragment under ### Fixed, or Changelog: none if nothing user-visible changes).
  • check-docs:web and check:migration-numbering if a migration is added.
  • task scan:secrets with its canaries-detected line.
  • PRD Decision Log entries for items 2 and 3, and the readopt boundary in item 5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions