From 5dcd43912f5d8975461984aba362e8395e2424df Mon Sep 17 00:00:00 2001 From: Vlad Mocanu <1106868+vtmocanu@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:00:10 +0200 Subject: [PATCH 01/16] Accept a committed Codex adoption record in the nightly RC planner (#4174) * fix(release): accept committed Codex adoption records Let scheduled RC planning accept a maintainer adoption assertion bound to regular lock and record blobs at the same pinned HEAD. Parse the strict digest and runtime tag without executing data; keep invalid assertions fail closed before the independent publication checks. Exclude assertions from pin sync, seed the reviewed 0.166.0 lock and update the release guidance and ADR. Cover committed, stale, malformed, untracked and symlink assertions, publication refusal, abandon and real pin sync. Fixes #4172. * fix(release): exclude adoption records from worker rolls Adoption assertions record release review and do not change worker execution. Exclude that exact path in the shared runtime diff and shipping classifier, so seeding or correcting an assertion preserves the decoupled worker pin. Exercise both record seeding and later edits through autobump check/edit modes. Keep similarly named Codex files and the actual runtime lock in the surface, with a lock edit still advancing the pin. --- .../skills/uzi-lander/references/renovate.md | 7 ++ .agents/skills/uzi-release/SKILL.md | 22 ++++- .../skills/uzi-release/scripts/release-cut.sh | 47 ++++++++- .claude/agents/release.md | 2 +- adr/3512-release-automation.md | 14 ++- agent/codex/codex-package.adopted | 2 + agent/test/codex-pins-sync.test.ts | 46 +++++++++ agent/test/codex-pins.ts | 2 + .../b01bf006-734a-4b2a-9f96-01de70a73083.md | 4 + scripts/lib/shipping-paths.sh | 4 +- scripts/release-scripts-test.sh | 96 ++++++++++++++++++- scripts/worker-tag-autobump.sh | 2 + 12 files changed, 234 insertions(+), 14 deletions(-) create mode 100644 agent/codex/codex-package.adopted create mode 100644 changelog.d/b01bf006-734a-4b2a-9f96-01de70a73083.md diff --git a/.agents/skills/uzi-lander/references/renovate.md b/.agents/skills/uzi-lander/references/renovate.md index cdfdf54cca..e5fa41fef7 100644 --- a/.agents/skills/uzi-lander/references/renovate.md +++ b/.agents/skills/uzi-lander/references/renovate.md @@ -105,6 +105,13 @@ new version can break uzi or ship capabilities uzi should adopt. lock-only bump. On your own branch (below), take the lock and run `task codex-pins:sync`, which rewrites every registered Codex version and digest pin from it; keep the Codex guardrail/conformance checks green. +- After the Codex adoption read, hand-update `agent/codex/codex-package.adopted`: + exactly `lock-sha256=<64 lowercase hex>` then `runtime-tag=`, each + followed by LF, with no other lines. Hash the raw lock bytes with + `shasum -a 256 agent/codex/codex-package.lock`; take the tag from that lock. Commit both as + regular files. Pin sync and Renovate must never update this assertion. A lock + edit invalidates it; review the edit before recording adoption again. CI validates + the committed assertion, not whether the read occurred (ADR-3512, #4172). - Required PR CI covers the agent suite, the real-binary `test:codex-m4` on amd64 and the worker-uid lane (`test-agent`). The required `codex-packaged-proof-gate` covers the strict packaged proof on amd64 and the arm64 package check (`e2e/codex-proof/package-exec.sh`). The amd64 image builds run but are not required. diff --git a/.agents/skills/uzi-release/SKILL.md b/.agents/skills/uzi-release/SKILL.md index ff8f39bdd8..21c2f9d89c 100644 --- a/.agents/skills/uzi-release/SKILL.md +++ b/.agents/skills/uzi-release/SKILL.md @@ -23,7 +23,7 @@ Do NOT bake step details that live in the repo into your head. Read the source o - A green CI `report` confirms incident delivery only; inspect the failed phase, planned tag and uncited identities, then fix the cause. Prepare/watch failure still makes the run red. - An unconfirmed push receipt stops watch before publication recovery or verification. Inspect remote refs, then run local watch/verify against the same tag/SHA; this branch does not run automatic publication retries. - If promotion succeeds but diagnostics cannot be written, the cutter exits nonzero and retains its local stable tag. CI does not push; inspect local and remote refs before recovery. -- Before local fallback, finish or stop any CI release run, inspect remote main/tags and resolve interrupted publication on the same tag. No local/CI race. A Codex lock change blocks CI planning; perform the adoption read and cut locally. On abandon the range starts at the highest stable, so an already adopted change can still require local fallback. +- Before local fallback, finish or stop any CI release run, inspect remote main/tags and resolve interrupted publication on the same tag. No local/CI race. A Codex lock change without a valid committed adoption record blocks CI planning; perform the adoption read and commit the record, or cut locally. Abandon uses the highest stable boundary and checks the same HEAD record. - Nightly RC attempts are scheduled for 01:47 Europe/Bucharest only when repository variable `RELEASE_NIGHTLY_ENABLED` is exact `true` (default off). Observed scheduler delays can move cuts into daytime (ADR-3512). The shared plan may no-op; scheduled tip waiting is capped at three hours and the original run date's 13:00 pre-push cutoff. Neither applies to manual dispatch or recovery of a pushed tag. Only the user decides activation. Enablement is rechecked at job boundaries; pausing may stop a not-yet-pushed attempt there, possibly red with `nightly-disabled`. Cancel an in-flight run explicitly for an immediate stop; pushed-tag recovery is unaffected. The bounded attempt journal reports release-commit CI without success polling or reruns; tagged-smoke and targeted assertion recovery use the bounded exception above. - NEVER `docker compose -p uzi down -v` and never glob `uzi-` containers (see `CLAUDE.md` "Destructive operations"). This skill touches git, `gh`, and (if you have deploy access) `kubectl` only. - Work on `main` in the repo root worktree; `release-cut.sh` makes its own throwaway worktree for a promote. Never check the root worktree out onto another branch. @@ -36,7 +36,25 @@ Do NOT bake step details that live in the repo into your head. Read the source o **Artifact membership.** Query the public OCI registry anonymously; no Packages PAT is needed. GitHub release and Actions evidence still uses `gh` read access. Pass `--ci` to the cutter in automation (also enabled by `CI=true`): an unknown worker-pin query or checkout identity refuses the cut. Local cuts retain a warning on query errors; a confirmed missing worker image always refuses. -`release-cut.sh --plan [--version X.Y.Z] [--abandon-train]` reports the scheduled RC decision without fetching or changing Git state, CHANGELOG or fragments. Shared validators remove temporary snapshots but may leave an empty `.uzi/scratch` directory. Refresh refs first. Read `OP` (`nextrc`, `new-base`, `noop`, `blocked`), `BASE`, `TAG`, `REASON`, `SHIPPING_COUNT`, `PREVIOUS_TAG`, `PUBLICATION_STATE`, and `HEAD_SHA`; exit 0 includes no-op/blocked plans and some instrument failures (API or fragment reads); exit 2 is reserved for Git/history instruments, exit 3 for usage. An open train continues, otherwise 0.x defaults to the next minor; automatic 1.x needs a policy. A Codex runtime lock change in the plan range blocks with `codex-runtime-needs-local-cut`: perform the adoption read above and cut locally. Failed, pending, missing or unreadable previous publication blocks even a no-op. Only non-metadata shipping changes count, and notes or dependency citations must make them releasable. This recommendation does not remove manual empty-delta cuts or replace exact-tip CI checks. +`release-cut.sh --plan [--version X.Y.Z] [--abandon-train]` reports the scheduled RC decision without fetching or changing Git state, CHANGELOG or fragments. Shared validators remove temporary snapshots but may leave an empty `.uzi/scratch` directory. Refresh refs first. Read `OP` (`nextrc`, `new-base`, `noop`, `blocked`), `BASE`, `TAG`, `REASON`, `SHIPPING_COUNT`, `PREVIOUS_TAG`, `PUBLICATION_STATE`, and `HEAD_SHA`; exit 0 includes no-op/blocked plans and some instrument failures (API or fragment reads); exit 2 is reserved for Git/history instruments, exit 3 for usage. An open train continues, otherwise 0.x defaults to the next minor; automatic 1.x needs a policy. A Codex runtime lock change in the plan range requires the committed adoption record below; a missing or invalid record blocks with `codex-runtime-needs-local-cut`. Perform the adoption read and commit the record, or cut locally. Failed, pending, missing or unreadable previous publication blocks even a no-op. Only non-metadata shipping changes count, and notes or dependency citations must make them releasable. This recommendation does not remove manual empty-delta cuts or replace exact-tip CI checks. + +### Committed Codex adoption (#4172) + +After reading the runtime release notes and reviewing its manifest, hand-update +`agent/codex/codex-package.adopted`. Use exactly two LF-terminated lines, no extras: + +```text +lock-sha256=SHA256_OF_RAW_LOCK_BYTES +runtime-tag=CODEX_TAG_FROM_LOCK +``` + +Use `shasum -a 256 agent/codex/codex-package.lock` for the 64 lowercase hex digest; +copy the lock's `CODEX_TAG` value for the second line. Commit both as mode 100644. +Planning validates both blobs at its pinned HEAD, including the digest and tag; +worktree-only files, symlinks and malformed or stale records do not authorize it. +Pin sync and Renovate never write the record. A later lock edit needs a fresh +adoption assertion. This records the maintainer's review, not a review by CI; +publication, exact-tip coverage and release authorization remain separate gates. ## 2. Local fallback: cut inline with the deterministic scripts — do NOT dispatch a subagent Once step 1 holds (nothing wanted left unmerged, `main` green on the tip), cut the release **inline as the lead**, driving three bundled scripts. Do NOT dispatch the `release` subagent for this: a background subagent spawns a watch and then idles, so every wait becomes a manual re-ping (the "agent keeps dying" ping-pong), while the steps that actually need judgment — flake-vs-defect, whether to rerun, authoring the CHANGELOG — are the lead's regardless. `.claude/agents/release.md` stays the MECHANICS reference (what each step proves, the signing traps, deploy verify), but the driver is the lead + scripts, not that agent. diff --git a/.agents/skills/uzi-release/scripts/release-cut.sh b/.agents/skills/uzi-release/scripts/release-cut.sh index 8c89333313..4f49349407 100755 --- a/.agents/skills/uzi-release/scripts/release-cut.sh +++ b/.agents/skills/uzi-release/scripts/release-cut.sh @@ -332,6 +332,47 @@ EOF # --plan is deliberately before every fetch, index edit and release mutation. # Scratch snapshots use the cutter's existing bounded validators and clean traps. +# The assertion records a maintainer's adoption read, not a review performed by CI. +# Read only regular blobs at the pinned commit; never execute assertion/lock content. +codex_adoption_record_matches() { + python3 -I - "$1" <<'PY' +import hashlib +import re +import subprocess +import sys + + +def blob(path): + entry = subprocess.check_output( + ["git", "ls-tree", "-z", sys.argv[1], "--", path], stderr=subprocess.DEVNULL + ) + metadata, name = entry.removesuffix(b"\0").split(b"\t") + mode, kind, oid = metadata.split(b" ") + if mode != b"100644" or kind != b"blob" or name != path.encode(): + raise ValueError("expected a committed regular file") + return subprocess.check_output( + ["git", "cat-file", "blob", oid.decode("ascii")], stderr=subprocess.DEVNULL + ) + + +try: + lock = blob("agent/codex/codex-package.lock") + record = blob("agent/codex/codex-package.adopted") + fields = re.fullmatch( + rb"lock-sha256=([0-9a-f]{64})\nruntime-tag=(rust-v[0-9]+\.[0-9]+\.[0-9]+)\n", + record, + ) + tags = [line.removeprefix(b"CODEX_TAG=") for line in lock.split(b"\n") + if line.startswith(b"CODEX_TAG=")] + valid = (fields is not None and len(tags) == 1 + and fields[1] == hashlib.sha256(lock).hexdigest().encode("ascii") + and fields[2] == tags[0]) +except (OSError, ValueError, subprocess.SubprocessError): + valid = False +sys.exit(0 if valid else 1) +PY +} + release_plan() ( export GIT_OPTIONAL_LOCKS=0 git() { command git -c core.fsmonitor=false "$@"; } @@ -382,13 +423,13 @@ release_plan() ( fi tag="v$base-rc.1"; op='new-base' fi - # Adoption still needs a local review/cut. Check before the publication refusal, - # which an explicitly authorized supersede may lift in the CI orchestrator. + # Adoption requires a matching committed assertion, or a local cut. Check before + # the publication refusal, which authorized supersede may lift independently. git diff --quiet --no-ext-diff --no-textconv "$previous" "$head" -- agent/codex/codex-package.lock runtime_rc=$? case "$runtime_rc" in 0) : ;; - 1) block_plan codex-runtime-needs-local-cut ;; + 1) codex_adoption_record_matches "$head" || block_plan codex-runtime-needs-local-cut ;; *) return 2 ;; esac # Check the previous publication even for a zero-delta scheduled attempt. diff --git a/.claude/agents/release.md b/.claude/agents/release.md index 7a8e599bf9..1e4b0031b5 100644 --- a/.claude/agents/release.md +++ b/.claude/agents/release.md @@ -60,7 +60,7 @@ NOT modify source code. ## For this repo (uzi) -**Standard releases use `release-auto.yml` for manual dispatch and opt-in nightly RCs** with shared cutter/watch/verify scripts. See [ADR-3512](../../adr/3512-release-automation.md) and `deploy/README.md`. The RC App atomically pushes metadata and an RC tag; stable requires a pinned candidate and human Environment approval. Agents never dispatch `kind=stable` or approve `release-stable`. This agent is a mechanics reference, not a background release driver. Local fallback remains inline with the lead after finishing/stopping CI release work and inspecting remote main/tags. Codex adoption exceptions require the adoption read and a local cut. +**Standard releases use `release-auto.yml` for manual dispatch and opt-in nightly RCs** with shared cutter/watch/verify scripts. See [ADR-3512](../../adr/3512-release-automation.md) and `deploy/README.md`. The RC App atomically pushes metadata and an RC tag; stable requires a pinned candidate and human Environment approval. Agents never dispatch `kind=stable` or approve `release-stable`. This agent is a mechanics reference, not a background release driver. Local fallback remains inline with the lead after finishing/stopping CI release work and inspecting remote main/tags. Codex lock changes require the adoption read and a matching committed adoption record for CI planning; missing or invalid records retain the local-cut refusal (uzi-release, ADR-3512). **RC-first release train (PRD #1265).** A release is cut as a CANDIDATE `vX.Y.Z-rc.1` by default and PROMOTED from that candidate. CI stable promotion stops there; the local --promote verb can couple the next candidate. `release-cut.sh` is a tag-derived state machine: default cuts the next RC (or refuses, exit 3, with the facts when a lower RC is in flight, so the lead asks the user to promote / re-spin / abandon); `--promote` creates the stable `vB` tag LOCALLY from a throwaway `release/B` worktree at the RC commit (metadata-only, a diff allowlist checked before any tag exists) and then cuts the next `vX.Y.Z-rc.1` on main; `--promote-only` (given the in-flight base `B`) runs that promote half alone: `vB` from the RC commit, no next candidate, `main` untouched, one push (the stable tag), for when `main` carries work not wanted in a candidate yet; `--skip-promote` abandons the RC; `--stable` is the old one-step cut, refused while an RC is in flight. An RC tag publishes images + chart at `X.Y.Z-rc.1`, a Release flagged pre-release (never latest), and the opt-in `uzi-cli-rc` Homebrew formula (PRD #1378: `brew.yml` triggers on every `v*` tag and routes an RC to the separate `uzi-cli-rc` formula, so stable users on `uzi-cli` never see a candidate; before #1378 its trigger excluded `v*-*` and cut no RC formula). **Push order on a promote is the STABLE tag, then `main`, then the RC tag** (the RC's `assert-changelog` derives its coverage base from the tags it can see and needs the new stable visible first); `release-cut.sh` prints the three lines in order. A deployment opts into running candidates with the chart range `0.*-0` instead of `0.*` (out of tree; `deploy/README.md`). diff --git a/adr/3512-release-automation.md b/adr/3512-release-automation.md index b682b6b3d3..c938c9087f 100644 --- a/adr/3512-release-automation.md +++ b/adr/3512-release-automation.md @@ -67,10 +67,16 @@ flowchart LR cutter still validates releasability. Supersede lifts only the matched latest failed/missing publication refusal, never pending/unreadable state or another guard. It cannot waive coverage, oracle, metadata, pin or adoption checks. -- Codex lock changes require the adoption read and a local cut. No model or - attestation parsing runs in CI. Abandon uses the highest stable boundary, so - an adopted Codex change in an abandoned train still blocks automatic abandon - until a local cut. This is deliberate fail-closed behavior. +- Codex lock changes require the adoption read. CI accepts a maintainer-reviewed + `agent/codex/codex-package.adopted` assertion binding the raw lock SHA256 and + runtime tag to regular-file blobs at the same pinned HEAD (#4172). Its exact + two-line format is documented in the uzi-release skill. Missing or invalid + assertions retain `codex-runtime-needs-local-cut`; pin sync and Renovate never + certify adoption. This records the maintainer's read, not a read performed by CI. + Abandon still uses the highest stable boundary and validates the same HEAD + assertion. Publication, coverage and authorization gates remain independent. + This supersedes #3524's unconditional local-cut requirement with the + maintainer's acknowledgement; no model, PR-body parsing or new authority is added. - A thin bundle requires its pinned parent to be fetched before verification. Validate digest, exact refs, release SHA, tag object and single parent before minting. No configuration or hooks are imported. A lost push receipt is diff --git a/agent/codex/codex-package.adopted b/agent/codex/codex-package.adopted new file mode 100644 index 0000000000..a17d3033d9 --- /dev/null +++ b/agent/codex/codex-package.adopted @@ -0,0 +1,2 @@ +lock-sha256=0fee0e06f81e2cd2ce19f6f7de121a3a11493ac52655ad60698e05563870a846 +runtime-tag=rust-v0.166.0 diff --git a/agent/test/codex-pins-sync.test.ts b/agent/test/codex-pins-sync.test.ts index ef334078c2..29a10d12da 100644 --- a/agent/test/codex-pins-sync.test.ts +++ b/agent/test/codex-pins-sync.test.ts @@ -1,6 +1,7 @@ import { it } from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; import { copyFileSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; @@ -172,3 +173,48 @@ it("syncs the small tree and is idempotent", () => { rmSync(dir, { recursive: true, force: true }); } }); + +it("never rewrites a recorded adoption assertion, even malformed text containing a live pin", () => { + const file = "agent/codex/codex-package.adopted"; + // Even an invalid assertion is not sync's to repair or re-certify. + for (const suffix of ["", `binary at ${INSTALL_ROOT}${current.version}/bin/codex\n`]) { + const assertion = `lock-sha256=${"a".repeat(64)}\nruntime-tag=rust-v${current.version}\n${suffix}`; + const dir = smallTree((files) => { files[LOCK] = bumped(realLock); files[file] = assertion; }); + try { + assert.ok(!scannedPinFiles(dir).includes(file), "adoption is excluded from live-pin discovery"); + assert.ok(!syncCodexPins(dir).includes(file)); + assert.equal(readFileSync(join(dir, file), "utf8"), assertion, "sync does not certify adoption"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + } +}); + +it("a synchronized runtime bump still fails the release adoption guard until hand-recorded", () => { + const file = "agent/codex/codex-package.adopted"; + const assertion = `lock-sha256=${createHash("sha256").update(realLock).digest("hex")}\nruntime-tag=rust-v${current.version}\n`; + const cutter = readFileSync(join(root, ".agents/skills/uzi-release/scripts/release-cut.sh"), "utf8"); + // Exercise the real planner guard without the unrelated release ceremony. + const start = cutter.indexOf("codex_adoption_record_matches() {"); + const end = cutter.indexOf("\n}\n", start); + assert.ok(start >= 0 && end > start); + const guard = `${cutter.slice(start, end + 3)}\ncodex_adoption_record_matches HEAD\n`; + const dir = smallTree((files) => { files[file] = assertion; }); + const commit = () => { git(dir, "add", "-A"); git(dir, "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test", "commit", "-qm", "fixture"); }; + const check = () => spawnSync("bash", ["-s"], { cwd: dir, input: guard, encoding: "utf8" }); + try { + commit(); + assert.equal(check().status, 0, "the original committed assertion authorizes its lock"); + writeFileSync(join(dir, LOCK), bumped(realLock)); + syncCodexPins(dir); + assert.equal(readFileSync(join(dir, file), "utf8"), assertion); + commit(); + assert.equal(check().status, 1, "pin sync cannot authorize the bumped runtime for nightly release"); + const nextLock = readFileSync(join(dir, LOCK)); + writeFileSync(join(dir, file), `lock-sha256=${createHash("sha256").update(nextLock).digest("hex")}\nruntime-tag=rust-v${NEXT.version}\n`); + commit(); + assert.equal(check().status, 0, "the explicit adoption update authorizes the new lock"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); diff --git a/agent/test/codex-pins.ts b/agent/test/codex-pins.ts index 79a021abda..94a3cc9266 100644 --- a/agent/test/codex-pins.ts +++ b/agent/test/codex-pins.ts @@ -22,6 +22,8 @@ const PIN_SCAN_EXCLUDES = [ ":(exclude)e2e/worker-uid/issue-2397-investigation.md", // A recorded frame tape is historical evidence, not a live deployment pin. ":(exclude)agent/test/fixtures/codex-plan-output-schema-0.164.0.json", + // A maintainer's adoption assertion must never be certified by pin sync. + ":(exclude)agent/codex/codex-package.adopted", // Synthetic version output deliberately includes mismatches to test the verifier. ":(exclude)e2e/codex-m4/provision.test.ts", ]; diff --git a/changelog.d/b01bf006-734a-4b2a-9f96-01de70a73083.md b/changelog.d/b01bf006-734a-4b2a-9f96-01de70a73083.md new file mode 100644 index 0000000000..f7f4f32c15 --- /dev/null +++ b/changelog.d/b01bf006-734a-4b2a-9f96-01de70a73083.md @@ -0,0 +1,4 @@ +### Fixed + +- **Nightly Codex runtime adoption (#4172).** + Scheduled RC planning accepts a committed maintainer adoption record matching the reviewed runtime lock, while retaining publication and coverage checks and refusing missing or stale records. diff --git a/scripts/lib/shipping-paths.sh b/scripts/lib/shipping-paths.sh index 1505ba9708..ab917e48fc 100644 --- a/scripts/lib/shipping-paths.sh +++ b/scripts/lib/shipping-paths.sh @@ -5,6 +5,7 @@ # AGENT_PATHS is the space-separated runtime surface used by autobump's raw Git # pathspec diffs: source, dependency manifests, TypeScript config, binaries, # templates, global devbox toolchain, baked sqlc archive and Codex packaging/supervisor. +# The exact Codex adoption assertion is release review metadata, not execution input. # is_shipping returns 0 for that set (exact entries or slash descendants), # api, controller, web app, chart and docs; 1 for tests, e2e, fixtures and everything # else (including skill/PRD-only changes). Tests are excluded before runtime matching. @@ -16,10 +17,11 @@ # next candidate; skill/PRD-only work with empty [Unreleased] is nonshipping. # - scripts/worker-tag-autobump.sh: which runtime paths warrant rolling workers. # - scripts/check-changelog-entry.sh: which branch changes need a changelog entry. -AGENT_PATHS="agent/src agent/package.json agent/package-lock.json agent/tsconfig.json agent/bin agent/templates agent/devbox-global agent/codex scripts/sqlc.sh" +AGENT_PATHS="agent/src agent/package.json agent/package-lock.json agent/tsconfig.json agent/bin agent/templates agent/devbox-global agent/codex scripts/sqlc.sh :(exclude)agent/codex/codex-package.adopted" is_shipping() { case "$1" in + agent/codex/codex-package.adopted) return 1 ;; *_test.go|*.test.ts|*.test.tsx|*/testdata/*|*/test/*|e2e/*|fixtures/*) return 1 ;; api/*|controller/*|web/src/*|deploy/chart/*|docs/*) return 0 ;; esac diff --git a/scripts/release-scripts-test.sh b/scripts/release-scripts-test.sh index 559f68346d..291a1f6917 100755 --- a/scripts/release-scripts-test.sh +++ b/scripts/release-scripts-test.sh @@ -1072,6 +1072,9 @@ fi if [ "${PLAN_HISTORY_FAILURE:-0}" = 1 ]; then case " $* " in *' log --first-parent '*) exit 74 ;; esac fi +if [ "${PLAN_BLOB_FAILURE:-0}" = 1 ]; then + case " $* " in *' cat-file '*) exit 75 ;; esac +fi exec "$PLAN_REAL_GIT" "$@" GIT chmod +x "$d/plan-bin/git" @@ -1183,11 +1186,62 @@ GIT gcommit "$d" 'docs: isolated fragment' plan_expect 'only committed fragment makes range releasable' $'OP=nextrc\nTAG=v0.2.0-rc.12\nSHIPPING_COUNT=1' mkdir -p "$d/agent/codex" - echo 'runtime pin fixture' > "$d/agent/codex/codex-package.lock" + printf 'CODEX_VERSION=2.0.0\nCODEX_TAG=rust-v2.0.0\n' > "$d/agent/codex/codex-package.lock" git -C "$d" add agent/codex/codex-package.lock gcommit "$d" 'chore: change Codex runtime' plan_expect 'Codex adoption requires a local cut' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' PLAN_PUBLICATION=failed plan_expect 'Codex block precedes liftable publication block' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' + # #4172: an adoption assertion belongs to the committed lock, never a worktree file. + local adoption digest valid_record malformed publication_reason + adoption=agent/codex/codex-package.adopted + digest="$(shasum -a 256 "$d/agent/codex/codex-package.lock" | awk '{print $1}')" + valid_record="$(printf 'lock-sha256=%s\nruntime-tag=rust-v2.0.0' "$digest")" + printf '%s\n' "$valid_record" > "$d/$adoption" + plan_expect 'Codex untracked adoption cannot authorize release' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' + git -C "$d" add "$adoption" + gcommit "$d" 'docs: record Codex adoption' + plan_expect 'Codex matching committed adoption' $'OP=nextrc\nTAG=v0.2.0-rc.12' + PLAN_BLOB_FAILURE=1 plan_expect 'Codex blob read failure stays closed' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' + for PLAN_PUBLICATION in failed pending missing error transient; do + export PLAN_PUBLICATION + publication_reason=previous-publication-not-successful + case "$PLAN_PUBLICATION" in error|transient) publication_reason=previous-publication-unreadable ;; esac + plan_expect "Codex adopted publication $PLAN_PUBLICATION" "$(printf 'OP=blocked\nREASON=%s' "$publication_reason")" + done + export PLAN_PUBLICATION=success + PLAN_PREVIOUS=v0.1.0 plan_expect 'Codex adopted abandon uses stable boundary' $'OP=new-base\nPREVIOUS_TAG=v0.1.0' --version 0.3.0 --abandon-train + for malformed in stale tag duplicate unknown extra uppercase short garbage missing newline crlf; do + case "$malformed" in + stale) printf 'lock-sha256=%064d\nruntime-tag=rust-v2.0.0\n' 0 ;; + tag) printf 'lock-sha256=%s\nruntime-tag=rust-v9.9.9\n' "$digest" ;; + duplicate) printf '%s\nlock-sha256=%s\n' "$valid_record" "$digest" ;; + unknown) printf '%s\nunknown=true\n' "$valid_record" ;; + extra) printf '%s\n\n' "$valid_record" ;; + uppercase) printf 'lock-sha256=%s\nruntime-tag=rust-v2.0.0\n' "$(printf '%s' "$digest" | tr '[:lower:]' '[:upper:]')" ;; + short) printf 'lock-sha256=abc\nruntime-tag=rust-v2.0.0\n' ;; + garbage) printf '$(touch adoption-executed)\n' ;; + missing) printf 'lock-sha256=%s\n' "$digest" ;; + newline) printf '%s' "$valid_record" ;; + crlf) printf 'lock-sha256=%s\r\nruntime-tag=rust-v2.0.0\r\n' "$digest" ;; + esac > "$d/$adoption" + git -C "$d" add "$adoption" + gcommit "$d" "test: Codex adoption $malformed" + plan_expect "Codex $malformed adoption refuses" $'OP=blocked\nREASON=codex-runtime-needs-local-cut' + done + assert_eq 'Codex adoption data never executed' no "$(if [ -e "$d/adoption-executed" ]; then echo yes; else echo no; fi)" + PLAN_PREVIOUS=v0.1.0 plan_expect 'Codex invalid adoption blocks abandon' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' --version 0.3.0 --abandon-train + # A matching worktree target cannot turn a committed symlink into an assertion. + printf '%s\n' "$valid_record" > "$d/adoption-target" + git -C "$d" rm -q "$adoption" + ln -s ../../adoption-target "$d/$adoption" + git -C "$d" add "$adoption" + gcommit "$d" 'test: Codex adoption symlink' + plan_expect 'Codex symlink adoption refuses' $'OP=blocked\nREASON=codex-runtime-needs-local-cut' + git -C "$d" rm -q "$adoption" + printf '%s\n' "$valid_record" > "$d/$adoption" + git -C "$d" add "$adoption" + gcommit "$d" 'docs: restore valid adoption' + plan_expect 'Codex restored committed adoption' $'OP=nextrc\nTAG=v0.2.0-rc.12' # A human's local cut establishes a new boundary after the adoption read. git -C "$d" tag v0.2.0-rc.12 export PLAN_PREVIOUS=v0.2.0-rc.12 @@ -1285,7 +1339,7 @@ shipping_path_regressions() { # shellcheck source=scripts/lib/shipping-paths.sh . "$SCRIPTS_DIR/lib/shipping-paths.sh" local expected="agent/src agent/package.json agent/package-lock.json agent/tsconfig.json agent/bin agent/templates agent/devbox-global agent/codex scripts/sqlc.sh" - assert_eq "shared runtime set stays exact" "$expected" "$AGENT_PATHS" + assert_eq "shared runtime set stays exact" "$expected :(exclude)agent/codex/codex-package.adopted" "$AGENT_PATHS" local p d out status subject for p in $expected; do if is_shipping "$p"; then pass "runtime entry $p ships"; else fail "runtime entry $p ships"; fi @@ -1295,7 +1349,7 @@ shipping_path_regressions() { for p in api/x.go controller/x.go web/src/x.ts deploy/chart/values.yaml docs/cli.md; do if is_shipping "$p"; then pass "existing shipping $p"; else fail "existing shipping $p"; fi done - for p in api/x_test.go agent/src/foo.test.ts web/src/foo.test.tsx api/testdata/x agent/test/x.ts agent/templates/test/x agent/codex/x_test.go e2e/x fixtures/x .agents/skills/x prds/x.md agent/README.md; do + for p in api/x_test.go agent/src/foo.test.ts web/src/foo.test.tsx api/testdata/x agent/test/x.ts agent/templates/test/x agent/codex/x_test.go agent/codex/codex-package.adopted e2e/x fixtures/x .agents/skills/x prds/x.md agent/README.md; do if is_shipping "$p"; then fail "excluded $p"; else pass "excluded $p"; fi done @@ -1374,6 +1428,42 @@ MD pass "autobump has no private AGENT_PATHS assignment" fi assert_contains "autobump sources shipping library" '/lib/shipping-paths.sh"' "$(cat "$SCRIPTS_DIR/worker-tag-autobump.sh")" + # #4172: the release adoption assertion is not worker execution input. Its + # addition AND later record-only edits must preserve the published worker pin. + d="$(mktemp -d)"; seed_repo "$d" + mkdir -p "$d/agent/codex" + for subject in seed amend; do + printf 'adoption assertion %s\n' "$subject" > "$d/agent/codex/codex-package.adopted" + git -C "$d" add agent/codex/codex-package.adopted + gcommit "$d" "docs: $subject adoption assertion" + status=0 + out="$(cd "$d" && bash scripts/worker-tag-autobump.sh --check 0.2.0 2>&1)" || status=$? + assert_eq "$subject adoption-only check needs no worker bump" 0 "$status" + status=0 + out="$(cd "$d" && bash scripts/worker-tag-autobump.sh 0.2.0 2>&1)" || status=$? + assert_eq "$subject adoption-only edit succeeds" 0 "$status" + assert_eq "$subject adoption-only keeps worker pin" 0.1.0 "$(pin_tag "$d")" + assert_eq "$subject adoption-only leaves tracked inputs unchanged" '' "$(git -C "$d" status --porcelain --untracked-files=no)" + # Keep the next fixture discriminating even against the unmodified autobump. + git -C "$d" checkout -- deploy/chart/values.yaml scripts/assert-worker-tag-decoupled.sh + done + # The exception is exactly one path; neither its neighbor nor the lock is exempt. + for p in agent/codex/codex-package.adopted.extra agent/codex/codex-package.lock; do + printf 'runtime change\n' > "$d/$p" + git -C "$d" add "$p"; gcommit "$d" "chore: change $p" + status=0 + out="$(cd "$d" && bash scripts/worker-tag-autobump.sh --check 0.2.0 2>&1)" || status=$? + assert_eq "$p still requires worker bump" 1 "$status" + assert_contains "$p appears in changed runtime paths" "$p" "$out" + git -C "$d" rm -q "$p"; gcommit "$d" "test: remove $p" + done + printf 'runtime change\n' > "$d/agent/codex/codex-package.lock" + git -C "$d" add agent/codex/codex-package.lock; gcommit "$d" 'chore: change actual runtime lock' + status=0 + out="$(cd "$d" && bash scripts/worker-tag-autobump.sh 0.2.0 2>&1)" || status=$? + assert_eq 'runtime lock edit still succeeds' 0 "$status" + assert_eq 'runtime lock still advances worker pin' 0.2.0 "$(pin_tag "$d")" + rm -rf "$d" # Override only the fixture-local library. A non-agent sentinel proves both actual # Git diff selection and changed-path reporting consume that library's pathspecs. d="$(mktemp -d)"; seed_repo "$d" diff --git a/scripts/worker-tag-autobump.sh b/scripts/worker-tag-autobump.sh index ada2b385a3..405645f7d4 100755 --- a/scripts/worker-tag-autobump.sh +++ b/scripts/worker-tag-autobump.sh @@ -27,6 +27,8 @@ # agent/bin agent/templates agent/devbox-global agent/codex scripts/sqlc.sh # (agent/codex -- the pinned Codex package lock, its install/assert scripts and the Go # supervisor the runtime stage builds -- was missing until #1720.) +# The exact codex-package.adopted assertion is excluded: it records release review +# and changes no worker execution input (#4172). # (A self-improve run DOES read /opt/uzi-src, but it is admin-only / off by default; if # that path ever needs a fresh per-release snapshot, widen AGENT_PATHS deliberately -- # do not silently fold in the whole tree, which defeats the decoupling.) From f19bc0739940c3309d669e5ab5dd7b06c34e5095 Mon Sep 17 00:00:00 2001 From: Mircea Tufan <134056869+MirceaTufanDev@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:08:59 +0200 Subject: [PATCH 02/16] refactor(forge): split Forge into IssueTracker and CodeHost (#4173 M1) (#4175) * forge: split the Forge interface into IssueTracker and CodeHost (PRD #4173 M1) Forge becomes the union of two disjoint halves, each in its own file with the types only it uses: IssueTracker (issuetracker.go: issues, labels, comments, issue state) and CodeHost (codehost.go: everything else). forge.go keeps the package-wide pieces (Type, RateLimitError, the sentinels, New) and the identity types Project and BotIdentity. Pure moves: no method, signature or caller changes. The three drivers and forgetest.BaseFake still implement Forge; the per-half var _ lines next to each driver are readable markers only. Adds prds/4173-external-issue-trackers.md. Co-Authored-By: uzi-agent Co-Authored-By: Claude * docs: PRD #4173 says issue tracker throughout and settles its open points - Terminology: "issue tracker", never the bare "tracker"; the driver package is named api/internal/issuetracker. - The withdrawal, comment/command and outbox gaps from the scope review are listed under Out of scope for the follow-up PRDs. - The Open questions section goes: identity and comment-author mapping are decided in M4 and recorded in the Decision Log before code; bot-assignment eligibility is not offered in this PRD. Changelog: none Co-Authored-By: Claude * docs: ARCHITECTURE.md cites PRD #4173 for the caller rule Changelog: none Co-Authored-By: Claude * docs: PRD #4173 adds the link/relink policy and the secret-scan gate Review findings on the PR: - D13: an issue number belongs to one issue source. Runs and cached issues record their source, a repo changes source only when idle, and lifecycle or MR-watcher updates never act on the same number in another source. M4's acceptance covers the transition. - Every milestone's gate names task scan:secrets and requires the canaries-detected line in the run's evidence. Changelog: none Co-Authored-By: Claude * docs: PRD #4173 D13 fences a source change against in-flight operations A per-repo issue-source generation guards sync results, and a per-repo lock makes a source change wait for issue writes in flight. M4's acceptance adds the two concurrent cases. Changelog: none Co-Authored-By: Claude * docs: PRD #4173 D13 states one per-repo fence for every source-dependent operation Run creation, sync and issue writes through their cache update complete against the source they resolved or are refused; the mechanism is M4's choice. M4's acceptance lists the concurrent cases. Changelog: none Co-Authored-By: Claude * docs: PRD #4173 r4 after the maintainer's review of M1 Decide the YouTrack id adapter (D10), the eligibility tag by id (D14) and the issue-author policy (D11) before the M3 driver. Fix a repo's issue source before its first enable instead of fencing relinks (D13). Run the contract suite on the real drivers (D7), wire the allowlist through the chart and compose in M3, move the ADR to M3, and state the in-run test boundary, the E2E sweep and hosted Kubernetes verification. Co-Authored-By: Claude * docs: PRD #4173 D10 and D14 wording from review Connection operations need no project; the eligibility label always resolves to the stored tag id. Co-Authored-By: Claude * docs: PRD #4173 second review pass Every repo existing at the M4 migration counts as previously enabled (D13). The driver refuses a projectID other than its own and listing states is project-scoped (D10). The eligibility tag is checked through its see and use settings, read-only (D14). No number is derived from a comment id (D10). Co-Authored-By: Claude --------- Co-authored-by: Mircea Tufan Co-authored-by: uzi-agent Co-authored-by: Claude --- AGENTS.md | 2 +- ARCHITECTURE.md | 2 +- api/internal/forge/codehost.go | 684 ++++++++++++++ api/internal/forge/forge.go | 848 +----------------- api/internal/forge/forgejo.go | 6 + api/internal/forge/forgetest/basefake.go | 2 + api/internal/forge/github.go | 6 + api/internal/forge/gitlab.go | 6 + api/internal/forge/issuetracker.go | 173 ++++ .../b6862b0d-2519-4896-8a76-d25fa506fb68.md | 4 + prds/4173-external-issue-trackers.md | 221 +++++ 11 files changed, 1116 insertions(+), 838 deletions(-) create mode 100644 api/internal/forge/codehost.go create mode 100644 api/internal/forge/issuetracker.go create mode 100644 changelog.d/b6862b0d-2519-4896-8a76-d25fa506fb68.md create mode 100644 prds/4173-external-issue-trackers.md diff --git a/AGENTS.md b/AGENTS.md index 255e42e0f1..394465b105 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -158,7 +158,7 @@ Full detail in `ARCHITECTURE.md`; read it for any cross-service work. The short - **Services**: `web` (nginx-unprivileged; serves the SPA, reverse-proxies `/api/*` same-origin, no CORS anywhere), `api` (Go, distroless, sole holder of secrets/keys), `db` (postgres:17, `pgdata` volume), `agent` (profile-gated worker, outbound-only to `api`). - **Trust boundaries**: only `web` publishes a port, on loopback. nginx overwrites `X-Forwarded-For`; `api` trusts it only from `TRUSTED_PROXIES`. Session = HttpOnly JWT + CSRF cookie (`api/internal/middleware/auth.go`). Workers use a Bearer join token (sha256 stored, shown once); no cookies or CSRF on `/api/worker/*`. -- **Forge layer**: `api/internal/forge` holds the `Forge` interface and neutral domain types; drivers are `gitlab.go`, `forgejo.go`, `github.go`. An interface change costs four sites: three drivers plus the shared fake `api/internal/forge/forgetest.BaseFake`. +- **Forge layer**: `api/internal/forge/forge.go` defines `Forge` as the union of `IssueTracker` (`issuetracker.go`: issues, labels, comments, state) and `CodeHost` (`codehost.go`: the rest). Drivers are `gitlab.go`, `forgejo.go`, `github.go`; an interface change costs four sites: three drivers plus the shared fake `forgetest.BaseFake`. - `BaseFake` implements every `Forge` method with loud defaults: an un-overridden method returns an error wrapping `forgetest.ErrNotStubbed`, except `LatestPipeline` / `LatestMRPipeline`, which return the absence sentinel `forge.ErrNoPipeline`. - Test fakes embed it and override only what they exercise (`grep -rlE '^\s+forgetest\.BaseFake\s*$' api` lists them). `workersvc/ci_fix_snapshot_test.go` embeds `forge.Forge` itself, as an unreached nil interface. - No other package imports a driver directly. Errors pass a PAT-scrubbing redactor; outbound base URLs are allowlisted (`FORGE_ALLOWED_BASE_URLS`, https-only: the SSRF guard). diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 0e6d68fa35..5e8841b1b7 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -80,7 +80,7 @@ Read-through forge views in the TUI (open PRs, live PR checks, CI runs — PRD # ### Forge abstraction -`api/internal/forge` defines the `Forge` interface (`VerifyToken`, `ListProjects`, `ListLabels`, `EnsureLabels`, `ListIssues`, `UpdateIssueLabels`, `UpdateIssueDescription`, the four CI reads, plus the guardrail reads `ProjectRole`/`DefaultBranchProtection`) and a neutral domain vocabulary (`BotIdentity`, `Project`, `Label`, `Issue`, `Role`, `BranchProtection`); `forge.New` selects a driver by `forge.Type` — **`gitlab.go`, `forgejo.go`, or `github.go`** — so no other package ever imports a driver directly. Test fakes embed the shared `api/internal/forge/forgetest.BaseFake` (PRD #922), which implements the whole interface with loud `ErrNotStubbed` defaults, so an interface change costs three drivers plus that one fake rather than one edit per hand-written fake. Every driver call goes through an `*http.Client` bounded by `FORGE_HTTP_TIMEOUT` (`timeoutClient` in `forge.go`) — never an untimeouted `http.DefaultClient` — and every returned error is passed through a `redactor` (`redact.go`) that scrubs the PAT and any `Authorization`/`PRIVATE-TOKEN`/`token`-scheme header value before the error can reach a log line or an HTTP response body. `error_class.go` classifies typed errors and HTTP status before scrubbing; the returned error retains only the scrubbed message and a closed `timeout`/`auth`/`server_error`/`rate_limited`/`other` class, with no original error chain. Poller issue-sync health consumes that safe class rather than raw error text. +`api/internal/forge` defines the `Forge` interface (`VerifyToken`, `ListProjects`, `ListLabels`, `EnsureLabels`, `ListIssues`, `UpdateIssueLabels`, `UpdateIssueDescription`, the four CI reads, plus the guardrail reads `ProjectRole`/`DefaultBranchProtection`) in `forge.go` as the union of two disjoint halves (PRD #4173), each in its own file with the types only it uses: `IssueTracker` in `issuetracker.go` (issues, labels, comments and issue state) and `CodeHost` in `codehost.go` (every other method: repository and identity reads, the guardrail reads, merge requests, CI, artifacts and refs). `forge.go` keeps the package-wide pieces (`Type`, `RateLimitError`, the sentinel errors, `New`) and the identity types `Project` and `BotIdentity`; each driver (`github.go`, `gitlab.go`, `forgejo.go`) and `forgetest.BaseFake` assert both halves at compile time. The package also defines a neutral domain vocabulary (`BotIdentity`, `Project`, `Label`, `Issue`, `Role`, `BranchProtection`). From PRD #4173 M2 on, callers narrow to the half of `Forge` they need. `forge.New` selects a driver by `forge.Type` — **`gitlab.go`, `forgejo.go`, or `github.go`** — so no other package ever imports a driver directly. Test fakes embed the shared `api/internal/forge/forgetest.BaseFake` (PRD #922), which implements the whole interface with loud `ErrNotStubbed` defaults, so an interface change costs three drivers plus that one fake rather than one edit per hand-written fake. Every driver call goes through an `*http.Client` bounded by `FORGE_HTTP_TIMEOUT` (`timeoutClient` in `forge.go`) — never an untimeouted `http.DefaultClient` — and every returned error is passed through a `redactor` (`redact.go`) that scrubs the PAT and any `Authorization`/`PRIVATE-TOKEN`/`token`-scheme header value before the error can reach a log line or an HTTP response body. `error_class.go` classifies typed errors and HTTP status before scrubbing; the returned error retains only the scrubbed message and a closed `timeout`/`auth`/`server_error`/`rate_limited`/`other` class, with no original error chain. Poller issue-sync health consumes that safe class rather than raw error text. The Forgejo driver (`code.gitea.io/sdk/gitea`, Forgejo ≥16.0.0 — [ADR-65](adr/0065-forgejo-driver.md) for why both) proved the abstraction was more than Go-deep by finding the three places it was **not**: (1) the worker held a second, un-abstracted GitLab client, now a minimal TS forge seam (`agent/src/forge.ts`, `GitLabClient`/`ForgejoClient`/`GitHubClient`); (2) the web reconstructed forge URLs by string surgery, now a per-card/run `forge_type` DTO field mapped only at `web/src/lib/forgeNoun.ts` (`forgeNoun`/`forgePlatform`, one Go twin in `slacksvc/notifier_state.go`, one CLI twin in `api/cmd/uzi/render.go`); (3) each forge stored its pipeline status verbatim, so `api/internal/pipelinestatus` is the one Go-side classifier that folds all three vocabularies — the domain twin of `web/src/lib/pipelineBadge.ts`, kept in sync by `TestMirrorsWebPipelineBadge`. Merge-permission is now modelled on all three forges (`BranchProtection.WriteRoleCanMerge`/`BotCanMerge`); the drivers **report** it, and **enforcement is implemented** — [PRD #66](prds/done/66-guardrail-enforcement.md) refuses a run whenever the bot could push or merge to the default branch, at repo-enable, at run creation, and at claim, live and fail-closed, with an admin-only per-repo override for the deliberate, audited exception. diff --git a/api/internal/forge/codehost.go b/api/internal/forge/codehost.go new file mode 100644 index 0000000000..8671936cbf --- /dev/null +++ b/api/internal/forge/codehost.go @@ -0,0 +1,684 @@ +package forge + +import ( + "context" + "time" +) + +// Ancestry is the tri-state answer CompareAncestry gives to "is candidate an ancestor +// of, or equal to, head" (issue #1582 M1). Only AncestryAncestor is a positive proof; +// AncestryUnknown is the fail-closed answer for every error, rate limit, oversize or +// unrecognized response, and the caller must never read it as "not an ancestor +// either" (it simply proves nothing). +type Ancestry string + +const ( + // AncestryAncestor: the forge explicitly reported candidate reachable from head + // (or the two are the same commit). + AncestryAncestor Ancestry = "ancestor" + // AncestryNotAncestor: the forge explicitly reported candidate NOT reachable from + // head (GitHub ahead/diverged, GitLab merge base != candidate). + AncestryNotAncestor Ancestry = "not_ancestor" + // AncestryUnknown: no conclusive answer. Always the result alongside an error. + AncestryUnknown Ancestry = "unknown" +) + +// Role is a bot's effective permission level on a project, in forge-neutral +// terms. It deliberately carries no number: GitLab's access levels (30 = +// Developer) are a GitLab implementation detail, and Forgejo has no numeric +// levels at all — its API returns exactly this vocabulary +// (none|read|write|admin|owner). A driver that mapped "write" onto 30 to satisfy +// a GitLab-shaped contract would be leaking one forge's model through the seam +// this package exists to be. +type Role string + +const ( + // RoleNone is no access. + RoleNone Role = "none" + // RoleRead can read but not write. + RoleRead Role = "read" + // RoleWrite can push branches and open merge requests. This is the exact role + // uzi's bot must hold: enough to do the work, not enough to be trusted with + // main. + RoleWrite Role = "write" + // RoleAdmin administers the project (settings, protection rules). + RoleAdmin Role = "admin" + // RoleOwner owns the project. + RoleOwner Role = "owner" +) + +// roleRank orders the roles least- to most-privileged. Unlike a forge's access +// level it is not a wire value and never leaves this package — AtLeast is the +// only way to read it, so no caller can reintroduce an "access level 30" +// comparison against a neutral type. +var roleRank = map[Role]int{RoleNone: 0, RoleRead: 1, RoleWrite: 2, RoleAdmin: 3, RoleOwner: 4} + +// AtLeast reports whether r is min or more privileged. An unrecognized role +// ranks as RoleNone, so an unknown value from a forge never reads as privileged. +func (r Role) AtLeast(min Role) bool { return roleRank[r] >= roleRank[min] } + +// TokenInfo is PAT introspection returned by TokenInfo: the scopes it carries, +// whether it is active, and when it expires. GitLab: GET +// /personal_access_tokens/self. +type TokenInfo struct { + // Scopes are the token's granted scopes (e.g. ["api"]). + Scopes []string + // Active is true when the token is usable (not revoked, not expired). + Active bool + // ExpiresAt is the token's expiry; the zero time means it never expires. + ExpiresAt time.Time +} + +// BranchProtection reports a branch's protection state, returned by +// DefaultBranchProtection. +// +// Protected, WriteRoleCanPush, and WriteRoleCanMerge are evaluated on EVERY path, +// including when Protected is false. That is a deliberate invariant, not an +// incidental one: an unprotected branch is the case where a write-role bot CAN +// push and CAN merge, so a driver that returned the zero value there would report +// the most dangerous branch in the repo as "cannot push, cannot merge". A +// consumer that reads those three without checking Protected first would invert +// its guardrail on exactly the worst case — so drivers report what is true of the +// branch, never what was convenient to skip (the GitLab 404 arm hardcodes these +// two CanPush/CanMerge fields true; the Forgejo unprotected early-return returns +// them true for the supported write-bot config — its user_can_* are bot-scoped, so +// a non-write bot, already a ProjectRole finding, reads false there, but the +// Protected-first guard fires "not protected" regardless). See +// TestDefaultBranchProtectionUnprotectedIsNotSafe and its Forgejo twin, which fail +// if either driver reverts to the zero value there. +// +// BotCanPush and BotCanMerge are DIFFERENT and the invariant above does NOT extend +// to them: they are per-user-grant flags — "a push/merge access level names the +// bot user directly" — meaningful ONLY when Protected is true. On an unprotected +// branch no such grant exists, so they are legitimately false there. That makes +// them a trap read on their own: a consumer writing `if BotCanPush || BotCanMerge` +// without a Protected-first guard reads false,false on unprotected main and +// concludes safe — the same R12 inversion. The GitLab driver populates them from a +// protected branch's access levels; the Forgejo driver never sets them (its branch +// endpoint folds the bot's own rights into user_can_push/user_can_merge, which map +// onto WriteRoleCan*), so on Forgejo they are always false. Consumers must gate on +// Protected first — M3's evaluateRepo does. +type BranchProtection struct { + // Protected is false when the branch has no protection rule at all (the + // driver maps a 404 from the protected-branches endpoint to this). It is a + // finding in its own right, not a precondition for reading the fields below. + Protected bool + // WriteRoleCanPush is true when any principal at the write role may push to + // the branch — either because a push access level admits it, or because the + // branch is unprotected and so admits every writer. It is load-bearing: a + // default branch a write-role bot may push to does not protect main. + WriteRoleCanPush bool + // WriteRoleCanMerge is true when any principal at the write role may merge + // into the branch. uzi's "an agent can only ever open a merge request" + // guarantee needs this as much as it needs WriteRoleCanPush: a bot that can + // merge its own MR does not need push to land code on main. + WriteRoleCanMerge bool + // BotCanPush is true when a push access level names the bot user directly (a + // per-user allow-to-push grant), which lets the bot push to the protected + // branch even at write role — a false negative the role/WriteRoleCanPush + // checks alone would miss. Group-level push grants to the bot are NOT detected + // (they need an extra membership call); that gap is documented for manual audit. + // GitLab only: the Forgejo driver never sets this (see the type doc). Read it + // behind a Protected-first guard, never on its own. + BotCanPush bool + // BotCanMerge is the merge counterpart of BotCanPush: a merge access level + // naming the bot user directly. Same group-level gap; likewise GitLab-only and + // meaningful only when Protected is true. + BotCanMerge bool + // ProtectionUnverified is true when the driver could not authoritatively + // determine the write role's push/merge rights on a PROTECTED branch — it is + // meaningful only when Protected is true. It exists for the GitHub D6 + // legacy-branch-protection case (PRD #238): GitHub's admin-gated + // /branches/{b}/protection endpoint 403s a write-role bot, and legacy branch + // protection is invisible through the reader-gated rulesets endpoint, so on a + // legacy-protected repo the bot can read that the branch is Protected but not + // who may push or merge. There the driver reports WriteRoleCanPush=false and + // WriteRoleCanMerge=false (NEVER a fabricated true) AND sets this flag, so + // "undetermined" is never conflated with an authoritatively-verified false. + // + // The zero value is false = verified, which is the safe default the five test + // fakes and the GitLab/Forgejo drivers get for free: they always determine + // push/merge authoritatively, so they never set it. A consumer must treat + // `Protected && ProtectionUnverified` as FAIL-CLOSED — the same tier as a + // detected write-role-can-push violation — because the Can* fields being false + // here means "could not tell", not "safe". Reading them as safe reopens the R12 + // inversion on exactly the case the flag exists to mark. This is a consumer + // requirement recorded for PRD #66; adding the field changes no interface + // method, so it does not break the fakes. + ProtectionUnverified bool +} + +// MR states as GitLab reports them on the single-MR GET. The MR-close watcher +// (PRD #24) only acts on the opened↔closed edges; merged and locked are recorded +// but never move a card (a merge closes the issue via `Closes #N`, which the +// existing issue-close sync owns; locked is transient during merge processing). +const ( + MRStateOpened = "opened" + MRStateClosed = "closed" + MRStateMerged = "merged" + MRStateLocked = "locked" +) + +// MergeRequest is a forge merge request as the MR-close watcher observes it +// (PRD #24). Only the fields the watcher needs are carried; add more as callers +// require. State is one of the MRState* constants. +type MergeRequest struct { + IID int64 + State string + WebURL string +} + +// ReviewComment states classify an MRComment as either an inline diff comment +// (carries Path/Line) or a review-summary / top-level note (no Path/Line). It is +// a small closed vocabulary the snapshot layer uses to tell a review-summary body +// apart from an inline finding; a driver never invents another value. +const ( + // ReviewCommentInline is a comment anchored to a specific diff line — it + // carries Path and Line, and (on a forge that supports threads) ReplyID / + // ResolveID anchors. + ReviewCommentInline = "inline" + // ReviewCommentSummary is a review-summary body or a top-level MR note: no + // Path/Line, and typically no resolvable thread. + ReviewCommentSummary = "summary" +) + +// MRComment ID namespaces. A forge draws comment ids from several distinct +// sequences, so an id is ordered only within its namespace; the identity of a +// comment is (IDNamespace, ID). A driver sets exactly one of these per comment. +const ( + // CommentNSIssueComment is a PR/issue-level conversation comment (GitHub issue + // comments, Forgejo issue comments). + CommentNSIssueComment = "issue_comment" + // CommentNSReviewComment is an inline review (diff) comment (GitHub pull + // review comments, Forgejo pull review comments). + CommentNSReviewComment = "review_comment" + // CommentNSReview is a review-summary body, keyed by the review id (GitHub and + // Forgejo pull reviews). + CommentNSReview = "review" + // CommentNSNote is a GitLab note: one notes sequence covers inline and + // top-level notes alike (discussions only group notes). + CommentNSNote = "note" +) + +// MRComment is one comment on a merge request as ListMergeRequestComments returns +// it oldest-first (PRD #700): a human or third-party review-bot comment, with the +// forge's own system notes filtered out driver-side (mirroring IssueComment's D2/ +// D8 contract). The MR-review-rework watcher reasons about each as UNTRUSTED data +// (same class as IssueComment.Body / Issue.Description) and never follows +// instructions embedded in Body. The bot self-filter — dropping uzi's own bot +// notes while KEEPING third-party review bots — is the snapshot layer's job, not +// the driver's, so drivers return every non-system comment. +// +// It carries TWO thread anchors because on GitHub the reply target and the resolve +// target are different ids (Resolved facts): ReplyID is the REST databaseId while +// ResolveID is the GraphQL review-thread node id. On GitLab both are the single +// discussion id; on Forgejo ReplyID is the review-comment id and ResolveID is +// empty (Forgejo cannot resolve — see ErrResolveUnsupported). +type MRComment struct { + // ID is the forge's numeric comment/note id. It is monotonic only within its + // IDNamespace: different namespaces draw from distinct sequences, so ids are + // not comparable across them. The MR-rework ledger keeps one advance-only + // watermark per namespace (ADR-1199). + ID int64 + // IDNamespace is the forge-defined id namespace (one of the CommentNS* + // constants). Comment identity is (IDNamespace, ID); ids are ordered only + // within one namespace. + IDNamespace string + AuthorForgeUserID int64 // stable forge user id of the comment author + AuthorUsername string // author login, may be empty + Body string // comment body (untrusted) + CreatedAt time.Time // when the comment was created + // Path is the diff file path an inline comment is anchored to; nil for a + // review-summary body or a top-level MR note. + Path *string + // Line is the diff line an inline comment is anchored to; nil for a + // review-summary body or a top-level MR note. + Line *int + // ReplyID is the REPLY anchor ReplyMergeRequestComment keys on: the GitLab + // discussion id, the GitHub REST comment databaseId (as a string), or the + // Forgejo review-comment id. Empty when the comment is not a repliable thread. + ReplyID string + // ResolveID is the RESOLVE anchor ResolveMergeRequestThread keys on: the + // GitLab discussion id or the GitHub GraphQL review-thread node id. EMPTY on + // Forgejo (no resolvable-thread concept) and for non-thread comments. + ResolveID string + // HeadSHA is the diff head SHA the comment was written against, for the + // staleness gate (Decision 6). Empty when the driver cannot supply one (e.g. a + // top-level note that carries no diff position). + HeadSHA string + // ReviewState is one of the ReviewComment* constants: "inline" for a diff-line + // comment, "summary" for a review-summary body or top-level note. + ReviewState string +} + +// IsKnownMRState reports whether s is one of the MR states this integration +// recognizes. The MR-close watcher records and acts on known states only; an +// unrecognized or empty value is ignored so a transient forge glitch cannot +// poison the watcher's edge baseline (reviewer hardening, PRD #24 Decision Log). +func IsKnownMRState(s string) bool { + switch s { + case MRStateOpened, MRStateClosed, MRStateMerged, MRStateLocked: + return true + default: + return false + } +} + +// Pipeline is the latest pipeline uzi caches for a watched ref (PRD #6). Status +// is the raw GitLab pipeline status — one of created|waiting_for_resource| +// preparing|pending|running|success|failed|canceled|skipped|manual|scheduled — +// which the web layer collapses to a handful of badge tones; the cache stores it +// verbatim so uzi never invents a status the forge did not report. CreatedAt / +// UpdatedAt are zero when the forge omitted them. +type Pipeline struct { + ID int64 + Ref string + SHA string + Status string + WebURL string + CreatedAt time.Time + UpdatedAt time.Time +} + +// Job is one job of a pipeline (PRD #6). The CI-fix path snapshots a failed +// pipeline's jobs (name/stage/status/url) plus each one's log tail so the run +// stays self-contained. Status is the raw GitLab job status. +// +// Steps and StartedAt/FinishedAt were added for the forge view (PRD #1255): a +// GitHub Actions job exposes per-step detail (WorkflowJob.Steps) and both +// timestamps; GitLab/Forgejo jobs carry no steps (Steps stays nil) but do carry +// timing. A driver fills what its SDK exposes and leaves the rest zero. +type Job struct { + ID int64 + Name string + Stage string + Status string + WebURL string + // Steps are the job's ordered steps (GitHub Actions only). Nil on GitLab and + // Forgejo, whose job APIs expose no step breakdown. + Steps []Step + // StartedAt / FinishedAt bound the job's execution; zero when the SDK omits + // them (a not-yet-started or not-yet-finished job), which stores as NULL. + StartedAt time.Time + FinishedAt time.Time + // CreatedAt is when the forge created the job (#3225); zero when unreported. + // StartedAt - CreatedAt includes dependency and manual waits, so it is not + // runner queue time. + CreatedAt time.Time + // Attempt is the forge-reported run attempt the job belongs to (GitHub and + // Forgejo run_attempt); nil when the forge reports none (GitLab). Never derived. + Attempt *int64 + // QueuedDuration is the forge-measured runner queue wait (GitLab only); nil when + // unreported. GitLab's field cannot distinguish absent from zero, so a zero is + // treated as unknown. + QueuedDuration *time.Duration +} + +// Step is one step of a job (PRD #1255). Only GitHub Actions exposes steps +// (WorkflowJob.Steps → TaskStep); the neutral type carries the raw Status and +// Conclusion verbatim so pipelinestatus.Tone classifies them exactly as the job +// and pipeline statuses. Number is the 1-based step index. +type Step struct { + Name string + Status string + Conclusion string + Number int + StartedAt time.Time + CompletedAt time.Time +} + +// ReviewDecision is the forge-neutral, closed review-decision enum for an open +// merge request (PRD #1255 D6). GitHub has no REST reviewDecision, GitLab has no +// review-decision concept at all, and Forgejo has neither — so every driver +// DERIVES this value (foldReviewDecision for GitHub/Forgejo, a free-tier +// approximation for GitLab), never trusts a single field. The four values are the +// only ones a driver may produce; an unrecognized state folds to ReviewNone. +type ReviewDecision string + +const ( + // ReviewChangesRequested means a reviewer's latest decision asks for changes. + // On GitLab it ALSO covers an unresolved blocking discussion (the closest + // free-tier signal; see D6), so an MR with one open thread bands as NEEDS YOU + // by design, not by bug. + ReviewChangesRequested ReviewDecision = "changes_requested" + // ReviewApproved means at least one reviewer approved and none of the latest + // per-reviewer decisions requests changes. + ReviewApproved ReviewDecision = "approved" + // ReviewRequired means a review is requested but none has landed a decision. + ReviewRequired ReviewDecision = "review_required" + // ReviewNone means no review is requested and none has landed a decision. + ReviewNone ReviewDecision = "none" +) + +// Review is one per-reviewer review on an open merge/pull request as the forge-view +// DETAIL route renders it (PRD #1255 D6): the reviewer's login, the review's RAW +// forge state (GitHub: APPROVED/CHANGES_REQUESTED/COMMENTED/DISMISSED/PENDING; +// Forgejo: APPROVED/REQUEST_CHANGES/COMMENT/…), and when it was submitted. It is the +// same review stream the drivers fold into ReviewDecision, returned verbatim so the +// detail view can list each reviewer's latest state; the fold (foldReviewDecision) is +// the classification, this is the display list. GitLab has no per-reviewer review +// stream (approvals are a separate Premium concept), so its driver returns none. +// Author and State are UNTRUSTED forge text. +type Review struct { + Author string + State string + SubmittedAt time.Time +} + +// MergeRequestSummary is one merge/pull request's full neutral summary as +// GetMergeRequestSummary returns it per iid (PRD #1255 D5/M2b): the fields the pulls +// screen bands, sorts and renders. Conflicts is a pointer because "unknown" is a +// real third state — GitHub omits mergeability on the list and computes it lazily, +// so a driver reports nil rather than guessing false. Every string field is +// forge-authored UNTRUSTED text (same class as Issue.Title / MRComment.Body). +type MergeRequestSummary struct { + IID int64 + Title string + Author string + SourceBranch string + TargetBranch string + HeadSHA string + Draft bool + Conflicts *bool + ReviewDecision ReviewDecision + // State is one of the MRState* constants (opened/closed/merged/locked), + // populated by GetMergeRequestSummary so GetPull can 404 a non-open PR. It is + // deliberately NOT carried on the wire DTOs — the forge view is open-only. + State string + WebURL string + Additions int + Deletions int + Commits int + CreatedAt time.Time + UpdatedAt time.Time +} + +// MergeRequestRef is the cheap list-row identity ListMergeRequestRefs returns for +// each open merge/pull request (PRD #1255 M2b): just what the forge yields in ONE +// list call, enough for the caller to address each PR by iid and enrich it with a +// per-iid GetMergeRequestSummary. Splitting the old bundled list into refs + per-iid +// summaries is what lets the memo (M2b part 2) make each PR's enrichment individually +// addressable, and what lets GetPull find ANY open PR by iid rather than scanning the +// most-recently-updated page. IID and HeadSHA are forge-authored identifiers, +// UpdatedAt the activity timestamp the list is ordered by. +type MergeRequestRef struct { + IID int64 + HeadSHA string + UpdatedAt time.Time +} + +// Check is one status/check for a commit sha (PRD #1255 D5), merging the two +// GitHub surfaces (check-runs and commit statuses) and their GitLab/Forgejo +// analogues into one neutral shape, deduplicated by Name so a bot reporting +// through either surface appears once. Status is the run phase — one of +// "queued", "in_progress" or "completed" — and Conclusion, meaningful only once +// completed, is one of "success", "failure", "neutral", "cancelled", "skipped", +// "timed_out", "action_required" or "" (empty while still running). A consumer +// collapses the pair the same way the driver collapses a job status (Conclusion +// once completed, else Status) and classifies it with pipelinestatus.Tone. Source +// is the reporting app's slug (a check-run's App.Slug) or the literal "status" for +// a commit-status surface. Name and Description are UNTRUSTED forge text. +type Check struct { + Name string + Status string + Conclusion string + Description string + WebURL string + StartedAt time.Time + CompletedAt time.Time + Source string +} + +// WorkflowRun is one CI run for the forge view's `ci` screen (PRD #1255 D5): a +// GitHub Actions workflow run, a GitLab pipeline, or a Forgejo Actions run. Status +// carries the raw run phase and Conclusion the terminal outcome where the forge +// splits them (GitHub); GitLab/Forgejo fold everything into Status and leave +// Conclusion empty, so a consumer collapses the pair exactly as for Check. +// JobsDone/JobsTotal are best-effort and filled only for in-flight rows by the +// route layer (no forge's runs list includes jobs), so a driver leaves them zero. +// Name, Title, Actor, Branch are UNTRUSTED forge text. +type WorkflowRun struct { + // RunAttempt is nil when the forge does not report an attempt. + RunAttempt *int64 + RepositoryID int64 + HeadRepositoryID int64 + // WorkflowKey is the provider identity: GitHub workflow ID, Forgejo path, GitLab name/ref. + WorkflowKey string + ID int64 + Name string + Number int64 + Event string + Branch string + SHA string + Status string + Conclusion string + Title string + Actor string + WebURL string + CreatedAt time.Time + UpdatedAt time.Time + StartedAt time.Time + JobsDone int + JobsTotal int +} + +// ListMergeRequestsOptions filters ListMergeRequestRefs. State's zero value means +// "opened" (the only state the pulls screen shows); Limit == 0 means the driver's +// default page (no explicit cap), a positive value stops once that many rows are +// collected. +type ListMergeRequestsOptions struct { + State string + Limit int +} + +// ListWorkflowRunsOptions filters ListWorkflowRuns. Limit == 0 means the driver's +// default page; Branch/Event/Status are optional server-side filters passed +// through to the forge (an unset field is not filtered on). +type ListWorkflowRunsOptions struct { + Limit int + Branch string + Event string + Status string +} + +// CodeHost is the code-host half of the driver contract: repository and identity +// reads, the guardrail reads (token, role, branch protection), merge requests, CI +// pipelines and checks, artifacts and refs. It holds every Forge method that is +// not on IssueTracker. +type CodeHost interface { + // RepositoryAuthorEligibility assesses a stable user ID independently of the + // authenticated bot's ProjectRole. Missing/deleted identities and incomplete + // evidence return AuthorUnknown with an error. + RepositoryAuthorEligibility(ctx context.Context, projectID, authorForgeUserID int64) (AuthorEligibility, error) + // VerifyToken confirms the PAT works and returns the bot identity. + VerifyToken(ctx context.Context) (BotIdentity, error) + // ListProjects returns every project the bot has at least Developer access + // to (membership=true). + ListProjects(ctx context.Context) ([]Project, error) + // GetProject freshly reads bounded metadata by positive numeric ID, without + // inventory enumeration or cached identity. It requires complete, coherent + // identity and WebURL; DefaultBranch may be empty. Any failure returns zero + // Project and invalidates the requested ID's cached slug. + GetProject(ctx context.Context, projectID int64) (Project, error) + // UserExists reports whether a user with the given username exists on the + // forge. It backs the best-effort verification of a user's self-declared + // human_username (PRD #19 M3): a false — or an error — downgrades a save to + // "saved with a warning", never a hard failure. A blank username is not a + // forge lookup; it returns (false, nil). + UserExists(ctx context.Context, username string) (bool, error) + // GetMergeRequest returns one merge request by its project-scoped IID. The + // MR-close watcher (PRD #24) polls this for cards parked in Human Review to + // detect an opened→closed (reviewer rejected the MR) edge. + GetMergeRequest(ctx context.Context, projectID, mrIID int64) (MergeRequest, error) + // ListMergeRequestComments returns an MR's HUMAN and third-party review-bot + // comments oldest-first (PRD #700), paginated internally and bounded by the + // shared forge sanity ceilings (maxForgeItems/maxForgePages). Each driver drops + // the forge's own system/timeline notes (D2) and normalizes ordering to + // oldest-first (D8), and populates the reply/resolve anchors, HeadSHA, and + // Path/Line wherever the SDK exposes them. It does NOT filter bot authors — the + // snapshot layer does that against the connection's own bot id, so a + // third-party review bot (CodeRabbit) stays readable. Bodies are untrusted free + // text. GitHub stitches three REST sources with a GraphQL reviewThreads read to + // supply both anchors (Resolved facts). Every comment carries its IDNamespace + // (CommentNS*): ids are ordered only within one namespace. + ListMergeRequestComments(ctx context.Context, projectID, mrIID int64) ([]MRComment, error) + // ReplyMergeRequestComment posts a reply in the thread keyed on replyID (an + // MRComment.ReplyID: the GitLab discussion id, the GitHub REST databaseId, or + // the Forgejo review-comment id). The MR-review-rework worker uses it to answer + // each finding ("done in " / "skipped because X"). No caller until PRD + // #700's plumbing. + ReplyMergeRequestComment(ctx context.Context, projectID, mrIID int64, replyID, body string) error + // ResolveMergeRequestThread resolves the review thread keyed on resolveID (an + // MRComment.ResolveID: the GitLab discussion id or the GitHub GraphQL thread + // node id). Returns ErrResolveUnsupported on Forgejo, whose forge has no + // resolvable-thread concept — the worker swallows that sentinel and keeps the + // reply (reply-only is the documented Forgejo contract). No caller until PRD + // #700's plumbing. + ResolveMergeRequestThread(ctx context.Context, projectID, mrIID int64, resolveID string) error + // TokenInfo returns introspection data for the PAT the client authenticates + // with: its scopes, whether it is active, and its expiry. GitLab: GET + // /personal_access_tokens/self. Returns ErrTokenIntrospectionUnsupported when + // the forge version lacks the endpoint (so the caller warns, not blocks). + TokenInfo(ctx context.Context) (TokenInfo, error) + // ProjectRole returns the bot's effective (direct or inherited) role on a + // project, and whether the bot is a member at all. member is false with a nil + // error when the bot has no effective membership (a 404 from the members/all + // lookup). GitLab: GET /projects/:id/members/all/:user_id, whose numeric + // access level the driver maps onto Role. + ProjectRole(ctx context.Context, projectID, forgeUserID int64) (role Role, member bool, err error) + // DefaultBranchProtection reports whether the given branch is protected, and + // whether the write role or the bot specifically may push to or merge into it. + // GitLab: GET /projects/:id/protected_branches/:name (a 404 means + // unprotected). botUserID is the bot's forge user id, used to flag a per-user + // grant. Every returned field is evaluated on every path — see + // BranchProtection. + DefaultBranchProtection(ctx context.Context, projectID int64, branch string, botUserID int64) (BranchProtection, error) + // LatestPipeline returns the newest branch pipeline for a ref, or + // ErrNoPipeline when the ref has none (no CI configured, or it never ran). + // GitLab: GET /projects/:id/pipelines?ref=&per_page=1 (default + // order_by=id desc, so the first row is newest). + LatestPipeline(ctx context.Context, projectID int64, ref string) (Pipeline, error) + // LatestMRPipeline returns the newest (max-by-id) pipeline attached to a merge + // request. This is what catches detached MR pipelines (refs/merge-requests/:iid/ + // head) and merged-results pipelines, which never appear under the source-branch + // ref — so run-branch status and fix verification key on the run's MR, not its + // branch ref. Returns ErrNoPipeline when the MR has no pipeline. GitLab: GET + // /projects/:id/merge_requests/:iid/pipelines — note this endpoint groups + // merge_request_event pipelines first (then id-desc within a group), NOT a plain + // id-desc, so the driver picks the max id rather than trusting the first row. + LatestMRPipeline(ctx context.Context, projectID, mrIID int64) (Pipeline, error) + // ListPipelineJobs returns the pipeline's jobs with status/stage/name, + // paginated internally. GitLab: GET /projects/:id/pipelines/:pipeline_id/jobs. + // Called only at fix-trigger time (to snapshot failed jobs), never on the poll + // tick. + ListPipelineJobs(ctx context.Context, projectID, pipelineID int64) ([]Job, error) + // ListPipelineJobAttempts returns the jobs of EVERY attempt of a run, not just + // the latest (#3225, the CI scout's history read): GitHub filter=all, GitLab + // include_retried=true. Timestamps are only what the forge reported: unlike + // ListPipelineJobs, GitLab's FinishedAt is never derived from StartedAt plus + // Duration. Same budget and pagination path as ListPipelineJobs. Returns + // ErrJobAttemptsUnsupported where complete history is unavailable (Forgejo). + ListPipelineJobAttempts(ctx context.Context, projectID, pipelineID int64) ([]Job, error) + // JobLogTail returns at most maxBytes from the END of a job's trace (a + // failure's cause concludes its log); maxBytes <= 0 returns the whole trace. + // GitLab: GET /projects/:id/jobs/:job_id/trace — the endpoint has no range/tail + // parameter, so this is a full download truncated client-side, capped by a + // fail-closed download ceiling; acceptable because it runs only at fix-trigger + // time, never on the poll tick. The returned tail passes through the driver's + // PAT redactor (the snapshot path applies a second, pattern-based scrub). NOTE: + // the returned tail is NOT itself a secret-safe log scrubber for arbitrary + // third-party tokens — see the snapshot scrubber in the ci-fix handler. + JobLogTail(ctx context.Context, projectID, jobID int64, maxBytes int) (string, error) + // ListMergeRequestRefs returns the project's open merge requests as cheap + // list-row refs (PRD #1255 M2b) in ONE list call, newest activity first, + // paginated internally — just IID/HeadSHA/UpdatedAt per row, with NO per-PR + // enrichment (that is GetMergeRequestSummary's job). opts.State defaults to + // "opened" (the only state the pulls screen shows); opts.Limit optionally caps + // the rows. Pagination is bounded by the driver's caps; errors are PAT-redacted, + // and on GitHub a rate-limit surfaces as *RateLimitError (GitLab/Forgejo return a + // plain error; see RateLimitError). + ListMergeRequestRefs(ctx context.Context, projectID int64, opts ListMergeRequestsOptions) ([]MergeRequestRef, error) + // GetMergeRequestSummary returns ONE merge/pull request's full neutral summary by + // iid (PRD #1255 M2b): IID, Title, Author, branches, HeadSHA, Draft, the Conflicts + // tri-state, ReviewDecision, State, WebURL and (where the forge exposes them) + // Additions/Deletions/Commits and timestamps. Each driver derives ReviewDecision + // (no forge exposes it on the row) and reads whatever per-MR detail the summary + // needs (GitHub: PullRequests.Get + ListReviews; GitLab: GetMergeRequest + + // approvals; Forgejo: GetPullRequest + the raw reviews fold). Returns + // ErrMergeRequestNotFound (errors.Is-matchable) when the forge 404s the iid. Every + // string field is untrusted forge text; errors are PAT-redacted, and on GitHub a + // rate-limit surfaces as *RateLimitError (GitLab/Forgejo return a plain error; see + // RateLimitError). + GetMergeRequestSummary(ctx context.Context, projectID, iid int64) (MergeRequestSummary, error) + // ListChecks returns every status/check for a commit sha (PRD #1255 D5), merging + // the forge's check and commit-status surfaces and DEDUPLICATING by Name so a bot + // reporting through either appears once. GitHub: ListCheckRunsForRef + + // GetCombinedStatus. GitLab: the sha's newest pipeline's jobs + GetCommitStatuses. + // Forgejo: the Actions jobs of the runs on that sha + ListStatuses. Paginated + // internally; errors are PAT-redacted. + ListChecks(ctx context.Context, projectID int64, sha string) ([]Check, error) + // ListWorkflowRuns returns the project's CI runs newest-first for the `ci` screen + // (PRD #1255 D5): GitHub workflow runs, GitLab pipelines, or Forgejo Actions runs. + // One page (opts.Limit caps it, bounded by the driver's max page size); + // Branch/Event/Status are optional server-side filters. Where the forge version + // lacks the endpoint the driver returns an error wrapping ErrForgeVersionUnsupported + // (the honest degrade path). Errors are PAT-redacted; on GitHub a rate-limit + // surfaces as *RateLimitError (GitLab/Forgejo return a plain error; see RateLimitError). + ListWorkflowRuns(ctx context.Context, projectID int64, opts ListWorkflowRunsOptions) ([]WorkflowRun, error) + // GetWorkflowRun returns one CI run's header (the neutral WorkflowRun) by its + // forge-native run/pipeline id, for the `ci` drill-in. ErrForgeVersionUnsupported + // on a forge whose Actions/pipelines API is absent (mirrors ListWorkflowRuns). + GetWorkflowRun(ctx context.Context, projectID, runID int64) (WorkflowRun, error) + + // ListRunArtifacts returns bounded metadata for this exact run, without URL authority. + ListRunArtifacts(ctx context.Context, projectID, runID int64) ([]Artifact, error) + // DownloadArtifact fetches an ID-addressed archive, enforcing maxBytes on actual bytes. + DownloadArtifact(ctx context.Context, projectID, artifactID, maxBytes int64) ([]byte, error) + // ListMergeRequestReviews returns an open MR's per-reviewer reviews for the + // forge-view detail route (PRD #1255 D6), oldest-first — the same review stream the + // driver folds into ReviewDecision, returned verbatim (State is the raw forge value) + // so the PR view can render each reviewer's latest state. GitHub reads + // PullRequests.ListReviews; Forgejo reads /pulls/{iid}/reviews; GitLab returns an + // empty slice (no per-reviewer review stream on the free tier — its decision is the + // BlockingDiscussionsResolved / approvals approximation, not a review list). Every + // string field is untrusted forge text; errors are PAT-redacted. + ListMergeRequestReviews(ctx context.Context, projectID, mrIID int64) ([]Review, error) + // ProjectCIConfigPath returns the project's configured CI config path (GitLab + // ci_config_path); empty string means the driver's default (.gitlab-ci.yml). It + // carries no secret material — a project's ci_config_path is a repo-relative + // glob, not a token — and any driver error is PAT-redacted like every other + // method. Used by the PRD #71 auto-fix guard to know which paths a fix may touch. + ProjectCIConfigPath(ctx context.Context, projectID int64) (string, error) + // BranchHead returns the 40-hex commit id the named branch currently points at + // (issue #1582 M1). GitHub: GET /repos/{o}/{r}/branches/{branch} (commit.sha); + // GitLab: GET /projects/:id/repository/branches/:branch (commit.id); Forgejo: GET + // /repos/{o}/{r}/branches/{branch} (commit.id). Every driver sends it through a + // redirect-refusing client, so ANY 3xx (a GitHub rename 301 included) is an error, + // never ErrRefNotFound and never another host's answer; GitHub and Forgejo also + // require the response to name the requested branch. A 404 returns ErrRefNotFound; a + // head that is not a 40-char lowercase hex id is an error. Errors are PAT-redacted. + BranchHead(ctx context.Context, projectID int64, branch string) (string, error) + // RefHead returns the 40-hex commit id a FULL ref name (for example + // refs/uzi-checkpoints/agent/issue-7) currently points at (issue #1751 M2), the head a + // live predecessor settle proves against when the successor published to its forge + // checkpoint ref rather than a branch. The ref must start with "refs/" and be a + // well-formed git ref name (no "..", "//", "@{", control character, space or any of + // ~ ^ : ? * [ \); anything else is an error with no request. GitHub: GET + // /repos/{o}/{r}/git/ref/{ref without "refs/"} (object.sha of a commit object, the + // echoed `ref` must equal the requested ref); GitLab: GET + // /projects/:id/repository/commits/{url-escaped ref} (id); Forgejo: GET + // /repos/{o}/{r}/git/refs/{ref without "refs/"} (the entry whose `ref` equals the + // requested ref exactly, object.sha of a commit object). Every driver refuses redirects + // like BranchHead. A 404 returns ErrRefNotFound; a head that is not a 40-char lowercase + // hex id, or an answer naming another ref, is an error. Errors are PAT-redacted. + RefHead(ctx context.Context, projectID int64, ref string) (string, error) + // CompareAncestry answers "is candidate an ancestor of, or equal to, head" using the + // forge's own compare API (issue #1582 M1). Both arguments must be 40-char + // lowercase hex commit ids (never ref names); anything else is AncestryUnknown with + // an error. head == candidate is AncestryAncestor WITHOUT a request. Every error, + // rate limit (a 429, or GitHub's rate-limit 403), oversize body, or unrecognized + // response is AncestryUnknown; only an explicit, recognized positive answer is + // AncestryAncestor. Every driver's compare requests refuse redirects (a 3xx is + // AncestryUnknown with an error). Errors are PAT-redacted. + CompareAncestry(ctx context.Context, projectID int64, head, candidate string) (Ancestry, error) +} diff --git a/api/internal/forge/forge.go b/api/internal/forge/forge.go index ad9840cfce..05ef5a0808 100644 --- a/api/internal/forge/forge.go +++ b/api/internal/forge/forge.go @@ -1,8 +1,8 @@ // Package forge is uzi's forge-generic integration layer. Callers depend only -// on the Forge interface and the neutral domain types below; a driver (GitLab -// today, Forgejo later) is selected by New and never imported directly. This is -// the seam that lets a second forge land without touching handlers, schema, or -// UI flows. +// on the Forge interface and the neutral domain types in this package; a driver +// (GitLab, GitHub or Forgejo) is selected by New and never imported directly. +// This is the seam that lets a second forge land without touching handlers, +// schema, or UI flows. package forge import ( @@ -83,24 +83,6 @@ var ErrMergeRequestNotFound = errors.New("forge: merge request not found") // token material. var ErrRefNotFound = errors.New("forge: ref not found") -// Ancestry is the tri-state answer CompareAncestry gives to "is candidate an ancestor -// of, or equal to, head" (issue #1582 M1). Only AncestryAncestor is a positive proof; -// AncestryUnknown is the fail-closed answer for every error, rate limit, oversize or -// unrecognized response, and the caller must never read it as "not an ancestor -// either" (it simply proves nothing). -type Ancestry string - -const ( - // AncestryAncestor: the forge explicitly reported candidate reachable from head - // (or the two are the same commit). - AncestryAncestor Ancestry = "ancestor" - // AncestryNotAncestor: the forge explicitly reported candidate NOT reachable from - // head (GitHub ahead/diverged, GitLab merge base != candidate). - AncestryNotAncestor Ancestry = "not_ancestor" - // AncestryUnknown: no conclusive answer. Always the result alongside an error. - AncestryUnknown Ancestry = "unknown" -) - // RateLimitError is the forge-neutral rate-limit error (PRD #1255 D4). The GitHub // driver wraps GitHub's rate-limit shapes into it so a caller can errors.As it and // map it to HTTP 429 + Retry-After. Only the GitHub driver produces it today: GitHub @@ -179,40 +161,6 @@ func SupportedTypesList() string { return strings.Join(names, ", ") } -// Role is a bot's effective permission level on a project, in forge-neutral -// terms. It deliberately carries no number: GitLab's access levels (30 = -// Developer) are a GitLab implementation detail, and Forgejo has no numeric -// levels at all — its API returns exactly this vocabulary -// (none|read|write|admin|owner). A driver that mapped "write" onto 30 to satisfy -// a GitLab-shaped contract would be leaking one forge's model through the seam -// this package exists to be. -type Role string - -const ( - // RoleNone is no access. - RoleNone Role = "none" - // RoleRead can read but not write. - RoleRead Role = "read" - // RoleWrite can push branches and open merge requests. This is the exact role - // uzi's bot must hold: enough to do the work, not enough to be trusted with - // main. - RoleWrite Role = "write" - // RoleAdmin administers the project (settings, protection rules). - RoleAdmin Role = "admin" - // RoleOwner owns the project. - RoleOwner Role = "owner" -) - -// roleRank orders the roles least- to most-privileged. Unlike a forge's access -// level it is not a wire value and never leaves this package — AtLeast is the -// only way to read it, so no caller can reintroduce an "access level 30" -// comparison against a neutral type. -var roleRank = map[Role]int{RoleNone: 0, RoleRead: 1, RoleWrite: 2, RoleAdmin: 3, RoleOwner: 4} - -// AtLeast reports whether r is min or more privileged. An unrecognized role -// ranks as RoleNone, so an unknown value from a forge never reads as privileged. -func (r Role) AtLeast(min Role) bool { return roleRank[r] >= roleRank[min] } - // BotIdentity is the connected bot account, returned by VerifyToken. type BotIdentity struct { // ForgeUserID is the bot's stable numeric user id on the forge. @@ -227,97 +175,6 @@ type BotIdentity struct { IsAdmin bool } -// TokenInfo is PAT introspection returned by TokenInfo: the scopes it carries, -// whether it is active, and when it expires. GitLab: GET -// /personal_access_tokens/self. -type TokenInfo struct { - // Scopes are the token's granted scopes (e.g. ["api"]). - Scopes []string - // Active is true when the token is usable (not revoked, not expired). - Active bool - // ExpiresAt is the token's expiry; the zero time means it never expires. - ExpiresAt time.Time -} - -// BranchProtection reports a branch's protection state, returned by -// DefaultBranchProtection. -// -// Protected, WriteRoleCanPush, and WriteRoleCanMerge are evaluated on EVERY path, -// including when Protected is false. That is a deliberate invariant, not an -// incidental one: an unprotected branch is the case where a write-role bot CAN -// push and CAN merge, so a driver that returned the zero value there would report -// the most dangerous branch in the repo as "cannot push, cannot merge". A -// consumer that reads those three without checking Protected first would invert -// its guardrail on exactly the worst case — so drivers report what is true of the -// branch, never what was convenient to skip (the GitLab 404 arm hardcodes these -// two CanPush/CanMerge fields true; the Forgejo unprotected early-return returns -// them true for the supported write-bot config — its user_can_* are bot-scoped, so -// a non-write bot, already a ProjectRole finding, reads false there, but the -// Protected-first guard fires "not protected" regardless). See -// TestDefaultBranchProtectionUnprotectedIsNotSafe and its Forgejo twin, which fail -// if either driver reverts to the zero value there. -// -// BotCanPush and BotCanMerge are DIFFERENT and the invariant above does NOT extend -// to them: they are per-user-grant flags — "a push/merge access level names the -// bot user directly" — meaningful ONLY when Protected is true. On an unprotected -// branch no such grant exists, so they are legitimately false there. That makes -// them a trap read on their own: a consumer writing `if BotCanPush || BotCanMerge` -// without a Protected-first guard reads false,false on unprotected main and -// concludes safe — the same R12 inversion. The GitLab driver populates them from a -// protected branch's access levels; the Forgejo driver never sets them (its branch -// endpoint folds the bot's own rights into user_can_push/user_can_merge, which map -// onto WriteRoleCan*), so on Forgejo they are always false. Consumers must gate on -// Protected first — M3's evaluateRepo does. -type BranchProtection struct { - // Protected is false when the branch has no protection rule at all (the - // driver maps a 404 from the protected-branches endpoint to this). It is a - // finding in its own right, not a precondition for reading the fields below. - Protected bool - // WriteRoleCanPush is true when any principal at the write role may push to - // the branch — either because a push access level admits it, or because the - // branch is unprotected and so admits every writer. It is load-bearing: a - // default branch a write-role bot may push to does not protect main. - WriteRoleCanPush bool - // WriteRoleCanMerge is true when any principal at the write role may merge - // into the branch. uzi's "an agent can only ever open a merge request" - // guarantee needs this as much as it needs WriteRoleCanPush: a bot that can - // merge its own MR does not need push to land code on main. - WriteRoleCanMerge bool - // BotCanPush is true when a push access level names the bot user directly (a - // per-user allow-to-push grant), which lets the bot push to the protected - // branch even at write role — a false negative the role/WriteRoleCanPush - // checks alone would miss. Group-level push grants to the bot are NOT detected - // (they need an extra membership call); that gap is documented for manual audit. - // GitLab only: the Forgejo driver never sets this (see the type doc). Read it - // behind a Protected-first guard, never on its own. - BotCanPush bool - // BotCanMerge is the merge counterpart of BotCanPush: a merge access level - // naming the bot user directly. Same group-level gap; likewise GitLab-only and - // meaningful only when Protected is true. - BotCanMerge bool - // ProtectionUnverified is true when the driver could not authoritatively - // determine the write role's push/merge rights on a PROTECTED branch — it is - // meaningful only when Protected is true. It exists for the GitHub D6 - // legacy-branch-protection case (PRD #238): GitHub's admin-gated - // /branches/{b}/protection endpoint 403s a write-role bot, and legacy branch - // protection is invisible through the reader-gated rulesets endpoint, so on a - // legacy-protected repo the bot can read that the branch is Protected but not - // who may push or merge. There the driver reports WriteRoleCanPush=false and - // WriteRoleCanMerge=false (NEVER a fabricated true) AND sets this flag, so - // "undetermined" is never conflated with an authoritatively-verified false. - // - // The zero value is false = verified, which is the safe default the five test - // fakes and the GitLab/Forgejo drivers get for free: they always determine - // push/merge authoritatively, so they never set it. A consumer must treat - // `Protected && ProtectionUnverified` as FAIL-CLOSED — the same tier as a - // detected write-role-can-push violation — because the Can* fields being false - // here means "could not tell", not "safe". Reading them as safe reopens the R12 - // inversion on exactly the case the flag exists to mark. This is a consumer - // requirement recorded for PRD #66; adding the field changes no interface - // method, so it does not break the fakes. - ProtectionUnverified bool -} - // Project is a repo the bot has membership on. type Project struct { // ForgeProjectID is the stable numeric project id (not the path, which can @@ -328,699 +185,18 @@ type Project struct { DefaultBranch string } -// Label is a forge label. Color is optional (used when creating column labels). -type Label struct { - Name string - Color string -} - -// Issue is a forge issue as uzi caches it. Description is carried so the caller -// can compute the PRD-link check at fetch time; it is never persisted. -type Issue struct { - // IsPullRequest distinguishes PRs returned by shared issue endpoints; internal only. - IsPullRequest bool `json:"-"` - IID int64 - Title string - State string // "opened" | "closed" - Labels []string - Description string - Author string // username, may be empty - AuthorForgeUserID int64 // stable author identity; zero means unresolved - WebURL string - UpdatedAt time.Time - Assignees []int64 // forge user ids of the issue's assignees (may be empty, never nil after mapping) -} - -// LabelEvent is one resource label event on an issue: a record that a user added -// or removed a single label at a point in time. Autopilot reads these to attribute -// the autopilot label to the human who added it (PRD #19 Decision 3). Username is -// the actor; it may be empty for a system-generated event. -type LabelEvent struct { - ID int64 - Action string // "add" | "remove" - LabelName string - Username string - CreatedAt time.Time -} - -// IssueNote is a comment created on an issue, returned by CreateIssueNote. -type IssueNote struct { - ID int64 - Body string -} - -// IssueComment is one human-authored comment on an issue, returned by -// ListIssueComments oldest-first. It deliberately carries NO System/type field: -// the GitLab driver drops system notes (D2), and the Forgejo/GitHub drivers read -// human-only comment endpoints (their timeline/event notes live behind separate -// endpoints uzi never calls), so the neutral type only ever represents a human -// comment. Bodies are attacker-influenceable free text (same untrusted class as -// Issue.Description). -type IssueComment struct { - AuthorForgeUserID int64 // stable forge user id of the comment author - AuthorUsername string // author login, may be empty - Body string // comment body (untrusted) - CreatedAt time.Time -} - -// MR states as GitLab reports them on the single-MR GET. The MR-close watcher -// (PRD #24) only acts on the opened↔closed edges; merged and locked are recorded -// but never move a card (a merge closes the issue via `Closes #N`, which the -// existing issue-close sync owns; locked is transient during merge processing). -const ( - MRStateOpened = "opened" - MRStateClosed = "closed" - MRStateMerged = "merged" - MRStateLocked = "locked" -) - -// MergeRequest is a forge merge request as the MR-close watcher observes it -// (PRD #24). Only the fields the watcher needs are carried; add more as callers -// require. State is one of the MRState* constants. -type MergeRequest struct { - IID int64 - State string - WebURL string -} - -// ReviewComment states classify an MRComment as either an inline diff comment -// (carries Path/Line) or a review-summary / top-level note (no Path/Line). It is -// a small closed vocabulary the snapshot layer uses to tell a review-summary body -// apart from an inline finding; a driver never invents another value. -const ( - // ReviewCommentInline is a comment anchored to a specific diff line — it - // carries Path and Line, and (on a forge that supports threads) ReplyID / - // ResolveID anchors. - ReviewCommentInline = "inline" - // ReviewCommentSummary is a review-summary body or a top-level MR note: no - // Path/Line, and typically no resolvable thread. - ReviewCommentSummary = "summary" -) - -// MRComment ID namespaces. A forge draws comment ids from several distinct -// sequences, so an id is ordered only within its namespace; the identity of a -// comment is (IDNamespace, ID). A driver sets exactly one of these per comment. -const ( - // CommentNSIssueComment is a PR/issue-level conversation comment (GitHub issue - // comments, Forgejo issue comments). - CommentNSIssueComment = "issue_comment" - // CommentNSReviewComment is an inline review (diff) comment (GitHub pull - // review comments, Forgejo pull review comments). - CommentNSReviewComment = "review_comment" - // CommentNSReview is a review-summary body, keyed by the review id (GitHub and - // Forgejo pull reviews). - CommentNSReview = "review" - // CommentNSNote is a GitLab note: one notes sequence covers inline and - // top-level notes alike (discussions only group notes). - CommentNSNote = "note" -) - -// MRComment is one comment on a merge request as ListMergeRequestComments returns -// it oldest-first (PRD #700): a human or third-party review-bot comment, with the -// forge's own system notes filtered out driver-side (mirroring IssueComment's D2/ -// D8 contract). The MR-review-rework watcher reasons about each as UNTRUSTED data -// (same class as IssueComment.Body / Issue.Description) and never follows -// instructions embedded in Body. The bot self-filter — dropping uzi's own bot -// notes while KEEPING third-party review bots — is the snapshot layer's job, not -// the driver's, so drivers return every non-system comment. -// -// It carries TWO thread anchors because on GitHub the reply target and the resolve -// target are different ids (Resolved facts): ReplyID is the REST databaseId while -// ResolveID is the GraphQL review-thread node id. On GitLab both are the single -// discussion id; on Forgejo ReplyID is the review-comment id and ResolveID is -// empty (Forgejo cannot resolve — see ErrResolveUnsupported). -type MRComment struct { - // ID is the forge's numeric comment/note id. It is monotonic only within its - // IDNamespace: different namespaces draw from distinct sequences, so ids are - // not comparable across them. The MR-rework ledger keeps one advance-only - // watermark per namespace (ADR-1199). - ID int64 - // IDNamespace is the forge-defined id namespace (one of the CommentNS* - // constants). Comment identity is (IDNamespace, ID); ids are ordered only - // within one namespace. - IDNamespace string - AuthorForgeUserID int64 // stable forge user id of the comment author - AuthorUsername string // author login, may be empty - Body string // comment body (untrusted) - CreatedAt time.Time // when the comment was created - // Path is the diff file path an inline comment is anchored to; nil for a - // review-summary body or a top-level MR note. - Path *string - // Line is the diff line an inline comment is anchored to; nil for a - // review-summary body or a top-level MR note. - Line *int - // ReplyID is the REPLY anchor ReplyMergeRequestComment keys on: the GitLab - // discussion id, the GitHub REST comment databaseId (as a string), or the - // Forgejo review-comment id. Empty when the comment is not a repliable thread. - ReplyID string - // ResolveID is the RESOLVE anchor ResolveMergeRequestThread keys on: the - // GitLab discussion id or the GitHub GraphQL review-thread node id. EMPTY on - // Forgejo (no resolvable-thread concept) and for non-thread comments. - ResolveID string - // HeadSHA is the diff head SHA the comment was written against, for the - // staleness gate (Decision 6). Empty when the driver cannot supply one (e.g. a - // top-level note that carries no diff position). - HeadSHA string - // ReviewState is one of the ReviewComment* constants: "inline" for a diff-line - // comment, "summary" for a review-summary body or top-level note. - ReviewState string -} - -// IsKnownMRState reports whether s is one of the MR states this integration -// recognizes. The MR-close watcher records and acts on known states only; an -// unrecognized or empty value is ignored so a transient forge glitch cannot -// poison the watcher's edge baseline (reviewer hardening, PRD #24 Decision Log). -func IsKnownMRState(s string) bool { - switch s { - case MRStateOpened, MRStateClosed, MRStateMerged, MRStateLocked: - return true - default: - return false - } -} - -// Pipeline is the latest pipeline uzi caches for a watched ref (PRD #6). Status -// is the raw GitLab pipeline status — one of created|waiting_for_resource| -// preparing|pending|running|success|failed|canceled|skipped|manual|scheduled — -// which the web layer collapses to a handful of badge tones; the cache stores it -// verbatim so uzi never invents a status the forge did not report. CreatedAt / -// UpdatedAt are zero when the forge omitted them. -type Pipeline struct { - ID int64 - Ref string - SHA string - Status string - WebURL string - CreatedAt time.Time - UpdatedAt time.Time -} - -// Job is one job of a pipeline (PRD #6). The CI-fix path snapshots a failed -// pipeline's jobs (name/stage/status/url) plus each one's log tail so the run -// stays self-contained. Status is the raw GitLab job status. -// -// Steps and StartedAt/FinishedAt were added for the forge view (PRD #1255): a -// GitHub Actions job exposes per-step detail (WorkflowJob.Steps) and both -// timestamps; GitLab/Forgejo jobs carry no steps (Steps stays nil) but do carry -// timing. A driver fills what its SDK exposes and leaves the rest zero. -type Job struct { - ID int64 - Name string - Stage string - Status string - WebURL string - // Steps are the job's ordered steps (GitHub Actions only). Nil on GitLab and - // Forgejo, whose job APIs expose no step breakdown. - Steps []Step - // StartedAt / FinishedAt bound the job's execution; zero when the SDK omits - // them (a not-yet-started or not-yet-finished job), which stores as NULL. - StartedAt time.Time - FinishedAt time.Time - // CreatedAt is when the forge created the job (#3225); zero when unreported. - // StartedAt - CreatedAt includes dependency and manual waits, so it is not - // runner queue time. - CreatedAt time.Time - // Attempt is the forge-reported run attempt the job belongs to (GitHub and - // Forgejo run_attempt); nil when the forge reports none (GitLab). Never derived. - Attempt *int64 - // QueuedDuration is the forge-measured runner queue wait (GitLab only); nil when - // unreported. GitLab's field cannot distinguish absent from zero, so a zero is - // treated as unknown. - QueuedDuration *time.Duration -} - -// Step is one step of a job (PRD #1255). Only GitHub Actions exposes steps -// (WorkflowJob.Steps → TaskStep); the neutral type carries the raw Status and -// Conclusion verbatim so pipelinestatus.Tone classifies them exactly as the job -// and pipeline statuses. Number is the 1-based step index. -type Step struct { - Name string - Status string - Conclusion string - Number int - StartedAt time.Time - CompletedAt time.Time -} - -// ReviewDecision is the forge-neutral, closed review-decision enum for an open -// merge request (PRD #1255 D6). GitHub has no REST reviewDecision, GitLab has no -// review-decision concept at all, and Forgejo has neither — so every driver -// DERIVES this value (foldReviewDecision for GitHub/Forgejo, a free-tier -// approximation for GitLab), never trusts a single field. The four values are the -// only ones a driver may produce; an unrecognized state folds to ReviewNone. -type ReviewDecision string - -const ( - // ReviewChangesRequested means a reviewer's latest decision asks for changes. - // On GitLab it ALSO covers an unresolved blocking discussion (the closest - // free-tier signal; see D6), so an MR with one open thread bands as NEEDS YOU - // by design, not by bug. - ReviewChangesRequested ReviewDecision = "changes_requested" - // ReviewApproved means at least one reviewer approved and none of the latest - // per-reviewer decisions requests changes. - ReviewApproved ReviewDecision = "approved" - // ReviewRequired means a review is requested but none has landed a decision. - ReviewRequired ReviewDecision = "review_required" - // ReviewNone means no review is requested and none has landed a decision. - ReviewNone ReviewDecision = "none" -) - -// Review is one per-reviewer review on an open merge/pull request as the forge-view -// DETAIL route renders it (PRD #1255 D6): the reviewer's login, the review's RAW -// forge state (GitHub: APPROVED/CHANGES_REQUESTED/COMMENTED/DISMISSED/PENDING; -// Forgejo: APPROVED/REQUEST_CHANGES/COMMENT/…), and when it was submitted. It is the -// same review stream the drivers fold into ReviewDecision, returned verbatim so the -// detail view can list each reviewer's latest state; the fold (foldReviewDecision) is -// the classification, this is the display list. GitLab has no per-reviewer review -// stream (approvals are a separate Premium concept), so its driver returns none. -// Author and State are UNTRUSTED forge text. -type Review struct { - Author string - State string - SubmittedAt time.Time -} - -// MergeRequestSummary is one merge/pull request's full neutral summary as -// GetMergeRequestSummary returns it per iid (PRD #1255 D5/M2b): the fields the pulls -// screen bands, sorts and renders. Conflicts is a pointer because "unknown" is a -// real third state — GitHub omits mergeability on the list and computes it lazily, -// so a driver reports nil rather than guessing false. Every string field is -// forge-authored UNTRUSTED text (same class as Issue.Title / MRComment.Body). -type MergeRequestSummary struct { - IID int64 - Title string - Author string - SourceBranch string - TargetBranch string - HeadSHA string - Draft bool - Conflicts *bool - ReviewDecision ReviewDecision - // State is one of the MRState* constants (opened/closed/merged/locked), - // populated by GetMergeRequestSummary so GetPull can 404 a non-open PR. It is - // deliberately NOT carried on the wire DTOs — the forge view is open-only. - State string - WebURL string - Additions int - Deletions int - Commits int - CreatedAt time.Time - UpdatedAt time.Time -} - -// MergeRequestRef is the cheap list-row identity ListMergeRequestRefs returns for -// each open merge/pull request (PRD #1255 M2b): just what the forge yields in ONE -// list call, enough for the caller to address each PR by iid and enrich it with a -// per-iid GetMergeRequestSummary. Splitting the old bundled list into refs + per-iid -// summaries is what lets the memo (M2b part 2) make each PR's enrichment individually -// addressable, and what lets GetPull find ANY open PR by iid rather than scanning the -// most-recently-updated page. IID and HeadSHA are forge-authored identifiers, -// UpdatedAt the activity timestamp the list is ordered by. -type MergeRequestRef struct { - IID int64 - HeadSHA string - UpdatedAt time.Time -} - -// Check is one status/check for a commit sha (PRD #1255 D5), merging the two -// GitHub surfaces (check-runs and commit statuses) and their GitLab/Forgejo -// analogues into one neutral shape, deduplicated by Name so a bot reporting -// through either surface appears once. Status is the run phase — one of -// "queued", "in_progress" or "completed" — and Conclusion, meaningful only once -// completed, is one of "success", "failure", "neutral", "cancelled", "skipped", -// "timed_out", "action_required" or "" (empty while still running). A consumer -// collapses the pair the same way the driver collapses a job status (Conclusion -// once completed, else Status) and classifies it with pipelinestatus.Tone. Source -// is the reporting app's slug (a check-run's App.Slug) or the literal "status" for -// a commit-status surface. Name and Description are UNTRUSTED forge text. -type Check struct { - Name string - Status string - Conclusion string - Description string - WebURL string - StartedAt time.Time - CompletedAt time.Time - Source string -} - -// WorkflowRun is one CI run for the forge view's `ci` screen (PRD #1255 D5): a -// GitHub Actions workflow run, a GitLab pipeline, or a Forgejo Actions run. Status -// carries the raw run phase and Conclusion the terminal outcome where the forge -// splits them (GitHub); GitLab/Forgejo fold everything into Status and leave -// Conclusion empty, so a consumer collapses the pair exactly as for Check. -// JobsDone/JobsTotal are best-effort and filled only for in-flight rows by the -// route layer (no forge's runs list includes jobs), so a driver leaves them zero. -// Name, Title, Actor, Branch are UNTRUSTED forge text. -type WorkflowRun struct { - // RunAttempt is nil when the forge does not report an attempt. - RunAttempt *int64 - RepositoryID int64 - HeadRepositoryID int64 - // WorkflowKey is the provider identity: GitHub workflow ID, Forgejo path, GitLab name/ref. - WorkflowKey string - ID int64 - Name string - Number int64 - Event string - Branch string - SHA string - Status string - Conclusion string - Title string - Actor string - WebURL string - CreatedAt time.Time - UpdatedAt time.Time - StartedAt time.Time - JobsDone int - JobsTotal int -} - -// ListMergeRequestsOptions filters ListMergeRequestRefs. State's zero value means -// "opened" (the only state the pulls screen shows); Limit == 0 means the driver's -// default page (no explicit cap), a positive value stops once that many rows are -// collected. -type ListMergeRequestsOptions struct { - State string - Limit int -} - -// ListWorkflowRunsOptions filters ListWorkflowRuns. Limit == 0 means the driver's -// default page; Branch/Event/Status are optional server-side filters passed -// through to the forge (an unset field is not filtered on). -type ListWorkflowRunsOptions struct { - Limit int - Branch string - Event string - Status string -} - -// IssueState is the neutral issue-state vocabulary, matching the values the -// drivers already normalize their RESPONSES to and the values the issue cache -// stores in issues.state. -// -// The neutral spelling is GitLab's, not Forgejo's, for the historical reason that -// GitLab was the only driver when the cache's state='opened' filter was written. -// Forgejo says "open"; forgejoIssueState maps that INBOUND, and -// forgejoIssueStateParam maps it OUTBOUND — that function's comment is where the -// asymmetry is explained, and it is the one that matters, since an untranslated -// neutral value is not rejected on the Forgejo lane, it is silently ignored. -// -// (This used to point at StateAll's note for the outbound explanation. StateAll's -// note says nothing about it — it documents the zero value.) -type IssueState string - -const ( - // StateAll is the default and reproduces pre-M6 behaviour exactly: every - // caller that leaves ListIssuesOptions.State zero gets "all". - StateAll IssueState = "" - // StateOpened restricts to open issues. - StateOpened IssueState = "opened" - // StateClosed restricts to closed issues. - StateClosed IssueState = "closed" -) - -// ListIssuesOptions filters ListIssues. Labels is ANDed; an empty UpdatedAfter -// means "no lower bound" (full fetch). -type ListIssuesOptions struct { - // Labels each issue must carry (AND semantics). Typically []string{"PRD"}. - Labels []string - // UpdatedAfter, when non-nil, restricts to issues updated at/after it. The - // bound is inclusive at second granularity on GitLab; callers dedupe the - // boundary by upsert. - UpdatedAfter *time.Time - // State restricts by issue state. The ZERO VALUE IS StateAll, which is what - // every pre-M6 caller relied on: the Closed column and de-label/close eviction - // both need to see closed issues, and the drivers used to hardcode it. - // - // Added by PRD #102 Decision 10 for M6's additive non-PRD fetch, which wants - // open-only — without it that fetch would pull the entire closed history on - // every reconcile purely to discard it. - State IssueState - // Limit optionally caps the number of issues returned. Limit == 0 means NO CAP: - // the driver paginates internally and returns the COMPLETE set, preserving the - // interface's "return complete result sets" contract for every current caller, - // all of which leave this zero. A positive Limit tells the driver to stop once - // that many issues have been collected (truncating a page as needed), so the - // worker forge read (PRD #158) can bound a list without the driver having to walk - // the whole project. It is a soft ceiling on count only — it does not change - // filtering, ordering, or the shape of any returned issue. - Limit int -} - // Forge is the driver contract. Every method takes a context and surfaces // redacted errors (the PAT never appears in an error string). Implementations // paginate internally and return complete result sets. +// +// Forge is the union of IssueTracker (issuetracker.go) and CodeHost (codehost.go). +// Each driver (github.go, gitlab.go, forgejo.go) and forgetest.BaseFake asserts +// at compile time that it implements both halves. +// From PRD #4173 M2 on, callers take the narrower half they need instead of the +// whole Forge. type Forge interface { - // RepositoryAuthorEligibility assesses a stable user ID independently of the - // authenticated bot's ProjectRole. Missing/deleted identities and incomplete - // evidence return AuthorUnknown with an error. - RepositoryAuthorEligibility(ctx context.Context, projectID, authorForgeUserID int64) (AuthorEligibility, error) - // VerifyToken confirms the PAT works and returns the bot identity. - VerifyToken(ctx context.Context) (BotIdentity, error) - // ListProjects returns every project the bot has at least Developer access - // to (membership=true). - ListProjects(ctx context.Context) ([]Project, error) - // GetProject freshly reads bounded metadata by positive numeric ID, without - // inventory enumeration or cached identity. It requires complete, coherent - // identity and WebURL; DefaultBranch may be empty. Any failure returns zero - // Project and invalidates the requested ID's cached slug. - GetProject(ctx context.Context, projectID int64) (Project, error) - // ListLabels returns all labels defined on a project. - ListLabels(ctx context.Context, projectID int64) ([]Label, error) - // EnsureLabels creates any of the given labels that do not already exist on - // the project (by name). Existing labels are left untouched. - EnsureLabels(ctx context.Context, projectID int64, labels []Label) error - // ListIssues returns issues matching opts. State defaults to StateAll (the - // zero value), which is what every pre-M6 caller relies on; each driver - // translates it into its own request vocabulary. opts.Limit optionally caps the - // number of issues returned — zero (the default) means no cap and the complete - // set is returned as ever, a positive value stops pagination once that many are - // collected. - ListIssues(ctx context.Context, projectID int64, opts ListIssuesOptions) ([]Issue, error) - // GetIssue returns one issue by its project-scoped IID. Its Description is - // populated (the run-create path snapshots it), unlike the cached list rows. - GetIssue(ctx context.Context, projectID, issueIID int64) (Issue, error) - // CreateIssue opens a new issue with the given title, description, and labels - // and returns it. uzi never fabricates a local-only card — the forge stays the - // source of truth and the board picks the issue up on the next sync. - CreateIssue(ctx context.Context, projectID int64, title, description string, labels []string) (Issue, error) - // UpdateIssueLabels adds and removes labels on an issue in one call. On - // GitLab this is atomic (a single add_labels/remove_labels update); - // single-column enforcement relies on that atomicity. - UpdateIssueLabels(ctx context.Context, projectID, issueIID int64, add, remove []string) error - // UpdateIssueDescription replaces an issue's description wholesale (PRD #72 M5). - // It is a read-modify-write from the CALLER's point of view — the caller reads - // via GetIssue, rewrites, and writes back — because no forge here offers a patch - // primitive. A driver may additionally need its own internal read to satisfy its - // forge's API shape; that is the driver's business, not the interface's. - UpdateIssueDescription(ctx context.Context, projectID, issueIID int64, description string) error - // SetIssueState closes or reopens an issue, forge-first. state must be - // StateOpened or StateClosed (the two live states); other values are a caller - // bug. Each driver translates the neutral state into its own mutate vocabulary - // (GitLab's state_event close/reopen, GitHub's state+state_reason, Forgejo's - // state), sending state alone so nothing else on the issue is clobbered. - SetIssueState(ctx context.Context, projectID, issueIID int64, state IssueState) error - // UserExists reports whether a user with the given username exists on the - // forge. It backs the best-effort verification of a user's self-declared - // human_username (PRD #19 M3): a false — or an error — downgrades a save to - // "saved with a warning", never a hard failure. A blank username is not a - // forge lookup; it returns (false, nil). - UserExists(ctx context.Context, username string) (bool, error) - // ListIssueLabelEvents returns the issue's resource label events (who added or - // removed which label, when), oldest first, paginated internally. Autopilot - // uses it to attribute the autopilot label to the human who added it (PRD #19 - // Decision 3). No caller until the autopilot-trigger milestone. - ListIssueLabelEvents(ctx context.Context, projectID, issueIID int64) ([]LabelEvent, error) - // ListIssueComments returns the issue's HUMAN comments, oldest first, - // paginated internally and bounded by the shared forge sanity ceilings - // (maxForgeItems/maxForgePages) — no per-call limit (PRD #381 D4; the size - // bound is an assembly concern, not the driver's). Each driver filters out - // forge system/timeline notes (D2) and normalizes ordering to oldest-first - // (D8). Bodies are untrusted free text. AuthorForgeUserID lets the caller drop - // uzi's own bot-authored comments (D1). No caller until PRD #381's plumbing. - ListIssueComments(ctx context.Context, projectID, issueIID int64) ([]IssueComment, error) - // CreateIssueNote posts a comment on an issue and returns it. Autopilot uses - // it to surface an outcome — no eligible user, a run failure, or the success MR - // link — back on the forge so a forge-only user is never left waiting (PRD #19 - // Decision 6). No caller until the autopilot trigger/execution milestones. - CreateIssueNote(ctx context.Context, projectID, issueIID int64, body string) (IssueNote, error) - // GetMergeRequest returns one merge request by its project-scoped IID. The - // MR-close watcher (PRD #24) polls this for cards parked in Human Review to - // detect an opened→closed (reviewer rejected the MR) edge. - GetMergeRequest(ctx context.Context, projectID, mrIID int64) (MergeRequest, error) - // ListMergeRequestComments returns an MR's HUMAN and third-party review-bot - // comments oldest-first (PRD #700), paginated internally and bounded by the - // shared forge sanity ceilings (maxForgeItems/maxForgePages). Each driver drops - // the forge's own system/timeline notes (D2) and normalizes ordering to - // oldest-first (D8), and populates the reply/resolve anchors, HeadSHA, and - // Path/Line wherever the SDK exposes them. It does NOT filter bot authors — the - // snapshot layer does that against the connection's own bot id, so a - // third-party review bot (CodeRabbit) stays readable. Bodies are untrusted free - // text. GitHub stitches three REST sources with a GraphQL reviewThreads read to - // supply both anchors (Resolved facts). Every comment carries its IDNamespace - // (CommentNS*): ids are ordered only within one namespace. - ListMergeRequestComments(ctx context.Context, projectID, mrIID int64) ([]MRComment, error) - // ReplyMergeRequestComment posts a reply in the thread keyed on replyID (an - // MRComment.ReplyID: the GitLab discussion id, the GitHub REST databaseId, or - // the Forgejo review-comment id). The MR-review-rework worker uses it to answer - // each finding ("done in " / "skipped because X"). No caller until PRD - // #700's plumbing. - ReplyMergeRequestComment(ctx context.Context, projectID, mrIID int64, replyID, body string) error - // ResolveMergeRequestThread resolves the review thread keyed on resolveID (an - // MRComment.ResolveID: the GitLab discussion id or the GitHub GraphQL thread - // node id). Returns ErrResolveUnsupported on Forgejo, whose forge has no - // resolvable-thread concept — the worker swallows that sentinel and keeps the - // reply (reply-only is the documented Forgejo contract). No caller until PRD - // #700's plumbing. - ResolveMergeRequestThread(ctx context.Context, projectID, mrIID int64, resolveID string) error - // TokenInfo returns introspection data for the PAT the client authenticates - // with: its scopes, whether it is active, and its expiry. GitLab: GET - // /personal_access_tokens/self. Returns ErrTokenIntrospectionUnsupported when - // the forge version lacks the endpoint (so the caller warns, not blocks). - TokenInfo(ctx context.Context) (TokenInfo, error) - // ProjectRole returns the bot's effective (direct or inherited) role on a - // project, and whether the bot is a member at all. member is false with a nil - // error when the bot has no effective membership (a 404 from the members/all - // lookup). GitLab: GET /projects/:id/members/all/:user_id, whose numeric - // access level the driver maps onto Role. - ProjectRole(ctx context.Context, projectID, forgeUserID int64) (role Role, member bool, err error) - // DefaultBranchProtection reports whether the given branch is protected, and - // whether the write role or the bot specifically may push to or merge into it. - // GitLab: GET /projects/:id/protected_branches/:name (a 404 means - // unprotected). botUserID is the bot's forge user id, used to flag a per-user - // grant. Every returned field is evaluated on every path — see - // BranchProtection. - DefaultBranchProtection(ctx context.Context, projectID int64, branch string, botUserID int64) (BranchProtection, error) - // LatestPipeline returns the newest branch pipeline for a ref, or - // ErrNoPipeline when the ref has none (no CI configured, or it never ran). - // GitLab: GET /projects/:id/pipelines?ref=&per_page=1 (default - // order_by=id desc, so the first row is newest). - LatestPipeline(ctx context.Context, projectID int64, ref string) (Pipeline, error) - // LatestMRPipeline returns the newest (max-by-id) pipeline attached to a merge - // request. This is what catches detached MR pipelines (refs/merge-requests/:iid/ - // head) and merged-results pipelines, which never appear under the source-branch - // ref — so run-branch status and fix verification key on the run's MR, not its - // branch ref. Returns ErrNoPipeline when the MR has no pipeline. GitLab: GET - // /projects/:id/merge_requests/:iid/pipelines — note this endpoint groups - // merge_request_event pipelines first (then id-desc within a group), NOT a plain - // id-desc, so the driver picks the max id rather than trusting the first row. - LatestMRPipeline(ctx context.Context, projectID, mrIID int64) (Pipeline, error) - // ListPipelineJobs returns the pipeline's jobs with status/stage/name, - // paginated internally. GitLab: GET /projects/:id/pipelines/:pipeline_id/jobs. - // Called only at fix-trigger time (to snapshot failed jobs), never on the poll - // tick. - ListPipelineJobs(ctx context.Context, projectID, pipelineID int64) ([]Job, error) - // ListPipelineJobAttempts returns the jobs of EVERY attempt of a run, not just - // the latest (#3225, the CI scout's history read): GitHub filter=all, GitLab - // include_retried=true. Timestamps are only what the forge reported: unlike - // ListPipelineJobs, GitLab's FinishedAt is never derived from StartedAt plus - // Duration. Same budget and pagination path as ListPipelineJobs. Returns - // ErrJobAttemptsUnsupported where complete history is unavailable (Forgejo). - ListPipelineJobAttempts(ctx context.Context, projectID, pipelineID int64) ([]Job, error) - // JobLogTail returns at most maxBytes from the END of a job's trace (a - // failure's cause concludes its log); maxBytes <= 0 returns the whole trace. - // GitLab: GET /projects/:id/jobs/:job_id/trace — the endpoint has no range/tail - // parameter, so this is a full download truncated client-side, capped by a - // fail-closed download ceiling; acceptable because it runs only at fix-trigger - // time, never on the poll tick. The returned tail passes through the driver's - // PAT redactor (the snapshot path applies a second, pattern-based scrub). NOTE: - // the returned tail is NOT itself a secret-safe log scrubber for arbitrary - // third-party tokens — see the snapshot scrubber in the ci-fix handler. - JobLogTail(ctx context.Context, projectID, jobID int64, maxBytes int) (string, error) - // ListMergeRequestRefs returns the project's open merge requests as cheap - // list-row refs (PRD #1255 M2b) in ONE list call, newest activity first, - // paginated internally — just IID/HeadSHA/UpdatedAt per row, with NO per-PR - // enrichment (that is GetMergeRequestSummary's job). opts.State defaults to - // "opened" (the only state the pulls screen shows); opts.Limit optionally caps - // the rows. Pagination is bounded by the driver's caps; errors are PAT-redacted, - // and on GitHub a rate-limit surfaces as *RateLimitError (GitLab/Forgejo return a - // plain error; see RateLimitError). - ListMergeRequestRefs(ctx context.Context, projectID int64, opts ListMergeRequestsOptions) ([]MergeRequestRef, error) - // GetMergeRequestSummary returns ONE merge/pull request's full neutral summary by - // iid (PRD #1255 M2b): IID, Title, Author, branches, HeadSHA, Draft, the Conflicts - // tri-state, ReviewDecision, State, WebURL and (where the forge exposes them) - // Additions/Deletions/Commits and timestamps. Each driver derives ReviewDecision - // (no forge exposes it on the row) and reads whatever per-MR detail the summary - // needs (GitHub: PullRequests.Get + ListReviews; GitLab: GetMergeRequest + - // approvals; Forgejo: GetPullRequest + the raw reviews fold). Returns - // ErrMergeRequestNotFound (errors.Is-matchable) when the forge 404s the iid. Every - // string field is untrusted forge text; errors are PAT-redacted, and on GitHub a - // rate-limit surfaces as *RateLimitError (GitLab/Forgejo return a plain error; see - // RateLimitError). - GetMergeRequestSummary(ctx context.Context, projectID, iid int64) (MergeRequestSummary, error) - // ListChecks returns every status/check for a commit sha (PRD #1255 D5), merging - // the forge's check and commit-status surfaces and DEDUPLICATING by Name so a bot - // reporting through either appears once. GitHub: ListCheckRunsForRef + - // GetCombinedStatus. GitLab: the sha's newest pipeline's jobs + GetCommitStatuses. - // Forgejo: the Actions jobs of the runs on that sha + ListStatuses. Paginated - // internally; errors are PAT-redacted. - ListChecks(ctx context.Context, projectID int64, sha string) ([]Check, error) - // ListWorkflowRuns returns the project's CI runs newest-first for the `ci` screen - // (PRD #1255 D5): GitHub workflow runs, GitLab pipelines, or Forgejo Actions runs. - // One page (opts.Limit caps it, bounded by the driver's max page size); - // Branch/Event/Status are optional server-side filters. Where the forge version - // lacks the endpoint the driver returns an error wrapping ErrForgeVersionUnsupported - // (the honest degrade path). Errors are PAT-redacted; on GitHub a rate-limit - // surfaces as *RateLimitError (GitLab/Forgejo return a plain error; see RateLimitError). - ListWorkflowRuns(ctx context.Context, projectID int64, opts ListWorkflowRunsOptions) ([]WorkflowRun, error) - // GetWorkflowRun returns one CI run's header (the neutral WorkflowRun) by its - // forge-native run/pipeline id, for the `ci` drill-in. ErrForgeVersionUnsupported - // on a forge whose Actions/pipelines API is absent (mirrors ListWorkflowRuns). - GetWorkflowRun(ctx context.Context, projectID, runID int64) (WorkflowRun, error) - - // ListRunArtifacts returns bounded metadata for this exact run, without URL authority. - ListRunArtifacts(ctx context.Context, projectID, runID int64) ([]Artifact, error) - // DownloadArtifact fetches an ID-addressed archive, enforcing maxBytes on actual bytes. - DownloadArtifact(ctx context.Context, projectID, artifactID, maxBytes int64) ([]byte, error) - // ListMergeRequestReviews returns an open MR's per-reviewer reviews for the - // forge-view detail route (PRD #1255 D6), oldest-first — the same review stream the - // driver folds into ReviewDecision, returned verbatim (State is the raw forge value) - // so the PR view can render each reviewer's latest state. GitHub reads - // PullRequests.ListReviews; Forgejo reads /pulls/{iid}/reviews; GitLab returns an - // empty slice (no per-reviewer review stream on the free tier — its decision is the - // BlockingDiscussionsResolved / approvals approximation, not a review list). Every - // string field is untrusted forge text; errors are PAT-redacted. - ListMergeRequestReviews(ctx context.Context, projectID, mrIID int64) ([]Review, error) - // ProjectCIConfigPath returns the project's configured CI config path (GitLab - // ci_config_path); empty string means the driver's default (.gitlab-ci.yml). It - // carries no secret material — a project's ci_config_path is a repo-relative - // glob, not a token — and any driver error is PAT-redacted like every other - // method. Used by the PRD #71 auto-fix guard to know which paths a fix may touch. - ProjectCIConfigPath(ctx context.Context, projectID int64) (string, error) - // BranchHead returns the 40-hex commit id the named branch currently points at - // (issue #1582 M1). GitHub: GET /repos/{o}/{r}/branches/{branch} (commit.sha); - // GitLab: GET /projects/:id/repository/branches/:branch (commit.id); Forgejo: GET - // /repos/{o}/{r}/branches/{branch} (commit.id). Every driver sends it through a - // redirect-refusing client, so ANY 3xx (a GitHub rename 301 included) is an error, - // never ErrRefNotFound and never another host's answer; GitHub and Forgejo also - // require the response to name the requested branch. A 404 returns ErrRefNotFound; a - // head that is not a 40-char lowercase hex id is an error. Errors are PAT-redacted. - BranchHead(ctx context.Context, projectID int64, branch string) (string, error) - // RefHead returns the 40-hex commit id a FULL ref name (for example - // refs/uzi-checkpoints/agent/issue-7) currently points at (issue #1751 M2), the head a - // live predecessor settle proves against when the successor published to its forge - // checkpoint ref rather than a branch. The ref must start with "refs/" and be a - // well-formed git ref name (no "..", "//", "@{", control character, space or any of - // ~ ^ : ? * [ \); anything else is an error with no request. GitHub: GET - // /repos/{o}/{r}/git/ref/{ref without "refs/"} (object.sha of a commit object, the - // echoed `ref` must equal the requested ref); GitLab: GET - // /projects/:id/repository/commits/{url-escaped ref} (id); Forgejo: GET - // /repos/{o}/{r}/git/refs/{ref without "refs/"} (the entry whose `ref` equals the - // requested ref exactly, object.sha of a commit object). Every driver refuses redirects - // like BranchHead. A 404 returns ErrRefNotFound; a head that is not a 40-char lowercase - // hex id, or an answer naming another ref, is an error. Errors are PAT-redacted. - RefHead(ctx context.Context, projectID int64, ref string) (string, error) - // CompareAncestry answers "is candidate an ancestor of, or equal to, head" using the - // forge's own compare API (issue #1582 M1). Both arguments must be 40-char - // lowercase hex commit ids (never ref names); anything else is AncestryUnknown with - // an error. head == candidate is AncestryAncestor WITHOUT a request. Every error, - // rate limit (a 429, or GitHub's rate-limit 403), oversize body, or unrecognized - // response is AncestryUnknown; only an explicit, recognized positive answer is - // AncestryAncestor. Every driver's compare requests refuse redirects (a 3xx is - // AncestryUnknown with an error). Errors are PAT-redacted. - CompareAncestry(ctx context.Context, projectID int64, head, candidate string) (Ancestry, error) + IssueTracker + CodeHost } // New constructs a driver for the given forge type. baseURL must already be diff --git a/api/internal/forge/forgejo.go b/api/internal/forge/forgejo.go index 2a7bde879f..e7940cd044 100644 --- a/api/internal/forge/forgejo.go +++ b/api/internal/forge/forgejo.go @@ -65,6 +65,12 @@ type forgejo struct { slugs map[int64]repoSlug } +// The driver implements both halves of the Forge contract (PRD #4173). +var ( + _ IssueTracker = (*forgejo)(nil) + _ CodeHost = (*forgejo)(nil) +) + // repoSlug is a repo's owner/name pair, the addressing the gitea SDK needs. type repoSlug struct { owner string diff --git a/api/internal/forge/forgetest/basefake.go b/api/internal/forge/forgetest/basefake.go index 0b73cbd55a..2ca82d7684 100644 --- a/api/internal/forge/forgetest/basefake.go +++ b/api/internal/forge/forgetest/basefake.go @@ -50,6 +50,8 @@ type BaseFake struct{} // is NOT a call, so it creates no reachability for deadcode analysis — the // table-driven test in basefake_test.go is what invokes every method. var _ forge.Forge = (*BaseFake)(nil) +var _ forge.IssueTracker = (*BaseFake)(nil) +var _ forge.CodeHost = (*BaseFake)(nil) // RepositoryAuthorEligibility implements forge.Forge. func (*BaseFake) RepositoryAuthorEligibility(context.Context, int64, int64) (forge.AuthorEligibility, error) { diff --git a/api/internal/forge/github.go b/api/internal/forge/github.go index de0a22959b..6e3f8619f1 100644 --- a/api/internal/forge/github.go +++ b/api/internal/forge/github.go @@ -66,6 +66,12 @@ type github struct { slugs map[int64]repoSlug } +// The driver implements both halves of the Forge contract (PRD #4173). +var ( + _ IssueTracker = (*github)(nil) + _ CodeHost = (*github)(nil) +) + // newGitHub builds a GitHub driver against baseURL using token, with a bounded // per-call HTTP timeout. baseURL is assumed allowlist-checked by the caller (the // SSRF guard lives in config). No network call happens here. diff --git a/api/internal/forge/gitlab.go b/api/internal/forge/gitlab.go index 9d508d67d3..39dbd98048 100644 --- a/api/internal/forge/gitlab.go +++ b/api/internal/forge/gitlab.go @@ -56,6 +56,12 @@ type gitLab struct { logClient *http.Client } +// The driver implements both halves of the Forge contract (PRD #4173). +var ( + _ IssueTracker = (*gitLab)(nil) + _ CodeHost = (*gitLab)(nil) +) + // newGitLab builds a GitLab driver against baseURL using token, with a bounded // per-call HTTP timeout. The official client's retryable transport already // honors 429 + Retry-After and backs off on 5xx; we route it through our diff --git a/api/internal/forge/issuetracker.go b/api/internal/forge/issuetracker.go new file mode 100644 index 0000000000..9a74fc4c5b --- /dev/null +++ b/api/internal/forge/issuetracker.go @@ -0,0 +1,173 @@ +package forge + +import ( + "context" + "time" +) + +// Label is a forge label. Color is optional (used when creating column labels). +type Label struct { + Name string + Color string +} + +// Issue is a forge issue as uzi caches it. Description is carried so the caller +// can compute the PRD-link check at fetch time; it is never persisted. +type Issue struct { + // IsPullRequest distinguishes PRs returned by shared issue endpoints; internal only. + IsPullRequest bool `json:"-"` + IID int64 + Title string + State string // "opened" | "closed" + Labels []string + Description string + Author string // username, may be empty + AuthorForgeUserID int64 // stable author identity; zero means unresolved + WebURL string + UpdatedAt time.Time + Assignees []int64 // forge user ids of the issue's assignees (may be empty, never nil after mapping) +} + +// LabelEvent is one resource label event on an issue: a record that a user added +// or removed a single label at a point in time. Autopilot reads these to attribute +// the autopilot label to the human who added it (PRD #19 Decision 3). Username is +// the actor; it may be empty for a system-generated event. +type LabelEvent struct { + ID int64 + Action string // "add" | "remove" + LabelName string + Username string + CreatedAt time.Time +} + +// IssueNote is a comment created on an issue, returned by CreateIssueNote. +type IssueNote struct { + ID int64 + Body string +} + +// IssueComment is one human-authored comment on an issue, returned by +// ListIssueComments oldest-first. It deliberately carries NO System/type field: +// the GitLab driver drops system notes (D2), and the Forgejo/GitHub drivers read +// human-only comment endpoints (their timeline/event notes live behind separate +// endpoints uzi never calls), so the neutral type only ever represents a human +// comment. Bodies are attacker-influenceable free text (same untrusted class as +// Issue.Description). +type IssueComment struct { + AuthorForgeUserID int64 // stable forge user id of the comment author + AuthorUsername string // author login, may be empty + Body string // comment body (untrusted) + CreatedAt time.Time +} + +// IssueState is the neutral issue-state vocabulary, matching the values the +// drivers already normalize their RESPONSES to and the values the issue cache +// stores in issues.state. +// +// The neutral spelling is GitLab's, not Forgejo's, for the historical reason that +// GitLab was the only driver when the cache's state='opened' filter was written. +// Forgejo says "open"; forgejoIssueState maps that INBOUND, and +// forgejoIssueStateParam maps it OUTBOUND — that function's comment is where the +// asymmetry is explained, and it is the one that matters, since an untranslated +// neutral value is not rejected on the Forgejo lane, it is silently ignored. +// +// (This used to point at StateAll's note for the outbound explanation. StateAll's +// note says nothing about it — it documents the zero value.) +type IssueState string + +const ( + // StateAll is the default and reproduces pre-M6 behaviour exactly: every + // caller that leaves ListIssuesOptions.State zero gets "all". + StateAll IssueState = "" + // StateOpened restricts to open issues. + StateOpened IssueState = "opened" + // StateClosed restricts to closed issues. + StateClosed IssueState = "closed" +) + +// ListIssuesOptions filters ListIssues. Labels is ANDed; an empty UpdatedAfter +// means "no lower bound" (full fetch). +type ListIssuesOptions struct { + // Labels each issue must carry (AND semantics). Typically []string{"PRD"}. + Labels []string + // UpdatedAfter, when non-nil, restricts to issues updated at/after it. The + // bound is inclusive at second granularity on GitLab; callers dedupe the + // boundary by upsert. + UpdatedAfter *time.Time + // State restricts by issue state. The ZERO VALUE IS StateAll, which is what + // every pre-M6 caller relied on: the Closed column and de-label/close eviction + // both need to see closed issues, and the drivers used to hardcode it. + // + // Added by PRD #102 Decision 10 for M6's additive non-PRD fetch, which wants + // open-only — without it that fetch would pull the entire closed history on + // every reconcile purely to discard it. + State IssueState + // Limit optionally caps the number of issues returned. Limit == 0 means NO CAP: + // the driver paginates internally and returns the COMPLETE set, preserving the + // interface's "return complete result sets" contract for every current caller, + // all of which leave this zero. A positive Limit tells the driver to stop once + // that many issues have been collected (truncating a page as needed), so the + // worker forge read (PRD #158) can bound a list without the driver having to walk + // the whole project. It is a soft ceiling on count only — it does not change + // filtering, ordering, or the shape of any returned issue. + Limit int +} + +// IssueTracker is the issue-side half of the driver contract: issues, labels, +// comments and issue state. It is split out of Forge (PRD #4173) so a later phase +// can let a non-forge issue tracker implement it without the code-host methods. +type IssueTracker interface { + // ListLabels returns all labels defined on a project. + ListLabels(ctx context.Context, projectID int64) ([]Label, error) + // EnsureLabels creates any of the given labels that do not already exist on + // the project (by name). Existing labels are left untouched. + EnsureLabels(ctx context.Context, projectID int64, labels []Label) error + // ListIssues returns issues matching opts. State defaults to StateAll (the + // zero value), which is what every pre-M6 caller relies on; each driver + // translates it into its own request vocabulary. opts.Limit optionally caps the + // number of issues returned — zero (the default) means no cap and the complete + // set is returned as ever, a positive value stops pagination once that many are + // collected. + ListIssues(ctx context.Context, projectID int64, opts ListIssuesOptions) ([]Issue, error) + // GetIssue returns one issue by its project-scoped IID. Its Description is + // populated (the run-create path snapshots it), unlike the cached list rows. + GetIssue(ctx context.Context, projectID, issueIID int64) (Issue, error) + // CreateIssue opens a new issue with the given title, description, and labels + // and returns it. uzi never fabricates a local-only card — the forge stays the + // source of truth and the board picks the issue up on the next sync. + CreateIssue(ctx context.Context, projectID int64, title, description string, labels []string) (Issue, error) + // UpdateIssueLabels adds and removes labels on an issue in one call. On + // GitLab this is atomic (a single add_labels/remove_labels update); + // single-column enforcement relies on that atomicity. + UpdateIssueLabels(ctx context.Context, projectID, issueIID int64, add, remove []string) error + // UpdateIssueDescription replaces an issue's description wholesale (PRD #72 M5). + // It is a read-modify-write from the CALLER's point of view — the caller reads + // via GetIssue, rewrites, and writes back — because no forge here offers a patch + // primitive. A driver may additionally need its own internal read to satisfy its + // forge's API shape; that is the driver's business, not the interface's. + UpdateIssueDescription(ctx context.Context, projectID, issueIID int64, description string) error + // SetIssueState closes or reopens an issue, forge-first. state must be + // StateOpened or StateClosed (the two live states); other values are a caller + // bug. Each driver translates the neutral state into its own mutate vocabulary + // (GitLab's state_event close/reopen, GitHub's state+state_reason, Forgejo's + // state), sending state alone so nothing else on the issue is clobbered. + SetIssueState(ctx context.Context, projectID, issueIID int64, state IssueState) error + // ListIssueLabelEvents returns the issue's resource label events (who added or + // removed which label, when), oldest first, paginated internally. Autopilot + // uses it to attribute the autopilot label to the human who added it (PRD #19 + // Decision 3). No caller until the autopilot-trigger milestone. + ListIssueLabelEvents(ctx context.Context, projectID, issueIID int64) ([]LabelEvent, error) + // ListIssueComments returns the issue's HUMAN comments, oldest first, + // paginated internally and bounded by the shared forge sanity ceilings + // (maxForgeItems/maxForgePages) — no per-call limit (PRD #381 D4; the size + // bound is an assembly concern, not the driver's). Each driver filters out + // forge system/timeline notes (D2) and normalizes ordering to oldest-first + // (D8). Bodies are untrusted free text. AuthorForgeUserID lets the caller drop + // uzi's own bot-authored comments (D1). No caller until PRD #381's plumbing. + ListIssueComments(ctx context.Context, projectID, issueIID int64) ([]IssueComment, error) + // CreateIssueNote posts a comment on an issue and returns it. Autopilot uses + // it to surface an outcome — no eligible user, a run failure, or the success MR + // link — back on the forge so a forge-only user is never left waiting (PRD #19 + // Decision 6). No caller until the autopilot trigger/execution milestones. + CreateIssueNote(ctx context.Context, projectID, issueIID int64, body string) (IssueNote, error) +} diff --git a/changelog.d/b6862b0d-2519-4896-8a76-d25fa506fb68.md b/changelog.d/b6862b0d-2519-4896-8a76-d25fa506fb68.md new file mode 100644 index 0000000000..fc94a19179 --- /dev/null +++ b/changelog.d/b6862b0d-2519-4896-8a76-d25fa506fb68.md @@ -0,0 +1,4 @@ +### Changed + +- **The forge interface splits into an issue tracker and a code host (#4173).** + The forge driver contract is now the union of an issue-tracker half (issues, labels, comments and issue state) and a code-host half (everything else); behaviour is unchanged for every forge. diff --git a/prds/4173-external-issue-trackers.md b/prds/4173-external-issue-trackers.md new file mode 100644 index 0000000000..6841372b62 --- /dev/null +++ b/prds/4173-external-issue-trackers.md @@ -0,0 +1,221 @@ +# PRD #4173: External issue trackers: `IssueTracker` and `CodeHost` interfaces, with YouTrack as the first external issue tracker + +**Issue:** #4173 + +**Priority:** Medium + +**Status:** In progress, r4 2026-11-11. M1 delivered. r3 followed the maintainer's scope review: one real issue tracker instead of abstract identity work, and the comment, command and withdrawal behaviours leave this PRD. r4 follows the maintainer's review of the M1 PR: the YouTrack id, tag and author decisions are made before the driver, and the issue source of a repo no longer changes. + +**Delivery:** one PR per milestone, each merged before the next starts. No implementation or validation step touches `.github/workflows/**`. + +**Builds on:** + +- **The forge layer** (`api/internal/forge`): three drivers (GitLab, `adr/0065-forgejo-driver.md`, `adr/0238-github-driver.md`) and the shared fake `forgetest.BaseFake`. +- **The board column automation** (PRD #12, `api/internal/runlifecycle/`): moves a card by swapping labels when its run changes status. +- **Issue comment snapshots** (PRD #381): the issue's comments are saved and given to the agent when a run starts. + +## Problem + +uzi takes work only from forge issues. Teams that plan in an issue tracker (YouTrack, Jira, Asana) cannot use it without copying every task into the forge. An issue tracker cannot be plugged in today, because the forge layer does two jobs through one interface: + +- **Code:** repos, merge requests, pipelines, branch protection, token and role checks. +- **Work items:** issues, labels, comments, issue state. + +An issue tracker does only the second job. Adding one as a fourth forge would force it to pretend it has repos and merge requests, so the issue job has to come out of the forge first. + +## Goal + +1. `Forge` splits into `IssueTracker` (issues, labels, comments, state) and `CodeHost` (everything else), and stays as their union. Done in M1. +2. Every caller receives only the half it needs. M2. +3. A repo can take its issues from a YouTrack project while its code, merge requests and CI stay on its forge. +4. Feature parity: every issue-side feature that works today on GitLab, GitHub and Forgejo works on YouTrack, or is listed below as not offered on an issue tracker yet. +5. Backwards compatible: a repo with no issue tracker link behaves exactly as today. No backfill, no setting to change. + +## Security invariants + +These hold in every milestone. A change that weakens one is out of scope for this PRD. + +- **S1. An issue tracker never gains code access.** An issue tracker driver implements `IssueTracker` only. Passing it where a `CodeHost` is required is a compile error. +- **S2. Code-host checks stay where they are.** The privilege check, the protected-branch check and the worker-held forge credential always act on the repo's forge connection, and they fail closed. Linking an issue tracker changes none of them. +- **S3. Issue tracker credentials follow the forge rules.** The token is sealed in `secretbox` with no reveal endpoint. The base URL is https only and must be on an allowlist that is separate from `FORGE_ALLOWED_BASE_URLS` and empty by default. Redirects are not followed. Every error passes the redactor. +- **S4. Issue tracker text is untrusted input.** Titles, descriptions, comments, tags and state names get the same bounds and fencing as forge issue text. +- **S5. The worker never holds an issue tracker token.** It reads issues through the existing worker API routes. + +## Out of scope + +- **The living status comment, retried comment writes, automatic withdrawal, comments that reach the agent and `/uzi` commands.** They change behaviour on all three forges and add trust boundaries, independently of issue trackers. They move to their own PRD(s). The design gaps found for them so far, to be carried into those PRDs: + - *Withdrawal.* `FullSync` keeps every open issue cached regardless of label, so de-labeling an open issue does not evict it and nothing would fire. Eligibility is "uzi label OR assigned to the uzi bot", so removing the label must not cancel a still-eligible run. Cancel versus hold needs a reason, and a rule for the branch and unfinished work. + - *Comments and commands.* Each comment needs an exact binding to the gate revision or question it answers, plus replay and ordering rules (`SubmitInput` already has `ExpectedGateRevision`; Slack already prevents a late answer to Q1 from answering Q2). `/uzi approve` cannot satisfy a semantic approval gate (`admitApproval` in `workersvc/approval_guard.go`), so it refuses there and points to the UI. Related: #1563 (untrusted issue text into prompts), #1420 (a follow-up shown delivered before acknowledgment). + - *Outbox.* A per-run outbox cannot cover autopilot's refusal comments, which are written when no run exists. "Lands once" also assumes the marker can be read back before a retry. +- **Jira and Asana.** One PRD each, after YouTrack. +- **How the worker clones, pushes and opens the merge request.** The code host is unchanged. + +## Current state (anchors from r1, verified on upstream `main` 6a76d09f7; line numbers drift, cite by symbol) + +M2's PR refreshes these anchors and the count of issue-side uses against `main` as it is then. + +- **Where the issue side is used today.** Every place in uzi's code, tests excluded, that calls one of the eleven issue-side methods through the `Forge` interface (47 in all; calls a driver makes inside itself are not counted), listed from the type-checked code (`golang.org/x/tools/go/packages`) and cross-checked with `gopls call_hierarchy`: + - *Sync:* `forgesvc` `FullSync`, `IncrementalSync` and `reconcileFindingGroupMarkers` (`ListIssues`, `api/internal/forgesvc/service.go:787`, `:795`, `:816`, `:911`, `:915`, `:926`, `:448`). + - *Board:* `AutoMove` and `SetIssueLabel` (`EnsureLabels`, `UpdateIssueLabels`, `forgesvc/service.go:534-542`, `:600-607`); column setup and label checks in the handlers (`EnsureLabels`, `ListLabels`, `api/internal/handler/board.go:366`, `:432`, `:760`, `handler/labels.go:36`, `:120`); close and reopen (`SetIssueState`, `forgesvc/close_reopen.go:29`, `:62`). + - *Run creation:* the issue's text and comments (`GetIssue`, `ListIssueComments`, `api/internal/issueinput/projection.go:158`, `:169`), reached from `workersvc` `CreateRun`, the scheduler (`api/internal/schedsvc/scheduler.go:594`, `:806`), autopilot (`api/internal/poller/autopilot.go:209`) and the composite flow (`api/internal/workersvc/composite.go:212`); a task continuing an issue (`GetIssue` through the helper `forge.GetIssueBounded`, `forge/response_limit.go:114`, called from `workersvc/task.go:100`); semantic drift (`GetIssue`, `workersvc/semantic_drift.go:75`). + - *The agent:* the worker reads issues through the API, `GET /api/worker/runs/{id}/forge/issues`, `…/issues/{iid}` and `…/issues/{iid}/label-events` (`ListIssues`, `GetIssue`, `ListIssueComments`, `ListIssueLabelEvents`, `handler/worker_forge.go:138`, `:183`, `:265`, `:310`). + - *The issue page:* `GetIssueDetail` (`GetIssue`, `handler/issues.go:216`). + - *Issues uzi creates:* the board's new issue (`handler/issues.go:65`), findings and finding groups, which also ensure their labels first (`EnsureLabels`, `handler/findings_file.go:163`, `:169`, `handler/findings_group_file.go:203`, `:227`), the judge's admin issue (`handler/judge_admin_issue.go:269`), review issues (`handler/review_issue_file.go:159`), the self-improve tracking issue (`schedsvc/self_improve.go:269`) and proposals (`workersvc/composite.go:150`, `workersvc/proposal_filing.go:175`), all through `CreateIssue`; the scheduler's proposal digest and self-improve lookup read issues (`ListIssues`, `schedsvc/scheduler.go:1426`, `schedsvc/self_improve.go:252`). + - *Issue description:* the PRD link patch (`GetIssue`, `UpdateIssueDescription`, `forgesvc/prd_link_patch.go:159`, `:180`). + - *Autopilot:* label events (`ListIssueLabelEvents`, `poller/autopilot.go:140`). + - *Comments out:* see below. +- **The privilege check is code-host only.** `VerifyToken`, `ProjectRole`, `DefaultBranchProtection` (`api/internal/privcheck/checker.go:77`, `:139`, `:147`; `privcheck/service.go:256-283` at claim), fail-closed. +- **Mixed callers.** Comment intake reads issue comments (`ListIssueComments`) and checks each author against the repository (`RepositoryAuthorEligibility`, `api/internal/issueinput/assessment.go:86`); merge request watchers read the MR from the code host (`forgesvc/mr_watch.go:60`) and move the card on the issue side (`AutoMove`, `mr_watch.go:227`). +- **Issue identity is a number.** `issues.forge_issue_iid bigint` (`api/internal/store/migrations/00002_forge.sql:47-50`), `runs.issue_iid`, and one active run per issue on `(repo_id, coalesce(issue_iid, continued_issue_iid))` for issue runs and tasks continuing an issue (`00351_task_continued_issue.sql:11-14`). +- **Board columns are labels.** `board_columns.label_name` (`00002_forge.sql:39`); moving a card swaps labels through `UpdateIssueLabels` (`api/internal/forgesvc/service.go:512-542`), forge-first, with the `move_pending_since` marker (`api/internal/runlifecycle/lifecycle.go:1-17`). +- **Autopilot.** Maps the label adder to a uzi user through `forge_connections.human_username` (`00037_autopilot_mapping.sql:6-20`, `poller/autopilot.go:194`). +- **Constraints a text key meets.** `issues.forge_issue_iid` is `bigint NOT NULL` with `UNIQUE (repo_id, forge_issue_iid)` (`00002_forge.sql:50`, `:59`), and `runs_kind_shape` requires `issue_iid IS NOT NULL` for `kind = 'issue'` (`00314_cross_check_claude_checker.sql`). An added `issue_key` column alone does not make an issue tracker issue storable. +- **The issue author is checked the same way.** Run creation assesses `Issue.AuthorForgeUserID` through `RepositoryAuthorEligibility` (`Assessment.Author`, `api/internal/issueinput/assessment.go`). An author that cannot be assessed gives the reason `permission_unknown`, and a scheduled start with that reason is not auto-approved (`createRun`, `api/internal/workersvc/service.go`). +- **The forge allowlist is deployment configuration.** `FORGE_ALLOWED_BASE_URLS` is rendered from the Helm value `forge.allowedBaseURLs` (`deploy/chart/values.yaml`, `deploy/chart/templates/api-configmap.yaml`) and passed through `docker-compose.yml`. +- **Disabling a repo drains nothing.** It flips `repos.enabled`; a sync or a run already in flight finishes. +- **Comment authors are checked against the repository.** Comment intake keeps a comment only when its author has repository access (`RepositoryAuthorEligibility`, a `CodeHost` method taking a forge user ID). An issue tracker comment's author is an issue tracker account, which that check cannot evaluate. + +## Decisions + +- **D1. The split.** `IssueTracker` is the eleven issue-side methods. `CodeHost` is the other 29. `RepositoryAuthorEligibility` and `UserExists` stay on `CodeHost`: they ask the forge about its accounts and permissions. +- **D2. `Forge` is the embedded union of the two.** The three forge drivers and `forgetest.BaseFake` implement `Forge`, so they satisfy both halves. +- **D3. Narrowest interface per caller.** A caller that needs both halves takes two parameters (`ch forge.CodeHost, tr forge.IssueTracker`), never the union. +- **D4. What enforces the split.** The interface declarations do: a method belongs to the half that declares it, and passing an `IssueTracker` where a `CodeHost` is required does not compile. The per-driver `var _ IssueTracker` / `var _ CodeHost` lines are readable markers only; implementing `Forge` already implies them. No reflection test in M1 or M2. +- **D5. A repo resolves its two sides.** `IssueTrackerForRepo` and `CodeHostForRepo` (`forgesvc`). Through M2 both return the repo's forge driver. From M4, `IssueTrackerForRepo` returns the linked issue tracker's driver. `CodeHostForRepo` always returns the forge driver (S2). +- **D6. Issue tracker drivers live in their own package**, `api/internal/issuetracker`, not in `forge`. A driver is `forge.IssueTracker` plus what a connection needs: verify the token, list projects, list tags and, for a named project, list its states. A new issue tracker is one kind constant, one registry entry, one driver and its fake. +- **D7. Every driver ships with a fake and contract tests, and the contract suite runs against the real drivers.** One contract suite covers every `IssueTracker` implementation. Each one is built by its production constructor and talks HTTP to a fake server: the YouTrack driver to an in-process HTTPS fake, the three forge drivers to `e2e/forge-fake`, as `api/internal/forge/fake_contract_test.go` already does for forge operations. An in-memory fake in the style of `forgetest.BaseFake` (loud `ErrNotStubbed` defaults) also ships, for the tests of callers; passing the suite on it is not evidence of parity. +- **D8. Issue tracker connections are separate from forge connections.** Per user, kind and base URL, with the rules in S3. A repo links to one connection and one issue tracker project. NULL means the forge is the issue source. +- **D9. Moving a card is an `IssueTracker` method.** `MoveToColumn`. The forge drivers implement it with one shared helper that does today's label swap in one `UpdateIssueLabels` call. YouTrack maps a column to a value of the project's State field. The column-to-state mapping is a side table, so `board_columns.label_name` keeps its meaning for the code that reads it. +- **D10. YouTrack ids are adapted inside the driver; the neutral types do not change.** `IssueTracker` takes `projectID int64` and `issueIID int64` and returns `IssueNote.ID int64`, while YouTrack project, comment and account ids are strings. Changing the neutral types would cost the three forge drivers and every caller and give the forges nothing, so the driver adapts, and M3 implements exactly this: + - *Project.* A YouTrack driver is built for one project. It holds that project's string id and short name, and the one `projectID int64` value it answers to: the repo's forge project id, which is the number every caller already passes for that repo. An `IssueTracker` method called with any other `projectID` is refused before any request is made, so a driver resolved for one repo cannot serve a call meant for another. The `IssueTracker` methods of a driver built without a project return an error. + - *Connection operations.* Verify the token, list projects and list tags need no project, so a connection can be set up before one is chosen. Listing states is project-scoped, because states belong to a project: it takes the project it reads, picked from the list of projects, and cannot be called without one. + - *Issue.* `issueIID` is YouTrack's `numberInProject`. It satisfies the `NOT NULL`, uniqueness and `runs_kind_shape` constraints above with no change to them. + - *Comment.* The driver does not derive a number from a YouTrack comment id. YouTrack documents these ids as strings and guarantees no grammar for them. `CreateIssueNote` returns `IssueNote.ID` 0 on YouTrack, and no caller reads that field today: all five call sites discard the returned note. + - *Accounts.* The numeric author id of an issue or comment is 0, which the existing code already reads as unresolved; the login is carried as text. D11 says what an unresolved author may do. + - *Every read and write checks where it landed.* The issue reached must be in the driver's project and carry the requested number; otherwise the driver returns issue-not-found. An issue moved to another YouTrack project gets a new number there, so in the old project it is not found, even where YouTrack still resolves the old readable id. + - *A live run whose issue was moved away.* Its later issue writes (card move, comment, description patch) are skipped and logged. The code-host side of the run (branch, merge request, CI, settle) continues. The next sync removes the issue from the repo's cache. + - *Storage, decided in M4.* A nullable `issue_key text` carries the readable id (`ABC-12`) for display and references, NULL for forge issues. M4 records the column and its constraints in the Decision Log before code. +- **D11. An issue tracker author is unproven until mapped, for comments and for the issue itself.** The forge's `RepositoryAuthorEligibility` is never asked about an issue tracker account. An issue tracker author counts as eligible only when the account maps to a uzi user who owns or can access the repo. Until then: + - *Comments* are withheld from the run, exactly like a comment whose author lookup failed. + - *The issue author* is assessed as `permission_unknown`. A manual start proceeds, because a uzi user chose that issue. A scheduled start is created but never auto-approved: its plan waits for a person. M4 tests the manual and the scheduled start separately. + + How an issue tracker account is proven to belong to a uzi user is designed in M4 and recorded in the Decision Log before code; the fail-closed default holds until then. +- **D12. Autopilot is not offered on a repo linked to an issue tracker in this PRD.** It maps the label adder to a uzi user through the forge identity, which an issue tracker account does not have. It stays unchanged on forge-backed repos. +- **D13. A repo's issue source is chosen before the repo is first enabled, and does not change.** Linking is accepted only for a repo that has never been enabled. Such a repo has had no sync and no run, so no stored reference and no operation in flight can meet the change. M4 verifies in code that neither can start on a repo that is not enabled, and refuses the link when anything else already stores an issue number for the repo, such as a schedule pinned to an issue. Relinking and unlinking are out of scope for this PRD, and so is linking a repo that is already in use; moving a repo in use to an issue tracker is left for a later PRD. A repo with no link is unaffected (Goal 5). + - *"Never enabled" is a stored fact, not a reading of `repos.enabled`,* because disabling drains nothing. M4 adds a marker set in the transaction that first enables a repo and never cleared. Its migration marks every repo that exists at that moment as previously enabled, because current rows cannot prove otherwise: a repo that was used, then disabled, can be left with no cached issue, run or schedule. Only a repo created after the migration can be linked. + - *The link and the first enable take the same repo row lock,* so one of them sees the other: either the link lands first and the repo starts on YouTrack, or the enable lands first and the link is refused. + - *An issue tracker connection with a linked repo cannot be deleted.* + - A forge issue 12 and a YouTrack issue 12 can therefore never belong to the same repo. +- **D14. YouTrack tags are not project labels, and the eligibility tag is chosen by id.** A YouTrack tag belongs to the user who created it, is private unless shared, and can share its name with another user's tag. So: + - *Choice.* When a repo is linked (M4), the admin picks the eligibility tag from the tags the connection's token can see. uzi stores the tag's id with the link, and its name for display only. + - *Who must be able to use the tag.* YouTrack separates who can see a tag from who can apply it (`readSharingSettings` and `tagSharingSettings` on the tag). Two parties need the right to apply it: the connection's account, because uzi puts the tag on issues it creates, and the people who start work by tagging an issue. uzi cannot know who those people are, so the admin decides it in YouTrack and confirms it at link time. + - *Verification at link time, by reading only.* uzi reads the tag and its two sharing settings; no issue is modified. The link is refused, with what to change in YouTrack, unless the connection's account can see the tag and is covered by its use settings (it owns the tag, is named in them, or they cover all users), and the use settings cover someone other than the connection's account. The link screen then shows who can see and who can use the tag, and the admin confirms that this is who should start work. + - *Matching.* Intake selects issues by the stored tag id. Another tag with the same name is ignored. + - *Other tags uzi writes* (on issues it creates, such as findings). `EnsureLabels` resolves a name only to a tag owned by the connection's account and creates it there when missing, with the sharing settings of the eligibility tag. It never resolves a name to another user's tag and never creates the eligibility tag. When uzi writes the eligibility label itself, on an issue it creates or moves, that name resolves to the stored tag id, whoever owns the tag, and never to a same-named tag of the connection's account. + - *`ListLabels`* returns the tags the token can see. Board columns are not tags on YouTrack (D9). + - *In the contract suite* the shared rule is "a label is addressed by name within the project the driver serves". The YouTrack subject is seeded with an eligibility tag owned by another user and shared, and a second tag of the same name owned by the connection's account, and the suite shows that reads, `EnsureLabels` and `CreateIssue` only ever use the first. + +## Feature parity on a repo linked to an issue tracker + +- Bring tagged issues into uzi: the eligibility tag chosen at link time (D14), in the linked project. Milestone M4. +- Give the run the issue's title, body and comments: yes, comments subject to D11. M4. +- Auto-approve a scheduled run's plan: only when the issue author is mapped (D11). +- Let the agent read an issue through the worker API: yes, same routes. M4. +- Show the board and move cards: yes, through the State field (D9). M5. +- Close or reopen an issue: yes, through the State field. M5. +- Create an issue (board, findings, judge, review, self-improve, proposals): yes, in the linked project. M5. +- Write comments on the issue (today's automatic templates): yes. M5. +- PRD link patch in the issue description: yes. M5. +- Eligibility by assigning the issue to the uzi bot: not offered in this PRD; the tag is the only signal. +- Autopilot: not offered (D12). +- Change a repo's issue source after it was first enabled: not offered (D13). +- Privilege checks, claim and settle, pipeline sync, merge request watchers, clone and push: unchanged, on the forge (S2). + +## Milestones + +Progress (one PR per milestone, each merged before the next starts): + +- [x] **M1:** the `IssueTracker` and `CodeHost` interfaces +- [ ] **M2:** every caller on its half +- [ ] **M3:** issue tracker connections and the YouTrack driver +- [ ] **M4:** issue identity, the repo link and issue intake from YouTrack +- [ ] **M5:** the board and issue writes on YouTrack +- [ ] **M6:** web and CLI surfaces, docs and live acceptance + +### M1: the `IssueTracker` and `CodeHost` interfaces + +D1, D2 and D4. One file per interface with the types only it uses (`issuetracker.go`, `codehost.go`); `forge.go` keeps the shared pieces. + +- **Acceptance:** pure moves (`git diff -M` shows no changed lines in moved code); no caller changes; every existing test passes unchanged. + +### M2: every caller on its half + +D3 and D5. + +- **Blocked by:** M1. +- **Acceptance:** every issue-side use in the refreshed inventory (47 at r1) goes through `IssueTracker`; `privcheck` cannot be handed a value that has only the issue side; existing tests change only in two mechanical ways (a fake's parameter type, and passing the same fake as both halves). + +### M3: issue tracker connections and the YouTrack driver + +D6, D7, D8, D10 (the id adapter), D14 (the tag rules inside the driver) and S3. The `api/internal/issuetracker` package, the YouTrack driver for every `IssueTracker` method plus verify, list projects, list tags and list a project's states, its fakes, the contract suite, the connection table and the allowlist. No repo can link to an issue tracker yet, so no behaviour changes. + +- **Deployment wiring for S3.** The issue tracker allowlist gets a Helm value rendered into the API ConfigMap, with the same https-only validation as `forge.allowedBaseURLs`, and a pass-through in `docker-compose.yml`. It is empty by default in both. It does not feed the worker egress list, because the worker never calls an issue tracker (S5). +- **ADR.** The ADR for the issue tracker seam ships with this milestone, where the package (D6) and the connections (D8) become real: adr/4173-issue-tracker-seam.md. +- **Blocked by:** M2. +- **Acceptance:** the contract suite passes on the real YouTrack driver against its HTTPS fake and on the three real forge drivers against `e2e/forge-fake`; the same-name tag case of D14 passes; a call with a `projectID` other than the driver's and an issue found in another project are each an error; the tag's sharing settings are parsed from a response recorded from a real YouTrack; with the allowlist empty no connection can be created and uzi still boots; the chart renders the allowlist and refuses a non-https entry; a redirect, an oversized body and an error carrying the token are each refused or scrubbed in a test. + +### M4: issue identity, the repo link and issue intake from YouTrack + +D5, D10 (storage), D11, D13, D14 (choice and verification of the tag) and S2. The storage decision and the author mapping (both recorded in the Decision Log before code), the never-enabled marker, the repo link to a connection, a project and an eligibility tag, `IssueTrackerForRepo` returning the issue tracker, sync on the poll cadence, run creation from an issue tracker issue. + +- **Blocked by:** M3. +- **Acceptance:** + - a YouTrack issue carrying the eligibility tag appears on the board and starts a run that receives its title and body, and an issue carrying only a same-named tag of another user does not; + - an issue tracker comment from an unmapped author is withheld; + - with an unmapped issue author, a manual start proceeds and a scheduled start is created without auto-approval, each in its own test; + - the privilege check and claim still run against the forge and fail closed with the issue tracker unreachable; + - a repo with no link is byte-for-byte unchanged in its forge calls (recorded-call test); + - linking is refused for every repo that existed before the migration and for a repo enabled since, including one that is disabled now; it is refused for a tag the connection's account cannot apply or that nobody else can apply, and the check modifies no issue; a link racing the first enable ends in exactly one of the two outcomes of D13; a connection with a linked repo cannot be deleted; + - when a run's issue is moved to another YouTrack project, its issue writes are skipped and its merge request still completes (D10). + +### M5: the board and issue writes on YouTrack + +D9. `MoveToColumn` on all four drivers, the column-to-state mapping, close and reopen, issues uzi creates, today's comment templates and the PRD link patch. + +- **Blocked by:** M4. +- **Acceptance:** the recorded-call test shows identical label calls on the three forges; a card move changes the YouTrack State and a failed move snaps back; closing never lands on a state that means "won't fix". + +### M6: web and CLI surfaces, docs and live acceptance + +Settings → Issue trackers, the CLI commands, a setup page under `docs/`. + +- **Blocked by:** M5. +- **Acceptance (maintainer-owned, live):** connect, link, tag, run, board move and close verified on a real YouTrack from a hosted Kubernetes install, with the allowlist set through the chart, and a regression pass on GitHub, GitLab and Forgejo. + +## Testing decisions + +- **In a uzi run, the in-run boundary of `AGENTS.md` "Run economy" applies:** `task check:api` plus `go test -race -count=1` on the affected packages, with each focused regression test watched red then green, `task check:web` plus the touched tests when web changes, and the companion checks for the files changed (`check:changelog-entry`, the migration checks, `docs:sync` with `check-docs:web`, `check:token-literals`). M3 to M5 touch credentials and untrusted input, so `task scan:secrets` also runs in the run and its canaries-detected line is part of the evidence. Token-shaped test strings are assembled at runtime. The full `gate:*` targets are the PR's CI on the exact head. +- **The E2E harness follows every changed issue or forge call in the same PR** (`AGENTS.md`, "A product contract change updates the E2E harness in the same PR"): the old identifiers are searched across `e2e/phases/`, `e2e/lib.sh`, `e2e/lib.test.sh` and `e2e/forge-fake/`, stale expectations and fake routes are updated, `task test:e2e-lib` and `task test:forge-fake` run, and the PR names the affected phases left unrun. +- **No change in behaviour on forge-backed repos:** the existing API, web, agent and E2E suites pass; M2's two mechanical test edits are the only ones allowed without a finding. +- **Parity is the contract suite on the real drivers (D7)**, not a per-driver checklist and not a run against in-memory fakes. +- **Hosted Kubernetes is verified, not only compose:** the chart test in M3 and the live acceptance in M6. + +## Risks + +- **Size of M2.** About 57 production and 31 test files, mechanical. A type-checked inventory of forge calls is the guard. +- **Drift from `main` between milestones.** Each milestone's branch is rebased on `main` before its PR. +- **YouTrack specifics leak into the seam.** The id adapter (D10) and the tag rules (D14) stay inside the driver, the contract suite runs on all four implementations, and the second issue tracker (Asana) is the real test of the seam. + +## Decision Log + +- **2026-11-05: the interface is named `IssueTracker`.** +- **2026-11-10: one file per interface.** `issuetracker.go` and `codehost.go`; r1 had both interfaces in `forge.go`. +- **2026-11-10: no reflection test.** The interface declarations enforce the split (D4). +- **2026-11-10: M2 test edits.** Fakes may change a parameter type, and mixed callers' test call sites pass the same fake twice. Nothing else in a test changes. +- **2026-11-10: scope after the maintainer's review.** YouTrack becomes part of this PRD and drives the identity design. The living comment, the outbox, withdrawal, comments to the agent and `/uzi` commands move out. Asana follows in its own PRD. +- **2026-11-11: the issue source is fixed before first enable (D13).** Review of the M1 PR found that project-local numbers let an old run or pinned schedule resolve to an unrelated issue after a repo changes its issue source. The first answer was a per-repo fence over run creation, sync and every issue write. The maintainer proposed the simpler policy: choose the source before the repo is first enabled, and leave relinking out. It holds, because a never-enabled repo has had no sync and no run, provided "never enabled" is stored rather than read from `repos.enabled` and the link and the first enable share the repo row lock. The fence is dropped. +- **2026-11-11: id adapter instead of new neutral types (D10).** The driver is built for one project, `issueIID` is `numberInProject`, the driver answers only to the repo's forge project id, `IssueNote.ID` is not derived and account ids are 0. Decided before the M3 driver; only the `issue_key` column waits for M4. +- **2026-11-11: the eligibility tag is chosen by id (D14)**, because YouTrack tags are user-owned, private by default and not unique by name. +- **2026-11-11: an unmapped issue author is `permission_unknown` (D11).** Manual start proceeds, scheduled start is never auto-approved. +- **2026-11-11: the contract suite runs on the real drivers (D7), the allowlist is wired through the chart and compose in M3, and the ADR moves from M6 to M3.** +- **2026-11-11: second review pass.** Every repo that exists at the M4 migration counts as previously enabled (D13). The driver refuses a `projectID` other than its own, and listing states is project-scoped (D10). The eligibility tag is checked through its see and use settings, read-only (D14). No number is derived from a comment id (D10). From 9e29bc5961543d8d3d5c8fdf2c61cd3d0113bcc8 Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 13:22:01 +0000 Subject: [PATCH 03/16] forge M2 U1: leaf seams on their half GetIssueBounded takes IssueTracker; BuildFailureSnapshot and privcheck take CodeHost; comment intake (issueinput.Fetch) takes (ch, tr). provisionsvc.Guard follows privcheck. Callers pass the driver as both halves; test edits are fake parameter types and f -> f, f only. Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/forge/response_limit.go | 2 +- .../handler/response_overflow_m2_test.go | 2 +- .../issueinput/aggregate_response_test.go | 2 +- .../issueinput/interactive_timeout_test.go | 4 ++-- api/internal/issueinput/projection.go | 12 +++++------ api/internal/issueinput/projection_test.go | 17 ++++++++-------- api/internal/poller/autopilot.go | 2 +- api/internal/privcheck/checker.go | 10 +++++----- api/internal/privcheck/service.go | 20 ++++++++++++++----- api/internal/provisionsvc/provisionsvc.go | 2 +- .../provisionsvc/provisionsvc_test.go | 2 +- api/internal/schedsvc/scheduler.go | 4 ++-- api/internal/setupcmd/setupcmd_test.go | 2 +- api/internal/workersvc/ci_fix_snapshot.go | 2 +- api/internal/workersvc/composite.go | 2 +- api/internal/workersvc/issue_input_m2_test.go | 2 +- api/internal/workersvc/service.go | 2 +- 17 files changed, 50 insertions(+), 39 deletions(-) diff --git a/api/internal/forge/response_limit.go b/api/internal/forge/response_limit.go index 40415780cc..60920daa7c 100644 --- a/api/internal/forge/response_limit.go +++ b/api/internal/forge/response_limit.go @@ -107,7 +107,7 @@ type responseLimitState struct { // (2 MiB success, at most 4 KiB error). Share the state with the issue driver so // SDK-discarded read errors retain their safe response provenance. Ordinary issue // reads keep each driver's existing policy. -func GetIssueBounded(ctx context.Context, f Forge, projectID, iid int64) (Issue, error) { +func GetIssueBounded(ctx context.Context, f IssueTracker, projectID, iid int64) (Issue, error) { state := newResponseLimitState(issueResponseBytes) state.continuationValidation = true ctx = context.WithValue(ctx, ciRepairJSONKey{}, false) diff --git a/api/internal/handler/response_overflow_m2_test.go b/api/internal/handler/response_overflow_m2_test.go index 442a52e35b..5f8b59396a 100644 --- a/api/internal/handler/response_overflow_m2_test.go +++ b/api/internal/handler/response_overflow_m2_test.go @@ -107,7 +107,7 @@ func TestM2RealDriverOverflowProjection(t *testing.T) { if err != nil { t.Fatal(err) } - capture, err := issueinput.Fetch(context.Background(), driver, 4242, 11, 1) + capture, err := issueinput.Fetch(context.Background(), driver, driver, 4242, 11, 1) if mode == "target" { var concrete *forge.ResponseTooLargeError if capture != nil || !errors.Is(err, forge.ErrResponseTooLarge) || !errors.As(err, &concrete) { diff --git a/api/internal/issueinput/aggregate_response_test.go b/api/internal/issueinput/aggregate_response_test.go index 275d36bcf7..02253899ff 100644 --- a/api/internal/issueinput/aggregate_response_test.go +++ b/api/internal/issueinput/aggregate_response_test.go @@ -74,7 +74,7 @@ func TestFetchAggregateRefusal(t *testing.T) { if err != nil { t.Fatal(err) } - c, err := issueinput.Fetch(context.Background(), d, 7, 9, 99) + c, err := issueinput.Fetch(context.Background(), d, d, 7, 9, 99) if err != nil { t.Fatal(err) } diff --git a/api/internal/issueinput/interactive_timeout_test.go b/api/internal/issueinput/interactive_timeout_test.go index 72111a50e2..db6fd8c4c2 100644 --- a/api/internal/issueinput/interactive_timeout_test.go +++ b/api/internal/issueinput/interactive_timeout_test.go @@ -113,9 +113,9 @@ func TestFetchTotalTimeout(t *testing.T) { var c *issueinput.Capture var err error if mode == "default" { - c, err = issueinput.Fetch(parent, f, 7, 11, 1) + c, err = issueinput.Fetch(parent, f, f, 7, 11, 1) } else { - c, err = issueinput.FetchWithTimeout(parent, f, 7, 11, 1, issueinput.InteractiveAssessmentTimeout) + c, err = issueinput.FetchWithTimeout(parent, f, f, 7, 11, 1, issueinput.InteractiveAssessmentTimeout) } switch mode { case "failed_raw": diff --git a/api/internal/issueinput/projection.go b/api/internal/issueinput/projection.go index 298aa529a4..abafe80948 100644 --- a/api/internal/issueinput/projection.go +++ b/api/internal/issueinput/projection.go @@ -146,16 +146,16 @@ func ProjectThread(a *Assessment, in []forge.IssueComment, botID int64) *Thread // Fetch begins the deadline before the raw fetch and closes the assessment after // projecting comments. Valid raw capture survives child expiry, but parent // cancellation never becomes a successful capture. -func Fetch(parent context.Context, f forge.Forge, project, iid, botID int64) (*Capture, error) { - return FetchWithTimeout(parent, f, project, iid, botID, AssessmentTimeout) +func Fetch(parent context.Context, ch forge.CodeHost, tr forge.IssueTracker, project, iid, botID int64) (*Capture, error) { + return FetchWithTimeout(parent, ch, tr, project, iid, botID, AssessmentTimeout) } // FetchWithTimeout shares a bounded total deadline across raw capture and author // assessment. Successful raw capture survives child expiry, but not parent cancellation. -func FetchWithTimeout(parent context.Context, f forge.Forge, project, iid, botID int64, timeout time.Duration) (*Capture, error) { - a := NewAssessmentWithTimeout(parent, f, project, timeout) +func FetchWithTimeout(parent context.Context, ch forge.CodeHost, tr forge.IssueTracker, project, iid, botID int64, timeout time.Duration) (*Capture, error) { + a := NewAssessmentWithTimeout(parent, ch, project, timeout) defer a.Close() - raw, err := f.GetIssue(a.Context(), project, iid) + raw, err := tr.GetIssue(a.Context(), project, iid) if err != nil { return nil, err } @@ -166,7 +166,7 @@ func FetchWithTimeout(parent context.Context, f forge.Forge, project, iid, botID d, _ := a.Author(raw) c.Reason = Reason(d) c.Issue.Author = AuthorName(raw.Author) - comments, err := f.ListIssueComments(a.Context(), project, iid) + comments, err := tr.ListIssueComments(a.Context(), project, iid) if err != nil { c.Thread = &Thread{Version: SnapshotVersion, Comments: []Comment{}, Unknown: true} } else { diff --git a/api/internal/issueinput/projection_test.go b/api/internal/issueinput/projection_test.go index 04f0ad918e..0544069f6e 100644 --- a/api/internal/issueinput/projection_test.go +++ b/api/internal/issueinput/projection_test.go @@ -52,7 +52,7 @@ func TestM2CaptureWholeThreadBeforeCaps(t *testing.T) { for i := 0; i < 201; i++ { f.comments = append(f.comments, forge.IssueComment{AuthorForgeUserID: 2, AuthorUsername: "alice\n\u202e\x1b[31m", Body: "eligible"}) } - c, err := issueinput.Fetch(context.Background(), f, 7, 11, 1) + c, err := issueinput.Fetch(context.Background(), f, f, 7, 11, 1) if err != nil { t.Fatal(err) } @@ -80,7 +80,7 @@ func TestM2CaptureDispositionBodiesAndFailures(t *testing.T) { for _, id := range []int64{2, 3, 4} { f := &captureForge{issue: forge.Issue{IID: 11, Title: "raw title", Description: "raw body", AuthorForgeUserID: id}, comments: []forge.IssueComment{{AuthorForgeUserID: id, Body: "raw comment"}}} - c, err := issueinput.Fetch(context.Background(), f, 7, 11, 1) + c, err := issueinput.Fetch(context.Background(), f, f, 7, 11, 1) if err != nil { t.Fatal(err) } @@ -95,23 +95,23 @@ func TestM2CaptureDispositionBodiesAndFailures(t *testing.T) { } } f := &captureForge{issue: forge.Issue{IID: 11, AuthorForgeUserID: 2}, commentsErr: errors.New("secret")} - c, err := issueinput.Fetch(context.Background(), f, 7, 11, 1) + c, err := issueinput.Fetch(context.Background(), f, f, 7, 11, 1) if err != nil || !c.Unknown { t.Fatalf("%+v %v", c, err) } - c, err = issueinput.Fetch(context.Background(), f, 7, 11, 0) + c, err = issueinput.Fetch(context.Background(), f, f, 7, 11, 0) if err != nil || !c.Unknown || len(c.Thread.Comments) != 0 { t.Fatalf("%+v %v", c, err) } f.getErr = errors.New("fetch failed") - if _, err = issueinput.Fetch(context.Background(), f, 7, 11, 1); err == nil { + if _, err = issueinput.Fetch(context.Background(), f, f, 7, 11, 1); err == nil { t.Fatal("failed raw fetch succeeded") } } func TestM2CaptureParentCancellation(t *testing.T) { parent, cancel := context.WithCancel(context.Background()) f := &captureForge{issue: forge.Issue{IID: 11, Title: "raw", Description: "raw", AuthorForgeUserID: 2}, childCancel: cancel} - if _, err := issueinput.Fetch(parent, f, 7, 11, 1); !errors.Is(err, context.Canceled) { + if _, err := issueinput.Fetch(parent, f, f, 7, 11, 1); !errors.Is(err, context.Canceled) { t.Fatal(err) } } @@ -126,7 +126,7 @@ func TestM2CaptureChildExpiryValidRaw(t *testing.T) { start := time.Now() f := &expiryForge{expireDuringAssessment: true} f.comments = []forge.IssueComment{{AuthorForgeUserID: 4, Body: "UNVERIFIED_RAW"}} - c, err := issueinput.FetchWithTimeout(parent, f, 7, 11, 1, budget) + c, err := issueinput.FetchWithTimeout(parent, f, f, 7, 11, 1, budget) if err != nil || c == nil { t.Fatalf("valid raw lost on child expiry: %+v %v", c, err) } @@ -153,7 +153,8 @@ func TestM2CaptureLateRawRejected(t *testing.T) { defer cancel() budget := issueinput.InteractiveAssessmentTimeout start := time.Now() - c, err := issueinput.FetchWithTimeout(parent, &expiryForge{}, 7, 11, 1, budget) + ef := &expiryForge{} + c, err := issueinput.FetchWithTimeout(parent, ef, ef, 7, 11, 1, budget) if !errors.Is(err, context.DeadlineExceeded) || c != nil { t.Fatalf("late raw accepted: %+v %v", c, err) } diff --git a/api/internal/poller/autopilot.go b/api/internal/poller/autopilot.go index 147d098ec2..6f8a880bdd 100644 --- a/api/internal/poller/autopilot.go +++ b/api/internal/poller/autopilot.go @@ -206,7 +206,7 @@ func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConn // issue eligibility and one-active-run gates all enforced there), only // with auto_approve set. The description is the fresh forge copy, snapshotted onto // the run exactly as the manual start path snapshots the description it is given. - capture, err := issueinput.Fetch(ctx, f, r.ForgeProjectID, iid, cc.BotForgeUserID) + capture, err := issueinput.Fetch(ctx, f, f, r.ForgeProjectID, iid, cc.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { a.recordThenComment(ctx, r, f, iid, eventID, tooLargeComment()) diff --git a/api/internal/privcheck/checker.go b/api/internal/privcheck/checker.go index 5399266013..6d8de4e7e8 100644 --- a/api/internal/privcheck/checker.go +++ b/api/internal/privcheck/checker.go @@ -39,7 +39,7 @@ func NewChecker() *Checker { // An introspection-unsupported forge or a transient introspection error becomes // a warning, never a blocking violation; the admin check still applies (it rides // on VerifyToken, not TokenInfo). now is injectable for tests. -func (c *Checker) CheckToken(ctx context.Context, f forge.Forge, forgeType forge.Type, isAdmin bool, now time.Time) TokenReport { +func (c *Checker) CheckToken(ctx context.Context, f forge.CodeHost, forgeType forge.Type, isAdmin bool, now time.Time) TokenReport { info, err := f.TokenInfo(ctx) if err != nil { warn := "could not verify token scopes against the forge; scope/expiry checks were skipped" @@ -73,7 +73,7 @@ func introspectionUnsupportedWarn(forgeType forge.Type) string { // error-as-report: if the token cannot even be verified against the forge // (revoked, forge unreachable) the report is StatusError with that finding, not // a returned error — a revoked token is exactly what the report must surface. -func (c *Checker) Check(ctx context.Context, f forge.Forge, forgeType forge.Type, repos []Repo, now time.Time) Report { +func (c *Checker) Check(ctx context.Context, f forge.CodeHost, forgeType forge.Type, repos []Repo, now time.Time) Report { identity, err := f.VerifyToken(ctx) if err != nil { // A version-gate refusal is a different condition from a bad token, and @@ -106,7 +106,7 @@ func (c *Checker) Check(ctx context.Context, f forge.Forge, forgeType forge.Type // checkRepos fans out the per-repo checks with a bounded worker pool, preserving // input order in the result. -func (c *Checker) checkRepos(ctx context.Context, f forge.Forge, botUserID int64, repos []Repo) []RepoReport { +func (c *Checker) checkRepos(ctx context.Context, f forge.CodeHost, botUserID int64, repos []Repo) []RepoReport { out := make([]RepoReport, len(repos)) if len(repos) == 0 { return out @@ -133,7 +133,7 @@ func (c *Checker) checkRepos(ctx context.Context, f forge.Forge, botUserID int64 // checkRepo runs the role + branch-protection checks for one repo. A forge call // failing mid-repo is a warning on that repo, never a crash — the sweep must // keep going across a flaky project. -func (c *Checker) checkRepo(ctx context.Context, f forge.Forge, botUserID int64, repo Repo) RepoReport { +func (c *Checker) checkRepo(ctx context.Context, f forge.CodeHost, botUserID int64, repo Repo) RepoReport { rr := RepoReport{RepoID: repo.ID, Path: repo.Path, Findings: []Finding{}} role, member, roleErr := f.ProjectRole(ctx, repo.ForgeProjectID, botUserID) @@ -154,7 +154,7 @@ func (c *Checker) checkRepo(ctx context.Context, f forge.Forge, botUserID int64, // evaluateRepo turns one repo's forge answers (the bot's role, and the default // branch's protection) into findings. It is pure — no forge calls — and it is // the single evaluator both drivers flow through, because checkRepo talks only -// to the forge.Forge interface and never learns which forge answered. That is +// to the forge.CodeHost interface and never learns which forge answered. That is // what "one shared evaluateRepo" (R12) means here: there is no per-forge branch // to keep in step. // diff --git a/api/internal/privcheck/service.go b/api/internal/privcheck/service.go index 9d1a0a0e71..a3b0983601 100644 --- a/api/internal/privcheck/service.go +++ b/api/internal/privcheck/service.go @@ -46,7 +46,7 @@ func NewService(q Store, forges ForgeBuilder) *Service { // client (the save-time path in CreateConnection, which holds the plaintext // token and the bot identity). It makes at most one forge call (TokenInfo) and // persists nothing — the caller decides whether the violations block the save. -func (s *Service) CheckToken(ctx context.Context, f forge.Forge, forgeType forge.Type, isAdmin bool) TokenReport { +func (s *Service) CheckToken(ctx context.Context, f forge.CodeHost, forgeType forge.Type, isAdmin bool) TokenReport { return s.checker.CheckToken(ctx, f, forgeType, isAdmin, s.now()) } @@ -55,7 +55,7 @@ func (s *Service) CheckToken(ctx context.Context, f forge.Forge, forgeType forge // StatusError report (so the badge reflects the problem) rather than a returned // error; only a genuine DB error (repo list, report write) returns an error. func (s *Service) CheckConnection(ctx context.Context, conn store.ForgeConnection) (Report, error) { - f, err := s.forges.ForgeForConnection(conn.ForgeType, conn.BaseUrl, conn.TokenCiphertext) + f, err := s.codeHostFor(conn.ForgeType, conn.BaseUrl, conn.TokenCiphertext) if err != nil { // Token can't be decrypted (key rotated) or the client won't build: record // it, don't crash the sweep. @@ -145,7 +145,7 @@ func (s *Service) GuardrailImpact(ctx context.Context) (ImpactReport, error) { // at either step is not fatal: it marks every one of this connection's repos // unevaluable (we could not read the forge to tell), never blocked and never // safe. - f, buildErr := s.forges.ForgeForConnection(conn.ForgeType, conn.BaseUrl, conn.TokenCiphertext) + f, buildErr := s.codeHostFor(conn.ForgeType, conn.BaseUrl, conn.TokenCiphertext) var ( botUserID int64 connErr = buildErr @@ -197,7 +197,7 @@ func (s *Service) GuardrailImpact(ctx context.Context) (ImpactReport, error) { // blocking set is the push/merge fields only) so that a repo whose forge // round-trips error is counted unevaluable exactly as the live gate would be // unable to clear it. -func (s *Service) impactForRepo(ctx context.Context, f forge.Forge, botUserID int64, repo Repo) (blocked, ok bool) { +func (s *Service) impactForRepo(ctx context.Context, f forge.CodeHost, botUserID int64, repo Repo) (blocked, ok bool) { if repo.DefaultBranch == "" { return false, false } @@ -254,7 +254,7 @@ type GuardResult struct { // Callers: the repo-enable gate (handler.SetRepoEnabled, M4). The run-create // service inserts and the claim backstop wire in at M5/M6. func (s *Service) GuardRepo(ctx context.Context, in GuardInput) GuardResult { - f, err := s.forges.ForgeForConnection(in.ForgeType, in.BaseURL, in.TokenCiphertext) + f, err := s.codeHostFor(in.ForgeType, in.BaseURL, in.TokenCiphertext) if err != nil { // Client won't build (token undecryptable, key rotated, misconfigured): we // cannot read the forge, so we cannot clear the bot. Fail closed. The message @@ -344,3 +344,13 @@ type SweepResult struct { ReportErrors int // checked, but the report itself is StatusError Errors int // could not check at all (DB error persisting) } + +// codeHostFor builds the connection's driver and narrows it to the CodeHost +// half, the only half the privilege checks use. +func (s *Service) codeHostFor(forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { + f, err := s.forges.ForgeForConnection(forgeType, baseURL, tokenCiphertext) + if err != nil { + return nil, err + } + return f, nil +} diff --git a/api/internal/provisionsvc/provisionsvc.go b/api/internal/provisionsvc/provisionsvc.go index 604ee666c4..e1bf1b4320 100644 --- a/api/internal/provisionsvc/provisionsvc.go +++ b/api/internal/provisionsvc/provisionsvc.go @@ -78,7 +78,7 @@ type Forges interface { // Guard runs the least-privilege checks (*privcheck.Service). type Guard interface { - CheckToken(ctx context.Context, f forge.Forge, forgeType forge.Type, isAdmin bool) privcheck.TokenReport + CheckToken(ctx context.Context, f forge.CodeHost, forgeType forge.Type, isAdmin bool) privcheck.TokenReport GuardRepo(ctx context.Context, in privcheck.GuardInput) privcheck.GuardResult } diff --git a/api/internal/provisionsvc/provisionsvc_test.go b/api/internal/provisionsvc/provisionsvc_test.go index ff84b9b589..98acc82724 100644 --- a/api/internal/provisionsvc/provisionsvc_test.go +++ b/api/internal/provisionsvc/provisionsvc_test.go @@ -49,7 +49,7 @@ type fakeGuard struct { overridden []bool } -func (g *fakeGuard) CheckToken(context.Context, forge.Forge, forge.Type, bool) privcheck.TokenReport { +func (g *fakeGuard) CheckToken(context.Context, forge.CodeHost, forge.Type, bool) privcheck.TokenReport { return privcheck.TokenReport{Violations: g.violations} } func (g *fakeGuard) GuardRepo(_ context.Context, in privcheck.GuardInput) privcheck.GuardResult { diff --git a/api/internal/schedsvc/scheduler.go b/api/internal/schedsvc/scheduler.go index 95ff7c4f51..78eb5a2a2d 100644 --- a/api/internal/schedsvc/scheduler.go +++ b/api/internal/schedsvc/scheduler.go @@ -591,7 +591,7 @@ func (e *Scheduler) fireIssue(ctx context.Context, sched store.RunSchedule) (Fir return FireOutcome{Matched: 1, Skips: []Skip{{IssueIID: &iidCopy, Reason: SkipAlreadyRunning}}}, nil } - capture, err := issueinput.Fetch(ctx, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) + capture, err := issueinput.Fetch(ctx, f, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { iidCopy := iid @@ -803,7 +803,7 @@ func (e *Scheduler) fireSweep(ctx context.Context, sched store.RunSchedule) (Fir out.Skips = append(out.Skips, Skip{IssueIID: &iidCopy, Reason: SkipAlreadyRunning}) continue } - capture, err := issueinput.Fetch(ctx, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) + capture, err := issueinput.Fetch(ctx, f, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) if err != nil { e.logger.Warn("scheduler: sweep fetch issue", "schedule", sched.ID.String(), "issue", iid, "error", err) reason := SkipFetchFailed diff --git a/api/internal/setupcmd/setupcmd_test.go b/api/internal/setupcmd/setupcmd_test.go index ba47f350d7..5ff79de159 100644 --- a/api/internal/setupcmd/setupcmd_test.go +++ b/api/internal/setupcmd/setupcmd_test.go @@ -60,7 +60,7 @@ type fakeGuard struct { blocked map[string]bool // by path_with_namespace } -func (g *fakeGuard) CheckToken(context.Context, forge.Forge, forge.Type, bool) privcheck.TokenReport { +func (g *fakeGuard) CheckToken(context.Context, forge.CodeHost, forge.Type, bool) privcheck.TokenReport { return privcheck.TokenReport{Violations: g.violations} } func (g *fakeGuard) GuardRepo(_ context.Context, in privcheck.GuardInput) privcheck.GuardResult { diff --git a/api/internal/workersvc/ci_fix_snapshot.go b/api/internal/workersvc/ci_fix_snapshot.go index 933d2661d5..c9dd5c2b94 100644 --- a/api/internal/workersvc/ci_fix_snapshot.go +++ b/api/internal/workersvc/ci_fix_snapshot.go @@ -60,7 +60,7 @@ func ScrubKnownTokens(s string) string { // trace. Log tails pass the driver's connection-PAT scrub (M1) plus a known-token // scrub here. A single unreadable trace degrades to an empty tail, not a // whole-snapshot failure. -func BuildFailureSnapshot(ctx context.Context, f forge.Forge, projectID int64, ps store.PipelineStatus, maxJobs, logTailBytes int) (FailureSnapshot, error) { +func BuildFailureSnapshot(ctx context.Context, f forge.CodeHost, projectID int64, ps store.PipelineStatus, maxJobs, logTailBytes int) (FailureSnapshot, error) { snap := FailureSnapshot{ PipelineID: ps.PipelineID, Ref: ps.Ref, diff --git a/api/internal/workersvc/composite.go b/api/internal/workersvc/composite.go index 15272261bc..9584c924c1 100644 --- a/api/internal/workersvc/composite.go +++ b/api/internal/workersvc/composite.go @@ -209,7 +209,7 @@ func (s *Service) StartRunForUser(ctx context.Context, userID, repoID uuid.UUID, if err != nil { return store.Run{}, fmt.Errorf("%w: %v", ErrForgeBuild, err) } - capture, err := issueinput.FetchWithTimeout(ctx, f, repo.ForgeProjectID, issueIID, repo.BotForgeUserID, issueinput.InteractiveAssessmentTimeout) + capture, err := issueinput.FetchWithTimeout(ctx, f, f, repo.ForgeProjectID, issueIID, repo.BotForgeUserID, issueinput.InteractiveAssessmentTimeout) if err != nil { if forge.IsIssueResponseTooLarge(err) { return store.Run{}, fmt.Errorf("%w: %w", ErrDescriptionTooLarge, err) diff --git a/api/internal/workersvc/issue_input_m2_test.go b/api/internal/workersvc/issue_input_m2_test.go index c31275ffff..385ac4b857 100644 --- a/api/internal/workersvc/issue_input_m2_test.go +++ b/api/internal/workersvc/issue_input_m2_test.go @@ -95,7 +95,7 @@ func TestM2CreatePolicyScheduleOnly(t *testing.T) { } func TestM2CreateCapturedGuidanceAndDedup(t *testing.T) { f := &m2Forge{disposition: forge.AuthorEligible} - capture, err := issueinput.Fetch(context.Background(), f, 0, 4, 1) + capture, err := issueinput.Fetch(context.Background(), f, f, 0, 4, 1) if err != nil { t.Fatal(err) } diff --git a/api/internal/workersvc/service.go b/api/internal/workersvc/service.go index 585da09490..6e3235fbfb 100644 --- a/api/internal/workersvc/service.go +++ b/api/internal/workersvc/service.go @@ -8071,7 +8071,7 @@ func (s *Service) createRun(ctx context.Context, userID, repoID uuid.UUID, issue if ferr != nil { return store.Run{}, ErrForgeBuild } - capture, err = issueinput.Fetch(ctx, f, row.ForgeProjectID, issueIID, row.BotForgeUserID) + capture, err = issueinput.Fetch(ctx, f, f, row.ForgeProjectID, issueIID, row.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { return store.Run{}, fmt.Errorf("%w: %w", ErrDescriptionTooLarge, err) From aa1c7f5741588571f9845dcfc0abd60c2d5ae39c Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 13:43:09 +0000 Subject: [PATCH 04/16] forge M2 U2: forgesvc on its halves, IssueTrackerForRepo/CodeHostForRepo Sync, board moves, close/reopen and the card-move interfaces take IssueTracker; pipeline sync, board-free MR watch, merged-predecessor and project-scope checks take CodeHost; the MR state watcher and PRD link patch take (ch, tr). New repo_sides.go resolves a repo's two sides (D8); both return the forge driver in this PRD. Test edits are fake parameter types and f -> f, f only. Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/forgesvc/board_columns.go | 2 +- api/internal/forgesvc/board_free_mr_watch.go | 4 +- api/internal/forgesvc/close_reopen.go | 4 +- api/internal/forgesvc/merged_predecessor.go | 2 +- .../forgesvc/mr_rework_cancel_livedb_test.go | 10 +-- api/internal/forgesvc/mr_watch.go | 16 ++-- api/internal/forgesvc/mr_watch_test.go | 6 +- api/internal/forgesvc/pipeline_sync.go | 4 +- api/internal/forgesvc/prd_link_patch.go | 12 +-- api/internal/forgesvc/prd_link_patch_test.go | 4 +- api/internal/forgesvc/projectsync.go | 4 +- .../forgesvc/projectsync_reverse_test.go | 2 +- api/internal/forgesvc/projectsync_sync.go | 2 +- api/internal/forgesvc/repo_sides.go | 33 ++++++++ api/internal/forgesvc/repo_sides_test.go | 83 +++++++++++++++++++ api/internal/forgesvc/service.go | 10 +-- .../handler/sweep_uzi_gate_livedb_test.go | 2 +- api/internal/poller/poller.go | 4 +- api/internal/privcheck/checker.go | 2 +- api/internal/runlifecycle/lifecycle.go | 6 +- api/internal/runlifecycle/lifecycle_test.go | 2 +- api/internal/schedsvc/ci_watch_test.go | 2 +- api/internal/schedsvc/scheduler.go | 2 +- api/internal/schedsvc/scheduler_test.go | 2 +- 24 files changed, 169 insertions(+), 51 deletions(-) create mode 100644 api/internal/forgesvc/repo_sides.go create mode 100644 api/internal/forgesvc/repo_sides_test.go diff --git a/api/internal/forgesvc/board_columns.go b/api/internal/forgesvc/board_columns.go index 60b5e0a20a..d09bf11fd2 100644 --- a/api/internal/forgesvc/board_columns.go +++ b/api/internal/forgesvc/board_columns.go @@ -16,7 +16,7 @@ type BoardColumnStore interface { InsertDefaultBoardColumns(ctx context.Context, arg store.InsertDefaultBoardColumnsParams) error } -// LabelEnsurer is the slice of forge.Forge EnsureBoardColumns needs. +// LabelEnsurer is the slice of forge.IssueTracker EnsureBoardColumns needs. type LabelEnsurer interface { EnsureLabels(ctx context.Context, projectID int64, labels []forge.Label) error } diff --git a/api/internal/forgesvc/board_free_mr_watch.go b/api/internal/forgesvc/board_free_mr_watch.go index 7f57a1a6fd..b64ebddf94 100644 --- a/api/internal/forgesvc/board_free_mr_watch.go +++ b/api/internal/forgesvc/board_free_mr_watch.go @@ -31,7 +31,7 @@ import ( // // Like SyncMRStates it never returns a per-candidate error (log-and-skip); only a failure // to enumerate candidates surfaces to the poller. -func (s *Service) SyncBoardFreeMRStates(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge) error { +func (s *Service) SyncBoardFreeMRStates(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.CodeHost) error { candidates, err := s.q.ListBoardFreeMRStateWatchCandidates(ctx, repoID) if err != nil { return err @@ -51,7 +51,7 @@ func (s *Service) SyncBoardFreeMRStates(ctx context.Context, repoID uuid.UUID, f // NULL bootstrap records without acting; opened->closed and ->merged cancel exactly once; // locked is transient (no cancel); a forge read failure or a cancel failure leaves mr_state // unadvanced so the next tick retries. -func (s *Service) recordBoardFreeMRState(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge, c store.ListBoardFreeMRStateWatchCandidatesRow) { +func (s *Service) recordBoardFreeMRState(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.CodeHost, c store.ListBoardFreeMRStateWatchCandidatesRow) { mr, err := f.GetMergeRequest(ctx, forgeProjectID, c.MrIid.Int64) if err != nil { slog.Warn("forgesvc: board-free MR-state read failed", "repo", repoID, "run", c.ID, "mr", c.MrIid.Int64, "error", err) diff --git a/api/internal/forgesvc/close_reopen.go b/api/internal/forgesvc/close_reopen.go index e63fceb305..4c391b2b75 100644 --- a/api/internal/forgesvc/close_reopen.go +++ b/api/internal/forgesvc/close_reopen.go @@ -18,7 +18,7 @@ import ( // left as the last sync/move stored them (a closed card renders in the Closed lane // via board.ResolveColumn regardless of its column labels), which is why this uses // the narrow UpdateIssueState query rather than UpsertIssueLabels. -func (s *Service) CloseIssue(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue) (store.Issue, error) { +func (s *Service) CloseIssue(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue) (store.Issue, error) { // Diff-first: if the cache already reflects the desired state, skip the forge // entirely and return the row unchanged (idempotent close, no forge call). if issue.State == string(forge.StateClosed) { @@ -55,7 +55,7 @@ func (s *Service) CloseIssue(ctx context.Context, f forge.Forge, forgeProjectID // Partial failure: if the move half fails, the reopened-but-unmoved row (state opened, // board_position nulled) is returned with the error so the handler can surface a 502 // for the move while the card still renders open — never lost. -func (s *Service) ReopenIssue(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) { +func (s *Service) ReopenIssue(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) { if issue.State != string(forge.StateOpened) { // Forge-first: reopen remotely before touching the cache. On failure the cache // is untouched (no ReopenIssueState, no UpsertIssueLabels) and the card snaps back. diff --git a/api/internal/forgesvc/merged_predecessor.go b/api/internal/forgesvc/merged_predecessor.go index 73b60aa1d8..4aefd5e69a 100644 --- a/api/internal/forgesvc/merged_predecessor.go +++ b/api/internal/forgesvc/merged_predecessor.go @@ -182,7 +182,7 @@ func (s *Service) verifyMergedPredecessor(ctx context.Context, row store.ListMer }() // The candidate query takes a fresh exact owner-scoped connection snapshot. // Never reuse the ordinary tick's connection or cached MR state. - f, err := s.ForgeForConnection(row.ForgeType, row.BaseUrl, row.TokenCiphertext) + f, err := s.CodeHostForRepo(ctx, row.RepoID, row.ForgeType, row.BaseUrl, row.TokenCiphertext) if err != nil { slog.Warn("merged predecessor reconciliation refused", "run_id", row.RunID, "reason", "client_unavailable") return diff --git a/api/internal/forgesvc/mr_rework_cancel_livedb_test.go b/api/internal/forgesvc/mr_rework_cancel_livedb_test.go index 22cb9095e8..887e6b3915 100644 --- a/api/internal/forgesvc/mr_rework_cancel_livedb_test.go +++ b/api/internal/forgesvc/mr_rework_cancel_livedb_test.go @@ -167,7 +167,7 @@ func TestMRReworkCancelOnMRCloseLiveDB(t *testing.T) { s := seed(t, 8531, 85310, "opened", false /* live */) f := &fakeForge{mrByIID: map[int64]forge.MergeRequest{s.mrIID: forgeMR(s.mrIID, "merged")}} - if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f); err != nil { + if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } @@ -187,7 +187,7 @@ func TestMRReworkCancelOnMRCloseLiveDB(t *testing.T) { s := seed(t, 8532, 85320, "opened", true /* live */) f := &fakeForge{mrByIID: map[int64]forge.MergeRequest{s.mrIID: forgeMR(s.mrIID, "merged")}} - if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f); err != nil { + if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } @@ -210,7 +210,7 @@ func TestMRReworkCancelOnMRCloseLiveDB(t *testing.T) { s := seed(t, 8533, 85330, "opened", false /* live */) f := &fakeForge{mrByIID: map[int64]forge.MergeRequest{s.mrIID: forgeMR(s.mrIID, "locked")}} - if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f); err != nil { + if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } @@ -234,7 +234,7 @@ func TestMRReworkCancelOnMRCloseLiveDB(t *testing.T) { s := seed(t, 8534, 85340, "closed", false /* live */) f := &fakeForge{mrByIID: map[int64]forge.MergeRequest{s.mrIID: forgeMR(s.mrIID, "closed")}} - if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f); err != nil { + if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } @@ -271,7 +271,7 @@ func TestMRReworkCancelOnMRCloseLiveDB(t *testing.T) { updateErr: fmt.Errorf("forge unreachable"), } - if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f); err != nil { + if err := forgeSvc.SyncMRStates(ctx, s.repoID, forgeProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } diff --git a/api/internal/forgesvc/mr_watch.go b/api/internal/forgesvc/mr_watch.go index 50ba9d1502..06fe3b54ca 100644 --- a/api/internal/forgesvc/mr_watch.go +++ b/api/internal/forgesvc/mr_watch.go @@ -26,7 +26,7 @@ import ( // It never returns per-candidate errors: one bad MR read or move must not stall // the tick (the poller's log-and-skip convention). Only a failure to enumerate // candidates is returned, so the poller can log it once for the repo. -func (s *Service) SyncMRStates(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge) error { +func (s *Service) SyncMRStates(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, ch forge.CodeHost, tr forge.IssueTracker) error { candidates, err := s.q.ListMRWatchCandidates(ctx, repoID) if err != nil { return err @@ -35,7 +35,7 @@ func (s *Service) SyncMRStates(ctx context.Context, repoID uuid.UUID, forgeProje if ctx.Err() != nil { return ctx.Err() } - s.syncOneMRState(ctx, repoID, forgeProjectID, f, c) + s.syncOneMRState(ctx, repoID, forgeProjectID, ch, tr, c) } return nil } @@ -56,8 +56,8 @@ func (s *Service) SyncMRStates(ctx context.Context, repoID uuid.UUID, forgeProje // read, or the move itself) mr_state is LEFT as-is, so the next poller tick // re-observes the same edge and retries — the poller cadence is the retry loop. // Consuming the edge on failure would re-create the stuck-card bug this PRD fixes. -func (s *Service) syncOneMRState(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge, c store.ListMRWatchCandidatesRow) { - mr, err := f.GetMergeRequest(ctx, forgeProjectID, c.MrIid.Int64) +func (s *Service) syncOneMRState(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, ch forge.CodeHost, tr forge.IssueTracker, c store.ListMRWatchCandidatesRow) { + mr, err := ch.GetMergeRequest(ctx, forgeProjectID, c.MrIid.Int64) if err != nil { // A read failure is not an edge: log-and-skip, leave mr_state for next tick. slog.Warn("forgesvc: MR-state read failed", "repo", repoID, "issue", c.IssueIid.Int64, "mr", c.MrIid.Int64, "error", err) @@ -110,7 +110,7 @@ func (s *Service) syncOneMRState(ctx context.Context, repoID uuid.UUID, forgePro if cancelErr != nil { slog.Warn("forgesvc: cancel rework on MR close failed, will retry", "repo", repoID, "issue", c.IssueIid.Int64, "mr", c.MrIid.Int64, "error", cancelErr) } - moved := s.guardedMRMove(ctx, repoID, forgeProjectID, c.IssueIid.Int64, f, board.ColumnHumanReview, board.ColumnInProgress) + moved := s.guardedMRMove(ctx, repoID, forgeProjectID, c.IssueIid.Int64, tr, board.ColumnHumanReview, board.ColumnInProgress) if moved == moveDeferred || cancelErr != nil { // forge failure / vanished card, or a cancel that must be retried: leave // mr_state unadvanced so the next tick re-observes the edge and retries. @@ -120,7 +120,7 @@ func (s *Service) syncOneMRState(ctx context.Context, repoID uuid.UUID, forgePro case stored == forge.MRStateClosed && observed == forge.MRStateOpened: // reopen-edge (Decision 6): restore the card Human Review-ward, // symmetrically, guarded from In Progress (where the close-edge left it). - if s.guardedMRMove(ctx, repoID, forgeProjectID, c.IssueIid.Int64, f, board.ColumnInProgress, board.ColumnHumanReview) == moveDeferred { + if s.guardedMRMove(ctx, repoID, forgeProjectID, c.IssueIid.Int64, tr, board.ColumnInProgress, board.ColumnHumanReview) == moveDeferred { return } s.recordMRState(ctx, c.ID, observed) @@ -185,7 +185,7 @@ const ( // source-column guard makes the retry a no-op (the card already moved) — except // the narrow case where a human dragged the card back to wantSource in the crash // window, which then gets re-moved once; accepted (crash-window-sized exposure). -func (s *Service) guardedMRMove(ctx context.Context, repoID uuid.UUID, forgeProjectID, issueIID int64, f forge.Forge, wantSource, target string) moveOutcome { +func (s *Service) guardedMRMove(ctx context.Context, repoID uuid.UUID, forgeProjectID, issueIID int64, tr forge.IssueTracker, wantSource, target string) moveOutcome { issue, err := s.q.GetIssueByIID(ctx, store.GetIssueByIIDParams{RepoID: repoID, ForgeIssueIid: issueIID}) if err != nil { // No cached card (evicted/de-labeled since the candidate scan): leave the @@ -224,7 +224,7 @@ func (s *Service) guardedMRMove(ctx context.Context, repoID uuid.UUID, forgeProj return moveSkipped } - if _, err := s.AutoMove(ctx, f, forgeProjectID, issue, cols, target); err != nil { + if _, err := s.AutoMove(ctx, tr, forgeProjectID, issue, cols, target); err != nil { // Forge error: leave mr_state unadvanced so the next tick re-observes this // edge and retries once the forge is reachable again. slog.Warn("forgesvc: MR-state move failed, will retry", "repo", repoID, "issue", issueIID, "target", target, "error", err) diff --git a/api/internal/forgesvc/mr_watch_test.go b/api/internal/forgesvc/mr_watch_test.go index a2362dd4c7..7d2699c567 100644 --- a/api/internal/forgesvc/mr_watch_test.go +++ b/api/internal/forgesvc/mr_watch_test.go @@ -62,7 +62,7 @@ func contains(ss []string, want string) bool { func run(t *testing.T, st *fakeStore, f *fakeForge) { t.Helper() svc := newTestService(st) - if err := svc.SyncMRStates(context.Background(), uuid.New(), testProjectID, f); err != nil { + if err := svc.SyncMRStates(context.Background(), uuid.New(), testProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } } @@ -336,7 +336,7 @@ func TestMRLockedClosedCancelsRework(t *testing.T) { svc := newTestService(st) svc.SetReworkCanceller(canceller) - if err := svc.SyncMRStates(context.Background(), repoID, testProjectID, f); err != nil { + if err := svc.SyncMRStates(context.Background(), repoID, testProjectID, f, f); err != nil { t.Fatalf("SyncMRStates: %v", err) } @@ -460,7 +460,7 @@ func TestMRCandidateListErrorPropagates(t *testing.T) { f := &fakeForge{} svc := newTestService(st) - if err := svc.SyncMRStates(context.Background(), uuid.New(), testProjectID, f); err == nil { + if err := svc.SyncMRStates(context.Background(), uuid.New(), testProjectID, f, f); err == nil { t.Fatal("a candidate-enumeration failure must surface so the poller logs it once") } if len(f.mrCalls) != 0 { diff --git a/api/internal/forgesvc/pipeline_sync.go b/api/internal/forgesvc/pipeline_sync.go index 6af2157c38..fa6b65f222 100644 --- a/api/internal/forgesvc/pipeline_sync.go +++ b/api/internal/forgesvc/pipeline_sync.go @@ -47,7 +47,7 @@ type PipelineSyncOptions struct { // and is retried next tick). ErrNoPipeline is not an error — it is "no CI for // this ref", so the ref is not cached and any stale row for it is allowed to // evict, which is how a default branch honestly flips to "no CI". -func (s *Service) SyncPipelines(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge, opts PipelineSyncOptions) error { +func (s *Service) SyncPipelines(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.CodeHost, opts PipelineSyncOptions) error { if opts.MaxRefs <= 0 { return nil // pipeline sync disabled (CI_WATCH_MAX_REFS=0) } @@ -183,7 +183,7 @@ func (s *Service) PrunePipelineCapState(live map[uuid.UUID]struct{}) { // eviction: true when a pipeline was cached OR the fetch/upsert failed (preserve // the existing row and retry next tick), false only on ErrNoPipeline (genuinely // no CI, so any stale row is allowed to evict). -func (s *Service) syncOneRef(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, ref string, mrIID pgtype.Int8, f forge.Forge) (keep bool) { +func (s *Service) syncOneRef(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, ref string, mrIID pgtype.Int8, f forge.CodeHost) (keep bool) { var ( p forge.Pipeline err error diff --git a/api/internal/forgesvc/prd_link_patch.go b/api/internal/forgesvc/prd_link_patch.go index 8a8fbf6369..eb6ee2e61c 100644 --- a/api/internal/forgesvc/prd_link_patch.go +++ b/api/internal/forgesvc/prd_link_patch.go @@ -32,7 +32,7 @@ const PRDLinkPatchBatch = 20 // narrowed to one poller tick and to only the occurrences of this run's own PRD // path; there is nothing further to do about it without a forge primitive that // does not exist. -func (s *Service) SyncPRDLinkPatches(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge) error { +func (s *Service) SyncPRDLinkPatches(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, ch forge.CodeHost, tr forge.IssueTracker) error { rows, err := s.q.ListPRDLinkPatchCandidates(ctx, store.ListPRDLinkPatchCandidatesParams{ RepoID: repoID, Lim: PRDLinkPatchBatch, @@ -44,7 +44,7 @@ func (s *Service) SyncPRDLinkPatches(ctx context.Context, repoID uuid.UUID, forg if ctx.Err() != nil { return ctx.Err() } - s.patchOnePRDLink(ctx, forgeProjectID, f, c) + s.patchOnePRDLink(ctx, forgeProjectID, ch, tr, c) } return nil } @@ -83,10 +83,10 @@ func (s *Service) SyncPRDLinkPatches(ctx context.Context, repoID uuid.UUID, forg // LATER, loses its patch. Narrow — the follow-up run normally moves the PRD itself // and patches through its own marker — and the alternative is polling an abandoned // open MR forever, which is the unbounded per-tick forge call the PRD forbids. -func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, f forge.Forge, c store.ListPRDLinkPatchCandidatesRow) { +func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, ch forge.CodeHost, tr forge.IssueTracker, c store.ListPRDLinkPatchCandidatesRow) { lg := slog.With("run", c.ID, "issue", c.IssueIid.Int64, "mr", c.MrIid.Int64) - mr, err := f.GetMergeRequest(ctx, forgeProjectID, c.MrIid.Int64) + mr, err := ch.GetMergeRequest(ctx, forgeProjectID, c.MrIid.Int64) if err != nil { // Case 1. A failed read is not evidence about the MR. lg.Warn("PRD link patch: read merge request", "error", err) @@ -156,7 +156,7 @@ func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, f f return } - issue, err := f.GetIssue(ctx, forgeProjectID, c.IssueIid.Int64) + issue, err := tr.GetIssue(ctx, forgeProjectID, c.IssueIid.Int64) if err != nil { // Case 1 again: a failed read is not evidence about the description. lg.Warn("PRD link patch: read issue", "error", err) @@ -177,7 +177,7 @@ func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, f f return } - if err := f.UpdateIssueDescription(ctx, forgeProjectID, c.IssueIid.Int64, desc); err != nil { + if err := tr.UpdateIssueDescription(ctx, forgeProjectID, c.IssueIid.Int64, desc); err != nil { // Leave the marker: the write is what we are retrying. lg.Warn("PRD link patch: update issue description", "error", err) return diff --git a/api/internal/forgesvc/prd_link_patch_test.go b/api/internal/forgesvc/prd_link_patch_test.go index 525e2ccf63..db0ee518e8 100644 --- a/api/internal/forgesvc/prd_link_patch_test.go +++ b/api/internal/forgesvc/prd_link_patch_test.go @@ -73,7 +73,7 @@ func enumerated(t *testing.T, st *fakeStore) { func runPRDPatch(t *testing.T, s *Service, f *fakeForge) { t.Helper() - if err := s.SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f); err != nil { + if err := s.SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f, f); err != nil { t.Fatalf("SyncPRDLinkPatches: %v", err) } } @@ -352,7 +352,7 @@ func TestPRDLinkPatchAsksForTheBoundedBatch(t *testing.T) { func TestPRDLinkPatchReturnsEnumerationErrors(t *testing.T) { st := &fakeStore{prdCandidatesErr: errors.New("db down")} f := &fakeForge{} - if err := newTestService(st).SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f); err == nil { + if err := newTestService(st).SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f, f); err == nil { t.Fatal("an enumeration failure must surface to the poller") } } diff --git a/api/internal/forgesvc/projectsync.go b/api/internal/forgesvc/projectsync.go index 0f5fd50cb7..3ac7d96b03 100644 --- a/api/internal/forgesvc/projectsync.go +++ b/api/internal/forgesvc/projectsync.go @@ -144,7 +144,7 @@ type ProjectSyncSettings interface { // the two forward-hooked call sites (D6), so a reverse label write does not // re-project back onto Status. type ProjectMover interface { - AutoMove(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) + AutoMove(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) } // ProjectSyncService adopts and seeds an existing GitHub Projects v2 board against @@ -316,7 +316,7 @@ func (s *ProjectSyncService) ProjectSyncStatus(ctx context.Context, repoID uuid. // An introspection-unsupported forge (no scopes header) proceeds best-effort — the // first mutation surfaces any real permission error. A transient introspection // error is surfaced so the caller does not silently proceed on unknown scopes. -func ensureProjectScope(ctx context.Context, f forge.Forge) error { +func ensureProjectScope(ctx context.Context, f forge.CodeHost) error { info, err := f.TokenInfo(ctx) if err != nil { if errors.Is(err, forge.ErrTokenIntrospectionUnsupported) { diff --git a/api/internal/forgesvc/projectsync_reverse_test.go b/api/internal/forgesvc/projectsync_reverse_test.go index f8e8e33133..0a169806f3 100644 --- a/api/internal/forgesvc/projectsync_reverse_test.go +++ b/api/internal/forgesvc/projectsync_reverse_test.go @@ -31,7 +31,7 @@ type moveCall struct { target string } -func (m *fakeMover) AutoMove(_ context.Context, _ forge.Forge, _ int64, issue store.Issue, _ []store.BoardColumn, target string) (store.Issue, error) { +func (m *fakeMover) AutoMove(_ context.Context, _ forge.IssueTracker, _ int64, issue store.Issue, _ []store.BoardColumn, target string) (store.Issue, error) { m.calls = append(m.calls, moveCall{issueIID: issue.ForgeIssueIid, target: target}) return issue, m.err } diff --git a/api/internal/forgesvc/projectsync_sync.go b/api/internal/forgesvc/projectsync_sync.go index 8e7a8b5098..ee1075ec6c 100644 --- a/api/internal/forgesvc/projectsync_sync.go +++ b/api/internal/forgesvc/projectsync_sync.go @@ -626,7 +626,7 @@ type plannedMove struct { // // A single genuine user drag (destructive count 1) always passes both gates. See // ReverseSync's doc for the convergence invariant the marker advance preserves. -func (s *ProjectSyncService) reverseDiff(ctx context.Context, repo store.GetRepoByIDRow, f forge.Forge, live []forge.ProjectV2ItemStatus, optionColumn map[string]string, issuesByIID map[int64]store.Issue, itemsByIID map[int64]store.GithubProjectItem, columns []store.BoardColumn) { +func (s *ProjectSyncService) reverseDiff(ctx context.Context, repo store.GetRepoByIDRow, f forge.IssueTracker, live []forge.ProjectV2ItemStatus, optionColumn map[string]string, issuesByIID map[int64]store.Issue, itemsByIID map[int64]store.GithubProjectItem, columns []store.BoardColumn) { repoID := repo.ID // Column-name → position map, so board.ResolveColumn can report each issue's CURRENT diff --git a/api/internal/forgesvc/repo_sides.go b/api/internal/forgesvc/repo_sides.go new file mode 100644 index 0000000000..b5d6378df4 --- /dev/null +++ b/api/internal/forgesvc/repo_sides.go @@ -0,0 +1,33 @@ +package forgesvc + +import ( + "context" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +// IssueTrackerForRepo returns the issue side of a repo (D8 of the IssueTracker / +// CodeHost split). Phase 1: the repo's own forge driver, built exactly as +// ForgeForConnection builds it. Phase 2 will map repoID to an external tracker. +// The caller has already resolved the connection through its own owner-scoped +// lookup, so forgeType, baseURL and tokenCiphertext come from that row. +func (s *Service) IssueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { + f, err := s.ForgeForConnection(forgeType, baseURL, tokenCiphertext) + if err != nil { + return nil, err + } + return f, nil +} + +// CodeHostForRepo returns the code-host side of a repo: always the repo's own +// forge driver, built as ForgeForConnection builds it. See IssueTrackerForRepo +// for the connection-resolution contract. +func (s *Service) CodeHostForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { + f, err := s.ForgeForConnection(forgeType, baseURL, tokenCiphertext) + if err != nil { + return nil, err + } + return f, nil +} diff --git a/api/internal/forgesvc/repo_sides_test.go b/api/internal/forgesvc/repo_sides_test.go new file mode 100644 index 0000000000..e346ef82d0 --- /dev/null +++ b/api/internal/forgesvc/repo_sides_test.go @@ -0,0 +1,83 @@ +package forgesvc + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/secretbox" +) + +type sidesFake struct{ forgetest.BaseFake } + +func TestRepoSidesResolveRepoForge(t *testing.T) { + const token = "plain-token-value" + const baseURL = "https://forge.example.test" + box, err := secretbox.New([]byte("0123456789abcdefghijklmnopqrstuv")) + if err != nil { + t.Fatal(err) + } + fake := &sidesFake{} + var gotType forge.Type + var gotURL, gotToken string + s := NewWithForgeBuilder(nil, box, time.Second, nil, func(kind forge.Type, u, pat string, _ time.Duration) (forge.Forge, error) { + gotType, gotURL, gotToken = kind, u, pat + return fake, nil + }) + sealed, err := s.EncryptToken(token) + if err != nil { + t.Fatal(err) + } + repoID := uuid.New() + + tr, err := s.IssueTrackerForRepo(context.Background(), repoID, string(forge.TypeGitLab), baseURL, sealed) + if err != nil { + t.Fatal(err) + } + if tr != forge.IssueTracker(fake) { + t.Errorf("IssueTrackerForRepo = %v, want the built driver", tr) + } + if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != token { + t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) + } + + gotType, gotURL, gotToken = "", "", "" + ch, err := s.CodeHostForRepo(context.Background(), repoID, string(forge.TypeGitLab), baseURL, sealed) + if err != nil { + t.Fatal(err) + } + if ch != forge.CodeHost(fake) { + t.Errorf("CodeHostForRepo = %v, want the built driver", ch) + } + if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != token { + t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) + } +} + +func TestRepoSidesPassBuilderErrorThrough(t *testing.T) { + box, err := secretbox.New([]byte("0123456789abcdefghijklmnopqrstuv")) + if err != nil { + t.Fatal(err) + } + boom := errors.New("builder failed") + s := NewWithForgeBuilder(nil, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { + return nil, boom + }) + sealed, err := s.EncryptToken("t") + if err != nil { + t.Fatal(err) + } + tr, err := s.IssueTrackerForRepo(context.Background(), uuid.New(), string(forge.TypeGitLab), "https://x.test", sealed) + if !errors.Is(err, boom) || tr != nil { + t.Errorf("IssueTrackerForRepo = (%v, %v), want (nil, builder error)", tr, err) + } + ch, err := s.CodeHostForRepo(context.Background(), uuid.New(), string(forge.TypeGitLab), "https://x.test", sealed) + if !errors.Is(err, boom) || ch != nil { + t.Errorf("CodeHostForRepo = (%v, %v), want (nil, builder error)", ch, err) + } +} diff --git a/api/internal/forgesvc/service.go b/api/internal/forgesvc/service.go index 8c64fda397..8f3c071c99 100644 --- a/api/internal/forgesvc/service.go +++ b/api/internal/forgesvc/service.go @@ -432,7 +432,7 @@ func (s *Service) recordFindingGroupMatches(ctx context.Context, repoID uuid.UUI // never triggers the fetch. Operations on other pages are matched as FullSync // rotates to them. A failed fetch returns errFindingGroupScanIncomplete with // nothing recorded, so every claim stays. -func (s *Service) reconcileFindingGroupMarkers(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge, pending []store.FindingGroupClaimOperation) error { +func (s *Service) reconcileFindingGroupMarkers(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.IssueTracker, pending []store.FindingGroupClaimOperation) error { if s.groupDB == nil { return nil } @@ -509,7 +509,7 @@ func (s *Service) ForgeForConnection(forgeType, baseURL string, tokenCiphertext // partially change remote labels before returning an error. No compensation is // attempted. The returned issue is the re-cached row. Caller guards own closed // issue and manual-drag policy. -func (s *Service) AutoMove(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) { +func (s *Service) AutoMove(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) { var current []string if err := json.Unmarshal(issue.Labels, ¤t); err != nil { current = []string{} @@ -582,7 +582,7 @@ func (s *Service) AutoMove(ctx context.Context, f forge.Forge, forgeProjectID in // success updates the cache: apply uses a current-row delta, removal uses // RemoveCachedIssueLabel and never inserts a missing row. A forge error leaves // the cache untouched, but remote labels may have partially changed. -func (s *Service) SetIssueLabel(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue, label, color string, apply bool) (store.Issue, error) { +func (s *Service) SetIssueLabel(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue, label, color string, apply bool) (store.Issue, error) { var current []string if err := json.Unmarshal(issue.Labels, ¤t); err != nil { current = []string{} @@ -777,7 +777,7 @@ func (m Marks) Advance(next Marks) Marks { // forge error. There is deliberately no "the extra fetch is best-effort, log and // continue" path: a soft-fail would also report a mark for a window nobody read // (Decision 11a). -func (s *Service) FullSync(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge) (Marks, error) { +func (s *Service) FullSync(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.IssueTracker) (Marks, error) { pendingGroups, finishGroups, advanceGroups, pendingErr := s.pendingFindingGroups(ctx, repoID) defer finishGroups() if pendingErr != nil { @@ -893,7 +893,7 @@ func (s *Service) FullSync(ctx context.Context, repoID uuid.UUID, forgeProjectID // misconfig ValidateMerged forbids): those issues are already covered by the uzi // fetch, so a third round trip would be pure duplicate work. Its mark then never // advances, which is correct — an unissued fetch is no evidence. -func (s *Service) IncrementalSync(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.Forge, m Marks) (Marks, error) { +func (s *Service) IncrementalSync(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, f forge.IssueTracker, m Marks) (Marks, error) { _, finishGroups, _, pendingErr := s.pendingFindingGroups(ctx, repoID) defer finishGroups() if pendingErr != nil { diff --git a/api/internal/handler/sweep_uzi_gate_livedb_test.go b/api/internal/handler/sweep_uzi_gate_livedb_test.go index 4046316b4c..e036d2a72e 100644 --- a/api/internal/handler/sweep_uzi_gate_livedb_test.go +++ b/api/internal/handler/sweep_uzi_gate_livedb_test.go @@ -68,7 +68,7 @@ func (sweepGateForge) ListIssueComments(context.Context, int64, int64) ([]forge. type sweepGateBuilder struct{ f forge.Forge } -func (sweepGateBuilder) SetIssueLabel(context.Context, forge.Forge, int64, store.Issue, string, string, bool) (store.Issue, error) { +func (sweepGateBuilder) SetIssueLabel(context.Context, forge.IssueTracker, int64, store.Issue, string, string, bool) (store.Issue, error) { panic("unexpected label removal") } diff --git a/api/internal/poller/poller.go b/api/internal/poller/poller.go index 91862bdd27..894cbf98ef 100644 --- a/api/internal/poller/poller.go +++ b/api/internal/poller/poller.go @@ -437,7 +437,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // Runs only after a successful issue sync (an early return above skips it when // the forge is unreachable). Per-candidate errors are log-and-skipped inside; // only a candidate-enumeration failure surfaces here. - if err := e.svc.SyncMRStates(ctx, r.ID, r.ForgeProjectID, f); err != nil { + if err := e.svc.SyncMRStates(ctx, r.ID, r.ForgeProjectID, f, f); err != nil { slog.Error("poller: sync MR states", "repo", r.PathWithNamespace, "error", err) } @@ -459,7 +459,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // above already guarantee. It makes NO use of the issue cache (it reads the // description live via GetIssue, since the cache has no description column), so // unlike SyncFiledIssueCloses it does not depend on the sync ordering. - if err := e.svc.SyncPRDLinkPatches(ctx, r.ID, r.ForgeProjectID, f); err != nil { + if err := e.svc.SyncPRDLinkPatches(ctx, r.ID, r.ForgeProjectID, f, f); err != nil { slog.Error("poller: sync PRD link patches", "repo", r.PathWithNamespace, "error", err) } diff --git a/api/internal/privcheck/checker.go b/api/internal/privcheck/checker.go index 6d8de4e7e8..3a7338fcac 100644 --- a/api/internal/privcheck/checker.go +++ b/api/internal/privcheck/checker.go @@ -20,7 +20,7 @@ const ( defaultConcurrency = 4 ) -// Checker computes privilege reports against a Forge. It holds no state beyond +// Checker computes privilege reports against a forge.CodeHost. It holds no state beyond // its tunables and is safe to share. type Checker struct { warnWindow time.Duration diff --git a/api/internal/runlifecycle/lifecycle.go b/api/internal/runlifecycle/lifecycle.go index ecd46df6b8..b13b15c4a4 100644 --- a/api/internal/runlifecycle/lifecycle.go +++ b/api/internal/runlifecycle/lifecycle.go @@ -74,7 +74,7 @@ type Store interface { // forge-first single-column move. *forgesvc.Service satisfies it. type Mover interface { ForgeForConnection(forgeType, baseURL string, tokenCiphertext []byte) (forge.Forge, error) - AutoMove(ctx context.Context, f forge.Forge, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) + AutoMove(ctx context.Context, f forge.IssueTracker, forgeProjectID int64, issue store.Issue, columns []store.BoardColumn, target string) (store.Issue, error) } // Projector is an OPTIONAL collaborator that projects a uzi-originated column move @@ -482,8 +482,10 @@ func (l *Lifecycle) maybeTerminalComment(ctx context.Context, runID uuid.UUID, s slog.Warn("run lifecycle: build forge for terminal comment", "run", runID, "error", err) return } + // Only the issue side of the driver is used here. + var tracker forge.IssueTracker = f body := l.terminalCommentBody(status, runID, mc) - if _, err := f.CreateIssueNote(ctx, mc.ForgeProjectID, mc.IssueIid.Int64, body); err != nil { + if _, err := tracker.CreateIssueNote(ctx, mc.ForgeProjectID, mc.IssueIid.Int64, body); err != nil { // Already PAT-redacted by the driver. Recorded but lost (never retried). slog.Warn("run lifecycle: post autopilot terminal comment", "run", runID, "error", err) } diff --git a/api/internal/runlifecycle/lifecycle_test.go b/api/internal/runlifecycle/lifecycle_test.go index 50ae8b2cb8..116539d926 100644 --- a/api/internal/runlifecycle/lifecycle_test.go +++ b/api/internal/runlifecycle/lifecycle_test.go @@ -99,7 +99,7 @@ func (m *fakeMover) ForgeForConnection(string, string, []byte) (forge.Forge, err } return m.forge, nil } -func (m *fakeMover) AutoMove(_ context.Context, _ forge.Forge, _ int64, issue store.Issue, _ []store.BoardColumn, target string) (store.Issue, error) { +func (m *fakeMover) AutoMove(_ context.Context, _ forge.IssueTracker, _ int64, issue store.Issue, _ []store.BoardColumn, target string) (store.Issue, error) { m.moves = append(m.moves, target) if m.autoMoveErr != nil { return store.Issue{}, m.autoMoveErr diff --git a/api/internal/schedsvc/ci_watch_test.go b/api/internal/schedsvc/ci_watch_test.go index c3d1da1a75..1431c884e1 100644 --- a/api/internal/schedsvc/ci_watch_test.go +++ b/api/internal/schedsvc/ci_watch_test.go @@ -101,7 +101,7 @@ type ciBuilder struct { scrubErr error } -func (b *ciBuilder) SetIssueLabel(context.Context, forge.Forge, int64, store.Issue, string, string, bool) (store.Issue, error) { +func (b *ciBuilder) SetIssueLabel(context.Context, forge.IssueTracker, int64, store.Issue, string, string, bool) (store.Issue, error) { panic("unexpected label removal") } func (b *ciBuilder) CIArtifactScrubber([]byte) (func(string) string, error) { diff --git a/api/internal/schedsvc/scheduler.go b/api/internal/schedsvc/scheduler.go index 78eb5a2a2d..69b3f58c37 100644 --- a/api/internal/schedsvc/scheduler.go +++ b/api/internal/schedsvc/scheduler.go @@ -177,7 +177,7 @@ func (e *storedOverrideLookupError) Unwrap() error { return e.cause } // seam selfimprove/autopilot use. *forgesvc.Service satisfies it. type ForgeBuilder interface { CIArtifactScrubber([]byte) (func(string) string, error) - SetIssueLabel(context.Context, forge.Forge, int64, store.Issue, string, string, bool) (store.Issue, error) + SetIssueLabel(context.Context, forge.IssueTracker, int64, store.Issue, string, string, bool) (store.Issue, error) ForgeForConnection(forgeType, baseURL string, tokenCiphertext []byte) (forge.Forge, error) } diff --git a/api/internal/schedsvc/scheduler_test.go b/api/internal/schedsvc/scheduler_test.go index 1ee4b715b3..a6b4361780 100644 --- a/api/internal/schedsvc/scheduler_test.go +++ b/api/internal/schedsvc/scheduler_test.go @@ -588,7 +588,7 @@ type fakeBuilder struct { err error } -func (b *fakeBuilder) SetIssueLabel(context.Context, forge.Forge, int64, store.Issue, string, string, bool) (store.Issue, error) { +func (b *fakeBuilder) SetIssueLabel(context.Context, forge.IssueTracker, int64, store.Issue, string, string, bool) (store.Issue, error) { panic("unexpected label removal") } From 58ffd31e1f9cc45ec813475c1933cbbdef8fc4e0 Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 16:30:10 +0000 Subject: [PATCH 05/16] forgesvc: rename repo_sides_test plaintext const (gosec G101) Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/forgesvc/repo_sides_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/internal/forgesvc/repo_sides_test.go b/api/internal/forgesvc/repo_sides_test.go index e346ef82d0..ced96b9a81 100644 --- a/api/internal/forgesvc/repo_sides_test.go +++ b/api/internal/forgesvc/repo_sides_test.go @@ -16,7 +16,7 @@ import ( type sidesFake struct{ forgetest.BaseFake } func TestRepoSidesResolveRepoForge(t *testing.T) { - const token = "plain-token-value" + const plain = "plain-value" const baseURL = "https://forge.example.test" box, err := secretbox.New([]byte("0123456789abcdefghijklmnopqrstuv")) if err != nil { @@ -29,7 +29,7 @@ func TestRepoSidesResolveRepoForge(t *testing.T) { gotType, gotURL, gotToken = kind, u, pat return fake, nil }) - sealed, err := s.EncryptToken(token) + sealed, err := s.EncryptToken(plain) if err != nil { t.Fatal(err) } @@ -42,7 +42,7 @@ func TestRepoSidesResolveRepoForge(t *testing.T) { if tr != forge.IssueTracker(fake) { t.Errorf("IssueTrackerForRepo = %v, want the built driver", tr) } - if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != token { + if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != plain { t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) } @@ -54,7 +54,7 @@ func TestRepoSidesResolveRepoForge(t *testing.T) { if ch != forge.CodeHost(fake) { t.Errorf("CodeHostForRepo = %v, want the built driver", ch) } - if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != token { + if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != plain { t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) } } From 693f00301a761e382c10407f6705bb6ac2fdb82f Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 16:37:30 +0000 Subject: [PATCH 06/16] forge M2 U3a: workersvc callers on their half Settle, prerequisite-closure, publication-proof, CI-fix binding and self-improve MR reads go through CodeHost; semantic drift, proposal filing and task continuation issue reads go through IssueTracker. The connection factory interface still returns the union. Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/workersvc/archive_redundancy.go | 7 ++++--- api/internal/workersvc/ci_fix_binding.go | 3 ++- api/internal/workersvc/claim_assembly.go | 3 ++- api/internal/workersvc/completed_publication.go | 5 +++-- api/internal/workersvc/composite.go | 3 ++- api/internal/workersvc/prerequisite_closure_cleanup.go | 2 +- api/internal/workersvc/prerequisite_closure_identity.go | 4 ++-- api/internal/workersvc/proposal_filing.go | 4 +++- api/internal/workersvc/publication_context.go | 3 ++- api/internal/workersvc/semantic_drift.go | 2 +- api/internal/workersvc/settle.go | 4 ++-- api/internal/workersvc/task.go | 3 ++- 12 files changed, 26 insertions(+), 17 deletions(-) diff --git a/api/internal/workersvc/archive_redundancy.go b/api/internal/workersvc/archive_redundancy.go index 428dabafa7..c69e32a644 100644 --- a/api/internal/workersvc/archive_redundancy.go +++ b/api/internal/workersvc/archive_redundancy.go @@ -219,11 +219,12 @@ func (p *archiveRedundancyProver) prove(ctx context.Context, w store.Worker, run if err != nil { return retain(apitypes.RecoveryRedundancyAncestryUnknown), nil } - head, reason := proveCompletedPublication(proofCtx, f, id) + var ch forge.CodeHost = f + head, reason := proveCompletedPublication(proofCtx, ch, id) if reason != "" { return refuse(reason), nil } - if reason := proveRedundantCoverage(proofCtx, f, id.ProjectID, head, in, req.CurrentSha, view.PrerequisiteShas); reason != "" { + if reason := proveRedundantCoverage(proofCtx, ch, id.ProjectID, head, in, req.CurrentSha, view.PrerequisiteShas); reason != "" { return refuse(reason), nil } @@ -680,7 +681,7 @@ func redundancyProvedCount(in redundancyInputs, prerequisites []string) int { // proveRedundantCoverage asks the forge, once per distinct commit, whether it is an ancestor of // head. Only an error-free Ancestor answer is proof; a NotAncestor answer names the class that // failed, and an error, timeout or inconclusive answer is never read as coverage. -func proveRedundantCoverage(ctx context.Context, f forge.Forge, projectID int64, head string, in redundancyInputs, currentSha string, prerequisites []string) string { +func proveRedundantCoverage(ctx context.Context, f forge.CodeHost, projectID int64, head string, in redundancyInputs, currentSha string, prerequisites []string) string { cache := map[string]forge.Ancestry{} ask := func(sha string) (forge.Ancestry, string) { if sha == head { diff --git a/api/internal/workersvc/ci_fix_binding.go b/api/internal/workersvc/ci_fix_binding.go index 475ba6f3f7..486c0bf7d5 100644 --- a/api/internal/workersvc/ci_fix_binding.go +++ b/api/internal/workersvc/ci_fix_binding.go @@ -130,6 +130,7 @@ func (s *Service) resolveCIFixBinding(ctx context.Context, row store.GetRepoForU if err != nil { return pgtype.Int8{}, nil, ciFixLookupFailed("forge for connection", err) } + var ch forge.CodeHost = f var ( open []int64 @@ -137,7 +138,7 @@ func (s *Service) resolveCIFixBinding(ctx context.Context, row store.GetRepoForU reason string ) for _, iid := range cands { - sum, gerr := f.GetMergeRequestSummary(ctx, row.ForgeProjectID, iid) + sum, gerr := ch.GetMergeRequestSummary(ctx, row.ForgeProjectID, iid) switch { case gerr != nil && errors.Is(gerr, forge.ErrMergeRequestNotFound): if reason == "" { diff --git a/api/internal/workersvc/claim_assembly.go b/api/internal/workersvc/claim_assembly.go index ade52daaf7..a73107f6fb 100644 --- a/api/internal/workersvc/claim_assembly.go +++ b/api/internal/workersvc/claim_assembly.go @@ -1207,6 +1207,7 @@ func (s *Service) selfImproveOpenMRs(ctx context.Context, run store.Run, rc stor slog.Warn("self_improve claim: build forge for open-MR set", "run", run.ID.String(), "error", err) return nil } + var ch forge.CodeHost = f rows, err := s.q.RecentSelfImproveMRRunsForRepo(ctx, store.RecentSelfImproveMRRunsForRepoParams{ RepoID: uuid.UUID(run.RepoID.Bytes), Lim: maxOpenSelfImproveMRCandidates, @@ -1220,7 +1221,7 @@ func (s *Service) selfImproveOpenMRs(ctx context.Context, run store.Run, rc stor if row.ID == run.ID || !row.MrIid.Valid { continue // exclude self and any row without an MR iid } - mr, err := f.GetMergeRequest(ctx, rc.ForgeProjectID, row.MrIid.Int64) + mr, err := ch.GetMergeRequest(ctx, rc.ForgeProjectID, row.MrIid.Int64) if err != nil { // Best-effort: this is advisory context, not the strict fire-time cap, so a // per-candidate forge error skips only this candidate and the loop continues. diff --git a/api/internal/workersvc/completed_publication.go b/api/internal/workersvc/completed_publication.go index 89424507ab..dfb667712d 100644 --- a/api/internal/workersvc/completed_publication.go +++ b/api/internal/workersvc/completed_publication.go @@ -155,7 +155,7 @@ func completedPublicationError(ctx context.Context, err error) string { return "ancestry_unknown" } -func proveCompletedPublication(ctx context.Context, f forge.Forge, id apitypes.CompletionIdentity) (string, string) { +func proveCompletedPublication(ctx context.Context, f forge.CodeHost, id apitypes.CompletionIdentity) (string, string) { if id.MRIID == nil || *id.MRIID <= 0 { return "", "mr_missing" } @@ -253,7 +253,8 @@ func (s *Service) verifyCompletedPublication(ctx context.Context, w store.Worker if err != nil { return refuse("ancestry_unknown") } - head, reason := proveCompletedPublication(proofCtx, f, id) + var ch forge.CodeHost = f + head, reason := proveCompletedPublication(proofCtx, ch, id) if reason != "" { return refuse(reason) } diff --git a/api/internal/workersvc/composite.go b/api/internal/workersvc/composite.go index 9584c924c1..11b11d1c2c 100644 --- a/api/internal/workersvc/composite.go +++ b/api/internal/workersvc/composite.go @@ -147,7 +147,8 @@ func (s *Service) ConfirmProposalForUser(ctx context.Context, userID, runID, pro } } - created, err := f.CreateIssue(ctx, repo.ForgeProjectID, claim.Title, claim.Description, labels) + var tr forge.IssueTracker = f + created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, claim.Title, claim.Description, labels) if err != nil { s.revertProposal(ctx, propID) // err is already PAT-redacted by the driver. diff --git a/api/internal/workersvc/prerequisite_closure_cleanup.go b/api/internal/workersvc/prerequisite_closure_cleanup.go index 9e364c61f5..76e40b4148 100644 --- a/api/internal/workersvc/prerequisite_closure_cleanup.go +++ b/api/internal/workersvc/prerequisite_closure_cleanup.go @@ -11,7 +11,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/store" ) -func (s *Service) prerequisiteList(ctx context.Context, b prerequisiteBinding, driver forge.Forge, row store.RecoveryPrerequisiteClosure, end time.Time) (string, bool, error) { +func (s *Service) prerequisiteList(ctx context.Context, b prerequisiteBinding, driver forge.CodeHost, row store.RecoveryPrerequisiteClosure, end time.Time) (string, bool, error) { if ctx.Err() != nil || end.Sub(s.now()) <= 1500*time.Millisecond { return "", false, context.DeadlineExceeded } diff --git a/api/internal/workersvc/prerequisite_closure_identity.go b/api/internal/workersvc/prerequisite_closure_identity.go index b3615b978f..037c4da6f8 100644 --- a/api/internal/workersvc/prerequisite_closure_identity.go +++ b/api/internal/workersvc/prerequisite_closure_identity.go @@ -26,7 +26,7 @@ func prerequisiteURL(raw string) (*url.URL, error) { return u, nil } -func (s *Service) prerequisiteDriver(b prerequisiteBinding) (forge.Forge, error) { +func (s *Service) prerequisiteDriver(b prerequisiteBinding) (forge.CodeHost, error) { if s.forges == nil { return nil, errors.New("prerequisite: forge unavailable") } @@ -35,7 +35,7 @@ func (s *Service) prerequisiteDriver(b prerequisiteBinding) (forge.Forge, error) // validate observes the stable ID using the same driver as ancestry. Callers // share their existing external deadline with Git and the second observation. -func (b *prerequisiteBinding) validate(ctx context.Context, driver forge.Forge) error { +func (b *prerequisiteBinding) validate(ctx context.Context, driver forge.CodeHost) error { p, err := driver.GetProject(ctx, b.repo.ForgeProjectID) if err != nil { return err diff --git a/api/internal/workersvc/proposal_filing.go b/api/internal/workersvc/proposal_filing.go index 1a33a821b4..272b6fb0cf 100644 --- a/api/internal/workersvc/proposal_filing.go +++ b/api/internal/workersvc/proposal_filing.go @@ -7,6 +7,7 @@ import ( "github.com/jackc/pgx/v5/pgtype" + "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/runkind" "github.com/vtmocanu/uzi/api/internal/schedtmpl" "github.com/vtmocanu/uzi/api/internal/store" @@ -172,7 +173,8 @@ func (s *Service) maybeFileProposal(ctx context.Context, run store.Run, proposal return } - created, err := f.CreateIssue(ctx, repo.ForgeProjectID, proposal.Title, proposal.Body, labels) + var tr forge.IssueTracker = f + created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, proposal.Title, proposal.Body, labels) if err != nil { // err is already PAT-redacted by the driver. The pending audit row stays for // diagnosis; nothing was filed. diff --git a/api/internal/workersvc/publication_context.go b/api/internal/workersvc/publication_context.go index a352a95a2d..69f9bca161 100644 --- a/api/internal/workersvc/publication_context.go +++ b/api/internal/workersvc/publication_context.go @@ -292,7 +292,8 @@ func (s *Service) ResolvePublicationContext(ctx context.Context, w store.Worker, if err != nil { return publicationUnknown("forge_unknown") } - summary, err := f.GetMergeRequestSummary(readCtx, snapshot.ProjectID, answer.Binding.MRIID) + var ch forge.CodeHost = f + summary, err := ch.GetMergeRequestSummary(readCtx, snapshot.ProjectID, answer.Binding.MRIID) if err != nil { return publicationUnknown("forge_unknown") } diff --git a/api/internal/workersvc/semantic_drift.go b/api/internal/workersvc/semantic_drift.go index 7eee3a5fca..21b7e55ccf 100644 --- a/api/internal/workersvc/semantic_drift.go +++ b/api/internal/workersvc/semantic_drift.go @@ -69,7 +69,7 @@ func issueBaseline(frozenText string, cur currentIssue) (baseline string, compos // readCurrentSemanticIssue reads the run's own issue with a bounded deadline. A // forge error never fails the observation: it becomes an explicit unknown reason. -func readCurrentSemanticIssue(ctx context.Context, f forge.Forge, projectID, iid int64) currentIssue { +func readCurrentSemanticIssue(ctx context.Context, f forge.IssueTracker, projectID, iid int64) currentIssue { ctx, cancel := context.WithTimeout(ctx, semanticIssueReadTimeout) defer cancel() issue, err := f.GetIssue(ctx, projectID, iid) diff --git a/api/internal/workersvc/settle.go b/api/internal/workersvc/settle.go index 534704ccf8..dd51d75837 100644 --- a/api/internal/workersvc/settle.go +++ b/api/internal/workersvc/settle.go @@ -314,7 +314,7 @@ func (s *Service) settleCaptureBinding(ctx context.Context, runID, holdID uuid.U // forge seam is ErrForgesUnavailable; a run with no repo/connection behind it is not_eligible // (nothing to prove against); a forge that cannot be built is ancestry_unknown. A non-empty // reason is the retained answer. -func (s *Service) settleForge(ctx context.Context, runID uuid.UUID, logUnknown func(string, error)) (forge.Forge, int64, string, error) { +func (s *Service) settleForge(ctx context.Context, runID uuid.UUID, logUnknown func(string, error)) (forge.CodeHost, int64, string, error) { if s.forges == nil { return nil, 0, "", ErrForgesUnavailable } @@ -355,7 +355,7 @@ func settleHeadReason(head string, err error, step string, logUnknown func(strin // of (or equal to) head. Any explicit not_ancestor wins (not_ancestor); otherwise any error or // inconclusive answer is ancestry_unknown — only an error-free positive answer is proof. "" // means every candidate is proven. -func proveSettleCandidates(ctx context.Context, f forge.Forge, projectID int64, head string, candidates []string, logUnknown func(string, error)) string { +func proveSettleCandidates(ctx context.Context, f forge.CodeHost, projectID int64, head string, candidates []string, logUnknown func(string, error)) string { notAncestor, unknown := false, false asked := map[string]bool{} for _, c := range candidates { diff --git a/api/internal/workersvc/task.go b/api/internal/workersvc/task.go index 379691b306..2a1ae03e1b 100644 --- a/api/internal/workersvc/task.go +++ b/api/internal/workersvc/task.go @@ -96,8 +96,9 @@ func (s *Service) CreateTaskRun(ctx context.Context, userID, repoID uuid.UUID, i if err != nil { return store.Run{}, ErrForgeBuild } + var tr forge.IssueTracker = f fetchCtx, cancel := context.WithTimeout(ctx, 15*time.Second) - issue, err := forge.GetIssueBounded(fetchCtx, f, row.ForgeProjectID, *continuedIssueIID) + issue, err := forge.GetIssueBounded(fetchCtx, tr, row.ForgeProjectID, *continuedIssueIID) cancel() if err != nil { var notFound *forge.IssueNotFoundError From 8b5d5199b4e2f0374112cf72db38ce2c09a5deed Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 17:35:15 +0000 Subject: [PATCH 07/16] forge M2 U3b: schedsvc callers on their half CI watch, CI scout and artifact collection take CodeHost; the self-improve tracking issue and proposal digest go through IssueTracker. Self-improve still builds one driver and hands each call the half it needs. Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/schedsvc/ci_scout.go | 6 +++--- api/internal/schedsvc/ci_watch.go | 2 +- api/internal/schedsvc/ci_watch_artifacts.go | 4 ++-- api/internal/schedsvc/scheduler.go | 3 ++- api/internal/schedsvc/self_improve.go | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/api/internal/schedsvc/ci_scout.go b/api/internal/schedsvc/ci_scout.go index 1b00b0f085..467c4c9e21 100644 --- a/api/internal/schedsvc/ci_scout.go +++ b/api/internal/schedsvc/ci_scout.go @@ -189,7 +189,7 @@ func scoutVisitOrder(main, pr []forge.WorkflowRun, now time.Time) []scoutVisit { return out } -func collectScout(ctx context.Context, f forge.Forge, forgeType string, projectID int64, branch string, now time.Time, configs ...scoutCollectorConfig) scoutData { +func collectScout(ctx context.Context, f forge.CodeHost, forgeType string, projectID int64, branch string, now time.Time, configs ...scoutCollectorConfig) scoutData { config := scoutCollectorConfig{} if len(configs) > 0 { config = configs[0] @@ -279,7 +279,7 @@ func collectScout(ctx context.Context, f forge.Forge, forgeType string, projectI // listScoutRuns reads one page of runs and keeps the terminal ones created in the window, // newest first. -func listScoutRuns(ctx context.Context, f forge.Forge, projectID int64, opts forge.ListWorkflowRunsOptions, now time.Time, d *scoutData) ([]forge.WorkflowRun, bool) { +func listScoutRuns(ctx context.Context, f forge.CodeHost, projectID int64, opts forge.ListWorkflowRunsOptions, now time.Time, d *scoutData) ([]forge.WorkflowRun, bool) { if scoutStop(ctx, nil, d) { return nil, true } @@ -306,7 +306,7 @@ func listScoutRuns(ctx context.Context, f forge.Forge, projectID int64, opts for // collectScoutLogs fetches tails for the top suspected-flake and failure candidates: the // failed execution of each, one per job identity, flakes first, at most scoutLogCalls. -func collectScoutLogs(ctx context.Context, f forge.Forge, projectID int64, d *scoutData, now time.Time) { +func collectScoutLogs(ctx context.Context, f forge.CodeHost, projectID int64, d *scoutData, now time.Time) { st := scoutAggregate(d.Runs, now) var picks []scoutLog seen := map[jobKey]bool{} diff --git a/api/internal/schedsvc/ci_watch.go b/api/internal/schedsvc/ci_watch.go index 1a65f81d15..9ff2abc653 100644 --- a/api/internal/schedsvc/ci_watch.go +++ b/api/internal/schedsvc/ci_watch.go @@ -410,7 +410,7 @@ type ciStartResult struct { // ciStartOne walks the candidates in order, building at most ciMaxSnapshots failure snapshots, // and starts the first run the loop guard and the locked recheck allow. -func (e *Scheduler) ciStartOne(ctx context.Context, sched store.RunSchedule, repo store.GetRepoForUserRow, f forge.Forge, cfg CIRepairConfig, target workersvc.CIWatchTarget, body string, candidates []*ciKey) (ciStartResult, error) { +func (e *Scheduler) ciStartOne(ctx context.Context, sched store.RunSchedule, repo store.GetRepoForUserRow, f forge.CodeHost, cfg CIRepairConfig, target workersvc.CIWatchTarget, body string, candidates []*ciKey) (ciStartResult, error) { var res ciStartResult if conflict, cerr := e.codexOverrideConflict(ctx, sched); cerr != nil { return res, cerr diff --git a/api/internal/schedsvc/ci_watch_artifacts.go b/api/internal/schedsvc/ci_watch_artifacts.go index 3405074f1e..c9e0a8ab97 100644 --- a/api/internal/schedsvc/ci_watch_artifacts.go +++ b/api/internal/schedsvc/ci_watch_artifacts.go @@ -45,7 +45,7 @@ func parseCIArtifacts(raw []byte) ([]string, error) { // ciRevalidate compares to discovery, never to a subsequently fetched revision. // An old red candidate cannot reset a now-green episode or consume an attempt. -func ciRevalidate(ctx context.Context, f forge.Forge, repo store.GetRepoForUserRow, k *ciKey) (bool, error) { +func ciRevalidate(ctx context.Context, f forge.CodeHost, repo store.GetRepoForUserRow, k *ciKey) (bool, error) { if k.run.ID <= 0 || k.run.WorkflowKey == "" || k.run.SHA == "" || k.run.UpdatedAt.IsZero() { return false, nil // equality of missing identity metadata proves no freshness } @@ -108,7 +108,7 @@ func artifactRefusal(err error) (reason string, safeContinue bool) { // ciCollectArtifacts performs no DB writes. One 60-second child context bounds // the sequential list and at most 100 downloads; only pre-body status refusals // permit siblings to continue. Unknown partial consumption stops the collection. -func (e *Scheduler) ciCollectArtifacts(ctx context.Context, sched store.RunSchedule, repo store.GetRepoForUserRow, f forge.Forge, cfg CIRepairConfig, k *ciKey) (*ciartifacts.Manifest, []ciartifacts.Input) { +func (e *Scheduler) ciCollectArtifacts(ctx context.Context, sched store.RunSchedule, repo store.GetRepoForUserRow, f forge.CodeHost, cfg CIRepairConfig, k *ciKey) (*ciartifacts.Manifest, []ciartifacts.Input) { selectors, err := parseCIArtifacts(sched.CiArtifacts) if err != nil || len(selectors) == 0 { return nil, nil diff --git a/api/internal/schedsvc/scheduler.go b/api/internal/schedsvc/scheduler.go index 69b3f58c37..858ed7fd99 100644 --- a/api/internal/schedsvc/scheduler.go +++ b/api/internal/schedsvc/scheduler.go @@ -1419,11 +1419,12 @@ func (e *Scheduler) proposalDigestSection(ctx context.Context, sched store.RunSc e.logger.Warn("scheduler: proposal digest skipped, cannot resolve repo/forge", "schedule", sched.ID.String(), "error", err) return "" } + var tr forge.IssueTracker = f // only the tracker half is needed here // The SAME label workersvc files a proposal under (single source of truth), so the // digest lists exactly this job's own proposal history. label := workersvc.ProposalLabel(catalogSlugOf(sched)) // Zero-value State is StateAll (open + closed) — exactly what dedup needs; do not set it. - issues, err := f.ListIssues(ctx, repo.ForgeProjectID, forge.ListIssuesOptions{Labels: []string{label}}) + issues, err := tr.ListIssues(ctx, repo.ForgeProjectID, forge.ListIssuesOptions{Labels: []string{label}}) if err != nil { e.logger.Warn("scheduler: proposal digest skipped, list issues failed", "schedule", sched.ID.String(), "label", label, "error", err) return "" diff --git a/api/internal/schedsvc/self_improve.go b/api/internal/schedsvc/self_improve.go index c8a97f214f..7cff78240f 100644 --- a/api/internal/schedsvc/self_improve.go +++ b/api/internal/schedsvc/self_improve.go @@ -248,7 +248,7 @@ func (e *Scheduler) notifySelfImproveSkipped(ctx context.Context, userID uuid.UU // project, reusing the newest OPEN issue carrying SelfImproveTrackingLabel or filing a new // one. The label is not a trigger label, so the poller never enqueues a rival issue run on // it. Relocated verbatim (retyped) from selfimprove/engine.go. -func ensureTrackingIssue(ctx context.Context, f forge.Forge, projectID int64) (int64, error) { +func ensureTrackingIssue(ctx context.Context, f forge.IssueTracker, projectID int64) (int64, error) { existing, err := f.ListIssues(ctx, projectID, forge.ListIssuesOptions{Labels: []string{SelfImproveTrackingLabel}}) if err != nil { return 0, fmt.Errorf("list tracking issues: %w", err) From 132b2d1e65f9aa7f49fe60e9a76627ce3b063388 Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 17:38:27 +0000 Subject: [PATCH 08/16] schedsvc: route CI watch and self-improve forge calls through their half fireCIWatch reads workflow runs through a CodeHost local; fireSelfImprove keeps its single build and reads MRs through CodeHost and files the tracking issue through IssueTracker (plan U3b; review N1/N2). Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/schedsvc/ci_watch.go | 5 +++-- api/internal/schedsvc/self_improve.go | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/api/internal/schedsvc/ci_watch.go b/api/internal/schedsvc/ci_watch.go index 9ff2abc653..06c353211d 100644 --- a/api/internal/schedsvc/ci_watch.go +++ b/api/internal/schedsvc/ci_watch.go @@ -187,6 +187,7 @@ func (e *Scheduler) fireCIWatch(ctx context.Context, sched store.RunSchedule) (F if err != nil { return FireOutcome{}, err } + var ch forge.CodeHost = f // CI watch reads only the code-host half branch := strings.TrimSpace(repo.DefaultBranch.String) if !repo.DefaultBranch.Valid || branch == "" { return FireOutcome{Matched: 1, Skips: []Skip{{Reason: SkipNoDefaultBranch}}}, nil @@ -201,7 +202,7 @@ func (e *Scheduler) fireCIWatch(ctx context.Context, sched store.RunSchedule) (F // One budget for the whole fire: the listing and every snapshot's job listing draw on it, // sequentially (the compressed per-fire reservation is not designed for concurrent reads). ctx = forge.WithCIRepairReadLimits(ctx) - runs, err := f.ListWorkflowRuns(ctx, repo.ForgeProjectID, forge.ListWorkflowRunsOptions{Branch: branch, Limit: ciListLimit}) + runs, err := ch.ListWorkflowRuns(ctx, repo.ForgeProjectID, forge.ListWorkflowRunsOptions{Branch: branch, Limit: ciListLimit}) if err != nil { return ciForgeErrOutcome(err) } @@ -288,7 +289,7 @@ func (e *Scheduler) fireCIWatch(ctx context.Context, sched store.RunSchedule) (F return candidates[i].key < candidates[j].key }) if len(candidates) > 0 { - o, ferr := e.ciStartOne(ctx, sched, repo, f, cfg, target, body, candidates) + o, ferr := e.ciStartOne(ctx, sched, repo, ch, cfg, target, body, candidates) if ferr != nil { return FireOutcome{}, ferr } diff --git a/api/internal/schedsvc/self_improve.go b/api/internal/schedsvc/self_improve.go index 7cff78240f..95522c77dd 100644 --- a/api/internal/schedsvc/self_improve.go +++ b/api/internal/schedsvc/self_improve.go @@ -117,6 +117,10 @@ func (e *Scheduler) fireSelfImprove(ctx context.Context, sched store.RunSchedule if err != nil { return FireOutcome{}, fmt.Errorf("build forge driver: %w", err) // transient } + // One driver, two halves: MR reads go through the code host, the tracking issue + // through the issue tracker. + var ch forge.CodeHost = f + var tr forge.IssueTracker = f // 4b. Cap concurrent OPEN self-improve MRs per repo (PRD #686 D10/D12). This runs BEFORE // ensureTrackingIssue so a capped cycle does NO forge write. Open-state is resolved LIVE @@ -138,7 +142,7 @@ func (e *Scheduler) fireSelfImprove(ctx context.Context, sched store.RunSchedule if !c.MrIid.Valid { continue } - mr, err := f.GetMergeRequest(ctx, repo.ForgeProjectID, c.MrIid.Int64) + mr, err := ch.GetMergeRequest(ctx, repo.ForgeProjectID, c.MrIid.Int64) if err != nil { // TRANSIENT — retry next tick. We neither fail-closed (counting an errored MR as // open would wedge the cap forever) nor fail-open (skipping the check would breach @@ -157,7 +161,7 @@ func (e *Scheduler) fireSelfImprove(ctx context.Context, sched store.RunSchedule } // 5. File (or reuse) the tracking issue. A forge error here is transient. - issueIID, err := ensureTrackingIssue(ctx, f, repo.ForgeProjectID) + issueIID, err := ensureTrackingIssue(ctx, tr, repo.ForgeProjectID) if err != nil { return FireOutcome{}, err // transient } From 545668e526574f0bcfeddf54b39df1eb0ff4db7f Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 17:49:03 +0000 Subject: [PATCH 09/16] forge M2 U3c: poller detectors take (ch, tr) Autopilot, CI autofix and MR rework detection take the code host and issue tracker as two parameters: label events, comments and comment intake on the issue side; CI config, failure snapshots, MR comments and author eligibility on the code side. The poll loop still builds one driver and passes it as both halves. Test edits are f -> f, f call sites plus four inline fakes hoisted into locals. Changelog: none Co-Authored-By: Claude Opus 5.5 --- .../application_overflow_retry_livedb_test.go | 4 +- .../poller/application_overflow_retry_test.go | 4 +- api/internal/poller/autopilot.go | 26 ++++---- api/internal/poller/autopilot_filed_test.go | 2 +- api/internal/poller/autopilot_test.go | 4 +- .../poller/autopilot_uzi_label_test.go | 6 +- api/internal/poller/ci_autofix.go | 14 ++-- api/internal/poller/ci_autofix_test.go | 44 ++++++------- api/internal/poller/halt_slack_test.go | 14 ++-- api/internal/poller/mr_review_watch.go | 12 ++-- .../mr_review_watch_eligibility_test.go | 10 +-- .../poller/mr_review_watch_evicted_test.go | 4 +- .../poller/mr_review_watch_livedb_test.go | 8 +-- .../poller/mr_review_watch_namespaces_test.go | 16 ++--- .../mr_review_watch_race_livedb_test.go | 2 +- api/internal/poller/mr_review_watch_test.go | 64 +++++++++---------- api/internal/poller/poller.go | 6 +- .../response_overflow_m2_livedb_test.go | 2 +- .../task_continued_issue_livedb_test.go | 10 +-- 19 files changed, 128 insertions(+), 124 deletions(-) diff --git a/api/internal/poller/application_overflow_retry_livedb_test.go b/api/internal/poller/application_overflow_retry_livedb_test.go index 0fce3c3a8c..24304bfb6a 100644 --- a/api/internal/poller/application_overflow_retry_livedb_test.go +++ b/api/internal/poller/application_overflow_retry_livedb_test.go @@ -74,9 +74,9 @@ func TestR3AutopilotDurableOverflowRetryLiveDB(t *testing.T) { if err != nil { t.Fatal(err) } - NewAutopilot(st, runs, nil).handle(ctx, row, fresh, apLabel, cc, candIssue(7, "alice"), 9, "alice") + NewAutopilot(st, runs, nil).handle(ctx, row, fresh, fresh, apLabel, cc, candIssue(7, "alice"), 9, "alice") } else { - NewAutopilot(st, runs, nil).detect(ctx, row, driver) + NewAutopilot(st, runs, nil).detect(ctx, row, driver, driver) } if len(runs.calls)+len(runs.resolutions) != 0 { t.Fatal("failed capture reached harness/create") diff --git a/api/internal/poller/application_overflow_retry_test.go b/api/internal/poller/application_overflow_retry_test.go index 2df246ff95..6b4d9ba357 100644 --- a/api/internal/poller/application_overflow_retry_test.go +++ b/api/internal/poller/application_overflow_retry_test.go @@ -47,9 +47,9 @@ func TestR3AutopilotOverflowRetry(t *testing.T) { if err != nil { t.Fatal(err) } - NewAutopilot(st, runs, nil).handle(context.Background(), repoRow(), fresh, apLabel, st.cc, candIssue(7, "alice"), 9, "alice") + NewAutopilot(st, runs, nil).handle(context.Background(), repoRow(), fresh, fresh, apLabel, st.cc, candIssue(7, "alice"), 9, "alice") } else { - NewAutopilot(st, runs, nil).detect(context.Background(), repoRow(), f) + NewAutopilot(st, runs, nil).detect(context.Background(), repoRow(), f, f) } if len(runs.calls)+len(runs.resolutions) != 0 { t.Fatal("failed capture reached harness/create") diff --git a/api/internal/poller/autopilot.go b/api/internal/poller/autopilot.go index 6f8a880bdd..144ca24ae9 100644 --- a/api/internal/poller/autopilot.go +++ b/api/internal/poller/autopilot.go @@ -81,7 +81,7 @@ func NewAutopilot(q autopilotStore, runs RunStarter, set SettingsReader) *Autopi // Every per-issue failure is log-and-skipped (the poller convention): a forge blip // or a DB error on one issue must not stall the tick or the other issues, and an // unrecorded event id simply re-evaluates the issue next tick. -func (a *Autopilot) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge) { +func (a *Autopilot) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker) { label := a.autopilotLabel(ctx) // The repo's owner is the only user who can satisfy the "repo connected by that @@ -128,16 +128,16 @@ func (a *Autopilot) detect(ctx context.Context, r store.ListEnabledReposWithConn if ctx.Err() != nil { return } - a.detectOne(ctx, r, f, label, cc, iss) + a.detectOne(ctx, r, ch, tr, label, cc, iss) } } // detectOne resolves the current autopilot-label application for one issue and, if // it is a not-yet-handled transition, hands it to handle. -func (a *Autopilot) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge, label string, cc store.GetAutopilotConnectionContextRow, iss store.ListAutopilotCandidateIssuesRow) { +func (a *Autopilot) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker, label string, cc store.GetAutopilotConnectionContextRow, iss store.ListAutopilotCandidateIssuesRow) { iid := iss.ForgeIssueIid - events, err := f.ListIssueLabelEvents(ctx, r.ForgeProjectID, iid) + events, err := tr.ListIssueLabelEvents(ctx, r.ForgeProjectID, iid) if err != nil { // Already PAT-redacted by the driver. Leave the issue for the next tick. slog.Warn("poller: autopilot label events", "repo", r.PathWithNamespace, "issue", iid, "error", err) @@ -165,14 +165,14 @@ func (a *Autopilot) detectOne(ctx context.Context, r store.ListEnabledReposWithC return } - a.handle(ctx, r, f, label, cc, iss, add.ID, add.Username) + a.handle(ctx, r, ch, tr, label, cc, iss, add.ID, add.Username) } // handle decides the outcome for a fresh label application: swallow (active run), // start a run (eligible), or record-then-comment (not eligible / no PRD link / too // large). eventID is the current application's label-event id; adder is who added // the label. -func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge, label string, cc store.GetAutopilotConnectionContextRow, iss store.ListAutopilotCandidateIssuesRow, eventID int64, adder string) { +func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker, label string, cc store.GetAutopilotConnectionContextRow, iss store.ListAutopilotCandidateIssuesRow, eventID int64, adder string) { iid := iss.ForgeIssueIid // Decision 5: a (re-)application while a run is active is swallowed — no comment, @@ -198,7 +198,7 @@ func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConn author = iss.Author.String } if !consentingOwner(cc, adder, author) { - a.recordThenComment(ctx, r, f, iid, eventID, noEligibleUserComment(label)) + a.recordThenComment(ctx, r, tr, iid, eventID, noEligibleUserComment(label)) return } @@ -206,10 +206,10 @@ func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConn // issue eligibility and one-active-run gates all enforced there), only // with auto_approve set. The description is the fresh forge copy, snapshotted onto // the run exactly as the manual start path snapshots the description it is given. - capture, err := issueinput.Fetch(ctx, f, f, r.ForgeProjectID, iid, cc.BotForgeUserID) + capture, err := issueinput.Fetch(ctx, ch, tr, r.ForgeProjectID, iid, cc.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { - a.recordThenComment(ctx, r, f, iid, eventID, tooLargeComment()) + a.recordThenComment(ctx, r, tr, iid, eventID, tooLargeComment()) return } // Transient forge error: leave the event unrecorded so the next tick retries. @@ -222,7 +222,7 @@ func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConn if selection.LabelSelected { _, err = a.runs.ResolveHarnessForUser(ctx, cc.UserID, selection.Explicit) } else if !cc.HasAnthropicToken { - a.recordThenComment(ctx, r, f, iid, eventID, noEligibleUserComment(label)) + a.recordThenComment(ctx, r, tr, iid, eventID, noEligibleUserComment(label)) return } } @@ -241,7 +241,7 @@ func (a *Autopilot) handle(ctx context.Context, r store.ListEnabledReposWithConn case errors.Is(err, workersvc.ErrDescriptionTooLarge): // The shared createRun cap (unified in M5): the description is too large to // snapshot onto an unattended run — explain it instead of running. - a.recordThenComment(ctx, r, f, iid, eventID, tooLargeComment()) + a.recordThenComment(ctx, r, tr, iid, eventID, tooLargeComment()) case errors.Is(err, workersvc.ErrActiveRunExists): // A run appeared between the pre-check and here: swallow, same as an active run. _ = a.record(ctx, r, iid, eventID) @@ -303,11 +303,11 @@ func (a *Autopilot) record(ctx context.Context, r store.ListEnabledReposWithConn // comment (Decision 6): a crash between the two loses one comment rather than ever // double-posting on a later tick. If the dedup record does not persist, no comment // is posted (a comment without its record could double-post next tick). -func (a *Autopilot) recordThenComment(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge, iid, eventID int64, body string) { +func (a *Autopilot) recordThenComment(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, tr forge.IssueTracker, iid, eventID int64, body string) { if a.record(ctx, r, iid, eventID) != nil { return } - if _, err := f.CreateIssueNote(ctx, r.ForgeProjectID, iid, body); err != nil { + if _, err := tr.CreateIssueNote(ctx, r.ForgeProjectID, iid, body); err != nil { // Already PAT-redacted by the driver. The event id is recorded, so this comment // is simply lost (never retried) — the accepted record-then-comment trade. slog.Warn("poller: autopilot comment", "repo", r.PathWithNamespace, "issue", iid, "error", err) diff --git a/api/internal/poller/autopilot_filed_test.go b/api/internal/poller/autopilot_filed_test.go index a3fd6f343c..8273c3d47b 100644 --- a/api/internal/poller/autopilot_filed_test.go +++ b/api/internal/poller/autopilot_filed_test.go @@ -37,7 +37,7 @@ func filedCandidate(author string) (*apStore, *apRuns, *apFiledForge) { } func detectFiled(st *apStore, runs *apRuns, f *apFiledForge) { - NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f) + NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f, f) } func assertFiledLabelsRetained(t *testing.T, f *apFiledForge) { diff --git a/api/internal/poller/autopilot_test.go b/api/internal/poller/autopilot_test.go index a5e1c36717..bcb27fbb0d 100644 --- a/api/internal/poller/autopilot_test.go +++ b/api/internal/poller/autopilot_test.go @@ -217,7 +217,7 @@ func detectWith(st *apStore, runs *apRuns, f *apForge) { if f.issue.Description == "" && f.issueErr == nil { f.issue = forge.Issue{Description: "see prds/19.md"} } - NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f) + NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f, f) } func lastUpsert(t *testing.T, st *apStore) store.UpsertAutopilotTriggerParams { @@ -420,7 +420,7 @@ func TestAutopilotRunsWithoutPRDLink(t *testing.T) { issue: forge.Issue{Description: "small typo fix, no PRD", Labels: []string{apLabel}}, } - NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f) + NewAutopilot(st, runs, apLabeler{label: apLabel}).detect(context.Background(), repoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("CreateAutopilotRun calls = %d, want 1 (a link-less run must now start)", len(runs.calls)) diff --git a/api/internal/poller/autopilot_uzi_label_test.go b/api/internal/poller/autopilot_uzi_label_test.go index 9755a5fecc..a2251349a6 100644 --- a/api/internal/poller/autopilot_uzi_label_test.go +++ b/api/internal/poller/autopilot_uzi_label_test.go @@ -48,7 +48,8 @@ func TestDetectFiltersCandidatesOnBothLabels(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { st := &apStore{} - NewAutopilot(st, &apRuns{}, tc.lab).detect(context.Background(), repoRow(), &apForge{}) + af := &apForge{} + NewAutopilot(st, &apRuns{}, tc.lab).detect(context.Background(), repoRow(), af, af) if len(st.candParams) != 1 { t.Fatalf("expected one candidate query, got %d", len(st.candParams)) @@ -67,7 +68,8 @@ func TestDetectFiltersCandidatesOnBothLabels(t *testing.T) { // resolves both labels from compiled-in defaults. func TestDetectFallsBackWithoutASettingsReader(t *testing.T) { st := &apStore{} - NewAutopilot(st, &apRuns{}, nil).detect(context.Background(), repoRow(), &apForge{}) + af := &apForge{} + NewAutopilot(st, &apRuns{}, nil).detect(context.Background(), repoRow(), af, af) if len(st.candParams) != 1 { t.Fatalf("expected one candidate query, got %d", len(st.candParams)) diff --git a/api/internal/poller/ci_autofix.go b/api/internal/poller/ci_autofix.go index 1e006f311b..292cda331b 100644 --- a/api/internal/poller/ci_autofix.go +++ b/api/internal/poller/ci_autofix.go @@ -101,7 +101,7 @@ func NewCIAutoFix(q ciAutofixStore, runs CIAutoFixRunStarter, notifier CIAutofix // Every per-candidate failure is log-and-skipped (the poller convention): a forge // blip or a DB error on one ref must not stall the tick or the other refs, and an // unrecorded pipeline simply re-evaluates next tick. -func (d *CIAutoFix) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge) { +func (d *CIAutoFix) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker) { // PRD #914 fail-closed admin gate: CiAutofixEnabled propagates its store error // (three-state read), and the detector — the caller that owns the fail-closed // decision — maps a non-nil error to OFF. Absent → ON is resolved inside @@ -127,13 +127,13 @@ func (d *CIAutoFix) detect(ctx context.Context, r store.ListEnabledReposWithConn if ctx.Err() != nil { return } - d.detectOne(ctx, r, f, cand) + d.detectOne(ctx, r, ch, tr, cand) } } // detectOne runs the loop-guard state machine for one candidate ref (PRD #71 // sequence diagram): dedup, active-run swallow, no-progress / cap halt, or proceed. -func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge, cand store.ListCIAutofixCandidateRefsRow) { +func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker, cand store.ListCIAutofixCandidateRefsRow) { ref := cand.Ref.String // Scheduled-run branches (`uzi/prompt-…`, `uzi/self-improve/…`) do not parse to an @@ -211,7 +211,7 @@ func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithC Sha: cand.Sha, WebUrl: cand.PipelineWebUrl, } - snap, err := workersvc.BuildFailureSnapshot(ctx, f, r.ForgeProjectID, ps, d.maxJobs, d.logTailBytes) + snap, err := workersvc.BuildFailureSnapshot(ctx, ch, r.ForgeProjectID, ps, d.maxJobs, d.logTailBytes) if err != nil { // Already PAT-redacted by the driver. slog.Warn("poller: ci-autofix build snapshot", "repo", r.PathWithNamespace, "ref", ref, "error", err) @@ -253,7 +253,7 @@ func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithC // The issue comment is the primary outward halt signal for an issue-backed // branch (best-effort). if ok { - if _, err := f.CreateIssueNote(ctx, r.ForgeProjectID, iid, haltCommentBody(reason, d.maxAttempts, cand.MrIid)); err != nil { + if _, err := tr.CreateIssueNote(ctx, r.ForgeProjectID, iid, haltCommentBody(reason, d.maxAttempts, cand.MrIid)); err != nil { // Already PAT-redacted by the driver; the latch is set, so the comment is lost, not retried. slog.Warn("poller: ci-autofix halt comment", "repo", r.PathWithNamespace, "ref", ref, "error", err) } @@ -269,7 +269,7 @@ func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithC // 6. PROCEED: start an automatic ci_fix run. Resolve the guard's watch set (default // globs unioned with the project's own ci_config_path); a fetch failure degrades to // the defaults. - projectPath, err := f.ProjectCIConfigPath(ctx, r.ForgeProjectID) + projectPath, err := ch.ProjectCIConfigPath(ctx, r.ForgeProjectID) if err != nil { slog.Warn("poller: ci-autofix ci_config_path", "repo", r.PathWithNamespace, "ref", ref, "error", err) projectPath = "" @@ -298,7 +298,7 @@ func (d *CIAutoFix) detectOne(ctx context.Context, r store.ListEnabledReposWithC return } if ok { - if _, err := f.CreateIssueNote(ctx, r.ForgeProjectID, iid, startCommentBody(ref, cand.PipelineWebUrl)); err != nil { + if _, err := tr.CreateIssueNote(ctx, r.ForgeProjectID, iid, startCommentBody(ref, cand.PipelineWebUrl)); err != nil { slog.Warn("poller: ci-autofix start comment", "repo", r.PathWithNamespace, "ref", ref, "error", err) } } diff --git a/api/internal/poller/ci_autofix_test.go b/api/internal/poller/ci_autofix_test.go index c3ad65f441..0e743c742e 100644 --- a/api/internal/poller/ci_autofix_test.go +++ b/api/internal/poller/ci_autofix_test.go @@ -296,7 +296,7 @@ func TestCIAutofixAdminGateOffNoFire(t *testing.T) { runs := &cfRuns{ops: &ops} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "panic: boom\nexit 1", ops: &ops} - newCFSet(st, runs, nil, cfSettings{enabled: false}).detect(context.Background(), cfRepoRow(), f) + newCFSet(st, runs, nil, cfSettings{enabled: false}).detect(context.Background(), cfRepoRow(), f, f) if st.listed { t.Fatalf("admin gate off must return before listing candidates") @@ -314,7 +314,7 @@ func TestCIAutofixAdminGateErrorFailsClosed(t *testing.T) { runs := &cfRuns{ops: &ops} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "panic: boom\nexit 1", ops: &ops} - newCFSet(st, runs, nil, cfSettings{enabled: true, enabledErr: context.DeadlineExceeded}).detect(context.Background(), cfRepoRow(), f) + newCFSet(st, runs, nil, cfSettings{enabled: true, enabledErr: context.DeadlineExceeded}).detect(context.Background(), cfRepoRow(), f, f) if st.listed { t.Fatalf("a settings read error must fail closed before listing candidates") @@ -331,7 +331,7 @@ func TestCIAutofixProceedStartsRun(t *testing.T) { notifier := &cfNotifier{ops: &ops} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "panic: boom\nexit 1", ops: &ops} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("CreateAutoCIFixRun calls = %d, want 1", len(runs.calls)) @@ -377,7 +377,7 @@ func TestCIAutofixDedupSkipsHandledPipeline(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 || len(st.records) != 0 || len(f.notes) != 0 || len(notifier.calls) != 0 { t.Fatalf("dedup must be a no-op: runs=%d upserts=%d records=%d notes=%d notifs=%d", @@ -397,7 +397,7 @@ func TestCIAutofixCapHalts(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("capped halt must not start a run, got %d", len(runs.calls)) @@ -442,7 +442,7 @@ func TestCIAutofixNoProgressHalts(t *testing.T) { runs := &cfRuns{} notifier := &cfNotifier{} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("no-progress halt must not start a run, got %d", len(runs.calls)) @@ -478,7 +478,7 @@ func TestCIAutofixHaltLatchSilent(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 || len(st.haltSets) != 0 || len(f.notes) != 0 || len(notifier.calls) != 0 { t.Fatalf("halt latch must be silent: runs=%d halts=%d notes=%d notifs=%d", @@ -501,7 +501,7 @@ func TestCIAutofixActiveRunSwallowCaps(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 || len(f.notes) != 0 || len(notifier.calls) != 0 { t.Fatalf("active-run swallow must not run/comment/notify: runs=%d notes=%d notifs=%d", @@ -520,7 +520,7 @@ func TestCIAutofixCreateRaceSwallows(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("expected one create attempt, got %d", len(runs.calls)) @@ -545,7 +545,7 @@ func TestCIAutofixBranchInUseSwallows(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("expected one create attempt, got %d", len(runs.calls)) @@ -574,7 +574,7 @@ func TestCIAutofixIssuelessBranchFiresNoComment(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "panic: boom\nexit 1"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) // The ci_fix run IS created for the issueless branch. if len(runs.calls) != 1 { @@ -612,7 +612,7 @@ func TestCIAutofixIssuelessBranchCapHaltsNoComment(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("capped halt must not start a run, got %d", len(runs.calls)) @@ -649,7 +649,7 @@ func TestCIAutofixHaltLatchWriteFailsNoComment(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(f.notes) != 0 { t.Fatalf("a failed latch write must post NO comment, got %+v", f.notes) @@ -678,7 +678,7 @@ func TestCIAutofixHaltNotifyFailureRetriesNextTick(t *testing.T) { f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom", ops: &ops} cf := newCF(st, runs, notifier) - cf.detect(context.Background(), cfRepoRow(), f) + cf.detect(context.Background(), cfRepoRow(), f, f) if len(st.haltSets) != 0 || len(f.notes) != 0 { t.Fatalf("a failed notify must set no latch and post no comment, got latches=%d notes=%d", len(st.haltSets), len(f.notes)) } @@ -688,7 +688,7 @@ func TestCIAutofixHaltNotifyFailureRetriesNextTick(t *testing.T) { notifier.err = nil ops = ops[:0] - cf.detect(context.Background(), cfRepoRow(), f) + cf.detect(context.Background(), cfRepoRow(), f, f) if got := strings.Join(ops, ","); got != "notify,halt,comment" { t.Fatalf("retry tick ops = %q, want notify,halt,comment", got) } @@ -730,7 +730,7 @@ func TestCIAutofixLatchResetsOnProceed(t *testing.T) { // Step A: the SAME signature on a new pipeline (count 1 < cap 2) → no-progress halt. st.candidates = []store.ListCIAutofixCandidateRefsRow{cfCand(9010)} - d.detect(ctx, cfRepoRow(), fA) + d.detect(ctx, cfRepoRow(), fA, fA) if len(st.haltSets) != 1 { t.Fatalf("step A: expected one halt latch, got %d", len(st.haltSets)) @@ -753,7 +753,7 @@ func TestCIAutofixLatchResetsOnProceed(t *testing.T) { t.Fatalf("test needs two distinct signatures; both hashed to %s", sigA) } st.candidates = []store.ListCIAutofixCandidateRefsRow{cfCand(9020)} - d.detect(ctx, cfRepoRow(), fB) + d.detect(ctx, cfRepoRow(), fB, fB) if len(runs.calls) != 1 { t.Fatalf("step B: expected one proceed/run, got %d", len(runs.calls)) @@ -769,7 +769,7 @@ func TestCIAutofixLatchResetsOnProceed(t *testing.T) { // NOT be suppressed by the earlier no-progress latch. fC := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "error: totally different\nexit 2"} st.candidates = []store.ListCIAutofixCandidateRefsRow{cfCand(9030)} - d.detect(ctx, cfRepoRow(), fC) + d.detect(ctx, cfRepoRow(), fC, fC) if len(runs.calls) != 1 { t.Fatalf("step C: cap halt must not start a run, got %d", len(runs.calls)) @@ -788,7 +788,7 @@ func TestCIAutofixNoCandidatesNoOp(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 || len(st.records) != 0 { t.Fatal("empty candidate set must be a no-op") @@ -830,7 +830,7 @@ func TestCIAutofixPRBindingRefusals(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, runs, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("create attempts = %d, want 1", len(runs.calls)) @@ -857,11 +857,11 @@ func TestCIAutofixUnavailableBindingRetriesNextTick(t *testing.T) { f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} cf := NewCIAutoFix(st, runs, &cfNotifier{}, cfSettings{enabled: true}, 5, 4096, 2, []string{".gitlab-ci.yml"}) - cf.detect(context.Background(), cfRepoRow(), f) + cf.detect(context.Background(), cfRepoRow(), f, f) if len(st.records) != 0 || len(st.upserts) != 0 { t.Fatalf("tick 1 must record nothing: records=%d upserts=%d", len(st.records), len(st.upserts)) } - cf.detect(context.Background(), cfRepoRow(), f) + cf.detect(context.Background(), cfRepoRow(), f, f) if runs.calls != 2 { t.Fatalf("create calls after tick 2 = %d, want 2 (retried)", runs.calls) } diff --git a/api/internal/poller/halt_slack_test.go b/api/internal/poller/halt_slack_test.go index ad0447a717..36f090e2bb 100644 --- a/api/internal/poller/halt_slack_test.go +++ b/api/internal/poller/halt_slack_test.go @@ -39,7 +39,7 @@ func TestCIAutofixHaltPublishesSlackPipelineLink(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(st, &cfRuns{}, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(st, &cfRuns{}, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(notifier.calls) != 1 || notifier.calls[0].kind != "ci_autofix_halted" { t.Fatalf("expected one halted notification, got %+v", notifier.calls) @@ -87,7 +87,7 @@ func TestCIAutofixHaltDropsUnsafePipelineLink(t *testing.T) { notifier := &cfNotifier{} f := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} - newCF(cfCappedStore(cand), &cfRuns{}, notifier).detect(context.Background(), cfRepoRow(), f) + newCF(cfCappedStore(cand), &cfRuns{}, notifier).detect(context.Background(), cfRepoRow(), f, f) if len(notifier.calls) != 1 || notifier.calls[0].slack == nil { t.Fatalf("url %q: expected one halted notification with a Slack render, got %+v", bad, notifier.calls) @@ -106,9 +106,11 @@ func TestCIAutofixHaltSlackOncePerHalt(t *testing.T) { notifier := &cfNotifier{} d := newCF(st, &cfRuns{}, notifier) - d.detect(context.Background(), cfRepoRow(), &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"}) + cf := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} + d.detect(context.Background(), cfRepoRow(), cf, cf) st.candidates = []store.ListCIAutofixCandidateRefsRow{cfCand(9011)} - d.detect(context.Background(), cfRepoRow(), &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"}) + cf2 := &cfForge{jobs: []forge.Job{cfJob()}, logTail: "boom"} + d.detect(context.Background(), cfRepoRow(), cf2, cf2) renders := 0 for _, c := range notifier.calls { @@ -132,7 +134,7 @@ func TestMRReworkHaltPublishesSlackRunLink(t *testing.T) { f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) newMRW(st, &mrwRuns{}, notifier, mrwSettings{enabled: true, capVal: 5, baseURL: "https://uzi.example.com/"}). - detect(context.Background(), mrwRepoRow(), f) + detect(context.Background(), mrwRepoRow(), f, f) if len(notifier.calls) != 1 || notifier.calls[0].kind != "mr_rework_halted" { t.Fatalf("expected one halted notification, got %+v", notifier.calls) @@ -168,7 +170,7 @@ func TestMRReworkHaltSlackNoBaseNoLink(t *testing.T) { notifier := &mrwNotifier{} f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - newMRW(st, &mrwRuns{}, notifier, set).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, &mrwRuns{}, notifier, set).detect(context.Background(), mrwRepoRow(), f, f) if len(notifier.calls) != 1 || notifier.calls[0].slack == nil { t.Fatalf("%s: expected one halted notification with a Slack render, got %+v", name, notifier.calls) diff --git a/api/internal/poller/mr_review_watch.go b/api/internal/poller/mr_review_watch.go index c2b336c4f8..c2b5107a32 100644 --- a/api/internal/poller/mr_review_watch.go +++ b/api/internal/poller/mr_review_watch.go @@ -139,7 +139,7 @@ func (d *MRReviewWatch) assessor() *workersvc.ReviewAssessor { // read error or a false value skips the repo entirely, so a settings blip never // auto-reworks. Every per-candidate failure is log-and-skipped (the poller // convention). -func (d *MRReviewWatch) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge) { +func (d *MRReviewWatch) detect(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker) { // Decision 5 fail-closed: MrReworkEnabled propagates its store error (three-state // read), and the detector — the caller that owns the fail-closed decision — maps a // non-nil error to OFF. Absent → ON is already resolved inside MrReworkEnabled. @@ -177,7 +177,7 @@ func (d *MRReviewWatch) detect(ctx context.Context, r store.ListEnabledReposWith if ctx.Err() != nil { return } - d.detectOne(ctx, r, f, cand, capLimit, trusted) + d.detectOne(ctx, r, ch, tr, cand, capLimit, trusted) } // Reconcile once per repo, best-effort: DeleteMRReworkLedgerNotIn retains rows @@ -200,7 +200,7 @@ func (d *MRReviewWatch) detect(ctx context.Context, r store.ListEnabledReposWith // capLimit → branch free (checked at create time inside CreateAutoMRReworkRunAndAdvance). Any gate // that fails is a silent no-op (no ledger write, no comment) except the capLimit halt, // which comments once. -func (d *MRReviewWatch) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, f forge.Forge, cand store.ListMRReworkCandidatesRow, capLimit int, trusted []settings.TrustedBot) { +func (d *MRReviewWatch) detectOne(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, ch forge.CodeHost, tr forge.IssueTracker, cand store.ListMRReworkCandidatesRow, capLimit int, trusted []settings.TrustedBot) { ref := cand.Ref.String mrIID := cand.MrIid.Int64 @@ -263,7 +263,7 @@ func (d *MRReviewWatch) detectOne(ctx context.Context, r store.ListEnabledReposW return } - comments, err := f.ListMergeRequestComments(ctx, r.ForgeProjectID, mrIID) + comments, err := ch.ListMergeRequestComments(ctx, r.ForgeProjectID, mrIID) if err != nil { // Already PAT-redacted by the driver. slog.Warn("poller: mr-rework list comments", "repo", r.PathWithNamespace, "ref", ref, "error", err) @@ -302,7 +302,7 @@ func (d *MRReviewWatch) detectOne(ctx context.Context, r store.ListEnabledReposW ProjectID: r.ForgeProjectID, BaseURL: r.BaseUrl, BotForgeUserID: cand.BotForgeUserID, - Lookup: f, + Lookup: ch, Trusted: trusted, Comments: comments, Ledger: state, @@ -415,7 +415,7 @@ func (d *MRReviewWatch) detectOne(ctx context.Context, r store.ListEnabledReposW return } if ok { - if _, err := f.CreateIssueNote(ctx, r.ForgeProjectID, issueIID, mrReworkHaltCommentBody(capLimit, mrIID)); err != nil { + if _, err := tr.CreateIssueNote(ctx, r.ForgeProjectID, issueIID, mrReworkHaltCommentBody(capLimit, mrIID)); err != nil { // Already PAT-redacted; the latch is set, so the comment is lost, not retried. slog.Warn("poller: mr-rework halt comment", "repo", r.PathWithNamespace, "ref", ref, "error", err) } diff --git a/api/internal/poller/mr_review_watch_eligibility_test.go b/api/internal/poller/mr_review_watch_eligibility_test.go index a50a707184..36d3cf2d0c 100644 --- a/api/internal/poller/mr_review_watch_eligibility_test.go +++ b/api/internal/poller/mr_review_watch_eligibility_test.go @@ -78,7 +78,7 @@ func (e *ew) tick(comments ...forge.MRComment) int { e.f.comments = comments before := len(e.f.lookups) e.d.set = e.set - e.d.detect(context.Background(), mrwRepoRow(), e.f) + e.d.detect(context.Background(), mrwRepoRow(), e.f, e.f) return len(e.f.lookups) - before } @@ -502,7 +502,7 @@ func TestAnotherMRDoesNotReorderThisMRsQueue(t *testing.T) { } e.f.comments = comments e.d.maxLookups = 2 - e.d.detect(context.Background(), mrwRepoRow(), e.f) + e.d.detect(context.Background(), mrwRepoRow(), e.f, e.f) // Both MRs share the author ids but own separate queues: each saw the same two attempts. orderA := e.st.ras().Order(mrwRepoID, mrwRef) @@ -514,7 +514,7 @@ func TestAnotherMRDoesNotReorderThisMRsQueue(t *testing.T) { } // A tick for MR B alone leaves A's order untouched. e.st.candidates = []store.ListMRReworkCandidatesRow{candB} - e.d.detect(context.Background(), mrwRepoRow(), e.f) + e.d.detect(context.Background(), mrwRepoRow(), e.f, e.f) if got := e.st.ras().Order(mrwRepoID, mrwRef); !slices.Equal(got, want) { t.Fatalf("MR A's queue changed while only MR B was processed: %v", got) } @@ -534,7 +534,7 @@ func TestTrustedBotSkipsLookupAndItsSummaryNeverTriggers(t *testing.T) { finding := e.comment(120, botAuthor, "guard nil here") e.f.comments = []forge.MRComment{finding} e.d.set = e.set - e.d.detect(context.Background(), row, e.f) + e.d.detect(context.Background(), row, e.f, e.f) if len(e.runs.calls) != 1 || len(e.f.lookups) != 0 { t.Fatalf("runs=%d lookups=%v, want the allowlisted bot's finding to fire with no lookup", len(e.runs.calls), e.f.lookups) } @@ -549,7 +549,7 @@ func TestTrustedBotSkipsLookupAndItsSummaryNeverTriggers(t *testing.T) { note.AuthorForgeUserID = botAuthor e.f.comments = []forge.MRComment{note} e.d.set = e.set - e.d.detect(context.Background(), row, e.f) + e.d.detect(context.Background(), row, e.f, e.f) if len(e.runs.calls) != 0 || len(e.st.upserts) != 0 { t.Fatalf("runs=%d upserts=%d, want an allowlisted bot's summary to be inert", len(e.runs.calls), len(e.st.upserts)) } diff --git a/api/internal/poller/mr_review_watch_evicted_test.go b/api/internal/poller/mr_review_watch_evicted_test.go index 3576e36781..48568ddfdc 100644 --- a/api/internal/poller/mr_review_watch_evicted_test.go +++ b/api/internal/poller/mr_review_watch_evicted_test.go @@ -35,7 +35,7 @@ func TestMRReworkEvictedPendingIDIsRemovedAndNextTickDoesNoAssessment(t *testing f := landedForge(comments...) d := newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}) - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("tick 1 created %d runs; nothing in the capped snapshot is new", len(runs.calls)) @@ -51,7 +51,7 @@ func TestMRReworkEvictedPendingIDIsRemovedAndNextTickDoesNoAssessment(t *testing t.Fatal("tick 1 should have assessed the pending id's author") } - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if n := len(f.lookups); n != lookupsAfterTick1 { t.Fatalf("tick 2 made %d more lookups; the removed id must not be assessed again", n-lookupsAfterTick1) } diff --git a/api/internal/poller/mr_review_watch_livedb_test.go b/api/internal/poller/mr_review_watch_livedb_test.go index 41a3917bc6..7e3dcdac71 100644 --- a/api/internal/poller/mr_review_watch_livedb_test.go +++ b/api/internal/poller/mr_review_watch_livedb_test.go @@ -121,7 +121,7 @@ func TestMRReworkEligibilityRoundTripLiveDB(t *testing.T) { } } candidates(1) - watch.detect(ctx, row, f) + watch.detect(ctx, row, f, f) before := ledger(1, 120) if runs.starts != 1 { t.Fatalf("first note: starts=%d", runs.starts) @@ -135,7 +135,7 @@ func TestMRReworkEligibilityRoundTripLiveDB(t *testing.T) { exec("UPDATE users SET mr_rework_enabled=false WHERE id=$1", owner) } candidates(0) - watch.detect(ctx, row, f) + watch.detect(ctx, row, f, f) after := ledger(1, 120) if !reflect.DeepEqual(after, before) { t.Fatalf("ineligible tick changed ledger: before=%+v after=%+v", before, after) @@ -152,7 +152,7 @@ func TestMRReworkEligibilityRoundTripLiveDB(t *testing.T) { exec("UPDATE users SET mr_rework_enabled=true WHERE id=$1", owner) } candidates(1) - watch.detect(ctx, row, f) + watch.detect(ctx, row, f, f) ledger(1, 120) if runs.starts != 1 || runs.attempts != 1 { t.Fatalf("same consumed note duplicated: starts=%d attempts=%d, want 1 and 1 (no new create attempt)", runs.starts, runs.attempts) @@ -160,7 +160,7 @@ func TestMRReworkEligibilityRoundTripLiveDB(t *testing.T) { // The first rework run finishes before the next cycle (one active rework per MR). exec("UPDATE runs SET status='completed' WHERE kind='mr_rework' AND target_run_id=$1", source) f.comments = append(f.comments, mrwComment(121, time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC), mrwHeadSHA)) - watch.detect(ctx, row, f) + watch.detect(ctx, row, f, f) ledger(2, 121) if runs.starts != 2 { t.Fatalf("higher-ID positive control: starts=%d", runs.starts) diff --git a/api/internal/poller/mr_review_watch_namespaces_test.go b/api/internal/poller/mr_review_watch_namespaces_test.go index 81c14d5ad6..11a0229da2 100644 --- a/api/internal/poller/mr_review_watch_namespaces_test.go +++ b/api/internal/poller/mr_review_watch_namespaces_test.go @@ -42,7 +42,7 @@ func TestMRReworkNewCommentInAnotherNamespaceFiresDespiteHigherConsumedID(t *tes mrwNSComment(forge.CommentNSReviewComment, newReview, landed()), ) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("runs = %d, want the new inline comment to fire a rework", len(runs.calls)) @@ -57,7 +57,7 @@ func TestMRReworkNewCommentInAnotherNamespaceFiresDespiteHigherConsumedID(t *tes } // The consumed issue comment alone never re-fires. f2 := landedForge(mrwNSComment(forge.CommentNSIssueComment, issueMark, landed()), mrwNSComment(forge.CommentNSReviewComment, newReview, landed())) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f2) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f2, f2) if len(runs.calls) != 1 { t.Fatalf("runs = %d after a repeat tick, want still 1", len(runs.calls)) } @@ -71,7 +71,7 @@ func TestMRReworkBootstrapIsGivenTheRowAndTheListing(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(100, landed(), mrwHeadSHA), mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.bootstraps) != 1 || runs.bootstraps[0].comments != 2 || runs.bootstraps[0].pre.HighWater != 100 { t.Fatalf("bootstrap calls = %+v, want one with the ledger row read before the listing and both comments", runs.bootstraps) @@ -88,7 +88,7 @@ func TestMRReworkFailedListingNeverBootstraps(t *testing.T) { f := landedForge() f.commentsErr = errors.New("forge unavailable") - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.bootstraps) != 0 || len(runs.calls) != 0 || !reflect.DeepEqual(st.ledgers[mrwRef], legacy) { t.Fatalf("a failed listing must leave the row legacy: bootstraps=%d runs=%d ledger=%+v", len(runs.bootstraps), len(runs.calls), st.ledgers[mrwRef]) @@ -106,8 +106,8 @@ func TestMRReworkBootstrapOverflowWarnsOncePerTickAndFiresNothing(t *testing.T) f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) d := newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}) - d.detect(context.Background(), mrwRepoRow(), f) - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) + d.detect(context.Background(), mrwRepoRow(), f, f) var warns []map[string]any for _, rec := range logRecords(t, logs) { @@ -150,7 +150,7 @@ func TestMRReworkBootstrapLedgerChangedIsSilentAndOtherErrorsWarn(t *testing.T) runs := &mrwRuns{bootstrapErr: tc.err} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || !reflect.DeepEqual(st.ledgers[mrwRef], legacy) { t.Fatalf("runs=%d ledger=%+v, want no run and no write", len(runs.calls), st.ledgers[mrwRef]) @@ -172,7 +172,7 @@ func TestMRReworkCreateLedgerChangedIsSwallowed(t *testing.T) { runs := &mrwRuns{err: workersvc.ErrReworkLedgerChanged} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 || len(st.upserts) != 0 || !reflect.DeepEqual(st.ledgers[mrwRef], row) { t.Fatalf("runs=%d upserts=%d ledger=%+v", len(runs.calls), len(st.upserts), st.ledgers[mrwRef]) diff --git a/api/internal/poller/mr_review_watch_race_livedb_test.go b/api/internal/poller/mr_review_watch_race_livedb_test.go index c596f9d140..bf8fdc4add 100644 --- a/api/internal/poller/mr_review_watch_race_livedb_test.go +++ b/api/internal/poller/mr_review_watch_race_livedb_test.go @@ -95,7 +95,7 @@ func (l *mrwLive) reworkRuns(t *testing.T) int { func (l *mrwLive) detect(capVal int, f *mrwForge) { row := mrwRepoRow() row.ID = l.repo - NewMRReviewWatch(l.q, l.svc, poolQueue{l.pool}, nil, mrwSettings{enabled: true, capVal: capVal}, capVal, 0).detect(l.ctx, row, f) + NewMRReviewWatch(l.q, l.svc, poolQueue{l.pool}, nil, mrwSettings{enabled: true, capVal: capVal}, capVal, 0).detect(l.ctx, row, f, f) } // A manual rework that consumes the comment and completes while the watcher is listing: the diff --git a/api/internal/poller/mr_review_watch_test.go b/api/internal/poller/mr_review_watch_test.go index 3e8c733305..715f48ae6d 100644 --- a/api/internal/poller/mr_review_watch_test.go +++ b/api/internal/poller/mr_review_watch_test.go @@ -573,7 +573,7 @@ func TestMRReworkProceedStartsRun(t *testing.T) { // Two kept comments; max id 120 > high_water 0 → fire, advance to 120. f := landedForge(mrwComment(100, landed(), mrwHeadSHA), mrwComment(120, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("CreateAutoMRReworkRun calls = %d, want 1", len(runs.calls)) @@ -609,7 +609,7 @@ func TestMRReworkPipelineRedNoFire(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(120, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("a red head pipeline must not fire: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -622,7 +622,7 @@ func TestMRReworkNoCachedPipelineNoFire(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(120, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("an absent head pipeline must not fire, got %d runs", len(runs.calls)) @@ -635,7 +635,7 @@ func TestMRReworkReviewNotLandedYoungComment(t *testing.T) { // Comment created NOW → inside the 5-minute quiet period → not landed. f := landedForge(mrwComment(120, time.Now(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a comment still inside the quiet period must not fire, got %d runs", len(runs.calls)) @@ -648,7 +648,7 @@ func TestMRReworkStaleHeadSHANoFire(t *testing.T) { // Landed, but written against a SUPERSEDED head SHA (!= the green pipeline's SHA). f := landedForge(mrwComment(120, landed(), "oldsha00")) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a comment on a stale head SHA must not fire, got %d runs", len(runs.calls)) @@ -665,7 +665,7 @@ func TestMRReworkCommentAtOrBelowHighWaterNoFire(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(100, landed(), mrwHeadSHA), mrwComment(120, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("no comment past the high-water must not fire: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -696,7 +696,7 @@ func TestMRReworkPostManualLedgerStateNoRefire(t *testing.T) { notifier := &mrwNotifier{} f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: cap}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: cap}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("no comment past the manual high-water must not fire, got %d runs", len(runs.calls)) @@ -718,7 +718,7 @@ func TestMRReworkPostManualLedgerStateNoRefire(t *testing.T) { // GATE 4 halts; halt_notified was reset by the manual cycle → it comments once more. f := landedForge(mrwComment(200, landed(), mrwHeadSHA), mrwComment(300, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: cap}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: cap}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a capped MR must not start a run, got %d", len(runs.calls)) @@ -744,7 +744,7 @@ func TestMRReworkStrictlyAboveHighWaterFires(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(100, landed(), mrwHeadSHA), mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("a comment above the high-water must fire, got %d runs", len(runs.calls)) @@ -769,7 +769,7 @@ func TestMRReworkAtCapHaltsOnceThenSilent(t *testing.T) { f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) d := newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}) - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a capped MR must not start a run, got %d", len(runs.calls)) @@ -792,7 +792,7 @@ func TestMRReworkAtCapHaltsOnceThenSilent(t *testing.T) { // Second tick: the latch is set → NO second comment, NO second notify. f2 := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - d.detect(context.Background(), mrwRepoRow(), f2) + d.detect(context.Background(), mrwRepoRow(), f2, f2) if len(f2.notes) != 0 || len(notifier.calls) != 1 || len(st.haltSets) != 1 { t.Fatalf("the halt latch must be silent on the next tick: notes=%d notifs=%d halts=%d", len(f2.notes), len(notifier.calls), len(st.haltSets)) @@ -812,7 +812,7 @@ func TestMRReworkHaltedAndNotifiedSkipsListingAndLookups(t *testing.T) { f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) d := newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}) - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if f.listCalls != 0 || len(f.lookups) != 0 { t.Fatalf("a halted, notified MR must skip the listing and lookups: list=%d lookups=%d", f.listCalls, len(f.lookups)) @@ -825,7 +825,7 @@ func TestMRReworkHaltedAndNotifiedSkipsListingAndLookups(t *testing.T) { // Raising the cap un-halts it: the next tick lists again and proceeds. d2 := newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 6}) f2 := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - d2.detect(context.Background(), mrwRepoRow(), f2) + d2.detect(context.Background(), mrwRepoRow(), f2, f2) if f2.listCalls != 1 || len(runs.calls) != 1 { t.Fatalf("under a raised cap the MR must be listed and reworked: list=%d runs=%d", f2.listCalls, len(runs.calls)) } @@ -844,7 +844,7 @@ func TestMRReworkHaltLatchWriteFailsNoComment(t *testing.T) { notifier := &mrwNotifier{} f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(f.notes) != 0 || len(runs.calls) != 0 { t.Fatalf("a failed latch write must post no comment and start no run: notes=%d runs=%d", len(f.notes), len(runs.calls)) @@ -869,7 +869,7 @@ func TestMRReworkHaltNotifyFailureRetriesNextTick(t *testing.T) { f.ops = &ops d := newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}) - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if len(st.haltSets) != 0 || len(f.notes) != 0 { t.Fatalf("a failed notify must set no latch and post no comment, got latches=%d notes=%d", len(st.haltSets), len(f.notes)) } @@ -879,7 +879,7 @@ func TestMRReworkHaltNotifyFailureRetriesNextTick(t *testing.T) { notifier.err = nil ops = ops[:0] - d.detect(context.Background(), mrwRepoRow(), f) + d.detect(context.Background(), mrwRepoRow(), f, f) if got := strings.Join(ops, ","); got != "notify,halt,comment" { t.Fatalf("retry tick ops = %q, want notify,halt,comment", got) } @@ -900,7 +900,7 @@ func TestMRReworkBranchInUseSwallows(t *testing.T) { notifier := &mrwNotifier{} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("expected one create attempt, got %d", len(runs.calls)) @@ -930,7 +930,7 @@ func TestMRReworkNoCredentialForHarnessSkipsWithoutErrorDetail(t *testing.T) { slog.SetDefault(slog.New(slog.NewJSONHandler(&logs, nil))) t.Cleanup(func() { slog.SetDefault(previous) }) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("expected one create attempt, got %+v", runs.calls) @@ -973,7 +973,7 @@ func TestMRReworkActiveExistsSwallows(t *testing.T) { runs := &mrwRuns{err: workersvc.ErrActiveMRReworkExists} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 || len(st.upserts) != 0 { t.Fatalf("ErrActiveMRReworkExists must swallow with no ledger advance: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -986,7 +986,7 @@ func TestMRReworkAdminGateOffNoFire(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: false, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: false, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.evicts) != 0 { t.Fatalf("admin gate off must be a full no-op (no candidates listed): runs=%d evicts=%d", len(runs.calls), len(st.evicts)) @@ -999,7 +999,7 @@ func TestMRReworkAdminGateErrorFailsClosed(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwComment(140, landed(), mrwHeadSHA)) - newMRW(st, runs, nil, mrwSettings{enabled: true, enabledErr: context.DeadlineExceeded, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, enabledErr: context.DeadlineExceeded, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a settings read error must fail closed (no fire), got %d runs", len(runs.calls)) @@ -1016,7 +1016,7 @@ func TestMRReworkEmptyCandidatesReconcilesRepo(t *testing.T) { runs := &mrwRuns{} f := landedForge() - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a merged/closed MR must not be acted on, got %d runs", len(runs.calls)) @@ -1046,7 +1046,7 @@ func TestMRReworkIssuelessBranchDecoupled(t *testing.T) { // One kept comment, id 120 > high_water 0 → fire. f := landedForge(mrwComment(120, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("an issueless branch below cap must still fire the rework, got %d runs", len(runs.calls)) @@ -1072,7 +1072,7 @@ func TestMRReworkIssuelessBranchDecoupled(t *testing.T) { // A new comment (id 200 > high_water 120) reaches the cap gate. f := landedForge(mrwComment(200, landed(), mrwHeadSHA)) - newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, notifier, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a capped issueless MR must not start a run, got %d", len(runs.calls)) @@ -1104,7 +1104,7 @@ func TestMRReworkBotOnlyCommentsNoFire(t *testing.T) { botComment.AuthorForgeUserID = mrwBotID f := landedForge(botComment) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 { t.Fatalf("a bot-only comment set must not fire, got %d runs", len(runs.calls)) @@ -1140,7 +1140,7 @@ func TestMRReworkBotWalkthroughSummaryNoFire(t *testing.T) { body := coderabbitSummaryMarker + "\n\nNo actionable comments were generated in the recent review." f := landedForge(mrwSummaryComment(400, landed(), "coderabbitai[bot]", body)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("a bot walkthrough/summary-only set must not fire or advance the ledger: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -1156,7 +1156,7 @@ func TestMRReworkHumanCodeRabbitControlCommentNoFire(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwSummaryComment(420, landed(), "maintainer", body)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("a CodeRabbit control command must not fire or advance the ledger: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -1175,7 +1175,7 @@ func TestMRReworkInlineBotFindingFires(t *testing.T) { inline.AuthorUsername = "coderabbitai[bot]" f := landedForge(inline) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("a bot INLINE finding must fire, got %d runs", len(runs.calls)) @@ -1192,7 +1192,7 @@ func TestMRReworkHumanTopLevelNoteFires(t *testing.T) { runs := &mrwRuns{} f := landedForge(mrwSummaryComment(130, landed(), "maintainer", "please also rename X before we merge")) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 1 { t.Fatalf("a human top-level note must fire, got %d runs", len(runs.calls)) @@ -1211,7 +1211,7 @@ func TestMRReworkGitLabMarkerNonBotSummaryNoFire(t *testing.T) { body := coderabbitSummaryMarker + "\n\nNo actionable comments were generated." f := landedForge(mrwSummaryComment(410, landed(), "coderabbit", body)) - newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f) + newMRW(st, runs, nil, mrwSettings{enabled: true, capVal: 5}).detect(context.Background(), mrwRepoRow(), f, f) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("a marker-carrying summary from a non-bot login must not fire: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) @@ -1229,7 +1229,7 @@ func TestMRReworkSummaryThenLowerIdInlineFires(t *testing.T) { // Tick 1: only a bot walkthrough with a HIGH id (300). No fire, no ledger advance. f1 := landedForge(mrwSummaryComment(300, landed(), "coderabbitai[bot]", walkthrough)) - d.detect(context.Background(), mrwRepoRow(), f1) + d.detect(context.Background(), mrwRepoRow(), f1, f1) if len(runs.calls) != 0 || len(st.upserts) != 0 { t.Fatalf("a summary-only tick must not fire or advance the ledger: runs=%d upserts=%d", len(runs.calls), len(st.upserts)) } @@ -1237,7 +1237,7 @@ func TestMRReworkSummaryThenLowerIdInlineFires(t *testing.T) { // Tick 2: the same walkthrough (300) plus a real inline finding with a LOWER id // (200). Because tick 1 left high_water at 0, the id-200 inline still clears GATE 3. f2 := landedForge(mrwSummaryComment(300, landed(), "coderabbitai[bot]", walkthrough), mrwComment(200, landed(), mrwHeadSHA)) - d.detect(context.Background(), mrwRepoRow(), f2) + d.detect(context.Background(), mrwRepoRow(), f2, f2) if len(runs.calls) != 1 { t.Fatalf("a later inline finding with a lower id than the summary must still fire, got %d runs", len(runs.calls)) } diff --git a/api/internal/poller/poller.go b/api/internal/poller/poller.go index 894cbf98ef..101db4a87b 100644 --- a/api/internal/poller/poller.go +++ b/api/internal/poller/poller.go @@ -507,7 +507,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // user (or one explanatory issue comment). All per-issue errors are handled // inside; nothing surfaces here. if e.autopilot != nil { - e.autopilot.detect(ctx, r, f) + e.autopilot.detect(ctx, r, f, f) } // CI-autofix detection (PRD #71 M6): also post-sync, a sibling of the autopilot @@ -518,7 +518,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // MR branch into an automatic ci_fix run (or one halt comment). All per-candidate // errors are handled inside; nothing surfaces here. if e.pipelineMaxRefs > 0 && e.ciAutoFix != nil { - e.ciAutoFix.detect(ctx, r, f) + e.ciAutoFix.detect(ctx, r, f, f) } // MR-review-rework detection (PRD #700 M3): also post-sync, a sibling of the @@ -530,7 +530,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // being wired (the instance kill-switch); the admin kill-switch + per-user opt-in + // per-MR cap gate activation inside it. All per-candidate errors are handled inside. if e.pipelineMaxRefs > 0 && e.mrReviewWatch != nil { - e.mrReviewWatch.detect(ctx, r, f) + e.mrReviewWatch.detect(ctx, r, f, f) } // Reverse Status→label sync (PRD #364 M6): a genuine per-tick sibling (NOT gated diff --git a/api/internal/poller/response_overflow_m2_livedb_test.go b/api/internal/poller/response_overflow_m2_livedb_test.go index 0bd31e827c..b369da1dd3 100644 --- a/api/internal/poller/response_overflow_m2_livedb_test.go +++ b/api/internal/poller/response_overflow_m2_livedb_test.go @@ -96,7 +96,7 @@ func TestM2AutopilotDurableOverflowLiveDB(t *testing.T) { st := &m2RecordStore{Queries: q, fail: mode == "record_failure"} runs := &apRuns{} row := store.ListEnabledReposWithConnectionsRow{ID: repo, ConnectionID: conn, ForgeProjectID: 42} - detect := func() { NewAutopilot(st, runs, nil).detect(ctx, row, driver) } + detect := func() { NewAutopilot(st, runs, nil).detect(ctx, row, driver, driver) } detect() if mode == "record_failure" || mode == "transient" { if notes.Load() != 0 { diff --git a/api/internal/poller/task_continued_issue_livedb_test.go b/api/internal/poller/task_continued_issue_livedb_test.go index a522140341..5427013708 100644 --- a/api/internal/poller/task_continued_issue_livedb_test.go +++ b/api/internal/poller/task_continued_issue_livedb_test.go @@ -40,24 +40,24 @@ func TestLinkedTaskAutopilotTransitionLiveDB(t *testing.T) { t.Fatalf("swallowed event caused create=%d comments=%d issue fetches=%d", len(runs.calls), len(f.noteAttempts), f.fetches) } } - ap.detect(e.ctx, repo, f) + ap.detect(e.ctx, repo, f, f) receipt(10) quiet() e.exec("UPDATE runs SET status='completed',finished_at=now() WHERE id=$1", linkedID) - ap.detect(e.ctx, repo, f) // consumed application never replays after terminal + ap.detect(e.ctx, repo, f, f) // consumed application never replays after terminal receipt(10) quiet() f.events[17] = append(f.events[17], remEvt(11, "owner")) - ap.detect(e.ctx, repo, f) + ap.detect(e.ctx, repo, f, f) receipt(10) quiet() f.events[17] = append(f.events[17], addEvt(12, "owner")) - ap.detect(e.ctx, repo, f) + ap.detect(e.ctx, repo, f, f) receipt(12) if len(runs.calls) != 1 || runs.calls[0].iid != 17 || runs.calls[0].repoID != e.repo || len(f.noteAttempts) != 0 || f.fetches != 1 { t.Fatalf("new application must create once: calls=%+v comments=%v fetches=%d", runs.calls, f.noteAttempts, f.fetches) } - ap.detect(e.ctx, repo, f) + ap.detect(e.ctx, repo, f, f) if len(runs.calls) != 1 || f.fetches != 1 || len(f.noteAttempts) != 0 { t.Fatal("new event replayed") } From f54493ce51c8f894b4a9deb5f957a161e2745e52 Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 17:59:44 +0000 Subject: [PATCH 10/16] forge M2 U3d: handlers on their half via repo-side wrappers Repo-scoped handlers resolve the half they need through new issueTrackerForRepo/codeHostForRepo wrappers (forgesvc IssueTrackerForRepo/CodeHostForRepo, keeping the forgeFactory test hook): issue creation, labels, board columns, moves, promote and sync on the issue tracker; pulls, CI runs, CI fix and MR rework on the code host. Connection-scoped handlers and the worker forge routes keep one build and narrow at each use; the worker get-issue route uses both. Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/handler/board.go | 30 +++++++++---------- api/internal/handler/ci_fix.go | 6 ++-- api/internal/handler/findings_file.go | 6 ++-- api/internal/handler/findings_group_file.go | 6 ++-- api/internal/handler/forge.go | 12 ++++---- api/internal/handler/forgeview.go | 24 +++++++-------- api/internal/handler/handler.go | 33 +++++++++++++++++++-- api/internal/handler/issues.go | 8 ++--- api/internal/handler/judge_admin_issue.go | 4 +-- api/internal/handler/labels.go | 12 ++++---- api/internal/handler/mrrework_run.go | 6 ++-- api/internal/handler/review_issue_file.go | 4 +-- api/internal/handler/worker_forge.go | 30 ++++++++++++------- 13 files changed, 110 insertions(+), 71 deletions(-) diff --git a/api/internal/handler/board.go b/api/internal/handler/board.go index 2c8a0cb8d9..1ece5851fa 100644 --- a/api/internal/handler/board.go +++ b/api/internal/handler/board.go @@ -356,14 +356,14 @@ func (h *Handler) ensureHumanReviewColumn(w http.ResponseWriter, r *http.Request return true // already present — idempotent, no forge or DB work } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return false } // Color matches DefaultColumns' Human Review entry. - if err := f.EnsureLabels(r.Context(), repo.ForgeProjectID, []forge.Label{{Name: board.ColumnHumanReview, Color: "#6e49cb"}}); err != nil { + if err := tr.EnsureLabels(r.Context(), repo.ForgeProjectID, []forge.Label{{Name: board.ColumnHumanReview, Color: "#6e49cb"}}); err != nil { slog.Warn("ensure Human Review label on the forge", "repo", repo.PathWithNamespace, "error", err) return true // non-fatal: render with existing columns, retry next load } @@ -423,13 +423,13 @@ func humanReviewPlacement(cols []store.BoardColumn) (pos int, needed bool) { // writing an error) only when the label-seeding step fails; an import failure // is logged but non-fatal (the board still renders and Refresh retries). func (h *Handler) seedBoard(w http.ResponseWriter, r *http.Request, repo store.GetRepoForUserRow) bool { - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return false } - if err := forgesvc.EnsureBoardColumns(r.Context(), h.q, f, repo.ID, repo.ForgeProjectID); err != nil { + if err := forgesvc.EnsureBoardColumns(r.Context(), h.q, tr, repo.ID, repo.ForgeProjectID); err != nil { var seedErr *forgesvc.LabelSeedError if errors.As(err, &seedErr) { httpx.Error(w, http.StatusBadGateway, "could not seed board labels on the forge: "+seedErr.Error()) @@ -439,7 +439,7 @@ func (h *Handler) seedBoard(w http.ResponseWriter, r *http.Request, repo store.G httpx.Error(w, http.StatusInternalServerError, "internal error") return false } - if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, f); err != nil { + if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { // Non-fatal: the board renders empty and the poller/Refresh will fill it. slog.Warn("initial board import failed", "repo", repo.PathWithNamespace, "error", err) } @@ -741,7 +741,7 @@ func (h *Handler) ConfigureColumns(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -752,7 +752,7 @@ func (h *Handler) ConfigureColumns(w http.ResponseWriter, r *http.Request) { for i, n := range names { labels[i] = forge.Label{Name: n, Color: defaultColumnColor} } - if err := f.EnsureLabels(r.Context(), repo.ForgeProjectID, labels); err != nil { + if err := tr.EnsureLabels(r.Context(), repo.ForgeProjectID, labels); err != nil { httpx.Error(w, http.StatusBadGateway, "could not ensure column labels on the forge: "+err.Error()) return } @@ -996,7 +996,7 @@ func (h *Handler) MoveIssue(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -1014,11 +1014,11 @@ func (h *Handler) MoveIssue(w http.ResponseWriter, r *http.Request) { var updated store.Issue switch { case isClose: - updated, err = h.svc.CloseIssue(r.Context(), f, repo.ForgeProjectID, issue) + updated, err = h.svc.CloseIssue(r.Context(), tr, repo.ForgeProjectID, issue) case sourceClosed: - updated, err = h.svc.ReopenIssue(r.Context(), f, repo.ForgeProjectID, issue, cols, target) + updated, err = h.svc.ReopenIssue(r.Context(), tr, repo.ForgeProjectID, issue, cols, target) default: - updated, err = h.svc.AutoMove(r.Context(), f, repo.ForgeProjectID, issue, cols, target) + updated, err = h.svc.AutoMove(r.Context(), tr, repo.ForgeProjectID, issue, cols, target) } if err != nil { httpx.Error(w, http.StatusBadGateway, "could not update the issue on the forge: "+err.Error()) @@ -1136,7 +1136,7 @@ func (h *Handler) PromoteIssue(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -1146,7 +1146,7 @@ func (h *Handler) PromoteIssue(w http.ResponseWriter, r *http.Request) { // failure the cache is untouched and the client keeps showing the pre-promote // state. Applying a label the issue already carries is a local no-op success // with no forge call, so a double click costs nothing. - updated, err := h.svc.SetIssueLabel(r.Context(), f, repo.ForgeProjectID, issue, label, forgesvc.PromoteLabelColor, true) + updated, err := h.svc.SetIssueLabel(r.Context(), tr, repo.ForgeProjectID, issue, label, forgesvc.PromoteLabelColor, true) if err != nil { httpx.Error(w, http.StatusBadGateway, "could not update the issue label on the forge: "+err.Error()) return @@ -1221,13 +1221,13 @@ func (h *Handler) SyncRepo(w http.ResponseWriter, r *http.Request) { if !ok { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, f); err != nil { + if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { httpx.Error(w, http.StatusBadGateway, "sync failed: "+err.Error()) return } diff --git a/api/internal/handler/ci_fix.go b/api/internal/handler/ci_fix.go index 7a64b62f24..dc99d26c40 100644 --- a/api/internal/handler/ci_fix.go +++ b/api/internal/handler/ci_fix.go @@ -64,13 +64,13 @@ func (h *Handler) CreateCIFixRun(w http.ResponseWriter, r *http.Request) { return } - f, err := h.forgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - snapshot, err := workersvc.BuildFailureSnapshot(r.Context(), f, repo.ForgeProjectID, ps, h.cfg.CIFixMaxJobs, h.cfg.CIFixLogTailBytes) + snapshot, err := workersvc.BuildFailureSnapshot(r.Context(), ch, repo.ForgeProjectID, ps, h.cfg.CIFixMaxJobs, h.cfg.CIFixLogTailBytes) if err != nil { // err is already PAT-redacted by the driver. httpx.Error(w, http.StatusBadGateway, "could not snapshot the failed pipeline: "+err.Error()) @@ -80,7 +80,7 @@ func (h *Handler) CreateCIFixRun(w http.ResponseWriter, r *http.Request) { // Resolve the guard's watch set (PRD #71 M2): the configured default globs unioned // with the project's own ci_config_path. A fetch failure degrades to defaults — // manual Fix CI runs are human-approved, so the guard never reads this set for them. - projectPath, err := f.ProjectCIConfigPath(r.Context(), repo.ForgeProjectID) + projectPath, err := ch.ProjectCIConfigPath(r.Context(), repo.ForgeProjectID) if err != nil { slog.Warn("ci-fix: fetch ci_config_path", "error", err) projectPath = "" // degrade to defaults; manual runs are human-approved so the guard never reads this diff --git a/api/internal/handler/findings_file.go b/api/internal/handler/findings_file.go index 4b0ed84bef..f903d6a88a 100644 --- a/api/internal/handler/findings_file.go +++ b/api/internal/handler/findings_file.go @@ -149,7 +149,7 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFindingClaim(ctx, finding) slog.Error("file finding: build forge for connection", "error", err) @@ -160,13 +160,13 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { // EnsureLabels the marker and opted-in run labels BEFORE CreateIssue (D5/R5): Forgejo // resolves label names to ids and errors on an unknown one, so required labels must pre-exist. A failure here reverts the // claim and is a 502 (an upstream forge problem), not a duplicate issue. - if err := f.EnsureLabels(ctx, repo.ForgeProjectID, requiredLabels); err != nil { + if err := tr.EnsureLabels(ctx, repo.ForgeProjectID, requiredLabels); err != nil { h.revertFindingClaim(ctx, finding) httpx.Error(w, http.StatusBadGateway, "could not ensure the finding label on the forge: "+err.Error()) return } - created, err := f.CreateIssue(ctx, repo.ForgeProjectID, title, description, filedLabels) + created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, title, description, filedLabels) if err != nil { // The forge rejected the write: the claim reverts, nothing is persisted, the finding // stays open and re-fileable. err is already PAT-redacted by the driver. diff --git a/api/internal/handler/findings_group_file.go b/api/internal/handler/findings_group_file.go index 72caed3326..e7aed0316e 100644 --- a/api/internal/handler/findings_group_file.go +++ b/api/internal/handler/findings_group_file.go @@ -184,7 +184,7 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { if !releaseFindingGroupOrReport(w, op.ID, dispositionIDs, release) { return @@ -200,7 +200,7 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { return } filedLabels, requiredLabels := h.findingFilingLabels(callCtx, marker, req.Labels, req.SendToUzi) - if err := f.EnsureLabels(callCtx, repo.ForgeProjectID, requiredLabels); err != nil { + if err := tr.EnsureLabels(callCtx, repo.ForgeProjectID, requiredLabels); err != nil { if callCtx.Err() != nil { writeStoppedFindingGroup(w, op.ID, dispositionIDs, "pre_call") return @@ -224,7 +224,7 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { writeStoppedFindingGroup(w, op.ID, dispositionIDs, "in_flight") return } - created, err := f.CreateIssue(callCtx, repo.ForgeProjectID, title, description, filedLabels) + created, err := tr.CreateIssue(callCtx, repo.ForgeProjectID, title, description, filedLabels) if err != nil { if forge.IsCreateIssueDefinitiveRejection(err) { if !h.releaseDefinitiveFindingGroup(user.ID, op.ID) { diff --git a/api/internal/handler/forge.go b/api/internal/handler/forge.go index 0408db5bfe..47ddd5027f 100644 --- a/api/internal/handler/forge.go +++ b/api/internal/handler/forge.go @@ -301,7 +301,8 @@ func (h *Handler) VerifyConnection(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } - if _, err := f.VerifyToken(r.Context()); err != nil { + var ch forge.CodeHost = f + if _, err := ch.VerifyToken(r.Context()); err != nil { httpx.Error(w, http.StatusBadGateway, "token verification failed: "+err.Error()) return } @@ -376,7 +377,8 @@ func (h *Handler) UpdateConnection(w http.ResponseWriter, r *http.Request) { slog.Error("build forge for connection", "error", ferr) warning = usernameUnverifiedWarning } else { - warning = humanUsernameWarning(r.Context(), f, username) + var ch forge.CodeHost = f + warning = humanUsernameWarning(r.Context(), ch, username) } } @@ -411,7 +413,7 @@ func (h *Handler) UpdateConnection(w http.ResponseWriter, r *http.Request) { // It returns "" when the user exists, and a warning string when the forge says no // such user or the lookup itself fails — a forge blip must never block the save // (PRD #19 Decision 3, verified-or-warned). -func humanUsernameWarning(ctx context.Context, f forge.Forge, username string) string { +func humanUsernameWarning(ctx context.Context, f forge.CodeHost, username string) string { exists, err := f.UserExists(ctx, username) if err != nil { return usernameUnverifiedWarning @@ -843,12 +845,12 @@ func (h *Handler) importAfterEnable(ctx context.Context, userID, repoID uuid.UUI slog.Warn("import after enable: read repo", "error", err) return } - f, err := h.svc.ForgeForConnection(row.ForgeType, row.BaseUrl, row.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, row.ID, row.ForgeType, row.BaseUrl, row.TokenCiphertext) if err != nil { slog.Warn("import after enable: build forge", "error", err) return } - if _, err := h.svc.FullSync(ctx, row.ID, row.ForgeProjectID, f); err != nil { + if _, err := h.svc.FullSync(ctx, row.ID, row.ForgeProjectID, tr); err != nil { slog.Warn("import after enable failed; the poller retries", "repo", row.PathWithNamespace, "error", err) } } diff --git a/api/internal/handler/forgeview.go b/api/internal/handler/forgeview.go index d18baed2b8..a0b5a59945 100644 --- a/api/internal/handler/forgeview.go +++ b/api/internal/handler/forgeview.go @@ -185,7 +185,7 @@ func (h *Handler) ListPulls(w http.ResponseWriter, r *http.Request) { if !ok { return } - f, err := h.forgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -209,7 +209,7 @@ func (h *Handler) ListPulls(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - refs, e := f.ListMergeRequestRefs(loadCtx, repo.ForgeProjectID, + refs, e := ch.ListMergeRequestRefs(loadCtx, repo.ForgeProjectID, forge.ListMergeRequestsOptions{State: forge.MRStateOpened, Limit: forgeViewPullsLimit}) if e != nil { return nil, 0, e @@ -244,7 +244,7 @@ func (h *Handler) ListPulls(w http.ResponseWriter, r *http.Request) { if ce := h.chargeBudget(repo.ConnectionID); ce != nil { return nil, 0, ce } - s, se := f.GetMergeRequestSummary(loadCtx, repo.ForgeProjectID, ref.IID) + s, se := ch.GetMergeRequestSummary(loadCtx, repo.ForgeProjectID, ref.IID) if se != nil { return nil, 0, se } @@ -294,7 +294,7 @@ func (h *Handler) GetPull(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid pull id") return } - f, err := h.forgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -317,7 +317,7 @@ func (h *Handler) GetPull(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - s, e := f.GetMergeRequestSummary(loadCtx, repo.ForgeProjectID, iid) + s, e := ch.GetMergeRequestSummary(loadCtx, repo.ForgeProjectID, iid) if e != nil { return nil, 0, e } @@ -340,7 +340,7 @@ func (h *Handler) GetPull(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - cs, e := f.ListChecks(loadCtx, repo.ForgeProjectID, s.HeadSHA) + cs, e := ch.ListChecks(loadCtx, repo.ForgeProjectID, s.HeadSHA) if e != nil { return nil, 0, e } @@ -355,7 +355,7 @@ func (h *Handler) GetPull(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - rv, e := f.ListMergeRequestReviews(loadCtx, repo.ForgeProjectID, iid) + rv, e := ch.ListMergeRequestReviews(loadCtx, repo.ForgeProjectID, iid) if e != nil { return nil, 0, e } @@ -393,7 +393,7 @@ func (h *Handler) ListCIRuns(w http.ResponseWriter, r *http.Request) { if limit > forgeViewCIRunsMax { limit = forgeViewCIRunsMax } - f, err := h.forgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -408,7 +408,7 @@ func (h *Handler) ListCIRuns(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - runs, e := f.ListWorkflowRuns(loadCtx, repo.ForgeProjectID, forge.ListWorkflowRunsOptions{Limit: limit}) + runs, e := ch.ListWorkflowRuns(loadCtx, repo.ForgeProjectID, forge.ListWorkflowRunsOptions{Limit: limit}) if e != nil { return nil, 0, e } @@ -445,7 +445,7 @@ func (h *Handler) GetCIRun(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid run id") return } - f, err := h.forgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -460,7 +460,7 @@ func (h *Handler) GetCIRun(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - run, e := f.GetWorkflowRun(loadCtx, repo.ForgeProjectID, runID) + run, e := ch.GetWorkflowRun(loadCtx, repo.ForgeProjectID, runID) if e != nil { return nil, 0, e } @@ -482,7 +482,7 @@ func (h *Handler) GetCIRun(w http.ResponseWriter, r *http.Request) { if e := h.chargeBudget(repo.ConnectionID); e != nil { return nil, 0, e } - jobs, e := f.ListPipelineJobs(loadCtx, repo.ForgeProjectID, runID) + jobs, e := ch.ListPipelineJobs(loadCtx, repo.ForgeProjectID, runID) if e != nil { return nil, 0, e } diff --git a/api/internal/handler/handler.go b/api/internal/handler/handler.go index 24e29fcf97..90f57bfed7 100644 --- a/api/internal/handler/handler.go +++ b/api/internal/handler/handler.go @@ -83,8 +83,11 @@ type Handler struct { // forgeFactory, when non-nil, replaces h.svc.ForgeForConnection for the forge-view // read routes (PRD #1255 M2a), so their handlers can be exercised against a // forgetest.BaseFake without a live forge or a wired forgesvc.Service (the auth - // live-DB suite injects one). nil in production — New leaves it unset and - // forgeForConnection falls back to h.svc. Mirrors tmplWriteStore. + // live-DB suite injects one). It also covers the repo-side wrappers + // issueTrackerForRepo and codeHostForRepo, which return the factory's value + // (narrowed to its half) instead of calling h.svc.IssueTrackerForRepo or + // h.svc.CodeHostForRepo. nil in production — New leaves it unset and the + // wrappers fall back to h.svc. Mirrors tmplWriteStore. forgeFactory func(forgeType, baseURL string, tokenCiphertext []byte) (forge.Forge, error) // forgeMemo folds duplicate forge reads under the forge-view routes (PRD #1255 // M2b, D4): a short-TTL + singleflight memo so N terminals polling one PR cost @@ -484,6 +487,32 @@ func (h *Handler) forgeForConnection(forgeType, baseURL string, tokenCiphertext return h.svc.ForgeForConnection(forgeType, baseURL, tokenCiphertext) } +// issueTrackerForRepo builds the issue-tracker half of a repo's forge, going through +// the injected forgeFactory when one is set and otherwise through +// forgesvc.Service.IssueTrackerForRepo. Like forgeForConnection it returns nil on error. +func (h *Handler) issueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { + if h.forgeFactory != nil { + f, err := h.forgeFactory(forgeType, baseURL, tokenCiphertext) + if err != nil { + return nil, err + } + return f, nil + } + return h.svc.IssueTrackerForRepo(ctx, repoID, forgeType, baseURL, tokenCiphertext) +} + +// codeHostForRepo is issueTrackerForRepo for the code-host half. +func (h *Handler) codeHostForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { + if h.forgeFactory != nil { + f, err := h.forgeFactory(forgeType, baseURL, tokenCiphertext) + if err != nil { + return nil, err + } + return f, nil + } + return h.svc.CodeHostForRepo(ctx, repoID, forgeType, baseURL, tokenCiphertext) +} + // memo returns the process-wide forge-view read memo (PRD #1255 M2b), constructing // it lazily and exactly once. New sets h.forgeMemo directly; this accessor covers a // struct-literal test Handler (cliLiveDB) where New did not run, so the forge-view diff --git a/api/internal/handler/issues.go b/api/internal/handler/issues.go index 9fcbd4c30b..757fdbc523 100644 --- a/api/internal/handler/issues.go +++ b/api/internal/handler/issues.go @@ -52,7 +52,7 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -62,7 +62,7 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) { // best-effort — a cold settings read yields the compiled-in default ("uzi"), // never an empty label. uziLabel, _ := h.settings.UziLabel(r.Context()) - created, err := f.CreateIssue(r.Context(), repo.ForgeProjectID, title, req.Description, []string{uziLabel}) + created, err := tr.CreateIssue(r.Context(), repo.ForgeProjectID, title, req.Description, []string{uziLabel}) if err != nil { // err is already PAT-redacted by the driver. httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) @@ -207,13 +207,13 @@ func (h *Handler) GetIssueDetail(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid issue id") return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - issue, err := f.GetIssue(r.Context(), repo.ForgeProjectID, iid) + issue, err := tr.GetIssue(r.Context(), repo.ForgeProjectID, iid) if err != nil { // err is already PAT-redacted by the driver. httpx.Error(w, http.StatusBadGateway, "could not read the issue from the forge: "+err.Error()) diff --git a/api/internal/handler/judge_admin_issue.go b/api/internal/handler/judge_admin_issue.go index 9de865f90c..7dc548ee92 100644 --- a/api/internal/handler/judge_admin_issue.go +++ b/api/internal/handler/judge_admin_issue.go @@ -259,14 +259,14 @@ func (h *Handler) AdminFileJudgeIssue(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFiledClaim(ctx, claimID) slog.Error("admin file issue: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - created, err := f.CreateIssue(ctx, repo.ForgeProjectID, title, description, labels) + created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, title, description, labels) if err != nil { // Forge rejected the write: the claim reverts, nothing persists, the coordinate is // fileable again. err is already PAT-redacted by the driver. diff --git a/api/internal/handler/labels.go b/api/internal/handler/labels.go index e3260708ce..26d6a802f9 100644 --- a/api/internal/handler/labels.go +++ b/api/internal/handler/labels.go @@ -27,13 +27,13 @@ func (h *Handler) CheckRepoLabels(w http.ResponseWriter, r *http.Request) { httpx.RespondDecodeError(w, err, "invalid request body") return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("check repo labels: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - existing, err := f.ListLabels(r.Context(), repo.ForgeProjectID) + existing, err := tr.ListLabels(r.Context(), repo.ForgeProjectID) if err != nil { // err is already PAT-redacted by the driver; an upstream forge read failure is a 502. httpx.Error(w, http.StatusBadGateway, "could not list the repo's labels on the forge: "+err.Error()) @@ -58,13 +58,13 @@ func (h *Handler) EnsureRepoLabels(w http.ResponseWriter, r *http.Request) { httpx.RespondDecodeError(w, err, "invalid request body") return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("ensure repo labels: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - ensured, err := ensureRepoLabels(r.Context(), f, repo.ForgeProjectID, req.Labels) + ensured, err := ensureRepoLabels(r.Context(), tr, repo.ForgeProjectID, req.Labels) if err != nil { // err is already PAT-redacted by the driver; an upstream forge write failure is a 502. httpx.Error(w, http.StatusBadGateway, "could not ensure the labels on the forge: "+err.Error()) @@ -106,9 +106,9 @@ func missingLabels(existing []forge.Label, requested []string) []string { // deduped, non-blank set it ensured. It hands EnsureLabels the requested labels directly // (EnsureLabels skips any that already exist), with an empty color so each of the three // drivers applies its own neutral default — mirroring findings_file.go's `forge.Label{{Name: marker}}`. An empty -// request is a no-op (no forge call). Split out as a package function taking a forge.Forge +// request is a no-op (no forge call). Split out as a package function taking a forge.IssueTracker // so the WARN/CONFIRM logic is unit-testable against a fake forge without a DB. -func ensureRepoLabels(ctx context.Context, f forge.Forge, projectID int64, requested []string) ([]string, error) { +func ensureRepoLabels(ctx context.Context, f forge.IssueTracker, projectID int64, requested []string) ([]string, error) { names := dedupNonBlank(requested) if len(names) == 0 { return []string{}, nil diff --git a/api/internal/handler/mrrework_run.go b/api/internal/handler/mrrework_run.go index b2a6f7d5d9..12f0fc0d0c 100644 --- a/api/internal/handler/mrrework_run.go +++ b/api/internal/handler/mrrework_run.go @@ -157,7 +157,7 @@ func (h *Handler) StartRunRework(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusConflict, "MR rework is unavailable: the trusted review bot list could not be read") return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("mr-rework: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -182,7 +182,7 @@ func (h *Handler) StartRunRework(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } - comments, err := f.ListMergeRequestComments(r.Context(), repo.ForgeProjectID, run.MrIid.Int64) + comments, err := ch.ListMergeRequestComments(r.Context(), repo.ForgeProjectID, run.MrIid.Int64) if err != nil { // err is already PAT-redacted by the driver. httpx.Error(w, http.StatusBadGateway, "could not read the merge request comments: "+err.Error()) @@ -211,7 +211,7 @@ func (h *Handler) StartRunRework(w http.ResponseWriter, r *http.Request) { ProjectID: repo.ForgeProjectID, BaseURL: repo.BaseUrl, BotForgeUserID: repo.BotForgeUserID, - Lookup: f, + Lookup: ch, Trusted: trusted, Comments: comments, Ledger: state, diff --git a/api/internal/handler/review_issue_file.go b/api/internal/handler/review_issue_file.go index 3c00accdf1..3238ef1231 100644 --- a/api/internal/handler/review_issue_file.go +++ b/api/internal/handler/review_issue_file.go @@ -149,14 +149,14 @@ func (h *Handler) FileIssue(w http.ResponseWriter, r *http.Request) { return } - f, err := h.svc.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFiledClaim(ctx, claimID) slog.Error("file issue: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - created, err := f.CreateIssue(ctx, repo.ForgeProjectID, title, description, labels) + created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, title, description, labels) if err != nil { // Forge rejected the write (mock state E): the claim reverts, nothing is // persisted, the draft stays open. err is already PAT-redacted by the driver. diff --git a/api/internal/handler/worker_forge.go b/api/internal/handler/worker_forge.go index 29301093c7..27b1aa4b6e 100644 --- a/api/internal/handler/worker_forge.go +++ b/api/internal/handler/worker_forge.go @@ -127,15 +127,16 @@ func (h *Handler) WorkerForgeGetIssue(w http.ResponseWriter, r *http.Request) { if !ok { return } + ch, tr := forge.CodeHost(f), forge.IssueTracker(f) iid, err := parseInt64(chi.URLParam(r, "iid")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) return } - assessment := issueinput.NewAssessment(r.Context(), f, conn.ForgeProjectID) + assessment := issueinput.NewAssessment(r.Context(), ch, conn.ForgeProjectID) defer assessment.Close() own := conn.IssueIID != nil && *conn.IssueIID == iid - issue, err := f.GetIssue(assessment.Context(), conn.ForgeProjectID, iid) + issue, err := tr.GetIssue(assessment.Context(), conn.ForgeProjectID, iid) unavailable := err != nil if unavailable && !own { h.forgeDriverError(w, err) @@ -180,7 +181,7 @@ func (h *Handler) WorkerForgeGetIssue(w http.ResponseWriter, r *http.Request) { var commentsTruncated, commentsWithheld bool commentsUnknown := unavailable if !unavailable { - raw, cerr := f.ListIssueComments(assessment.Context(), conn.ForgeProjectID, iid) + raw, cerr := tr.ListIssueComments(assessment.Context(), conn.ForgeProjectID, iid) if cerr != nil { commentsUnknown = true } else { @@ -229,6 +230,7 @@ func (h *Handler) WorkerForgeListIssues(w http.ResponseWriter, r *http.Request) if !ok { return } + tr := forge.IssueTracker(f) q := r.URL.Query() var opts forge.ListIssuesOptions switch q.Get("state") { @@ -262,7 +264,7 @@ func (h *Handler) WorkerForgeListIssues(w http.ResponseWriter, r *http.Request) // report truncated=true, regardless of what the worker requested. opts.Limit = MaxForgeListItems + 1 - issues, err := f.ListIssues(r.Context(), conn.ForgeProjectID, opts) + issues, err := tr.ListIssues(r.Context(), conn.ForgeProjectID, opts) if err != nil { h.forgeDriverError(w, err) return @@ -302,12 +304,13 @@ func (h *Handler) WorkerForgeListIssueLabelEvents(w http.ResponseWriter, r *http if !ok { return } + tr := forge.IssueTracker(f) iid, err := parseInt64(chi.URLParam(r, "iid")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) return } - events, err := f.ListIssueLabelEvents(r.Context(), conn.ForgeProjectID, iid) + events, err := tr.ListIssueLabelEvents(r.Context(), conn.ForgeProjectID, iid) if err != nil { h.forgeDriverError(w, err) return @@ -341,12 +344,13 @@ func (h *Handler) WorkerForgeGetMergeRequest(w http.ResponseWriter, r *http.Requ if !ok { return } + ch := forge.CodeHost(f) iid, err := parseInt64(chi.URLParam(r, "iid")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) return } - mrq, err := f.GetMergeRequest(r.Context(), conn.ForgeProjectID, iid) + mrq, err := ch.GetMergeRequest(r.Context(), conn.ForgeProjectID, iid) if err != nil { h.forgeDriverError(w, err) return @@ -366,12 +370,13 @@ func (h *Handler) WorkerForgePipelineJobs(w http.ResponseWriter, r *http.Request if !ok { return } + ch := forge.CodeHost(f) pipelineID, err := parseInt64(chi.URLParam(r, "pipeline_id")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) return } - jobs, err := f.ListPipelineJobs(r.Context(), conn.ForgeProjectID, pipelineID) + jobs, err := ch.ListPipelineJobs(r.Context(), conn.ForgeProjectID, pipelineID) if err != nil { h.forgeDriverError(w, err) return @@ -407,6 +412,7 @@ func (h *Handler) WorkerForgeLatestPipeline(w http.ResponseWriter, r *http.Reque if !ok { return } + ch := forge.CodeHost(f) q := r.URL.Query() ref := q.Get("ref") mrRaw := q.Get("mr_iid") @@ -418,14 +424,14 @@ func (h *Handler) WorkerForgeLatestPipeline(w http.ResponseWriter, r *http.Reque var pipe forge.Pipeline var err error if ref != "" { - pipe, err = f.LatestPipeline(r.Context(), conn.ForgeProjectID, ref) + pipe, err = ch.LatestPipeline(r.Context(), conn.ForgeProjectID, ref) } else { mrIID, perr := parseInt64(mrRaw) if perr != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) return } - pipe, err = f.LatestMRPipeline(r.Context(), conn.ForgeProjectID, mrIID) + pipe, err = ch.LatestMRPipeline(r.Context(), conn.ForgeProjectID, mrIID) } if err != nil { if errors.Is(err, forge.ErrNoPipeline) { @@ -511,6 +517,7 @@ func (h *Handler) WorkerForgeReplyMRThread(w http.ResponseWriter, r *http.Reques if !ok { return } + ch := forge.CodeHost(f) var req apitypes.ForgeMRThreadReplyRequest if err := httpx.DecodeJSON(r, &req); err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) @@ -534,7 +541,7 @@ func (h *Handler) WorkerForgeReplyMRThread(w http.ResponseWriter, r *http.Reques httpx.Error(w, http.StatusForbidden, forgeErrMRThreadScope) return } - if err := f.ReplyMergeRequestComment(r.Context(), conn.ForgeProjectID, mrIID, req.ReplyID, body); err != nil { + if err := ch.ReplyMergeRequestComment(r.Context(), conn.ForgeProjectID, mrIID, req.ReplyID, body); err != nil { h.forgeDriverError(w, err) return } @@ -551,6 +558,7 @@ func (h *Handler) WorkerForgeResolveMRThread(w http.ResponseWriter, r *http.Requ if !ok { return } + ch := forge.CodeHost(f) var req apitypes.ForgeMRThreadResolveRequest if err := httpx.DecodeJSON(r, &req); err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) @@ -568,7 +576,7 @@ func (h *Handler) WorkerForgeResolveMRThread(w http.ResponseWriter, r *http.Requ httpx.Error(w, http.StatusForbidden, forgeErrMRThreadScope) return } - err := f.ResolveMergeRequestThread(r.Context(), conn.ForgeProjectID, mrIID, req.ResolveID) + err := ch.ResolveMergeRequestThread(r.Context(), conn.ForgeProjectID, mrIID, req.ResolveID) if errors.Is(err, forge.ErrResolveUnsupported) { // Forgejo has no resolvable-thread concept: reply-only is the documented contract, // so a resolve is a tolerated no-op rather than a run failure. From 7b3457417b55d117a7dcbb648c45c05e04b3fc0c Mon Sep 17 00:00:00 2001 From: uzi-agent Date: Tue, 10 Nov 2026 18:27:43 +0000 Subject: [PATCH 11/16] handler: drop unused forgeForConnection, correct forgeFactory docs Every caller moved to issueTrackerForRepo/codeHostForRepo in the previous commit, so forgeForConnection is dead. The forgeFactory and wrapper doc comments now describe the wrappers as the only path (U3d review N1-N3). Changelog: none Co-Authored-By: Claude Opus 5.5 --- api/internal/handler/handler.go | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/api/internal/handler/handler.go b/api/internal/handler/handler.go index 90f57bfed7..9a5871ca16 100644 --- a/api/internal/handler/handler.go +++ b/api/internal/handler/handler.go @@ -80,14 +80,14 @@ type Handler struct { // leaves them unset); read only through productTokenListBound. myProductTokenRowsOverride int32 adminProductTokenRowsOverride int32 - // forgeFactory, when non-nil, replaces h.svc.ForgeForConnection for the forge-view - // read routes (PRD #1255 M2a), so their handlers can be exercised against a - // forgetest.BaseFake without a live forge or a wired forgesvc.Service (the auth - // live-DB suite injects one). It also covers the repo-side wrappers - // issueTrackerForRepo and codeHostForRepo, which return the factory's value - // (narrowed to its half) instead of calling h.svc.IssueTrackerForRepo or - // h.svc.CodeHostForRepo. nil in production — New leaves it unset and the - // wrappers fall back to h.svc. Mirrors tmplWriteStore. + // forgeFactory, when non-nil, replaces forgesvc's IssueTrackerForRepo and + // CodeHostForRepo behind the repo-side wrappers issueTrackerForRepo and + // codeHostForRepo, for every repo-scoped handler on either half (PRD #1255 M2a + // introduced it for the forge-view read routes). It lets those handlers be + // exercised against a forgetest.BaseFake without a live forge or a wired + // forgesvc.Service (the forge-view and ci-fix live-DB suites inject one). nil in + // production — New leaves it unset and the wrappers fall back to h.svc. Mirrors + // tmplWriteStore. forgeFactory func(forgeType, baseURL string, tokenCiphertext []byte) (forge.Forge, error) // forgeMemo folds duplicate forge reads under the forge-view routes (PRD #1255 // M2b, D4): a short-TTL + singleflight memo so N terminals polling one PR cost @@ -476,20 +476,11 @@ func (h *Handler) templateWriteStore() agentTemplateWriteStore { return h.q } -// forgeForConnection builds a forge driver for a repo's connection, going through the -// injected forgeFactory when one is set (the forge-view auth test) and otherwise -// through the real forgesvc.Service. nil-safe only when a factory is injected; in -// production h.svc is always wired. -func (h *Handler) forgeForConnection(forgeType, baseURL string, tokenCiphertext []byte) (forge.Forge, error) { - if h.forgeFactory != nil { - return h.forgeFactory(forgeType, baseURL, tokenCiphertext) - } - return h.svc.ForgeForConnection(forgeType, baseURL, tokenCiphertext) -} - // issueTrackerForRepo builds the issue-tracker half of a repo's forge, going through // the injected forgeFactory when one is set and otherwise through -// forgesvc.Service.IssueTrackerForRepo. Like forgeForConnection it returns nil on error. +// forgesvc.Service.IssueTrackerForRepo. It returns a nil tracker whenever err != nil, +// on both paths. nil-safe only when a factory is injected; in production h.svc is +// always wired. func (h *Handler) issueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { if h.forgeFactory != nil { f, err := h.forgeFactory(forgeType, baseURL, tokenCiphertext) From e12f2fccd5544854072fe63d3b5ba2a8477a524d Mon Sep 17 00:00:00 2001 From: Mircea Tufan Date: Wed, 11 Nov 2026 09:19:18 +0200 Subject: [PATCH 12/16] forge M2: say issue tracker in the three new comments Co-Authored-By: Claude --- api/internal/forgesvc/repo_sides.go | 2 +- api/internal/handler/handler.go | 2 +- api/internal/schedsvc/scheduler.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/internal/forgesvc/repo_sides.go b/api/internal/forgesvc/repo_sides.go index b5d6378df4..2ad0269033 100644 --- a/api/internal/forgesvc/repo_sides.go +++ b/api/internal/forgesvc/repo_sides.go @@ -10,7 +10,7 @@ import ( // IssueTrackerForRepo returns the issue side of a repo (D8 of the IssueTracker / // CodeHost split). Phase 1: the repo's own forge driver, built exactly as -// ForgeForConnection builds it. Phase 2 will map repoID to an external tracker. +// ForgeForConnection builds it. From PRD #4173 M4 it maps repoID to a linked external issue tracker. // The caller has already resolved the connection through its own owner-scoped // lookup, so forgeType, baseURL and tokenCiphertext come from that row. func (s *Service) IssueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { diff --git a/api/internal/handler/handler.go b/api/internal/handler/handler.go index 9a5871ca16..2da5f93a0d 100644 --- a/api/internal/handler/handler.go +++ b/api/internal/handler/handler.go @@ -478,7 +478,7 @@ func (h *Handler) templateWriteStore() agentTemplateWriteStore { // issueTrackerForRepo builds the issue-tracker half of a repo's forge, going through // the injected forgeFactory when one is set and otherwise through -// forgesvc.Service.IssueTrackerForRepo. It returns a nil tracker whenever err != nil, +// forgesvc.Service.IssueTrackerForRepo. It returns a nil issue tracker whenever err != nil, // on both paths. nil-safe only when a factory is injected; in production h.svc is // always wired. func (h *Handler) issueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { diff --git a/api/internal/schedsvc/scheduler.go b/api/internal/schedsvc/scheduler.go index 858ed7fd99..c3799b7846 100644 --- a/api/internal/schedsvc/scheduler.go +++ b/api/internal/schedsvc/scheduler.go @@ -1419,7 +1419,7 @@ func (e *Scheduler) proposalDigestSection(ctx context.Context, sched store.RunSc e.logger.Warn("scheduler: proposal digest skipped, cannot resolve repo/forge", "schedule", sched.ID.String(), "error", err) return "" } - var tr forge.IssueTracker = f // only the tracker half is needed here + var tr forge.IssueTracker = f // only the issue-tracker half is needed here // The SAME label workersvc files a proposal under (single source of truth), so the // digest lists exactly this job's own proposal history. label := workersvc.ProposalLabel(catalogSlugOf(sched)) From 6753a16219c515066b1286df0954c42db66ec3dd Mon Sep 17 00:00:00 2001 From: Mircea Tufan Date: Wed, 11 Nov 2026 09:19:35 +0200 Subject: [PATCH 13/16] forgesvc: point the repo-side resolver doc at PRD #4173 D5 Co-Authored-By: Claude --- api/internal/forgesvc/repo_sides.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/internal/forgesvc/repo_sides.go b/api/internal/forgesvc/repo_sides.go index 2ad0269033..87c1e1dfd9 100644 --- a/api/internal/forgesvc/repo_sides.go +++ b/api/internal/forgesvc/repo_sides.go @@ -8,8 +8,8 @@ import ( "github.com/vtmocanu/uzi/api/internal/forge" ) -// IssueTrackerForRepo returns the issue side of a repo (D8 of the IssueTracker / -// CodeHost split). Phase 1: the repo's own forge driver, built exactly as +// IssueTrackerForRepo returns the issue side of a repo (D5 of PRD #4173, the IssueTracker / +// CodeHost split). Through M3: the repo's own forge driver, built exactly as // ForgeForConnection builds it. From PRD #4173 M4 it maps repoID to a linked external issue tracker. // The caller has already resolved the connection through its own owner-scoped // lookup, so forgeType, baseURL and tokenCiphertext come from that row. From 9139e9da44dbc5c17b19e115377510d0e0831809 Mon Sep 17 00:00:00 2001 From: Mircea Tufan Date: Wed, 11 Nov 2026 15:25:43 +0200 Subject: [PATCH 14/16] docs: PRD #4173 M2 delivered; forge callers on their half Tick M2, refresh the Current state anchors and the inventory of issue-side calls against main, record the M2 decisions, and state that the projectID guard needs the owner-scoped resolver for two connections exposing the same forge project number (D10, tested in M4). Co-Authored-By: uzi-agent Co-Authored-By: Claude --- AGENTS.md | 2 +- ARCHITECTURE.md | 2 +- .../5a57a987-23fb-4f8c-8740-5a15476f7286.md | 4 ++ prds/4173-external-issue-trackers.md | 52 ++++++++++--------- 4 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 changelog.d/5a57a987-23fb-4f8c-8740-5a15476f7286.md diff --git a/AGENTS.md b/AGENTS.md index 394465b105..c830866450 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -158,7 +158,7 @@ Full detail in `ARCHITECTURE.md`; read it for any cross-service work. The short - **Services**: `web` (nginx-unprivileged; serves the SPA, reverse-proxies `/api/*` same-origin, no CORS anywhere), `api` (Go, distroless, sole holder of secrets/keys), `db` (postgres:17, `pgdata` volume), `agent` (profile-gated worker, outbound-only to `api`). - **Trust boundaries**: only `web` publishes a port, on loopback. nginx overwrites `X-Forwarded-For`; `api` trusts it only from `TRUSTED_PROXIES`. Session = HttpOnly JWT + CSRF cookie (`api/internal/middleware/auth.go`). Workers use a Bearer join token (sha256 stored, shown once); no cookies or CSRF on `/api/worker/*`. -- **Forge layer**: `api/internal/forge/forge.go` defines `Forge` as the union of `IssueTracker` (`issuetracker.go`: issues, labels, comments, state) and `CodeHost` (`codehost.go`: the rest). Drivers are `gitlab.go`, `forgejo.go`, `github.go`; an interface change costs four sites: three drivers plus the shared fake `forgetest.BaseFake`. +- **Forge layer**: `api/internal/forge/forge.go` defines `Forge` as the union of `IssueTracker` (`issuetracker.go`: issues, labels, comments, state) and `CodeHost` (`codehost.go`: the rest). Callers take the narrowest half, or both as two parameters, never the union. An interface change costs four sites: three drivers and `forgetest.BaseFake`. - `BaseFake` implements every `Forge` method with loud defaults: an un-overridden method returns an error wrapping `forgetest.ErrNotStubbed`, except `LatestPipeline` / `LatestMRPipeline`, which return the absence sentinel `forge.ErrNoPipeline`. - Test fakes embed it and override only what they exercise (`grep -rlE '^\s+forgetest\.BaseFake\s*$' api` lists them). `workersvc/ci_fix_snapshot_test.go` embeds `forge.Forge` itself, as an unreached nil interface. - No other package imports a driver directly. Errors pass a PAT-scrubbing redactor; outbound base URLs are allowlisted (`FORGE_ALLOWED_BASE_URLS`, https-only: the SSRF guard). diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 5e8841b1b7..b84488dd69 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -80,7 +80,7 @@ Read-through forge views in the TUI (open PRs, live PR checks, CI runs — PRD # ### Forge abstraction -`api/internal/forge` defines the `Forge` interface (`VerifyToken`, `ListProjects`, `ListLabels`, `EnsureLabels`, `ListIssues`, `UpdateIssueLabels`, `UpdateIssueDescription`, the four CI reads, plus the guardrail reads `ProjectRole`/`DefaultBranchProtection`) in `forge.go` as the union of two disjoint halves (PRD #4173), each in its own file with the types only it uses: `IssueTracker` in `issuetracker.go` (issues, labels, comments and issue state) and `CodeHost` in `codehost.go` (every other method: repository and identity reads, the guardrail reads, merge requests, CI, artifacts and refs). `forge.go` keeps the package-wide pieces (`Type`, `RateLimitError`, the sentinel errors, `New`) and the identity types `Project` and `BotIdentity`; each driver (`github.go`, `gitlab.go`, `forgejo.go`) and `forgetest.BaseFake` assert both halves at compile time. The package also defines a neutral domain vocabulary (`BotIdentity`, `Project`, `Label`, `Issue`, `Role`, `BranchProtection`). From PRD #4173 M2 on, callers narrow to the half of `Forge` they need. `forge.New` selects a driver by `forge.Type` — **`gitlab.go`, `forgejo.go`, or `github.go`** — so no other package ever imports a driver directly. Test fakes embed the shared `api/internal/forge/forgetest.BaseFake` (PRD #922), which implements the whole interface with loud `ErrNotStubbed` defaults, so an interface change costs three drivers plus that one fake rather than one edit per hand-written fake. Every driver call goes through an `*http.Client` bounded by `FORGE_HTTP_TIMEOUT` (`timeoutClient` in `forge.go`) — never an untimeouted `http.DefaultClient` — and every returned error is passed through a `redactor` (`redact.go`) that scrubs the PAT and any `Authorization`/`PRIVATE-TOKEN`/`token`-scheme header value before the error can reach a log line or an HTTP response body. `error_class.go` classifies typed errors and HTTP status before scrubbing; the returned error retains only the scrubbed message and a closed `timeout`/`auth`/`server_error`/`rate_limited`/`other` class, with no original error chain. Poller issue-sync health consumes that safe class rather than raw error text. +`api/internal/forge` defines the `Forge` interface (`VerifyToken`, `ListProjects`, `ListLabels`, `EnsureLabels`, `ListIssues`, `UpdateIssueLabels`, `UpdateIssueDescription`, the four CI reads, plus the guardrail reads `ProjectRole`/`DefaultBranchProtection`) in `forge.go` as the union of two disjoint halves (PRD #4173), each in its own file with the types only it uses: `IssueTracker` in `issuetracker.go` (issues, labels, comments and issue state) and `CodeHost` in `codehost.go` (every other method: repository and identity reads, the guardrail reads, merge requests, CI, artifacts and refs). `forge.go` keeps the package-wide pieces (`Type`, `RateLimitError`, the sentinel errors, `New`) and the identity types `Project` and `BotIdentity`; each driver (`github.go`, `gitlab.go`, `forgejo.go`) and `forgetest.BaseFake` assert both halves at compile time. The package also defines a neutral domain vocabulary (`BotIdentity`, `Project`, `Label`, `Issue`, `Role`, `BranchProtection`). Callers narrow to the half of `Forge` they need (PRD #4173 M2): the privilege check, settle and claim proofs, pipeline sync and CI reads take `CodeHost`; issue sync, board moves, issue creation and issue reads take `IssueTracker`; a function that needs both takes the two halves as separate parameters (for example comment intake, the autopilot, CI-autofix and MR-rework detectors, the MR state watcher and the PRD link patch) or, where it builds the driver itself, narrows that one driver into both (self-improve, the worker get-issue route). No caller takes the union as a parameter; only connection factories and resolvers still return `forge.Forge`. `forgesvc.(*Service).IssueTrackerForRepo` and `CodeHostForRepo` (`forgesvc/repo_sides.go`) resolve a repo's two sides; both return its forge driver, and only `IssueTrackerForRepo` is meant to change when an external issue tracker can be mapped. `forge.New` selects a driver by `forge.Type` — **`gitlab.go`, `forgejo.go`, or `github.go`** — so no other package ever imports a driver directly. Test fakes embed the shared `api/internal/forge/forgetest.BaseFake` (PRD #922), which implements the whole interface with loud `ErrNotStubbed` defaults, so an interface change costs three drivers plus that one fake rather than one edit per hand-written fake. Every driver call goes through an `*http.Client` bounded by `FORGE_HTTP_TIMEOUT` (`timeoutClient` in `forge.go`) — never an untimeouted `http.DefaultClient` — and every returned error is passed through a `redactor` (`redact.go`) that scrubs the PAT and any `Authorization`/`PRIVATE-TOKEN`/`token`-scheme header value before the error can reach a log line or an HTTP response body. `error_class.go` classifies typed errors and HTTP status before scrubbing; the returned error retains only the scrubbed message and a closed `timeout`/`auth`/`server_error`/`rate_limited`/`other` class, with no original error chain. Poller issue-sync health consumes that safe class rather than raw error text. The Forgejo driver (`code.gitea.io/sdk/gitea`, Forgejo ≥16.0.0 — [ADR-65](adr/0065-forgejo-driver.md) for why both) proved the abstraction was more than Go-deep by finding the three places it was **not**: (1) the worker held a second, un-abstracted GitLab client, now a minimal TS forge seam (`agent/src/forge.ts`, `GitLabClient`/`ForgejoClient`/`GitHubClient`); (2) the web reconstructed forge URLs by string surgery, now a per-card/run `forge_type` DTO field mapped only at `web/src/lib/forgeNoun.ts` (`forgeNoun`/`forgePlatform`, one Go twin in `slacksvc/notifier_state.go`, one CLI twin in `api/cmd/uzi/render.go`); (3) each forge stored its pipeline status verbatim, so `api/internal/pipelinestatus` is the one Go-side classifier that folds all three vocabularies — the domain twin of `web/src/lib/pipelineBadge.ts`, kept in sync by `TestMirrorsWebPipelineBadge`. Merge-permission is now modelled on all three forges (`BranchProtection.WriteRoleCanMerge`/`BotCanMerge`); the drivers **report** it, and **enforcement is implemented** — [PRD #66](prds/done/66-guardrail-enforcement.md) refuses a run whenever the bot could push or merge to the default branch, at repo-enable, at run creation, and at claim, live and fail-closed, with an admin-only per-repo override for the deliberate, audited exception. diff --git a/changelog.d/5a57a987-23fb-4f8c-8740-5a15476f7286.md b/changelog.d/5a57a987-23fb-4f8c-8740-5a15476f7286.md new file mode 100644 index 0000000000..552a6aaa0b --- /dev/null +++ b/changelog.d/5a57a987-23fb-4f8c-8740-5a15476f7286.md @@ -0,0 +1,4 @@ +### Changed + +- **Every forge caller now uses only the issue-tracker or code-host half it needs (#4173).** + Issue sync, the board, posting comments and issue creation go through the issue tracker; the privilege check, settle, pipeline sync and CI reads go through the code host; autopilot and comment intake use both; behaviour is unchanged for GitLab, GitHub and Forgejo. diff --git a/prds/4173-external-issue-trackers.md b/prds/4173-external-issue-trackers.md index 6841372b62..5f3fa6ac25 100644 --- a/prds/4173-external-issue-trackers.md +++ b/prds/4173-external-issue-trackers.md @@ -4,7 +4,7 @@ **Priority:** Medium -**Status:** In progress, r4 2026-11-11. M1 delivered. r3 followed the maintainer's scope review: one real issue tracker instead of abstract identity work, and the comment, command and withdrawal behaviours leave this PRD. r4 follows the maintainer's review of the M1 PR: the YouTrack id, tag and author decisions are made before the driver, and the issue source of a repo no longer changes. +**Status:** In progress, r5 2026-11-11. M1 and M2 delivered. r3 followed the maintainer's scope review: one real issue tracker instead of abstract identity work, and the comment, command and withdrawal behaviours leave this PRD. r4 follows the maintainer's review of the M1 PR: the YouTrack id, tag and author decisions are made before the driver, and the issue source of a repo no longer changes. **Delivery:** one PR per milestone, each merged before the next starts. No implementation or validation step touches `.github/workflows/**`. @@ -26,7 +26,7 @@ An issue tracker does only the second job. Adding one as a fourth forge would fo ## Goal 1. `Forge` splits into `IssueTracker` (issues, labels, comments, state) and `CodeHost` (everything else), and stays as their union. Done in M1. -2. Every caller receives only the half it needs. M2. +2. Every caller receives only the half it needs. Done in M2. 3. A repo can take its issues from a YouTrack project while its code, merge requests and CI stay on its forge. 4. Feature parity: every issue-side feature that works today on GitLab, GitHub and Forgejo works on YouTrack, or is listed below as not offered on an issue tracker yet. 5. Backwards compatible: a repo with no issue tracker link behaves exactly as today. No backfill, no setting to change. @@ -50,25 +50,24 @@ These hold in every milestone. A change that weakens one is out of scope for thi - **Jira and Asana.** One PRD each, after YouTrack. - **How the worker clones, pushes and opens the merge request.** The code host is unchanged. -## Current state (anchors from r1, verified on upstream `main` 6a76d09f7; line numbers drift, cite by symbol) - -M2's PR refreshes these anchors and the count of issue-side uses against `main` as it is then. - -- **Where the issue side is used today.** Every place in uzi's code, tests excluded, that calls one of the eleven issue-side methods through the `Forge` interface (47 in all; calls a driver makes inside itself are not counted), listed from the type-checked code (`golang.org/x/tools/go/packages`) and cross-checked with `gopls call_hierarchy`: - - *Sync:* `forgesvc` `FullSync`, `IncrementalSync` and `reconcileFindingGroupMarkers` (`ListIssues`, `api/internal/forgesvc/service.go:787`, `:795`, `:816`, `:911`, `:915`, `:926`, `:448`). - - *Board:* `AutoMove` and `SetIssueLabel` (`EnsureLabels`, `UpdateIssueLabels`, `forgesvc/service.go:534-542`, `:600-607`); column setup and label checks in the handlers (`EnsureLabels`, `ListLabels`, `api/internal/handler/board.go:366`, `:432`, `:760`, `handler/labels.go:36`, `:120`); close and reopen (`SetIssueState`, `forgesvc/close_reopen.go:29`, `:62`). - - *Run creation:* the issue's text and comments (`GetIssue`, `ListIssueComments`, `api/internal/issueinput/projection.go:158`, `:169`), reached from `workersvc` `CreateRun`, the scheduler (`api/internal/schedsvc/scheduler.go:594`, `:806`), autopilot (`api/internal/poller/autopilot.go:209`) and the composite flow (`api/internal/workersvc/composite.go:212`); a task continuing an issue (`GetIssue` through the helper `forge.GetIssueBounded`, `forge/response_limit.go:114`, called from `workersvc/task.go:100`); semantic drift (`GetIssue`, `workersvc/semantic_drift.go:75`). - - *The agent:* the worker reads issues through the API, `GET /api/worker/runs/{id}/forge/issues`, `…/issues/{iid}` and `…/issues/{iid}/label-events` (`ListIssues`, `GetIssue`, `ListIssueComments`, `ListIssueLabelEvents`, `handler/worker_forge.go:138`, `:183`, `:265`, `:310`). - - *The issue page:* `GetIssueDetail` (`GetIssue`, `handler/issues.go:216`). - - *Issues uzi creates:* the board's new issue (`handler/issues.go:65`), findings and finding groups, which also ensure their labels first (`EnsureLabels`, `handler/findings_file.go:163`, `:169`, `handler/findings_group_file.go:203`, `:227`), the judge's admin issue (`handler/judge_admin_issue.go:269`), review issues (`handler/review_issue_file.go:159`), the self-improve tracking issue (`schedsvc/self_improve.go:269`) and proposals (`workersvc/composite.go:150`, `workersvc/proposal_filing.go:175`), all through `CreateIssue`; the scheduler's proposal digest and self-improve lookup read issues (`ListIssues`, `schedsvc/scheduler.go:1426`, `schedsvc/self_improve.go:252`). - - *Issue description:* the PRD link patch (`GetIssue`, `UpdateIssueDescription`, `forgesvc/prd_link_patch.go:159`, `:180`). - - *Autopilot:* label events (`ListIssueLabelEvents`, `poller/autopilot.go:140`). - - *Comments out:* see below. -- **The privilege check is code-host only.** `VerifyToken`, `ProjectRole`, `DefaultBranchProtection` (`api/internal/privcheck/checker.go:77`, `:139`, `:147`; `privcheck/service.go:256-283` at claim), fail-closed. -- **Mixed callers.** Comment intake reads issue comments (`ListIssueComments`) and checks each author against the repository (`RepositoryAuthorEligibility`, `api/internal/issueinput/assessment.go:86`); merge request watchers read the MR from the code host (`forgesvc/mr_watch.go:60`) and move the card on the issue side (`AutoMove`, `mr_watch.go:227`). +## Current state (anchors refreshed in M2's PR on upstream `main` f19bc0739 with M2 applied; cited by file and symbol, because line numbers drift) + +- **Where the issue side is used.** Every place in uzi's code, tests excluded, that calls one of the eleven issue-side methods (45 calls, all through `IssueTracker` since M2; calls a driver makes inside itself are not counted), listed from the type-checked code (`golang.org/x/tools/go/packages`). r1 counted 47 through `Forge` on an older `main`. + - *Sync:* `FullSync`, `IncrementalSync` and `reconcileFindingGroupMarkers` (`ListIssues`, seven calls, `api/internal/forgesvc/service.go`). + - *Board:* `AutoMove` and `SetIssueLabel` (`EnsureLabels`, `UpdateIssueLabels`, `forgesvc/service.go`); column setup and label checks in the handlers (`EnsureLabels` in `ConfigureColumns` and `ensureHumanReviewColumn`, `api/internal/handler/board.go`, and in `ensureRepoLabels`, `handler/labels.go`; `ListLabels` in `CheckRepoLabels`, `handler/labels.go`); close and reopen (`SetIssueState` in `CloseIssue` and `ReopenIssue`, `forgesvc/close_reopen.go`). + - *Run creation:* the issue's text and comments (`GetIssue`, `ListIssueComments` in `FetchWithTimeout`, `api/internal/issueinput/projection.go`), reached from `workersvc` `CreateRun`, the scheduler, autopilot and the composite flow; a task continuing an issue (`GetIssue` through the helper `forge.GetIssueBounded`); semantic drift (`GetIssue` in `readCurrentSemanticIssue`, `api/internal/workersvc/semantic_drift.go`). + - *The agent:* the worker reads issues through the API, `GET /api/worker/runs/{id}/forge/issues`, `…/issues/{iid}` and `…/issues/{iid}/label-events` (`ListIssues` in `WorkerForgeListIssues`, `GetIssue` and `ListIssueComments` in `WorkerForgeGetIssue`, `ListIssueLabelEvents` in `WorkerForgeListIssueLabelEvents`, `handler/worker_forge.go`). + - *The issue page:* `GetIssueDetail` (`GetIssue`, `handler/issues.go`). + - *Issues uzi creates* (`CreateIssue`, eight calls): the board's new issue (`CreateIssue`, `handler/issues.go`), findings and finding groups, which also ensure their labels first (`FileFinding`, `handler/findings_file.go`; `FileFindingGroup`, `handler/findings_group_file.go`), the judge's admin issue (`AdminFileJudgeIssue`, `handler/judge_admin_issue.go`), review issues (`FileIssue`, `handler/review_issue_file.go`), the self-improve tracking issue (`ensureTrackingIssue`, `api/internal/schedsvc/self_improve.go`) and proposals (`ConfirmProposalForUser`, `workersvc/composite.go`; `maybeFileProposal`, `workersvc/proposal_filing.go`). The scheduler's proposal digest and the self-improve lookup read issues (`ListIssues` in `proposalDigestSection`, `schedsvc/scheduler.go`, and in `ensureTrackingIssue`). + - *Issue description:* the PRD link patch (`GetIssue`, `UpdateIssueDescription` in `patchOnePRDLink`, `forgesvc/prd_link_patch.go`). + - *Autopilot:* label events (`ListIssueLabelEvents` in `detectOne`, `api/internal/poller/autopilot.go`). + - *Comments out* (`CreateIssueNote`, five calls, all discarding the returned note): autopilot's refusal comment (`recordThenComment`, `poller/autopilot.go`), CI autofix start and halt comments (`detectOne`, `poller/ci_autofix.go`), the merge request rework halt comment (`detectOne`, `poller/mr_review_watch.go`) and the terminal comment (`maybeTerminalComment`, `api/internal/runlifecycle/lifecycle.go`). +- **Code-host calls still made on a `forge.Forge` value.** Four: `VerifyToken` and `ListProjects` in `validateConnection` and `DiscoverRepos` (`api/internal/provisionsvc/provisionsvc.go`) and in `ForgeConnection` (`api/internal/seed/seed.go`), each called directly on what the connection factory returns. No issue-side call goes through the union. +- **The privilege check is code-host only.** `VerifyToken`, `ProjectRole`, `DefaultBranchProtection` (`Check`, `CheckToken` and `checkRepo`, `api/internal/privcheck/checker.go`, all taking `forge.CodeHost`; `privcheck/service.go` at claim), fail-closed. +- **Mixed callers.** Comment intake reads issue comments (`ListIssueComments`) and checks each author against the repository (`RepositoryAuthorEligibility`, in `Assessment.Author`, `api/internal/issueinput/assessment.go`); merge request watchers read the MR from the code host and move the card on the issue side (`AutoMove`), both in `forgesvc/mr_watch.go`. - **Issue identity is a number.** `issues.forge_issue_iid bigint` (`api/internal/store/migrations/00002_forge.sql:47-50`), `runs.issue_iid`, and one active run per issue on `(repo_id, coalesce(issue_iid, continued_issue_iid))` for issue runs and tasks continuing an issue (`00351_task_continued_issue.sql:11-14`). -- **Board columns are labels.** `board_columns.label_name` (`00002_forge.sql:39`); moving a card swaps labels through `UpdateIssueLabels` (`api/internal/forgesvc/service.go:512-542`), forge-first, with the `move_pending_since` marker (`api/internal/runlifecycle/lifecycle.go:1-17`). -- **Autopilot.** Maps the label adder to a uzi user through `forge_connections.human_username` (`00037_autopilot_mapping.sql:6-20`, `poller/autopilot.go:194`). +- **Board columns are labels.** `board_columns.label_name` (`00002_forge.sql:39`); moving a card swaps labels through `UpdateIssueLabels` (`AutoMove`, `api/internal/forgesvc/service.go`), forge-first, with the `move_pending_since` marker (`api/internal/runlifecycle/lifecycle.go`). +- **Autopilot.** Maps the label adder to a uzi user through `forge_connections.human_username` (`00037_autopilot_mapping.sql:6-20`, `detectOne` in `poller/autopilot.go`). - **Constraints a text key meets.** `issues.forge_issue_iid` is `bigint NOT NULL` with `UNIQUE (repo_id, forge_issue_iid)` (`00002_forge.sql:50`, `:59`), and `runs_kind_shape` requires `issue_iid IS NOT NULL` for `kind = 'issue'` (`00314_cross_check_claude_checker.sql`). An added `issue_key` column alone does not make an issue tracker issue storable. - **The issue author is checked the same way.** Run creation assesses `Issue.AuthorForgeUserID` through `RepositoryAuthorEligibility` (`Assessment.Author`, `api/internal/issueinput/assessment.go`). An author that cannot be assessed gives the reason `permission_unknown`, and a scheduled start with that reason is not auto-approved (`createRun`, `api/internal/workersvc/service.go`). - **The forge allowlist is deployment configuration.** `FORGE_ALLOWED_BASE_URLS` is rendered from the Helm value `forge.allowedBaseURLs` (`deploy/chart/values.yaml`, `deploy/chart/templates/api-configmap.yaml`) and passed through `docker-compose.yml`. @@ -87,7 +86,7 @@ M2's PR refreshes these anchors and the count of issue-side uses against `main` - **D8. Issue tracker connections are separate from forge connections.** Per user, kind and base URL, with the rules in S3. A repo links to one connection and one issue tracker project. NULL means the forge is the issue source. - **D9. Moving a card is an `IssueTracker` method.** `MoveToColumn`. The forge drivers implement it with one shared helper that does today's label swap in one `UpdateIssueLabels` call. YouTrack maps a column to a value of the project's State field. The column-to-state mapping is a side table, so `board_columns.label_name` keeps its meaning for the code that reads it. - **D10. YouTrack ids are adapted inside the driver; the neutral types do not change.** `IssueTracker` takes `projectID int64` and `issueIID int64` and returns `IssueNote.ID int64`, while YouTrack project, comment and account ids are strings. Changing the neutral types would cost the three forge drivers and every caller and give the forges nothing, so the driver adapts, and M3 implements exactly this: - - *Project.* A YouTrack driver is built for one project. It holds that project's string id and short name, and the one `projectID int64` value it answers to: the repo's forge project id, which is the number every caller already passes for that repo. An `IssueTracker` method called with any other `projectID` is refused before any request is made, so a driver resolved for one repo cannot serve a call meant for another. The `IssueTracker` methods of a driver built without a project return an error. + - *Project.* A YouTrack driver is built for one project. It holds that project's string id and short name, and the one `projectID int64` value it answers to: the repo's forge project id, which is the number every caller already passes for that repo. An `IssueTracker` method called with a different `projectID` is refused before any request. This catches mismatched forge project numbers; the owner-scoped repo resolver must also bind the driver to the correct repo and issue tracker connection, including when two connections expose the same forge project number. M4 tests that collision. The `IssueTracker` methods of a driver built without a project return an error. - *Connection operations.* Verify the token, list projects and list tags need no project, so a connection can be set up before one is chosen. Listing states is project-scoped, because states belong to a project: it takes the project it reads, picked from the list of projects, and cannot be called without one. - *Issue.* `issueIID` is YouTrack's `numberInProject`. It satisfies the `NOT NULL`, uniqueness and `runs_kind_shape` constraints above with no change to them. - *Comment.* The driver does not derive a number from a YouTrack comment id. YouTrack documents these ids as strings and guarantees no grammar for them. `CreateIssueNote` returns `IssueNote.ID` 0 on YouTrack, and no caller reads that field today: all five call sites discard the returned note. @@ -136,7 +135,7 @@ M2's PR refreshes these anchors and the count of issue-side uses against `main` Progress (one PR per milestone, each merged before the next starts): - [x] **M1:** the `IssueTracker` and `CodeHost` interfaces -- [ ] **M2:** every caller on its half +- [x] **M2:** every caller on its half. The type-checked inventory shows 0 issue-side calls through `forge.Forge` and 45 through `IssueTracker`, and handing `privcheck` an `IssueTracker`-only value fails to compile. - [ ] **M3:** issue tracker connections and the YouTrack driver - [ ] **M4:** issue identity, the repo link and issue intake from YouTrack - [ ] **M5:** the board and issue writes on YouTrack @@ -153,7 +152,7 @@ D1, D2 and D4. One file per interface with the types only it uses (`issuetracker D3 and D5. - **Blocked by:** M1. -- **Acceptance:** every issue-side use in the refreshed inventory (47 at r1) goes through `IssueTracker`; `privcheck` cannot be handed a value that has only the issue side; existing tests change only in two mechanical ways (a fake's parameter type, and passing the same fake as both halves). +- **Acceptance:** every issue-side use in the refreshed inventory (45 calls, 47 at r1) goes through `IssueTracker`; `privcheck` cannot be handed a value that has only the issue side; existing tests change only in two mechanical ways (a fake's parameter type, and passing the same fake as both halves). ### M3: issue tracker connections and the YouTrack driver @@ -173,6 +172,7 @@ D5, D10 (storage), D11, D13, D14 (choice and verification of the tag) and S2. Th - a YouTrack issue carrying the eligibility tag appears on the board and starts a run that receives its title and body, and an issue carrying only a same-named tag of another user does not; - an issue tracker comment from an unmapped author is withheld; - with an unmapped issue author, a manual start proceeds and a scheduled start is created without auto-approval, each in its own test; + - two repos on different forge connections that expose the same forge project number, one linked to YouTrack and one not, each resolve to their own issue source: a call for one never reaches the other's driver (D10); - the privilege check and claim still run against the forge and fail closed with the issue tracker unreachable; - a repo with no link is byte-for-byte unchanged in its forge calls (recorded-call test); - linking is refused for every repo that existed before the migration and for a repo enabled since, including one that is disabled now; it is refused for a tag the connection's account cannot apply or that nobody else can apply, and the check modifies no issue; a link racing the first enable ends in exactly one of the two outcomes of D13; a connection with a linked repo cannot be deleted; @@ -202,7 +202,7 @@ Settings → Issue trackers, the CLI commands, a setup page under `docs/`. ## Risks -- **Size of M2.** About 57 production and 31 test files, mechanical. A type-checked inventory of forge calls is the guard. +- **Size of M2.** 53 production and 31 test files, mechanical. A type-checked inventory of forge calls was the guard. - **Drift from `main` between milestones.** Each milestone's branch is rebased on `main` before its PR. - **YouTrack specifics leak into the seam.** The id adapter (D10) and the tag rules (D14) stay inside the driver, the contract suite runs on all four implementations, and the second issue tracker (Asana) is the real test of the seam. @@ -212,6 +212,9 @@ Settings → Issue trackers, the CLI commands, a setup page under `docs/`. - **2026-11-10: one file per interface.** `issuetracker.go` and `codehost.go`; r1 had both interfaces in `forge.go`. - **2026-11-10: no reflection test.** The interface declarations enforce the split (D4). - **2026-11-10: M2 test edits.** Fakes may change a parameter type, and mixed callers' test call sites pass the same fake twice. Nothing else in a test changes. +- **2026-11-10: M2 wires `IssueTrackerForRepo`/`CodeHostForRepo` (D5) where a repo is in hand without new test fakes.** They are `forgesvc.(*Service)` methods in `forgesvc/repo_sides.go` taking `(ctx, repoID, forgeType, baseURL, tokenCiphertext)` (the caller's own owner-scoped lookup, never the unscoped `GetRepoByID`), used by the repo-scoped handlers and the merged-predecessor check. `workersvc`, `schedsvc`, `runlifecycle`, `privcheck`, `provisionsvc` and `seed` keep their own `ForgeForConnection` factory interfaces (adding the resolvers there would add methods to about 30 test fakes) and narrow at the first use; M4 routes them through the resolvers. Connection-scoped handlers, the run-scoped worker forge proxy (`resolveForgeRun`), `ProjectSyncService`'s own factory and the poll loop build one driver and narrow it at each use or pass it as both halves. +- **2026-11-10: the PRD link patch takes both halves.** D3 placed it on `IssueTracker`, but it also reads the merge request (`GetMergeRequest`), so `SyncPRDLinkPatches` takes `(ch forge.CodeHost, tr forge.IssueTracker)`. Comment intake (`issueinput.Fetch`), the autopilot, CI-autofix and MR-rework detectors, the MR state watcher, self-improve and the worker get-issue route are mixed the same way. +- **2026-11-10: "`privcheck` compiles against `CodeHost` only" means every privcheck parameter, local and forge call is `CodeHost`.** Its `ForgeBuilder` factory interface still returns `forge.Forge`, converted to `CodeHost` straight after the build; handing privcheck an `IssueTracker`-only value is a compile error. - **2026-11-10: scope after the maintainer's review.** YouTrack becomes part of this PRD and drives the identity design. The living comment, the outbox, withdrawal, comments to the agent and `/uzi` commands move out. Asana follows in its own PRD. - **2026-11-11: the issue source is fixed before first enable (D13).** Review of the M1 PR found that project-local numbers let an old run or pinned schedule resolve to an unrelated issue after a repo changes its issue source. The first answer was a per-repo fence over run creation, sync and every issue write. The maintainer proposed the simpler policy: choose the source before the repo is first enabled, and leave relinking out. It holds, because a never-enabled repo has had no sync and no run, provided "never enabled" is stored rather than read from `repos.enabled` and the link and the first enable share the repo row lock. The fence is dropped. - **2026-11-11: id adapter instead of new neutral types (D10).** The driver is built for one project, `issueIID` is `numberInProject`, the driver answers only to the repo's forge project id, `IssueNote.ID` is not derived and account ids are 0. Decided before the M3 driver; only the `issue_key` column waits for M4. @@ -219,3 +222,4 @@ Settings → Issue trackers, the CLI commands, a setup page under `docs/`. - **2026-11-11: an unmapped issue author is `permission_unknown` (D11).** Manual start proceeds, scheduled start is never auto-approved. - **2026-11-11: the contract suite runs on the real drivers (D7), the allowlist is wired through the chart and compose in M3, and the ADR moves from M6 to M3.** - **2026-11-11: second review pass.** Every repo that exists at the M4 migration counts as previously enabled (D13). The driver refuses a `projectID` other than its own, and listing states is project-scoped (D10). The eligibility tag is checked through its see and use settings, read-only (D14). No number is derived from a comment id (D10). +- **2026-11-11: the `projectID` guard is not the whole binding (D10).** `repos` is unique on `(connection_id, forge_project_id)`, so two connections can expose the same forge project number. The guard catches a mismatched number; the owner-scoped repo resolver binds the driver to the right repo and issue tracker connection, and M4 tests the collision. From 350f880a1de081d3f6ca5e6a0030bd4291537003 Mon Sep 17 00:00:00 2001 From: Mircea Tufan Date: Wed, 11 Nov 2026 15:31:58 +0200 Subject: [PATCH 15/16] forge M2: provisionsvc and seed narrow the driver before use The last four code-host calls made on a forge.Forge value now go through CodeHost, and board-column setup takes the IssueTracker half (PRD #4173 D3). The inventory shows no call through the union. Co-Authored-By: Claude --- api/internal/provisionsvc/provisionsvc.go | 14 ++++++++++---- api/internal/seed/seed.go | 10 +++++++--- prds/4173-external-issue-trackers.md | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/api/internal/provisionsvc/provisionsvc.go b/api/internal/provisionsvc/provisionsvc.go index e1bf1b4320..4448cfa02f 100644 --- a/api/internal/provisionsvc/provisionsvc.go +++ b/api/internal/provisionsvc/provisionsvc.go @@ -211,14 +211,16 @@ func (s *Service) validateConnection(ctx context.Context, forgeType, baseURL, to if err != nil { return validatedConnection{}, &Error{Kind: KindBadRequest, Message: "could not initialize forge client"} } - identity, err := f.VerifyToken(ctx) + // PRD #4173 D3: connection validation uses the code-host half only. + var ch forge.CodeHost = f + identity, err := ch.VerifyToken(ctx) if err != nil { // err is already PAT-redacted by the driver. return validatedConnection{}, &Error{Kind: KindBadGateway, Message: "token verification failed: " + err.Error()} } // Least-privilege gate (PRD #5): token-level violations block the save. - if tr := s.guard.CheckToken(ctx, f, forge.Type(forgeType), identity.IsAdmin); len(tr.Violations) > 0 { + if tr := s.guard.CheckToken(ctx, ch, forge.Type(forgeType), identity.IsAdmin); len(tr.Violations) > 0 { return validatedConnection{}, &Error{ Kind: KindOverPrivileged, Message: "the bot token is over-privileged and was not saved; mint a least-privilege token (see the bot setup doc)", @@ -246,7 +248,9 @@ func (s *Service) DiscoverRepos(ctx context.Context, userID, connID uuid.UUID) ( s.logger().Error("build forge for connection", "error", err) return store.ForgeConnection{}, internal() } - projects, err := f.ListProjects(ctx) + // PRD #4173 D3: discovery lists projects on the code-host half. + var ch forge.CodeHost = f + projects, err := ch.ListProjects(ctx) if err != nil { return store.ForgeConnection{}, &Error{Kind: KindBadGateway, Message: "could not list projects: " + err.Error()} } @@ -306,7 +310,9 @@ func (s *Service) EnableRepo(ctx context.Context, userID, repoID uuid.UUID) (sto s.logger().Error("build forge for connection", "error", err) return store.Repo{}, internal() } - if err := forgesvc.EnsureBoardColumns(ctx, s.q, f, row.ID, row.ForgeProjectID); err != nil { + // PRD #4173 D3: board columns are labels, on the issue-tracker half. + var tr forge.IssueTracker = f + if err := forgesvc.EnsureBoardColumns(ctx, s.q, tr, row.ID, row.ForgeProjectID); err != nil { var seedErr *forgesvc.LabelSeedError if errors.As(err, &seedErr) { return store.Repo{}, &Error{Kind: KindBadGateway, Message: "could not seed board labels on the forge: " + seedErr.Error()} diff --git a/api/internal/seed/seed.go b/api/internal/seed/seed.go index ddf89302e3..354cd39a10 100644 --- a/api/internal/seed/seed.go +++ b/api/internal/seed/seed.go @@ -99,13 +99,17 @@ func ForgeConnection(ctx context.Context, q Store, svc ForgeService, cfg config. slog.Error("seed forge: build forge client failed; skipping seed", "error", err) return nil } - identity, err := f.VerifyToken(ctx) + // PRD #4173 D3: the token and project reads use the code-host half, the + // board columns below the issue-tracker half. + var ch forge.CodeHost = f + var tr forge.IssueTracker = f + identity, err := ch.VerifyToken(ctx) if err != nil { // err is already PAT-redacted by the driver. slog.Error("seed forge: token verification failed; skipping seed (retries next boot)", "error", err) return nil } - projects, err := f.ListProjects(ctx) + projects, err := ch.ListProjects(ctx) if err != nil { slog.Error("seed forge: listing projects failed; skipping seed (retries next boot)", "error", err) return nil @@ -166,7 +170,7 @@ func ForgeConnection(ctx context.Context, q Store, svc ForgeService, cfg config. // and boot continues, like every other seed forge call; the operator // fixes the forge access and reruns /server setup. A database error is // boot-fatal, like every other seed database error. - if err := forgesvc.EnsureBoardColumns(ctx, q, f, repo.ID, repo.ForgeProjectID); err != nil { + if err := forgesvc.EnsureBoardColumns(ctx, q, tr, repo.ID, repo.ForgeProjectID); err != nil { var seedErr *forgesvc.LabelSeedError if !errors.As(err, &seedErr) { return fmt.Errorf("seed forge: board columns for repo %q: %w", path, err) diff --git a/prds/4173-external-issue-trackers.md b/prds/4173-external-issue-trackers.md index 5f3fa6ac25..f212be1b49 100644 --- a/prds/4173-external-issue-trackers.md +++ b/prds/4173-external-issue-trackers.md @@ -62,7 +62,7 @@ These hold in every milestone. A change that weakens one is out of scope for thi - *Issue description:* the PRD link patch (`GetIssue`, `UpdateIssueDescription` in `patchOnePRDLink`, `forgesvc/prd_link_patch.go`). - *Autopilot:* label events (`ListIssueLabelEvents` in `detectOne`, `api/internal/poller/autopilot.go`). - *Comments out* (`CreateIssueNote`, five calls, all discarding the returned note): autopilot's refusal comment (`recordThenComment`, `poller/autopilot.go`), CI autofix start and halt comments (`detectOne`, `poller/ci_autofix.go`), the merge request rework halt comment (`detectOne`, `poller/mr_review_watch.go`) and the terminal comment (`maybeTerminalComment`, `api/internal/runlifecycle/lifecycle.go`). -- **Code-host calls still made on a `forge.Forge` value.** Four: `VerifyToken` and `ListProjects` in `validateConnection` and `DiscoverRepos` (`api/internal/provisionsvc/provisionsvc.go`) and in `ForgeConnection` (`api/internal/seed/seed.go`), each called directly on what the connection factory returns. No issue-side call goes through the union. +- **No call goes through the union.** The same inventory shows 66 code-host calls, all through `CodeHost`, and none of either kind through `forge.Forge`. The connection factories still return `forge.Forge`; each caller narrows the value before its first call. - **The privilege check is code-host only.** `VerifyToken`, `ProjectRole`, `DefaultBranchProtection` (`Check`, `CheckToken` and `checkRepo`, `api/internal/privcheck/checker.go`, all taking `forge.CodeHost`; `privcheck/service.go` at claim), fail-closed. - **Mixed callers.** Comment intake reads issue comments (`ListIssueComments`) and checks each author against the repository (`RepositoryAuthorEligibility`, in `Assessment.Author`, `api/internal/issueinput/assessment.go`); merge request watchers read the MR from the code host and move the card on the issue side (`AutoMove`), both in `forgesvc/mr_watch.go`. - **Issue identity is a number.** `issues.forge_issue_iid bigint` (`api/internal/store/migrations/00002_forge.sql:47-50`), `runs.issue_iid`, and one active run per issue on `(repo_id, coalesce(issue_iid, continued_issue_iid))` for issue runs and tasks continuing an issue (`00351_task_continued_issue.sql:11-14`). @@ -135,7 +135,7 @@ These hold in every milestone. A change that weakens one is out of scope for thi Progress (one PR per milestone, each merged before the next starts): - [x] **M1:** the `IssueTracker` and `CodeHost` interfaces -- [x] **M2:** every caller on its half. The type-checked inventory shows 0 issue-side calls through `forge.Forge` and 45 through `IssueTracker`, and handing `privcheck` an `IssueTracker`-only value fails to compile. +- [x] **M2:** every caller on its half. The type-checked inventory shows 45 issue-side calls through `IssueTracker`, 66 code-host calls through `CodeHost` and none through `forge.Forge`, and handing `privcheck` an `IssueTracker`-only value fails to compile. - [ ] **M3:** issue tracker connections and the YouTrack driver - [ ] **M4:** issue identity, the repo link and issue intake from YouTrack - [ ] **M5:** the board and issue writes on YouTrack From bde305f408a3c6a914fb396895152da15a9f60ad Mon Sep 17 00:00:00 2001 From: Mircea Tufan Date: Wed, 11 Nov 2026 15:32:27 +0200 Subject: [PATCH 16/16] docs: PRD #4173 M2 file count Co-Authored-By: Claude --- prds/4173-external-issue-trackers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prds/4173-external-issue-trackers.md b/prds/4173-external-issue-trackers.md index f212be1b49..b95ec67bd3 100644 --- a/prds/4173-external-issue-trackers.md +++ b/prds/4173-external-issue-trackers.md @@ -202,7 +202,7 @@ Settings → Issue trackers, the CLI commands, a setup page under `docs/`. ## Risks -- **Size of M2.** 53 production and 31 test files, mechanical. A type-checked inventory of forge calls was the guard. +- **Size of M2.** 54 production and 31 test files, mechanical. A type-checked inventory of forge calls was the guard. - **Drift from `main` between milestones.** Each milestone's branch is rebased on `main` before its PR. - **YouTrack specifics leak into the seam.** The id adapter (D10) and the tag rules (D14) stay inside the driver, the contract suite runs on all four implementations, and the second issue tracker (Asana) is the real test of the seam.