diff --git a/packages/server/src/server/agent/change-request-watches/index.ts b/packages/server/src/server/agent/change-request-watches/index.ts index 4143fd95d3..d07bd9e751 100644 --- a/packages/server/src/server/agent/change-request-watches/index.ts +++ b/packages/server/src/server/agent/change-request-watches/index.ts @@ -1,11 +1,12 @@ import { readFile } from "node:fs/promises"; import { join } from "node:path"; +import { FORGE_IDS } from "@getpaseo/protocol/forge-manifest"; import type { Logger } from "pino"; import { z } from "zod"; +import type { ChangeRequestRouter } from "../../../services/change-request-router.js"; import { changeRequestStatusSchema, type ChangeRequestSnapshot, - type ChangeRequestSource, } from "../../../services/change-request-source.js"; import type { AgentMessageDelivery } from "../agent-messages/index.js"; import type { @@ -15,13 +16,17 @@ import type { import { writeJsonFileAtomic } from "../../atomic-file.js"; const DIGEST_WINDOW_MS = 30_000; -const watchGroupSchema = z.object({ +const LEGACY_DEFAULT_FORGE = "github"; +const LEGACY_INFERENCE_FORGES = ["github", "gitlab"] as const; +const persistedWatchGroupSchema = z.object({ + forge: z.string().min(1).optional(), url: z.url(), snapshot: changeRequestStatusSchema, seenActivity: z.array(z.string()), watchers: z.array(z.object({ agentId: z.string(), startedAt: z.string().optional() })), }); -type WatchGroup = z.infer; +type PersistedWatchGroup = z.infer; +type WatchGroup = Omit & { forge: string }; type Watcher = WatchGroup["watchers"][number]; interface DigestWindow { dueAt: number; @@ -35,7 +40,7 @@ interface Observation { release(): void; } interface Dependencies { - source: ChangeRequestSource; + router: ChangeRequestRouter; paseoHome: string; logger: Logger; isActive(agentId: string): Promise; @@ -73,7 +78,7 @@ function describeUpdate(group: WatchGroup, next: ChangeRequestSnapshot) { } for (const activity of next.activity) { if (seen.has(activity.id)) continue; - changes.push(`New ${activity.kind}: ${activity.url}`); + changes.push(activity.message ?? `New ${activity.kind}: ${activity.url}`); seen.add(activity.id); } if (next.state !== "open") changes.push(`Change request ${next.state}.`); @@ -85,6 +90,7 @@ export type ChangeRequestWatches = ReturnType export function createChangeRequestWatches(deps: Dependencies) { const file = join(deps.paseoHome, "change-request-watches.json"); const groups = new Map(); + const unrestored: WatchGroup[] = []; const observations = new Map(); const registrations = new Map(); let mutations = Promise.resolve(); @@ -156,32 +162,47 @@ export function createChangeRequestWatches(deps: Dependencies) { } } - function withdraw(agentId: string, url?: string): boolean { - let removed = false; - for (const [address, window] of windows) { - if (url && address !== url) continue; - removed = window.recipients.delete(agentId) || removed; + function withdraw(agentId: string, matches: (url: string) => boolean): string | undefined { + let withdrawn: string | undefined; + for (const [url, window] of windows) { + if (!window.recipients.has(agentId) || !matches(url)) continue; + window.recipients.delete(agentId); + withdrawn ??= url; if (!window.recipients.size) { clearTimeout(window.timer); - windows.delete(address); + windows.delete(url); } } - const queued = deps - .messages() - .withdrawPending( - agentId, - (source) => source.kind === "change-request-update" && (!url || source.url === url), - ); - return removed || queued > 0; + deps.messages().withdrawPending(agentId, (source) => { + if (source.kind !== "change-request-update" || !matches(source.url)) return false; + withdrawn ??= source.url; + return true; + }); + return withdrawn; + } + + function withdrawByInput(agentId: string, input: string): string | undefined { + const trimmed = input.trim(); + const candidates = new Set([trimmed]); + for (const forge of FORGE_IDS) { + const source = deps.router.source(forge); + if (!source) continue; + try { + candidates.add(source.resolve(trimmed)); + } catch { + // This source does not recognize the URL. + } + } + return withdraw(agentId, (url) => candidates.has(url)); } async function commit(...updates: WatchGroup[]): Promise { const next = new Map(groups); for (const group of updates) next.set(group.url, group); - await writeJsonFileAtomic( - file, - [...next.values()].filter(({ watchers }) => watchers.length), - ); + await writeJsonFileAtomic(file, [ + ...[...next.values()].filter(({ watchers }) => watchers.length), + ...unrestored, + ]); for (const group of updates) { const previous = groups.get(group.url); // An in-flight subscription still needs the committed baseline and its observation. @@ -195,7 +216,20 @@ export function createChangeRequestWatches(deps: Dependencies) { } } - function syncBackgroundWork({ url, watchers }: WatchGroup, previous: Watcher[] = []): void { + function describeWatch(forge: string, url: string): string { + const source = deps.router.source(forge); + if (!source) return url; + try { + return source.describe(url); + } catch { + return url; + } + } + + function syncBackgroundWork( + { forge, url, watchers }: WatchGroup, + previous: Watcher[] = [], + ): void { if (previous === watchers) return; const current = new Set(watchers); for (const watcher of previous) { @@ -209,7 +243,7 @@ export function createChangeRequestWatches(deps: Dependencies) { deps.backgroundWork.setDaemonBackgroundWork({ ...backgroundWorkKey(watcher.agentId, url), kind: "monitor", - description: deps.source.describe(url), + description: describeWatch(forge, url), startedAt: watcher.startedAt ?? new Date().toISOString(), stop: async () => { await unsubscribe(watcher.agentId, url, watcher); @@ -218,9 +252,13 @@ export function createChangeRequestWatches(deps: Dependencies) { } } - function observe(url: string): Observation { + function observe(forge: string, url: string): Observation { const existing = observations.get(url); if (existing) return existing; + const source = deps.router.source(forge); + if (!source) { + throw new Error(`Change request watches are not supported for ${forge}.`); + } let resolve!: (snapshot: ChangeRequestSnapshot) => void; let reject!: (error: unknown) => void; const ready = new Promise((accept, fail) => { @@ -228,7 +266,7 @@ export function createChangeRequestWatches(deps: Dependencies) { reject = fail; }); void ready.catch(() => undefined); - const release = deps.source.observe(url, { + const release = source.observe(url, { knownActivity: () => new Set(groups.get(url)?.seenActivity), snapshot: (snapshot) => serialize(async () => { @@ -266,14 +304,14 @@ export function createChangeRequestWatches(deps: Dependencies) { }), error(error) { if (!observation.stale) { - deps.logger.warn({ err: error, url }, "Change request observation became stale"); + deps.logger.warn({ err: error, url, forge }, "Change request observation became stale"); } observation.stale = true; if (!groups.has(url)) { reject(error); stopObserving(url); } - deps.logger.debug({ err: error, url }, "Change request observation failed"); + deps.logger.debug({ err: error, url, forge }, "Change request observation failed"); }, }); const observation: Observation = { @@ -288,16 +326,62 @@ export function createChangeRequestWatches(deps: Dependencies) { return observation; } - async function subscribe(agentId: string, input: string) { - if (closed) throw new Error("Change request watches are closed"); - const url = deps.source.resolve(input); - if (recoveryError) - throw new Error("Change request watches could not recover.", { cause: recoveryError }); + function matchActiveGroup(input: string): { forge: string; url: string } | null { + const trimmed = input.trim(); + const direct = groups.get(trimmed); + if (direct) return { forge: direct.forge, url: direct.url }; + const sourcesTried = new Set(); + for (const group of groups.values()) { + if (sourcesTried.has(group.forge)) continue; + sourcesTried.add(group.forge); + const source = deps.router.source(group.forge); + if (!source) continue; + try { + const canonical = source.resolve(trimmed); + const matched = groups.get(canonical); + if (matched && matched.forge === group.forge) { + return { forge: matched.forge, url: matched.url }; + } + } catch { + // Input did not belong to this forge. + } + } + return null; + } + + function subscribe(agentId: string, input: string) { + if (closed) return Promise.reject(new Error("Change request watches are closed")); + if (recoveryError) { + return Promise.reject( + new Error("Change request watches could not recover.", { cause: recoveryError }), + ); + } + const active = matchActiveGroup(input); + if (active) { + return subscribeResolved(agentId, active.forge, active.url); + } + return deps.router + .resolve(input) + .then(({ forge, url }) => subscribeResolved(agentId, forge, url)); + } + + function subscribeResolved(agentId: string, forge: string, url: string) { registrations.set(url, (registrations.get(url) ?? 0) + 1); - try { - const existing = groups.get(url); - const initial = existing ? { ...existing.snapshot, activity: [] } : await observe(url).ready; - return await serialize(async () => { + const existing = groups.get(url); + const finish = () => { + const remaining = registrations.get(url)! - 1; + if (remaining) { + registrations.set(url, remaining); + } else { + registrations.delete(url); + if (!groups.get(url)?.watchers.length) { + groups.delete(url); + stopObserving(url); + } + } + }; + const commitSubscription = (initial: ChangeRequestSnapshot) => + serialize(async () => { if (closed || !(await deps.isActive(agentId))) throw new Error("Agent is no longer active"); const group = groups.get(url); const { activity, ...initialStatus } = initial; @@ -305,6 +389,7 @@ export function createChangeRequestWatches(deps: Dependencies) { const subscribed = snapshot.state === "open"; if (subscribed && !group?.watchers.some((watcher) => watcher.agentId === agentId)) { await commit({ + forge: group?.forge ?? forge, url, snapshot, seenActivity: group?.seenActivity ?? activity.map(({ id }) => id), @@ -322,21 +407,13 @@ export function createChangeRequestWatches(deps: Dependencies) { checks: snapshot.checks, reviewDecision: snapshot.reviewDecision, subscribed, - stale: observations.get(url)?.stale ?? false, + stale: observations.get(url)?.stale ?? Boolean(existing), }; }); - } finally { - const remaining = registrations.get(url)! - 1; - if (remaining) { - registrations.set(url, remaining); - } else { - registrations.delete(url); - if (!groups.get(url)?.watchers.length) { - groups.delete(url); - stopObserving(url); - } - } - } + const pending = existing + ? commitSubscription({ ...existing.snapshot, activity: [] }) + : observe(forge, url).ready.then(commitSubscription); + return pending.finally(finish); } function removeWatcher(group: WatchGroup, agentId: string): Promise { @@ -355,15 +432,15 @@ export function createChangeRequestWatches(deps: Dependencies) { throw new Error("Background work is no longer available to stop"); } if (group && watcher) await removeWatcher(group, agentId); - const withdrawn = withdraw(agentId, url); - return { url, removed: Boolean(watcher) || withdrawn }; + const withdrawn = withdraw(agentId, (address) => address === url); + return { url, removed: Boolean(watcher || withdrawn) }; }); } function archive(agentId: string): Promise { return serialize(async () => { if (closed) return; - withdraw(agentId); + withdraw(agentId, () => true); const updates: WatchGroup[] = []; for (const group of groups.values()) { const watchers = group.watchers.filter((watcher) => watcher.agentId !== agentId); @@ -373,6 +450,20 @@ export function createChangeRequestWatches(deps: Dependencies) { }); } + function inferLegacyForge(url: string): string { + for (const candidate of LEGACY_INFERENCE_FORGES) { + const source = deps.router.source(candidate); + if (!source) continue; + try { + source.resolve(url); + return candidate; + } catch { + // URL does not match this forge's canonical path shape. + } + } + return LEGACY_DEFAULT_FORGE; + } + return { async start() { const active: WatchGroup[] = []; @@ -382,8 +473,12 @@ export function createChangeRequestWatches(deps: Dependencies) { if (error.code === "ENOENT") return "[]"; throw error; }); - const restored = z.array(watchGroupSchema).parse(JSON.parse(stored)); - for (const group of restored) { + const restored = z.array(persistedWatchGroupSchema).parse(JSON.parse(stored)); + for (const record of restored) { + const group: WatchGroup = { + ...record, + forge: record.forge ?? inferLegacyForge(record.url), + }; if (group.snapshot.state !== "open") { pruned = true; continue; @@ -393,7 +488,17 @@ export function createChangeRequestWatches(deps: Dependencies) { if (await deps.isActive(watcher.agentId)) watchers.push(watcher); } pruned ||= watchers.length !== group.watchers.length; - if (watchers.length) active.push({ ...group, watchers }); + if (!watchers.length) continue; + const nextGroup = { ...group, watchers }; + if (!deps.router.source(nextGroup.forge)) { + deps.logger.warn( + { forge: nextGroup.forge, url: nextGroup.url }, + "Skipping persisted change request watch for unsupported forge", + ); + unrestored.push(nextGroup); + continue; + } + active.push(nextGroup); } } catch (error) { recoveryError = error; @@ -402,7 +507,7 @@ export function createChangeRequestWatches(deps: Dependencies) { } if (pruned) { try { - await writeJsonFileAtomic(file, active); + await writeJsonFileAtomic(file, [...active, ...unrestored]); } catch (error) { deps.logger.warn({ err: error, file }, "Could not prune archived change request watches"); } @@ -410,12 +515,24 @@ export function createChangeRequestWatches(deps: Dependencies) { for (const group of active) { groups.set(group.url, group); syncBackgroundWork(group); - observe(group.url); + try { + observe(group.forge, group.url); + } catch (error) { + deps.logger.warn( + { err: error, forge: group.forge, url: group.url }, + "Could not start observing restored change request watch", + ); + } } }, subscribe, - unsubscribe(agentId: string, input: string) { - return unsubscribe(agentId, deps.source.resolve(input)); + async unsubscribe(agentId: string, input: string) { + const active = matchActiveGroup(input); + if (active) return unsubscribe(agentId, active.url); + const withdrawn = await serialize(async () => withdrawByInput(agentId, input)); + if (withdrawn) return { url: withdrawn, removed: true }; + const { url } = await deps.router.resolve(input); + return unsubscribe(agentId, url); }, archive, async close() { diff --git a/packages/server/src/server/agent/change-request-watches/watches.test.ts b/packages/server/src/server/agent/change-request-watches/watches.test.ts index 210a6524f2..8c5719ed5e 100644 --- a/packages/server/src/server/agent/change-request-watches/watches.test.ts +++ b/packages/server/src/server/agent/change-request-watches/watches.test.ts @@ -1,16 +1,18 @@ -import { mkdir, mkdtemp, readFile, rename, rm } from "node:fs/promises"; +import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises"; import { join } from "node:path"; import pino from "pino"; import { afterEach, expect, test, vi } from "vitest"; import { createChangeRequestWatches } from "./index.js"; import { AgentBackgroundWorkStore } from "../background-work/index.js"; import { createAgentMessageDelivery, parseAgentMessages } from "../agent-messages/index.js"; +import type { ChangeRequestRouter } from "../../../services/change-request-router.js"; import type { ChangeRequestSnapshot, ChangeRequestSource, } from "../../../services/change-request-source.js"; const url = "https://github.com/owner/repo/pull/42"; +const gitlabUrl = "https://gitlab.com/org/sub/app/-/merge_requests/9"; const reviewUrl = url + "#pullrequestreview-1"; const initial: ChangeRequestSnapshot = { state: "open", @@ -31,13 +33,19 @@ async function createTestHome() { function createHarness( home: string, - { isActive = async (_id: string) => true, observationFails = false } = {}, + { + isActive = async (_id: string) => true, + observationFails = false, + resolveFails = false, + extraSources = {} as Record, + } = {}, ) { const observations = new Map[1]>(); const backgroundWork = new AgentBackgroundWorkStore(); const busy = new Set(); const turns: { recipientId: string; prompt: string }[] = []; const listeners = new Set<(id: string) => void>(); + const resolveCalls: string[] = []; const messages = createAgentMessageDelivery({ isRunning: (id) => busy.has(id), subscribe(listener) { @@ -52,10 +60,16 @@ function createHarness( throw error; }, }); - const watches = createChangeRequestWatches({ - source: { - resolve: (input) => input, - describe: () => "owner/repo #42", + function createFakeSource( + describe: (address: string) => string, + accepts: (input: string) => boolean, + ): ChangeRequestSource { + return { + resolve: (input) => { + if (!accepts(input)) throw new Error("Unsupported URL for fake forge"); + return input.replace(/\/+$/, ""); + }, + describe, observe(address, callbacks) { observations.set(address, callbacks); queueMicrotask(() => { @@ -64,7 +78,34 @@ function createHarness( }); return () => observations.delete(address); }, + }; + } + const sources: Record = { + github: createFakeSource( + () => "owner/repo #42", + (input) => input.includes("/pull/"), + ), + gitlab: createFakeSource( + () => "org/sub/app !9", + (input) => input.includes("/merge_requests/"), + ), + ...extraSources, + }; + const router: ChangeRequestRouter = { + async resolve(input) { + resolveCalls.push(input); + if (resolveFails) throw new Error("Host probe failed"); + const forge = input.includes("gitlab") ? "gitlab" : "github"; + const source = sources[forge]; + if (!source) throw new Error(`Unsupported forge: ${forge}`); + return { forge, url: source.resolve(input) }; }, + source(forge) { + return sources[forge] ?? null; + }, + }; + const watches = createChangeRequestWatches({ + router, paseoHome: home, logger: pino({ enabled: false }), isActive, @@ -78,6 +119,7 @@ function createHarness( messages, watches, backgroundWork, + resolveCalls, idle(id: string) { busy.delete(id); for (const listener of listeners) listener(id); @@ -255,6 +297,47 @@ test("can withdraw a terminal digest after its observation has stopped", async ( } }); +test.each([0, 32_000])( + "withdraws a terminal update without a host probe after %i ms", + async (elapsed) => { + vi.useFakeTimers(); + const home = await createTestHome(); + const enterpriseUrl = "https://github.example.com/owner/repo/pull/42"; + const { activity: _activity, ...snapshot } = initial; + await writeFile( + join(home, "change-request-watches.json"), + JSON.stringify([ + { + forge: "github", + url: enterpriseUrl, + snapshot, + seenActivity: [], + watchers: [{ agentId: "first" }], + }, + ]), + ); + const h = createHarness(home, { resolveFails: true }); + try { + h.busy.add("first"); + await h.watches.start(); + await h.observations.get(enterpriseUrl)!.snapshot({ ...initial, state: "merged" }); + expect(h.observations.has(enterpriseUrl)).toBe(false); + await vi.advanceTimersByTimeAsync(elapsed); + await expect(h.watches.unsubscribe("first", `${enterpriseUrl}/`)).resolves.toEqual({ + url: enterpriseUrl, + removed: true, + }); + expect(h.resolveCalls).toEqual([]); + h.idle("first"); + await vi.advanceTimersByTimeAsync(32_000); + expect(h.turns).toEqual([]); + } finally { + await h.close(); + await rm(home, { recursive: true, force: true }); + } + }, +); + test("retains the current observation as the last subscriber leaves and a new one joins", async () => { const home = await createTestHome(); const h = createHarness(home); @@ -343,3 +426,119 @@ test("recovers active watches even when pruning archived watchers cannot be writ await rm(home, { recursive: true, force: true }); } }); + +test("persists forge ids, restores legacy GitHub watches without probing, and preserves unknown forges", async () => { + const home = await createTestHome(); + const file = join(home, "change-request-watches.json"); + const legacyRecord = { + url, + snapshot: { + state: "open", + headSha: "aaa", + checksState: "pending", + checks: [], + reviewDecision: null, + }, + seenActivity: [], + watchers: [{ agentId: "legacy-agent", startedAt: "2026-01-01T00:00:00.000Z" }], + }; + const unknownForgeRecord = { + forge: "futureforge", + url: "https://future.example.com/team/repo/cr/5", + snapshot: legacyRecord.snapshot, + seenActivity: [], + watchers: [{ agentId: "future-agent", startedAt: "2026-01-01T00:00:00.000Z" }], + }; + const strippedGitLabRecord = { + url: gitlabUrl, + snapshot: legacyRecord.snapshot, + seenActivity: [], + watchers: [{ agentId: "gitlab-agent", startedAt: "2026-01-01T00:00:00.000Z" }], + }; + await writeFile( + file, + JSON.stringify([legacyRecord, strippedGitLabRecord, unknownForgeRecord]), + "utf8", + ); + + const h = createHarness(home, { resolveFails: true }); + try { + await h.watches.start(); + expect(h.resolveCalls).toEqual([]); + expect(h.backgroundWork.list("legacy-agent")).toEqual([ + expect.objectContaining({ description: "owner/repo #42", kind: "monitor" }), + ]); + expect(h.backgroundWork.list("gitlab-agent")).toEqual([ + expect.objectContaining({ description: "org/sub/app !9", kind: "monitor" }), + ]); + expect(h.observations.has(url)).toBe(true); + expect(h.observations.has(gitlabUrl)).toBe(true); + + // Unsubscribing an active watch succeeds even when host resolution is temporarily down. + await expect(h.watches.unsubscribe("legacy-agent", `${url}/`)).resolves.toEqual({ + url, + removed: true, + }); + expect(h.backgroundWork.list("legacy-agent")).toEqual([]); + const persisted = JSON.parse(await readFile(file, "utf8")) as Array<{ + forge?: string; + url: string; + }>; + expect(persisted).toEqual([ + expect.objectContaining({ forge: "gitlab", url: gitlabUrl }), + unknownForgeRecord, + ]); + } finally { + await h.close(); + await rm(home, { recursive: true, force: true }); + } +}); + +test("watches multiple forges concurrently with forge-native descriptions and custom approval messages", async () => { + vi.useFakeTimers(); + const home = await createTestHome(); + const file = join(home, "change-request-watches.json"); + const h = createHarness(home); + try { + await h.watches.start(); + await h.watches.subscribe("agent-1", url); + await h.watches.subscribe("agent-1", gitlabUrl); + expect(h.backgroundWork.list("agent-1").map((item) => item.description)).toEqual([ + "owner/repo #42", + "org/sub/app !9", + ]); + + const persisted = JSON.parse(await readFile(file, "utf8")) as Array<{ + forge?: string; + url: string; + }>; + expect(persisted.map(({ forge, url: entryUrl }) => ({ forge, url: entryUrl }))).toEqual([ + { forge: "github", url }, + { forge: "gitlab", url: gitlabUrl }, + ]); + + await h.observations.get(gitlabUrl)!.snapshot({ + ...initial, + activity: [ + { + id: "approval-note:77", + kind: "approval", + url: `${gitlabUrl}#note_77`, + message: `Merge request approved by @alice (1/1 approvals): ${gitlabUrl}#note_77`, + }, + ], + }); + await vi.advanceTimersByTimeAsync(32_000); + expect(h.turns).toEqual([ + { + recipientId: "agent-1", + prompt: expect.stringContaining( + `Merge request approved by @alice (1/1 approvals): ${gitlabUrl}#note_77`, + ), + }, + ]); + } finally { + await h.close(); + await rm(home, { recursive: true, force: true }); + } +}); diff --git a/packages/server/src/server/agent/tools/paseo-tools.ts b/packages/server/src/server/agent/tools/paseo-tools.ts index fdca93fbbc..06e7ccff9c 100644 --- a/packages/server/src/server/agent/tools/paseo-tools.ts +++ b/packages/server/src/server/agent/tools/paseo-tools.ts @@ -2728,13 +2728,19 @@ function createToolCatalog( if (options.changeRequestWatches) { const watches = options.changeRequestWatches; - const inputSchema = { url: z.string().trim().min(1).describe("URL of a GitHub pull request.") }; + const inputSchema = { + url: z + .string() + .trim() + .min(1) + .describe("URL of a pull request or merge request on a supported forge."), + }; registerTool( "subscribe_change_request", { title: "Watch change request", description: - "Watch a pull request for check failures, passing checks, new comments, reviews, and closure. Returns the current state and sends future updates after your current turn.", + "Watch a pull request or merge request for check failures, passing checks, new comments, reviews, approvals, and closure. Returns the current state and sends future updates after your current turn.", inputSchema, outputSchema: { url: z.string(), diff --git a/packages/server/src/server/bootstrap.ts b/packages/server/src/server/bootstrap.ts index 3a12d6dfa6..d2db7d42a6 100644 --- a/packages/server/src/server/bootstrap.ts +++ b/packages/server/src/server/bootstrap.ts @@ -132,6 +132,9 @@ export async function fanOutReconciledWorkspaceUpdates(input: { import { VoiceAssistantWebSocketServer, isDaemonOriginAllowed } from "./websocket-server.js"; import { WorkspaceSetupRuntime } from "./workspace-setup-runtime.js"; import { createWorkspaceLabelService } from "./workspace-labels/index.js"; +import { createChangeRequestRouter } from "../services/change-request-router.js"; +import { createForgeService } from "../services/forge-registry.js"; +import { createForgeResolver } from "../services/forge-resolver.js"; import { createGitHubService } from "../services/github-service.js"; import { createPaseoWorktree as createRegisteredPaseoWorktree } from "./paseo-worktree-service.js"; import { createWorkspaceProvisioningService } from "./session/workspace-provisioning/workspace-provisioning-service.js"; @@ -954,12 +957,15 @@ export async function createPaseoDaemon( workspaceRegistry, }); const github = createGitHubService(); + const forgeResolver = createForgeResolver({ + createService: (forge) => (forge === "github" ? github : createForgeService(forge)), + }); const workspaceGitService = new WorkspaceGitServiceImpl({ logger, paseoHome: config.paseoHome, worktreesRoot: config.worktreesRoot, deps: { - forgeOverrides: { github }, + forgeResolver, }, }); const workspaceRuntimeStore = createWorkspaceRuntimeStore(); @@ -1472,7 +1478,7 @@ export async function createPaseoDaemon( await scheduleService.start(); const changeRequestWatches = createChangeRequestWatches({ - source: github.changeRequests, + router: createChangeRequestRouter({ forgeResolver }), backgroundWork: agentManager, paseoHome: config.paseoHome, logger, @@ -2056,6 +2062,7 @@ export async function createPaseoDaemon( if (wsServer) { await wsServer.close(); } + forgeResolver.dispose(); await serviceProxy.stopStandalone(); // Force-drop remaining sockets so httpServer.close() resolves promptly. // We've already closed wsServer (which sent ws-layer close frames) and diff --git a/packages/server/src/server/workspace-git-service.ts b/packages/server/src/server/workspace-git-service.ts index 8e50a7cffb..921ddb2fbf 100644 --- a/packages/server/src/server/workspace-git-service.ts +++ b/packages/server/src/server/workspace-git-service.ts @@ -422,6 +422,8 @@ interface WorkspaceGitServiceDependencies { * fakes. Any forge not listed here is built (and cached once) by the registry. */ forgeOverrides?: Record; + /** Shared forge resolver when callers share adapter instances with other services. */ + forgeResolver?: ForgeResolver; /** Resolves an SSH config alias to its HostName; defaults to `ssh -G`. */ resolveSshHostname?: SshHostnameResolver; resolveAbsoluteGitDir: (cwd: string) => Promise; @@ -723,10 +725,12 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService { this.fileObserver.subscribe.bind(this.fileObserver), options.deps, ); - this.forgeResolver = createForgeResolver({ - createService: (forge) => this.deps.forgeOverrides?.[forge] ?? createForgeService(forge), - resolveSshHostname: this.deps.resolveSshHostname, - }); + this.forgeResolver = + this.deps.forgeResolver ?? + createForgeResolver({ + createService: (forge) => this.deps.forgeOverrides?.[forge] ?? createForgeService(forge), + resolveSshHostname: this.deps.resolveSshHostname, + }); } resolveForge(cwd: string): Promise { diff --git a/packages/server/src/services/change-request-poller.test.ts b/packages/server/src/services/change-request-poller.test.ts new file mode 100644 index 0000000000..770b84b4ec --- /dev/null +++ b/packages/server/src/services/change-request-poller.test.ts @@ -0,0 +1,230 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { ChangeRequestSnapshot } from "./change-request-source.js"; +import { + ChangeRequestRateLimitError, + createPerHostChangeRequestPoller, + parseCliHttpResponse, + parseRetryAfterMs, +} from "./change-request-poller.js"; + +const sampleSnapshot: ChangeRequestSnapshot = { + state: "open", + headSha: "abc123", + checksState: "passed", + checks: [], + reviewDecision: null, + activity: [], +}; + +afterEach(() => { + vi.useRealTimers(); +}); + +describe("parseCliHttpResponse", () => { + it("parses status line, mixed line endings, redirect blocks, and JSON body", () => { + const raw = [ + "HTTP/1.1 100 Continue\r\n\r\n", + "HTTP/2.0 200 OK\n", + "Content-Type: application/json\r\n", + "X-Next-Page: 2\r\n", + "Ratelimit-Remaining: 95\r\n", + "Ratelimit-Reset: 1700000120\r\n", + "\r\n", + '{"ok":true}', + ].join(""); + const parsed = parseCliHttpResponse(raw); + expect(parsed.status).toBe(200); + expect(parsed.headers.get("x-next-page")).toBe("2"); + expect(parsed.headers.get("ratelimit-remaining")).toBe("95"); + expect(parsed.body).toBe('{"ok":true}'); + }); + + it("returns null status and raw body when headers are absent", () => { + const parsed = parseCliHttpResponse('{"ok":true}'); + expect(parsed.status).toBeNull(); + expect(parsed.headers.size).toBe(0); + expect(parsed.body).toBe('{"ok":true}'); + }); +}); + +describe("parseRetryAfterMs", () => { + it("parses Retry-After seconds and exhausted Ratelimit-Reset headers", () => { + const now = 1_700_000_000_000; + expect(parseRetryAfterMs(new Map([["retry-after", "45"]]), now)).toBe(now + 45_000); + expect( + parseRetryAfterMs( + new Map([ + ["ratelimit-remaining", "0"], + ["ratelimit-reset", "1700000060"], + ]), + now, + ), + ).toBe(1_700_000_060_000); + expect( + parseRetryAfterMs( + new Map([ + ["ratelimit-remaining", "50"], + ["ratelimit-reset", "1700000060"], + ]), + now, + ), + ).toBeNull(); + }); +}); + +describe("createPerHostChangeRequestPoller", () => { + it("bounds concurrent reads per host and drains waiting watches as slots free", async () => { + vi.useFakeTimers(); + let active = 0; + let maxActive = 0; + const resolvers = new Map void>(); + const poller = createPerHostChangeRequestPoller({ + hostOf: (url) => new URL(url).hostname, + maxConcurrencyPerHost: 2, + pollIntervalMs: 20_000, + async read({ url }) { + active += 1; + maxActive = Math.max(maxActive, active); + await new Promise((resolve) => { + resolvers.set(url, resolve); + }); + active -= 1; + return sampleSnapshot; + }, + }); + const received: string[] = []; + try { + for (const n of [1, 2, 3]) { + const url = `https://gitlab.com/org/repo/-/merge_requests/${n}`; + poller.observe(url, { + async snapshot() { + received.push(url); + }, + error(error) { + throw error; + }, + }); + } + await vi.advanceTimersByTimeAsync(25); + expect(maxActive).toBe(2); + expect(resolvers.size).toBe(2); + + resolvers.get("https://gitlab.com/org/repo/-/merge_requests/1")?.(); + await vi.advanceTimersByTimeAsync(0); + expect(maxActive).toBe(2); + expect(resolvers.has("https://gitlab.com/org/repo/-/merge_requests/3")).toBe(true); + + resolvers.get("https://gitlab.com/org/repo/-/merge_requests/2")?.(); + resolvers.get("https://gitlab.com/org/repo/-/merge_requests/3")?.(); + await vi.advanceTimersByTimeAsync(0); + expect(received).toHaveLength(3); + } finally { + poller.dispose(); + } + }); + + it("backs off exponentially on transient errors and resets after a successful poll", async () => { + vi.useFakeTimers(); + let calls = 0; + let shouldFail = true; + const poller = createPerHostChangeRequestPoller({ + hostOf: (url) => new URL(url).hostname, + pollIntervalMs: 10_000, + maxErrorBackoffMs: 40_000, + async read() { + calls += 1; + if (shouldFail) throw new Error(`fail ${calls}`); + return sampleSnapshot; + }, + }); + const errors: unknown[] = []; + const snapshots: ChangeRequestSnapshot[] = []; + try { + poller.observe("https://gitlab.com/org/repo/-/merge_requests/1", { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + errors.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(25); + expect(calls).toBe(1); + expect(errors).toHaveLength(1); + + // 1st failure -> 10s backoff + await vi.advanceTimersByTimeAsync(9_999); + expect(calls).toBe(1); + await vi.advanceTimersByTimeAsync(1); + expect(calls).toBe(2); + + // 2nd failure -> 20s backoff + await vi.advanceTimersByTimeAsync(19_999); + expect(calls).toBe(2); + shouldFail = false; + await vi.advanceTimersByTimeAsync(1); + expect(calls).toBe(3); + expect(snapshots).toHaveLength(1); + + // Reset to 10s base interval after success + await vi.advanceTimersByTimeAsync(10_000); + expect(calls).toBe(4); + } finally { + poller.dispose(); + } + }); + + it("pauses all watches on a rate-limited host until resetAt and fast-fails new observers", async () => { + vi.useFakeTimers(); + vi.setSystemTime(1_700_000_000_000); + let calls = 0; + const poller = createPerHostChangeRequestPoller({ + hostOf: (url) => new URL(url).hostname, + pollIntervalMs: 10_000, + async read() { + calls += 1; + if (calls === 1) { + throw new ChangeRequestRateLimitError({ + host: "gitlab.com", + resetAtMs: Date.now() + 60_000, + }); + } + return sampleSnapshot; + }, + }); + const errors1: unknown[] = []; + const errors2: unknown[] = []; + const snapshots2: ChangeRequestSnapshot[] = []; + try { + poller.observe("https://gitlab.com/org/repo/-/merge_requests/1", { + async snapshot() {}, + error(err) { + errors1.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(25); + expect(calls).toBe(1); + expect(errors1[0]).toBeInstanceOf(ChangeRequestRateLimitError); + + poller.observe("https://gitlab.com/org/repo/-/merge_requests/2", { + async snapshot(s) { + snapshots2.push(s); + }, + error(err) { + errors2.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(0); + expect(errors2[0]).toBeInstanceOf(ChangeRequestRateLimitError); + + await vi.advanceTimersByTimeAsync(59_999); + expect(calls).toBe(1); + + await vi.advanceTimersByTimeAsync(1); + expect(calls).toBe(3); + expect(snapshots2).toHaveLength(1); + } finally { + poller.dispose(); + } + }); +}); diff --git a/packages/server/src/services/change-request-poller.ts b/packages/server/src/services/change-request-poller.ts new file mode 100644 index 0000000000..bc97b88a18 --- /dev/null +++ b/packages/server/src/services/change-request-poller.ts @@ -0,0 +1,362 @@ +import type { ChangeRequestSnapshot, ChangeRequestSource } from "./change-request-source.js"; + +const DEFAULT_INITIAL_DELAY_MS = 25; +const DEFAULT_POLL_INTERVAL_MS = 20_000; +const DEFAULT_MAX_ERROR_BACKOFF_MS = 300_000; +const DEFAULT_MAX_CONCURRENCY_PER_HOST = 2; +const MIN_RATE_LIMIT_PAUSE_MS = 1_000; +const MAX_RATE_LIMIT_PAUSE_MS = 15 * 60_000; + +export interface ParsedCliHttpResponse { + status: number | null; + headers: Map; + body: string; +} + +const HTTP_STATUS_PREFIX = /^HTTP\/\d(?:\.\d)?\s+(\d{3})[^\r\n]*\r?\n/; +const HEADER_BODY_SEPARATOR = /\r?\n\r?\n/; + +/** + * Parse output from CLI API commands invoked with `-i` / `--include`. + * Consumes multiple leading HTTP response blocks (e.g. `100 Continue` or + * redirect headers) and returns the final status code, lowercased headers, + * and response body. Returns `status: null` when no HTTP header block is + * present. + */ +export function parseCliHttpResponse(raw: string): ParsedCliHttpResponse { + let remaining = raw.trimStart(); + let status: number | null = null; + let headers = new Map(); + + while (true) { + const statusMatch = HTTP_STATUS_PREFIX.exec(remaining); + if (!statusMatch) { + break; + } + const separatorMatch = HEADER_BODY_SEPARATOR.exec(remaining); + if (!separatorMatch) { + status = Number(statusMatch[1]); + headers = parseHeaderLines(remaining.slice(statusMatch[0].length)); + remaining = ""; + break; + } + status = Number(statusMatch[1]); + const headerBlock = remaining.slice(statusMatch[0].length, separatorMatch.index); + headers = parseHeaderLines(headerBlock); + remaining = remaining.slice(separatorMatch.index + separatorMatch[0].length); + } + + return { status, headers, body: remaining.trim() }; +} + +function parseHeaderLines(block: string): Map { + const headers = new Map(); + for (const line of block.split(/\r?\n/)) { + const colon = line.indexOf(":"); + if (colon <= 0) continue; + const name = line.slice(0, colon).trim().toLowerCase(); + const value = line.slice(colon + 1).trim(); + if (!name) continue; + const existing = headers.get(name); + headers.set(name, existing ? `${existing}, ${value}` : value); + } + return headers; +} + +function clampPauseUntil(targetMs: number, nowMs: number): number { + return Math.min( + Math.max(targetMs, nowMs + MIN_RATE_LIMIT_PAUSE_MS), + nowMs + MAX_RATE_LIMIT_PAUSE_MS, + ); +} + +/** + * Extract the timestamp (epoch ms) until which a host should pause polling + * based on `Retry-After` or exhausted `Ratelimit-Remaining` / `Ratelimit-Reset` + * headers. Returns null when no pause is signaled. + */ +export function parseRetryAfterMs( + headers: ReadonlyMap, + nowMs: number, + options: { forceOnMissing?: boolean; defaultPauseMs?: number } = {}, +): number | null { + const retryAfter = headers.get("retry-after"); + if (retryAfter) { + const seconds = Number(retryAfter); + if (Number.isFinite(seconds) && seconds >= 0) { + return clampPauseUntil(nowMs + Math.ceil(seconds * 1_000), nowMs); + } + const parsedDate = Date.parse(retryAfter); + if (Number.isFinite(parsedDate)) { + return clampPauseUntil(parsedDate, nowMs); + } + } + + const remainingRaw = headers.get("ratelimit-remaining") ?? headers.get("x-ratelimit-remaining"); + const resetRaw = headers.get("ratelimit-reset") ?? headers.get("x-ratelimit-reset"); + const remaining = remainingRaw !== undefined ? Number(remainingRaw) : Number.NaN; + const resetSec = resetRaw !== undefined ? Number(resetRaw) : Number.NaN; + if ( + (options.forceOnMissing || (Number.isFinite(remaining) && remaining <= 1)) && + Number.isFinite(resetSec) && + resetSec > 0 + ) { + return clampPauseUntil(resetSec * 1_000, nowMs); + } + + if (options.forceOnMissing) { + return clampPauseUntil(nowMs + (options.defaultPauseMs ?? 60_000), nowMs); + } + return null; +} + +export class ChangeRequestRateLimitError extends Error { + readonly host: string; + readonly resetAtMs: number; + + constructor(params: { host: string; resetAtMs: number; message?: string }) { + super( + params.message ?? + `Change request polling for ${params.host} is rate-limited until ${new Date(params.resetAtMs).toISOString()}`, + ); + this.name = "ChangeRequestRateLimitError"; + this.host = params.host; + this.resetAtMs = params.resetAtMs; + } +} + +export interface PerHostReadContext { + url: string; + host: string; + knownActivity(): ReadonlySet | undefined; + isObserved(): boolean; + assertHostAvailable(): void; + deferPollsUntil(untilMs: number): void; + pauseHost(untilMs: number): void; +} + +export interface PerHostChangeRequestPollerOptions { + hostOf(url: string): string; + read(context: PerHostReadContext): Promise; + now?: () => number; + initialDelayMs?: number; + pollIntervalMs?: number; + maxErrorBackoffMs?: number; + maxConcurrencyPerHost?: number; + onError?(error: unknown, host: string): void; +} + +interface WatchedEntry { + url: string; + host: string; + callbacks: Parameters[1]; + nextDueAt: number; + consecutiveErrors: number; + inFlight: boolean; +} + +export interface PerHostChangeRequestPoller { + observe: ChangeRequestSource["observe"]; + dispose(): void; +} + +/** + * Bounded per-host polling loop shared by REST-backed forge watch adapters. + * Limits concurrent reads per host, applies exponential backoff on transient + * failures, and pauses all watches for a host when rate-limited. + */ +export function createPerHostChangeRequestPoller( + options: PerHostChangeRequestPollerOptions, +): PerHostChangeRequestPoller { + const now = options.now ?? Date.now; + const initialDelayMs = options.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS; + const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS; + const maxErrorBackoffMs = options.maxErrorBackoffMs ?? DEFAULT_MAX_ERROR_BACKOFF_MS; + const maxConcurrencyPerHost = options.maxConcurrencyPerHost ?? DEFAULT_MAX_CONCURRENCY_PER_HOST; + + const watches = new Map(); + const inFlightByHost = new Map(); + const pausesByHost = new Map(); + let timer: NodeJS.Timeout | null = null; + let disposed = false; + + function clearTimer(): void { + if (timer) { + clearTimeout(timer); + timer = null; + } + } + + function deferHost(host: string, untilMs: number, blockRequests: boolean): void { + const current = pausesByHost.get(host) ?? { pollsUntil: 0, requestsUntil: 0 }; + pausesByHost.set(host, { + pollsUntil: Math.max(current.pollsUntil, untilMs), + requestsUntil: blockRequests + ? Math.max(current.requestsUntil, untilMs) + : current.requestsUntil, + }); + } + + function hostPauseUntil(host: string, nowMs: number): number { + const pause = pausesByHost.get(host); + if (!pause) return 0; + if (pause.pollsUntil > nowMs) return pause.pollsUntil; + if (!pause.requestsUntil) pausesByHost.delete(host); + return 0; + } + + function pollLimit(host: string): number { + // After a 429, admit one poll until it has delivered a full snapshot. + // This lets it use the reset quota without competing with another poll. + return pausesByHost.get(host)?.requestsUntil ? 1 : maxConcurrencyPerHost; + } + + function armTimer(): void { + clearTimer(); + if (disposed || watches.size === 0) return; + + const nowMs = now(); + let earliestDue = Number.POSITIVE_INFINITY; + for (const watch of watches.values()) { + if (watch.inFlight) continue; + if ((inFlightByHost.get(watch.host) ?? 0) >= pollLimit(watch.host)) continue; + const dueAt = Math.max(watch.nextDueAt, hostPauseUntil(watch.host, nowMs)); + if (dueAt < earliestDue) { + earliestDue = dueAt; + } + } + if (!Number.isFinite(earliestDue)) return; + + const delay = Math.max(0, earliestDue - nowMs); + timer = setTimeout(() => { + timer = null; + drainDueWatches(); + }, delay); + timer.unref(); + } + + function drainDueWatches(): void { + if (disposed) return; + const nowMs = now(); + for (const watch of watches.values()) { + if (watch.inFlight) continue; + const inFlightCount = inFlightByHost.get(watch.host) ?? 0; + if (inFlightCount >= pollLimit(watch.host)) continue; + const dueAt = Math.max(watch.nextDueAt, hostPauseUntil(watch.host, nowMs)); + if (dueAt > nowMs) continue; + startPoll(watch); + } + armTimer(); + } + + function startPoll(watch: WatchedEntry): void { + watch.inFlight = true; + inFlightByHost.set(watch.host, (inFlightByHost.get(watch.host) ?? 0) + 1); + const isObserved = () => !disposed && watches.get(watch.url) === watch; + const recovering = Boolean(pausesByHost.get(watch.host)?.requestsUntil); + let completed = false; + + void (async () => { + try { + const snapshot = await options.read({ + url: watch.url, + host: watch.host, + knownActivity: () => watch.callbacks.knownActivity?.(), + isObserved, + assertHostAvailable() { + const nowMs = now(); + const resetAtMs = pausesByHost.get(watch.host)?.requestsUntil ?? 0; + if (resetAtMs > nowMs) { + throw new ChangeRequestRateLimitError({ host: watch.host, resetAtMs }); + } + }, + deferPollsUntil: (untilMs) => deferHost(watch.host, untilMs, false), + pauseHost: (untilMs) => deferHost(watch.host, untilMs, true), + }); + if (!isObserved()) return; + watch.consecutiveErrors = 0; + await watch.callbacks.snapshot(snapshot); + if (!isObserved()) return; + const nowMs = now(); + const pause = pausesByHost.get(watch.host); + if (pause && pause.requestsUntil <= nowMs) pause.requestsUntil = 0; + watch.nextDueAt = Math.max(nowMs + pollIntervalMs, hostPauseUntil(watch.host, nowMs)); + completed = true; + } catch (error) { + if (!isObserved()) return; + options.onError?.(error, watch.host); + const nowMs = now(); + if (error instanceof ChangeRequestRateLimitError) { + deferHost(watch.host, error.resetAtMs, true); + watch.nextDueAt = Math.max(nowMs + pollIntervalMs, error.resetAtMs); + } else { + watch.consecutiveErrors += 1; + const backoff = Math.min( + pollIntervalMs * 2 ** (watch.consecutiveErrors - 1), + maxErrorBackoffMs, + ); + watch.nextDueAt = nowMs + backoff; + } + watch.callbacks.error(error); + } finally { + // Rotate after a snapshot or a recovery attempt so a watch that + // keeps failing cannot take every turn after a quota reset. + if ((completed || recovering) && isObserved()) { + watches.delete(watch.url); + watches.set(watch.url, watch); + } + watch.inFlight = false; + const remaining = (inFlightByHost.get(watch.host) ?? 1) - 1; + if (remaining > 0) inFlightByHost.set(watch.host, remaining); + else inFlightByHost.delete(watch.host); + if (!disposed) { + // Immediately admit any queued due watch on this host, then re-arm timer. + drainDueWatches(); + } + } + })(); + } + + return { + observe(url, callbacks) { + if (disposed) { + queueMicrotask(() => callbacks.error(new Error("Change request poller is disposed"))); + return () => {}; + } + const host = options.hostOf(url); + const nowMs = now(); + const watch: WatchedEntry = { + url, + host, + callbacks, + nextDueAt: nowMs + initialDelayMs, + consecutiveErrors: 0, + inFlight: false, + }; + watches.set(url, watch); + const pauseUntil = hostPauseUntil(host, nowMs); + if (pauseUntil > nowMs) { + queueMicrotask(() => { + if (!disposed && watches.get(url) === watch) { + callbacks.error(new ChangeRequestRateLimitError({ host, resetAtMs: pauseUntil })); + } + }); + } + armTimer(); + return () => { + if (watches.get(url) === watch) { + watches.delete(url); + armTimer(); + } + }; + }, + + dispose() { + disposed = true; + clearTimer(); + watches.clear(); + inFlightByHost.clear(); + pausesByHost.clear(); + }, + }; +} diff --git a/packages/server/src/services/change-request-router.test.ts b/packages/server/src/services/change-request-router.test.ts new file mode 100644 index 0000000000..fbfe62aed6 --- /dev/null +++ b/packages/server/src/services/change-request-router.test.ts @@ -0,0 +1,120 @@ +import { describe, expect, it, vi } from "vitest"; +import { createChangeRequestRouter } from "./change-request-router.js"; +import type { ChangeRequestSource } from "./change-request-source.js"; +import { createForgeResolver } from "./forge-resolver.js"; +import type { ForgeService } from "./forge-service.js"; + +function createFakeSource( + forge: string, + canonicalize: (url: string) => string, +): ChangeRequestSource { + return { + resolve: canonicalize, + describe: (url) => `${forge}:${url}`, + observe: () => () => {}, + }; +} + +function createFakeForgeService(changeRequests?: ChangeRequestSource): ForgeService { + return { + ...(changeRequests ? { changeRequests } : {}), + invalidate() {}, + } as unknown as ForgeService; +} + +describe("createChangeRequestRouter", () => { + it("routes cloud hosts without probing and delegates canonicalization to the matched forge", async () => { + const probeForge = vi.fn(async () => { + throw new Error("cloud host must not trigger a probe"); + }); + const githubSource = createFakeSource( + "github", + (url) => `https://github.com/canonical/${new URL(url).pathname.split("/").at(-1)}`, + ); + const gitlabSource = createFakeSource( + "gitlab", + (url) => `https://gitlab.com/canonical/${new URL(url).pathname.split("/").at(-1)}`, + ); + const resolver = createForgeResolver({ + probeForge, + createService: (forge) => { + if (forge === "github") return createFakeForgeService(githubSource); + if (forge === "gitlab") return createFakeForgeService(gitlabSource); + return null; + }, + }); + const router = createChangeRequestRouter({ forgeResolver: resolver }); + + await expect(router.resolve("https://GitHub.com/Org/Repo/pull/7")).resolves.toEqual({ + forge: "github", + url: "https://github.com/canonical/7", + }); + await expect( + router.resolve("https://gitlab.com/org/sub/repo/-/merge_requests/12"), + ).resolves.toEqual({ + forge: "gitlab", + url: "https://gitlab.com/canonical/12", + }); + expect(probeForge).not.toHaveBeenCalled(); + expect(router.source("github")).toBe(githubSource); + expect(router.source("gitlab")).toBe(gitlabSource); + }); + + it("probes self-hosted hosts before parsing the URL path", async () => { + const probeForge = vi.fn(async (host: string) => + host === "git.corp.internal" ? "gitlab" : null, + ); + const gitlabResolve = vi.fn(() => "https://git.corp.internal/team/app/-/merge_requests/9"); + const githubResolve = vi.fn(() => { + throw new Error("github must not parse a gitlab host"); + }); + const resolver = createForgeResolver({ + probeForge, + createService: (forge) => { + if (forge === "github") + return createFakeForgeService(createFakeSource("github", githubResolve)); + if (forge === "gitlab") + return createFakeForgeService(createFakeSource("gitlab", gitlabResolve)); + return null; + }, + }); + const router = createChangeRequestRouter({ forgeResolver: resolver }); + + await expect( + router.resolve("https://git.corp.internal/team/app/-/merge_requests/9"), + ).resolves.toEqual({ + forge: "gitlab", + url: "https://git.corp.internal/team/app/-/merge_requests/9", + }); + expect(probeForge).toHaveBeenCalledWith("git.corp.internal"); + expect(gitlabResolve).toHaveBeenCalledTimes(1); + expect(githubResolve).not.toHaveBeenCalled(); + }); + + it("rejects unrecognized hosts, credentials, non-http schemes, and forges without watch support", async () => { + const resolver = createForgeResolver({ + probeForge: async () => null, + createService: (forge) => { + if (forge === "gitea") return createFakeForgeService(); + return null; + }, + }); + const router = createChangeRequestRouter({ forgeResolver: resolver }); + + await expect(router.resolve("not a url")).rejects.toThrow(/change request URL/i); + await expect(router.resolve("ftp://github.com/org/repo/pull/1")).rejects.toThrow( + /change request URL/i, + ); + await expect(router.resolve("https://user:pass@github.com/org/repo/pull/1")).rejects.toThrow( + /change request URL/i, + ); + await expect(router.resolve("https://unknown.example.com/org/repo/pull/1")).rejects.toThrow( + /unknown\.example\.com/, + ); + await expect(router.resolve("https://gitea.com/org/repo/pulls/1")).rejects.toThrow( + /not supported.*gitea/i, + ); + expect(router.source("gitea")).toBeNull(); + expect(router.source("unknown")).toBeNull(); + }); +}); diff --git a/packages/server/src/services/change-request-router.ts b/packages/server/src/services/change-request-router.ts new file mode 100644 index 0000000000..affba11adc --- /dev/null +++ b/packages/server/src/services/change-request-router.ts @@ -0,0 +1,80 @@ +import { normalizeHost } from "@getpaseo/protocol/git-remote"; +import type { ChangeRequestSource } from "./change-request-source.js"; +import type { ForgeResolver } from "./forge-resolver.js"; + +export interface ResolvedChangeRequest { + forge: string; + url: string; +} + +export interface ChangeRequestRouter { + /** Route a user-supplied URL host-first through registered forges and canonicalize it. */ + resolve(url: string): Promise; + /** Return the watch source for a known forge id without probing the network. */ + source(forge: string): ChangeRequestSource | null; +} + +export interface CreateChangeRequestRouterOptions { + forgeResolver: Pick; +} + +function extractUrlHost(input: string): { trimmed: string; host: string; authority: string } { + const trimmed = input.trim(); + if (!trimmed || /\s/.test(trimmed)) { + throw new Error("Enter a supported change request URL."); + } + let parsed: URL; + try { + parsed = new URL(trimmed); + } catch { + throw new Error("Enter a supported change request URL."); + } + if ( + (parsed.protocol !== "https:" && parsed.protocol !== "http:") || + parsed.username || + parsed.password + ) { + throw new Error("Enter a supported change request URL."); + } + const host = normalizeHost(parsed.hostname); + if (!host) { + throw new Error("Enter a supported change request URL."); + } + return { trimmed, host, authority: parsed.port ? `${host}:${parsed.port}` : host }; +} + +/** + * Host-first router for change request watches. Resolves the target forge from + * the URL host via ForgeResolver (cloud match first, then cached CLI auth + * probe) before delegating path parsing and canonicalization to that forge's + * ChangeRequestSource. + */ +export function createChangeRequestRouter({ + forgeResolver, +}: CreateChangeRequestRouterOptions): ChangeRequestRouter { + return { + async resolve(input: string): Promise { + const { trimmed, host, authority } = extractUrlHost(input); + const resolution = + (await forgeResolver.resolveHost(authority)) ?? + (authority === host ? null : await forgeResolver.resolveHost(host)); + if (!resolution) { + throw new Error( + `Could not recognize a supported forge for host ${host}. Authenticate the forge CLI for this host first.`, + ); + } + const source = resolution.service.changeRequests; + if (!source) { + throw new Error(`Change request watches are not supported for ${resolution.forge} yet.`); + } + return { + forge: resolution.forge, + url: source.resolve(trimmed), + }; + }, + + source(forge: string): ChangeRequestSource | null { + return forgeResolver.getService(forge)?.changeRequests ?? null; + }, + }; +} diff --git a/packages/server/src/services/change-request-source.ts b/packages/server/src/services/change-request-source.ts index 7d1c41f4d8..8beaecf344 100644 --- a/packages/server/src/services/change-request-source.ts +++ b/packages/server/src/services/change-request-source.ts @@ -17,8 +17,9 @@ const changeRequestSnapshotSchema = changeRequestStatusSchema.extend({ activity: z.array( z.object({ id: z.string(), - kind: z.enum(["comment", "inline comment", "review"]), + kind: z.enum(["comment", "inline comment", "review", "approval"]), url: z.string(), + message: z.string().optional(), }), ), }); diff --git a/packages/server/src/services/forge-resolver.test.ts b/packages/server/src/services/forge-resolver.test.ts index 414a4cee92..9b194b5f8c 100644 --- a/packages/server/src/services/forge-resolver.test.ts +++ b/packages/server/src/services/forge-resolver.test.ts @@ -373,4 +373,40 @@ describe("createForgeResolver", () => { expect(b).toMatchObject({ forge: "gitlab", host: "git.acme.internal" }); expect(probeForge).toHaveBeenCalledTimes(1); }); + + it("resolves cloud and probed hosts directly via resolveHost and shares service instances", async () => { + const probeForge = vi.fn(async (host: string) => + host === "git.acme.internal" ? "gitlab" : null, + ); + const created: string[] = []; + const disposed: string[] = []; + const resolver = createForgeResolver({ + probeForge, + createService: (forge) => { + created.push(forge); + const base = createForgeService(forge); + if (!base) return null; + return { + ...base, + dispose: () => { + disposed.push(forge); + }, + }; + }, + }); + + const cloud = await resolver.resolveHost("GitLab.com."); + expect(cloud).toMatchObject({ forge: "gitlab", host: "gitlab.com" }); + expect(probeForge).not.toHaveBeenCalled(); + + const selfHosted = await resolver.resolveHost("GIT.ACME.INTERNAL"); + expect(selfHosted).toMatchObject({ forge: "gitlab", host: "git.acme.internal" }); + expect(selfHosted?.service).toBe(cloud?.service); + expect(probeForge).toHaveBeenCalledTimes(1); + expect(resolver.getService("gitlab")).toBe(cloud?.service); + expect(created).toEqual(["gitlab"]); + + resolver.dispose(); + expect(disposed).toEqual(["gitlab"]); + }); }); diff --git a/packages/server/src/services/forge-resolver.ts b/packages/server/src/services/forge-resolver.ts index 0dff027bcd..ae90224168 100644 --- a/packages/server/src/services/forge-resolver.ts +++ b/packages/server/src/services/forge-resolver.ts @@ -1,5 +1,9 @@ import { LRUCache } from "lru-cache"; -import { parseGitRemoteLocation, type GitRemoteLocation } from "@getpaseo/protocol/git-remote"; +import { + normalizeHost, + parseGitRemoteLocation, + type GitRemoteLocation, +} from "@getpaseo/protocol/git-remote"; import { resolveSshHostname, type SshHostnameResolver } from "../utils/ssh-hostname.js"; import { defaultResolveRemoteUrl } from "./forge-cli-command.js"; import type { ForgeService } from "./forge-service.js"; @@ -24,6 +28,7 @@ export type ForgeHostProbe = (host: string) => Promise; export interface CreateForgeResolverOptions { resolveRemoteUrl?: (cwd: string) => Promise; createService?: (forge: string) => ForgeService | null; + matchHost?: (host: string) => string | null; probeForge?: ForgeHostProbe; resolveSshHostname?: SshHostnameResolver; now?: () => number; @@ -36,12 +41,18 @@ export interface ForgeResolver { resolveFromRemoteUrl(remoteUrl: string | null): ForgeResolution | null; /** Resolve from a known origin remote URL, running the per-host probe when needed. */ resolveFromRemoteUrlAsync(remoteUrl: string | null): Promise; + /** Resolve a forge directly by hostname (cloud match first, then cached async probe). */ + resolveHost(host: string): Promise; + /** Return the shared adapter instance for a known forge id without probing the network. */ + getService(forge: string): ForgeService | null; /** * Invalidate every cached adapter's state for a cwd. Routed through the * resolver so invalidation hits the same adapter instance the poller reads, * regardless of which forge backs the workspace. */ invalidate(cwd: string): void; + /** Dispose all instantiated forge adapters owned by this resolver. */ + dispose(): void; } // A positive probe (host IS a known forge) is cached permanently; a negative one @@ -64,6 +75,7 @@ export function forgeForHost(host: string): string | null { export function createForgeResolver(options: CreateForgeResolverOptions = {}): ForgeResolver { const resolveRemoteUrl = options.resolveRemoteUrl ?? defaultResolveRemoteUrl; const create = options.createService ?? createForgeService; + const matchHost = options.matchHost ?? forgeForHost; const probeForge = options.probeForge ?? probeRegisteredForgeHost; const resolveSsh = options.resolveSshHostname ?? resolveSshHostname; const now = options.now ?? Date.now; @@ -110,22 +122,34 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F if (!entry) { return undefined; } - if (entry.expiresAt !== null && Date.now() >= entry.expiresAt) { + if (entry.expiresAt !== null && now() >= entry.expiresAt) { probedForgeByHost.delete(host); return undefined; } return entry.forge; } - function buildResolution(forge: string, host: string): ForgeResolution | null { - let service = services.get(forge); + function getService(forge: string): ForgeService | null { + const normalized = forge.trim().toLowerCase(); + if (!normalized) { + return null; + } + let service = services.get(normalized); if (!service) { - const created = create(forge); + const created = create(normalized); if (!created) { return null; } service = created; - services.set(forge, service); + services.set(normalized, service); + } + return service; + } + + function buildResolution(forge: string, host: string): ForgeResolution | null { + const service = getService(forge); + if (!service) { + return null; } return { forge, host, service }; } @@ -151,7 +175,7 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F } probedForgeByHost.set(host, { forge: probed, - expiresAt: probed === null ? Date.now() + NEGATIVE_PROBE_TTL_MS : null, + expiresAt: probed === null ? now() + NEGATIVE_PROBE_TTL_MS : null, }); return probed; } finally { @@ -170,13 +194,29 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F if (!host) { return null; } - const forge = forgeForHost(host) ?? readFreshProbe(host) ?? null; + const forge = matchHost(host) ?? readFreshProbe(host) ?? null; if (!forge) { return null; } return buildResolution(forge, host); } + async function resolveHost(host: string): Promise { + const normalized = normalizeHost(host); + if (!normalized) { + return null; + } + const directForge = matchHost(normalized); + if (directForge) { + return buildResolution(directForge, normalized); + } + const probed = await probeHostForge(normalized); + if (!probed) { + return null; + } + return buildResolution(probed, normalized); + } + async function resolveFromRemoteUrlAsync( remoteUrl: string | null, ): Promise { @@ -187,7 +227,7 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F if (!location) { return null; } - const directForge = forgeForHost(location.host); + const directForge = matchHost(location.host); if (directForge) { return buildResolution(directForge, location.host); } @@ -215,7 +255,7 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F } probedForgeByHost.set(aliasHost, { forge, - expiresAt: forge === null ? Date.now() + NEGATIVE_PROBE_TTL_MS : null, + expiresAt: forge === null ? now() + NEGATIVE_PROBE_TTL_MS : null, }); } @@ -231,7 +271,7 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F return { host: location.host }; } - const resolvedForge = forgeForHost(resolvedHost); + const resolvedForge = matchHost(resolvedHost); if (resolvedForge) { return { host: resolvedHost, forge: resolvedForge }; } @@ -246,10 +286,19 @@ export function createForgeResolver(options: CreateForgeResolverOptions = {}): F } } + function dispose(): void { + for (const service of services.values()) { + service.dispose?.(); + } + } + return { resolveFromRemoteUrl, resolveFromRemoteUrlAsync, + resolveHost, + getService, invalidate, + dispose, async resolve(cwd: string): Promise { return resolveFromRemoteUrlAsync(await resolveRemoteUrlCached(cwd)); }, diff --git a/packages/server/src/services/forge-service.ts b/packages/server/src/services/forge-service.ts index 264ed7acd6..ae410e075e 100644 --- a/packages/server/src/services/forge-service.ts +++ b/packages/server/src/services/forge-service.ts @@ -1,4 +1,5 @@ import type { ForgeSearchKind } from "@getpaseo/protocol/messages"; +import type { ChangeRequestSource } from "./change-request-source.js"; export type ForgeSearchRequestKind = ForgeSearchKind | "github-issue" | "github-pr" | "pr"; @@ -533,6 +534,8 @@ export interface ForgeService { onError?: (error: unknown) => void; }, ): { unsubscribe: () => void }; + /** Exact-URL change request observation adapter for watches, when supported by the forge. */ + changeRequests?: ChangeRequestSource; invalidate(options: { cwd: string }): void; dispose?(): void; } diff --git a/packages/server/src/services/gitlab-change-requests.test.ts b/packages/server/src/services/gitlab-change-requests.test.ts new file mode 100644 index 0000000000..68bb40a8ac --- /dev/null +++ b/packages/server/src/services/gitlab-change-requests.test.ts @@ -0,0 +1,1311 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { createChangeRequestRouter } from "./change-request-router.js"; +import type { ChangeRequestSnapshot } from "./change-request-source.js"; +import { createForgeResolver } from "./forge-resolver.js"; +import { + describeGitLabChangeRequest, + gitlabChangeRequestHost, + resolveGitLabChangeRequest, +} from "./gitlab-change-requests.js"; +import { createGitLabService, type GlabCommandRunner } from "./gitlab-service.js"; + +function httpJson( + body: unknown, + headers: Record = {}, + status = "200 OK", +): { stdout: string; stderr: string } { + const lines = [`HTTP/2.0 ${status}`]; + for (const [key, value] of Object.entries(headers)) { + lines.push(`${key}: ${value}`); + } + return { + stdout: `${lines.join("\r\n")}\r\n\r\n${JSON.stringify(body)}`, + stderr: "", + }; +} + +function createFakeGlabRunner( + handler: ( + args: string[], + options: { cwd: string; envOverlay?: Record }, + ) => Promise<{ stdout: string; stderr: string }> | { stdout: string; stderr: string }, +) { + const calls: Array<{ args: string[]; cwd: string; envOverlay?: Record }> = []; + const runner: GlabCommandRunner = async (args, options) => { + calls.push({ args: [...args], cwd: options.cwd, envOverlay: options.envOverlay }); + return await handler(args, options); + }; + return { runner, calls }; +} + +afterEach(() => { + vi.useRealTimers(); +}); + +describe("GitLab change request URL resolution and description", () => { + it("canonicalizes standard, nested-group, legacy, tab, query, and hash URLs while preserving path casing", () => { + expect( + resolveGitLabChangeRequest( + "https://GitLab.com/MyOrg/SubGroup/MyRepo/-/merge_requests/042/diffs?view=inline#note_9", + ), + ).toBe("https://gitlab.com/MyOrg/SubGroup/MyRepo/-/merge_requests/42"); + + expect( + resolveGitLabChangeRequest("https://gitlab.com/Team/App/merge_requests/7/pipelines/"), + ).toBe("https://gitlab.com/Team/App/-/merge_requests/7"); + + expect( + resolveGitLabChangeRequest( + "https://GitLab.Example.Internal:8443/Group/Sub/Repo/-/merge_requests/12/commits", + ), + ).toBe("https://gitlab.example.internal:8443/Group/Sub/Repo/-/merge_requests/12"); + + expect(resolveGitLabChangeRequest("http://gitlab.lan/Group/Repo/-/merge_requests/3")).toBe( + "http://gitlab.lan/Group/Repo/-/merge_requests/3", + ); + }); + + it("describes cloud and self-hosted GitLab MRs with native !N numbering", () => { + expect( + describeGitLabChangeRequest("https://gitlab.com/MyOrg/SubGroup/MyRepo/-/merge_requests/42"), + ).toBe("MyOrg/SubGroup/MyRepo !42"); + expect( + describeGitLabChangeRequest( + "https://gitlab.example.internal:8443/Group/Repo/-/merge_requests/12", + ), + ).toBe("gitlab.example.internal:8443/Group/Repo !12"); + expect( + gitlabChangeRequestHost( + "https://gitlab.example.internal:8443/Group/Repo/-/merge_requests/12", + ), + ).toBe("gitlab.example.internal:8443"); + }); + + it("rejects invalid GitLab MR URLs", () => { + for (const invalid of [ + "https://gitlab.com/only-one-segment/-/merge_requests/1", + "https://gitlab.com/org/repo/-/issues/1", + "https://gitlab.com/org/repo/-/merge_requests/0", + "https://gitlab.com/org/repo/-/merge_requests/abc", + "https://gitlab.com/org/../repo/-/merge_requests/1", + "https://gitlab.com/org/repo/-/merge_requests/1/unknown-tab", + "http://gitlab.com/org/repo/-/merge_requests/1", + "https://gitlab.com/org/repo/-/something/merge_requests/1", + ]) { + expect(() => resolveGitLabChangeRequest(invalid), invalid).toThrow( + /GitLab merge request URL/i, + ); + } + }); +}); + +describe("GitLab ChangeRequestSource polling", () => { + it("uses cwd-free glab api --hostname, maps head-pipeline jobs, discussions, and approval transitions without reviewDecision", async () => { + vi.useFakeTimers(); + let poll = 0; + const mrUrl = "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/merge_requests/42"; + const encodedProject = encodeURIComponent("MyOrg/SubGroup/MyRepo"); + + const { runner, calls } = createFakeGlabRunner((args) => { + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encodedProject}/merge_requests/42`) { + poll += 1; + return httpJson({ + iid: 42, + state: "opened", + sha: "sha-1", + web_url: mrUrl, + target_project_id: 900, + head_pipeline: { + id: 500, + project_id: 901, + sha: "merge-sha-1", + status: poll === 1 ? "running" : "success", + web_url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/pipelines/500", + }, + }); + } + if (endpoint.startsWith("projects/901/pipelines/500/jobs")) { + return httpJson( + [ + { + id: 10, + name: "lint", + stage: "test", + status: "failed", + allow_failure: true, + web_url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/10", + }, + { + id: 11, + name: "unit", + stage: "test", + status: poll === 1 ? "running" : "success", + allow_failure: false, + web_url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/11", + }, + { + id: 12, + name: "deploy", + stage: "deploy", + status: "manual", + allow_failure: true, + web_url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/12", + }, + ], + { "X-Next-Page": "" }, + ); + } + if (endpoint.startsWith(`projects/${encodedProject}/merge_requests/42/discussions`)) { + const notes = + poll === 1 + ? [ + { + id: 100, + system: false, + body: "Initial comment", + author: { username: "bob" }, + }, + { + id: 101, + system: false, + type: "DiffNote", + body: "Inline note", + position: { new_path: "src/app.ts", new_line: 10 }, + author: { username: "bob" }, + }, + { + id: 102, + system: true, + body: "assigned to @bob", + author: { username: "bob" }, + }, + ] + : [ + { + id: 100, + system: false, + body: "Initial comment", + author: { username: "bob" }, + }, + { + id: 101, + system: false, + type: "DiffNote", + body: "Inline note", + position: { new_path: "src/app.ts", new_line: 10 }, + author: { username: "bob" }, + }, + { + id: 103, + system: true, + body: "approved this merge request", + author: { username: "alice" }, + }, + ]; + return httpJson([{ id: "disc-1", notes }], { "X-Next-Page": "" }); + } + if (endpoint === `projects/${encodedProject}/merge_requests/42/approvals`) { + return httpJson( + poll === 1 + ? { approvals_required: 1, approvals_left: 1, approved: false, approved_by: [] } + : { + approvals_required: 1, + approvals_left: 1, + approved: false, + approved_by: [ + { user: { id: 1, username: "alice" }, approved_at: "2026-11-20T06:00:00Z" }, + ], + }, + ); + } + throw new Error(`Unexpected glab args: ${args.join(" ")}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + resolveRemoteUrl: async () => { + throw new Error("watch must not inspect local git remote"); + }, + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + const seen = new Set(); + try { + const release = service.changeRequests!.observe(mrUrl, { + knownActivity: () => seen, + async snapshot(s) { + snapshots.push(s); + for (const item of s.activity) seen.add(item.id); + }, + error(err) { + errors.push(err); + }, + }); + + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([]); + expect(snapshots).toHaveLength(1); + expect(snapshots[0]).toEqual({ + state: "open", + headSha: "sha-1", + checksState: "pending", + checks: [ + { + id: "job:10", + name: "lint", + state: "neutral", + url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/10", + }, + { + id: "job:11", + name: "unit", + state: "pending", + url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/11", + }, + { + id: "job:12", + name: "deploy", + state: "neutral", + url: "https://gitlab.com/MyOrg/SubGroup/MyRepo/-/jobs/12", + }, + ], + reviewDecision: null, + activity: [ + { id: "comment:100", kind: "comment", url: `${mrUrl}#note_100` }, + { id: "inline:101", kind: "inline comment", url: `${mrUrl}#note_101` }, + ], + }); + // Every glab api call targets --hostname gitlab.com explicitly. + for (const call of calls) { + expect(call.args.slice(0, 4)).toEqual(["api", "--hostname", "gitlab.com", "-i"]); + } + + await vi.advanceTimersByTimeAsync(10_000); + expect(errors).toEqual([]); + expect(snapshots).toHaveLength(2); + expect(snapshots[1]!.checksState).toBe("passed"); + expect(snapshots[1]!.reviewDecision).toBeNull(); + const approvalEvents = snapshots[1]!.activity.filter((a) => a.kind === "approval"); + expect(approvalEvents).toEqual([ + { + id: "approval-note:103", + kind: "approval", + url: `${mrUrl}#note_103`, + message: `Merge request approved by @alice (0/1 approvals): ${mrUrl}#note_103`, + }, + ]); + release(); + } finally { + service.dispose?.(); + } + }); + + it("reports unapproval and approval-reset system notes and ignores 429/rate-limit text inside 200 OK payloads", async () => { + vi.useFakeTimers(); + let poll = 0; + const mrUrl = "https://gitlab.com/org/repo/-/merge_requests/429"; + const encoded = encodeURIComponent("org/repo"); + let approvalCalls = 0; + + const { runner } = createFakeGlabRunner((args) => { + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encoded}/merge_requests/429`) { + poll += 1; + return httpJson({ + iid: 429, + title: "Fix rate limit handling for HTTP 429 responses", + description: "Too many requests should trigger backoff", + state: "opened", + sha: "abc", + web_url: mrUrl, + head_pipeline: null, + }); + } + if (endpoint.startsWith(`projects/${encoded}/merge_requests/429/discussions`)) { + const notes: Array<{ + id: number; + system: boolean; + body: string; + author: { username: string }; + }> = [ + { + id: 1, + system: false, + body: "We hit HTTP 429 and rate limit errors in CI", + author: { username: "dev" }, + }, + ]; + if (poll >= 2) { + notes.push({ + id: 2, + system: true, + body: "unapproved this merge request", + author: { username: "carol" }, + }); + } + if (poll >= 3) { + notes.push({ + id: 3, + system: true, + body: "reset approvals from @carol by pushing to the branch", + author: { username: "dev" }, + }); + } + return httpJson([{ id: "d1", notes }], { "X-Next-Page": "" }); + } + if (endpoint === `projects/${encoded}/merge_requests/429/approvals`) { + approvalCalls += 1; + return httpJson({ approvals_required: 1, approvals_left: 1, approved_by: [] }); + } + throw new Error(`Unexpected: ${endpoint}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const seen = new Set(); + try { + service.changeRequests!.observe(mrUrl, { + knownActivity: () => seen, + async snapshot(s) { + snapshots.push(s); + for (const item of s.activity) seen.add(item.id); + }, + error(err) { + throw err; + }, + }); + await vi.advanceTimersByTimeAsync(25); + expect(snapshots).toHaveLength(1); + expect(approvalCalls).toBe(0); + expect(snapshots[0]!.activity).toEqual([ + { id: "comment:1", kind: "comment", url: `${mrUrl}#note_1` }, + ]); + + await vi.advanceTimersByTimeAsync(10_000); + expect(approvalCalls).toBe(1); + expect(snapshots[1]!.activity).toContainEqual({ + id: "approval-note:2", + kind: "approval", + url: `${mrUrl}#note_2`, + message: `Merge request approval removed by @carol (0/1 approvals): ${mrUrl}#note_2`, + }); + + await vi.advanceTimersByTimeAsync(10_000); + expect(approvalCalls).toBe(2); + expect(snapshots[2]!.activity).toContainEqual({ + id: "approval-note:3", + kind: "approval", + url: `${mrUrl}#note_3`, + message: `Merge request approvals reset by @dev (0/1 approvals): ${mrUrl}#note_3`, + }); + } finally { + service.dispose?.(); + } + }); + + it("paginates jobs and discussions, rejects mid-pagination headSha changes, and treats partial errors as stale", async () => { + vi.useFakeTimers(); + let mrReads = 0; + let discussionPage2Fails = true; + const mrUrl = "https://gitlab.com/org/repo/-/merge_requests/8"; + const encoded = encodeURIComponent("org/repo"); + + const { runner } = createFakeGlabRunner((args) => { + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encoded}/merge_requests/8`) { + mrReads += 1; + // 1st poll: headSha changes mid-pagination (read 1 -> sha-1, recheck read 2 -> sha-2) + if (mrReads === 1) { + return httpJson({ + iid: 8, + state: "opened", + sha: "sha-1", + web_url: mrUrl, + target_project_id: 1, + head_pipeline: { id: 20, project_id: 1, status: "running" }, + }); + } + return httpJson({ + iid: 8, + state: "opened", + sha: "sha-2", + web_url: mrUrl, + target_project_id: 1, + head_pipeline: { id: 21, project_id: 1, status: "failed" }, + }); + } + if (endpoint.includes("/pipelines/20/jobs") && endpoint.endsWith("&page=1")) { + return httpJson( + [{ id: 1, name: "j1", stage: "test", status: "running", allow_failure: false }], + { "X-Next-Page": "2" }, + ); + } + if (endpoint.includes("/pipelines/20/jobs") && endpoint.endsWith("&page=2")) { + return httpJson( + [{ id: 2, name: "j2", stage: "test", status: "success", allow_failure: false }], + { "X-Next-Page": "" }, + ); + } + if (endpoint.includes("/pipelines/21/jobs")) { + return httpJson( + [{ id: 3, name: "j3", stage: "test", status: "failed", allow_failure: false }], + { "X-Next-Page": "" }, + ); + } + if (endpoint.includes("/discussions") && endpoint.endsWith("&page=1")) { + return httpJson([{ id: "d1", notes: [{ id: 1, system: false, body: "p1" }] }], { + "X-Next-Page": "2", + }); + } + if (endpoint.includes("/discussions") && endpoint.endsWith("&page=2")) { + if (discussionPage2Fails) { + discussionPage2Fails = false; + throw Object.assign(new Error("500"), { code: 1, stderr: "500 Internal Server Error" }); + } + return httpJson([{ id: "d2", notes: [{ id: 2, system: false, body: "p2" }] }], { + "X-Next-Page": "", + }); + } + if (endpoint.endsWith("/approvals")) { + return httpJson({ approvals_required: 0, approvals_left: 0, approved_by: [] }); + } + throw new Error(`Unexpected: ${endpoint}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: Error[] = []; + try { + service.changeRequests!.observe(mrUrl, { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + errors.push(err as Error); + }, + }); + + // Poll 1 fails because headSha changed during multi-page job pagination + await vi.advanceTimersByTimeAsync(25); + expect(snapshots).toHaveLength(0); + expect(errors).toHaveLength(1); + expect(errors[0]!.message).toMatch(/changed during pagination/i); + + // Poll 2 (after 10s backoff) fails because discussion page 2 returns 500 — must NOT emit partial snapshot + await vi.advanceTimersByTimeAsync(10_000); + expect(snapshots).toHaveLength(0); + expect(errors).toHaveLength(2); + + // Poll 3 (after 20s exponential backoff) succeeds with complete paginated discussions and failed check + await vi.advanceTimersByTimeAsync(20_000); + expect(snapshots).toHaveLength(1); + expect(snapshots[0]).toMatchObject({ + headSha: "sha-2", + checksState: "failed", + checks: [{ id: "job:3", name: "j3", state: "failed" }], + activity: [ + { id: "comment:1", kind: "comment", url: `${mrUrl}#note_1` }, + { id: "comment:2", kind: "comment", url: `${mrUrl}#note_2` }, + ], + }); + } finally { + service.dispose?.(); + } + }); + + it("honors a self-hosted rate limit before retrying the merge request", async () => { + vi.useFakeTimers(); + vi.setSystemTime(1_700_000_000_000); + const mrUrl = "https://git.corp.internal/team/app/-/merge_requests/3"; + const encoded = encodeURIComponent("team/app"); + let mrCalls = 0; + + const { runner } = createFakeGlabRunner((args) => { + if (args[0] === "auth" && args[1] === "status") { + return { stdout: "Logged in", stderr: "" }; + } + if (args[0] === "config") return { stdout: "", stderr: "" }; + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encoded}/merge_requests/3`) { + mrCalls += 1; + if (mrCalls === 1) { + throw Object.assign(new Error("429"), { + code: 1, + stdout: + 'HTTP/2.0 429 Too Many Requests\r\nRetry-After: 45\r\n\r\n{"message":"Too many requests"}', + stderr: "429 Too Many Requests", + }); + } + return httpJson({ + iid: 3, + state: "opened", + sha: "xyz", + web_url: mrUrl, + head_pipeline: null, + }); + } + if (endpoint.includes("/discussions")) { + return httpJson([], { "X-Next-Page": "" }); + } + if (endpoint.endsWith("/approvals")) { + throw Object.assign(new Error("404"), { code: 1, stderr: "404 Not Found" }); + } + throw new Error(`Unexpected: ${args.join(" ")}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + const release = service.changeRequests!.observe(mrUrl, { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + errors.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(25); + expect(mrCalls).toBe(1); + expect(errors).toHaveLength(1); + + // Still paused before 45s Retry-After expires + await vi.advanceTimersByTimeAsync(44_999); + expect(mrCalls).toBe(1); + + // Resumes when the server's Retry-After interval expires. + await vi.advanceTimersByTimeAsync(1); + expect(mrCalls).toBe(2); + expect(snapshots).toHaveLength(1); + expect(snapshots[0]!.headSha).toBe("xyz"); + release(); + } finally { + service.dispose?.(); + } + }); + + it("stops paginating immediately when released mid-read and surfaces failed bridge jobs", async () => { + vi.useFakeTimers(); + const mrUrl = "https://gitlab.com/org/repo/-/merge_requests/15"; + const encoded = encodeURIComponent("org/repo"); + let releaseFirstPage!: () => void; + let discussionPage2Called = false; + + const { runner } = createFakeGlabRunner(async (args) => { + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encoded}/merge_requests/15`) { + return httpJson({ + iid: 15, + state: "opened", + sha: "sha-bridge", + web_url: mrUrl, + target_project_id: 7, + head_pipeline: { + id: 88, + project_id: 7, + status: "failed", + web_url: "https://gitlab.com/org/repo/-/pipelines/88", + }, + }); + } + if (endpoint.includes("/pipelines/88/jobs")) { + return httpJson( + [{ id: 1, name: "prepare", stage: "prep", status: "success", allow_failure: false }], + { "X-Next-Page": "" }, + ); + } + if (endpoint.includes("/pipelines/88/bridges")) { + return httpJson( + [ + { + id: 2, + name: "downstream-e2e", + stage: "trigger", + status: "failed", + allow_failure: false, + web_url: "https://gitlab.com/org/repo/-/jobs/2", + }, + ], + { "X-Next-Page": "" }, + ); + } + if (endpoint.includes("/discussions") && endpoint.endsWith("&page=1")) { + await new Promise((resolve) => { + releaseFirstPage = resolve; + }); + return httpJson([{ id: "d1", notes: [{ id: 10, system: false, body: "hi" }] }], { + "X-Next-Page": "2", + }); + } + if (endpoint.includes("/discussions") && endpoint.endsWith("&page=2")) { + discussionPage2Called = true; + return httpJson([], { "X-Next-Page": "" }); + } + if (endpoint.endsWith("/approvals")) { + return httpJson({ approvals_required: 0, approvals_left: 0, approved_by: [] }); + } + throw new Error(`Unexpected: ${endpoint}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + const release = service.changeRequests!.observe(mrUrl, { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + errors.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(25); + await vi.waitFor(() => expect(releaseFirstPage).toBeTypeOf("function")); + release(); + releaseFirstPage(); + await vi.advanceTimersByTimeAsync(0); + expect(discussionPage2Called).toBe(false); + expect(snapshots).toEqual([]); + expect(errors).toEqual([]); + + // Re-observe without releasing early to verify failed bridge check is surfaced. + service.changeRequests!.observe(mrUrl, { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + errors.push(err); + }, + }); + await vi.advanceTimersByTimeAsync(25); + await vi.waitFor(() => expect(releaseFirstPage).toBeTypeOf("function")); + releaseFirstPage(); + await vi.advanceTimersByTimeAsync(0); + expect(discussionPage2Called).toBe(true); + expect(snapshots).toHaveLength(1); + expect(snapshots[0]!.checks).toContainEqual({ + id: "bridge:2", + name: "downstream-e2e", + state: "failed", + url: "https://gitlab.com/org/repo/-/jobs/2", + }); + } finally { + service.dispose?.(); + } + }); + + it.each([ + { subfolder: "/apps/gitlab/", apiHost: "gitlab.example.org" }, + { subfolder: "", apiHost: "gitlab.example.org/apps/gitlab" }, + ])( + "looks up the GitLab project with profile $subfolder $apiHost", + async ({ subfolder, apiHost }) => { + vi.useFakeTimers(); + const url = "https://gitlab.example.org/apps/gitlab/team/app/-/merge_requests/1"; + const { runner } = createFakeGlabRunner((args) => { + if (args[0] === "auth") return { stdout: "Logged in", stderr: "" }; + if (args[0] === "config") { + expect(args.slice(-2)).toEqual(["--host", "gitlab.example.org"]); + if (args[2] === "subfolder") return { stdout: subfolder, stderr: "" }; + if (args[2] === "api_host") return { stdout: apiHost, stderr: "" }; + throw new Error(`Unexpected profile key: ${args[2]}`); + } + if (args.at(-1) === "projects/team%2Fapp/merge_requests/1") { + return httpJson({ iid: 1, state: "opened", web_url: url, sha: "intended-project" }); + } + if (args.at(-1)?.startsWith("projects/team%2Fapp/merge_requests/1/discussions?")) { + return httpJson([]); + } + return httpJson({ message: "404 Project Not Found" }, {}, "404 Not Found"); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe(url, { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([]); + expect(snapshots).toEqual([expect.objectContaining({ headSha: "intended-project" })]); + } finally { + service.dispose?.(); + } + }, + ); + + it("rejects a different GitLab project on the same origin", async () => { + vi.useFakeTimers(); + const { runner, calls } = createFakeGlabRunner((args) => { + if (args[0] === "auth" || args[0] === "config") return { stdout: "", stderr: "" }; + return httpJson({ + iid: 1, + state: "merged", + sha: "other-project-head", + web_url: "https://gitlab.example.org/gitlab/gitlab/team/app/-/merge_requests/1", + }); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe( + "https://gitlab.example.org/gitlab/team/app/-/merge_requests/1", + { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }, + ); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([ + expect.objectContaining({ message: expect.stringContaining("different project") }), + ]); + expect(snapshots).toEqual([]); + expect(calls.filter(({ args }) => args[0] === "api")).toHaveLength(1); + } finally { + service.dispose?.(); + } + }); + + it.each(["500 Internal Server Error", "429 Too Many Requests"])( + "reports the merge and new notes if approval enrichment returns %s", + async (status) => { + vi.useFakeTimers(); + const url = "https://gitlab.com/team/app/-/merge_requests/1"; + let polls = 0; + const { runner, calls } = createFakeGlabRunner((args) => { + const endpoint = args.at(-1)!; + if (endpoint.endsWith("/merge_requests/1")) { + polls += 1; + return httpJson({ + iid: 1, + state: polls === 1 ? "opened" : "merged", + sha: "head", + web_url: url, + }); + } + if (endpoint.includes("/discussions?")) { + return httpJson( + polls === 1 + ? [] + : [ + { + id: "d", + notes: [ + { id: 10, body: "Done" }, + { + id: 11, + system: true, + body: "approved this merge request", + author: { username: "alice" }, + }, + ], + }, + ], + ); + } + if (endpoint.endsWith("/approvals")) { + return httpJson({ message: status }, { "Retry-After": "60" }, status); + } + throw new Error(`Unexpected GitLab endpoint: ${endpoint}`); + }); + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + const observer = { + knownActivity: () => new Set(), + async snapshot(snapshot: ChangeRequestSnapshot) { + snapshots.push(snapshot); + }, + error: (error: unknown) => errors.push(error), + }; + try { + const release = service.changeRequests!.observe(url, observer); + await vi.advanceTimersByTimeAsync(25); + await vi.advanceTimersByTimeAsync(10_000); + expect(errors).toEqual([]); + expect(snapshots.at(-1)).toMatchObject({ + state: "merged", + activity: [ + { id: "comment:10", kind: "comment" }, + { + id: "approval-note:11", + kind: "approval", + message: `Merge request approved by @alice: ${url}#note_11`, + }, + ], + }); + release(); + if (status.startsWith("429")) { + service.changeRequests!.observe(url, observer); + await vi.advanceTimersByTimeAsync(59_999); + expect( + calls.filter(({ args }) => args.at(-1)?.endsWith("/merge_requests/1")), + ).toHaveLength(2); + await vi.advanceTimersByTimeAsync(1); + expect( + calls.filter(({ args }) => args.at(-1)?.endsWith("/merge_requests/1")), + ).toHaveLength(3); + } + } finally { + service.dispose?.(); + } + }, + ); + + it("rejects a merge request served by a different GitLab web origin", async () => { + vi.useFakeTimers(); + const { runner, calls } = createFakeGlabRunner((args) => { + if (args[0] === "auth" || args[0] === "config") return { stdout: "", stderr: "" }; + return httpJson({ + iid: 1, + state: "merged", + sha: "other-head", + web_url: "https://gitlab.example.org:3443/team/app/-/merge_requests/1", + }); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe( + "https://gitlab.example.org:9443/team/app/-/merge_requests/1", + { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }, + ); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([ + expect.objectContaining({ message: expect.stringContaining("web origin") }), + ]); + expect(snapshots).toEqual([]); + expect(calls.filter(({ args }) => args[0] === "api")).toHaveLength(1); + } finally { + service.dispose?.(); + } + }); + + it.each(["403 Forbidden", "404 Not Found"])( + "publishes a merge and its discussion when pipeline jobs return %s", + async (deniedStatus) => { + vi.useFakeTimers(); + const url = "https://gitlab.com/team/app/-/merge_requests/1"; + let polls = 0; + const { runner } = createFakeGlabRunner((args) => { + const endpoint = args.at(-1)!; + if (endpoint === "projects/team%2Fapp/merge_requests/1") { + polls += 1; + return httpJson({ + iid: 1, + state: polls === 1 ? "opened" : "merged", + sha: "head", + web_url: url, + head_pipeline: { id: 3, status: polls === 1 ? "running" : "failed" }, + }); + } + if (endpoint.includes("/jobs?")) { + return polls === 1 + ? httpJson([{ id: 4, name: "test", status: "running" }]) + : httpJson({ message: deniedStatus }, {}, deniedStatus); + } + if (endpoint.includes("/discussions?")) { + return httpJson(polls === 1 ? [] : [{ id: "d", notes: [{ id: 7, body: "Done" }] }]); + } + throw new Error(`Unexpected GitLab endpoint: ${endpoint}`); + }); + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe(url, { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }); + await vi.advanceTimersByTimeAsync(25); + await vi.advanceTimersByTimeAsync(10_000); + expect(errors).toEqual([]); + expect(snapshots).toHaveLength(2); + expect(snapshots[1]).toMatchObject({ + state: "merged", + checksState: "failed", + checks: [{ id: "pipeline:3", name: "Pipeline #3", state: "failed" }], + activity: [{ id: "comment:7", kind: "comment", url: `${url}#note_7` }], + }); + } finally { + service.dispose?.(); + } + }, + ); + + it("reads merge updates when GitLab's authentication status endpoint is down", async () => { + vi.useFakeTimers(); + const url = "https://gitlab.example.org/team/app/-/merge_requests/1"; + let merged = false; + const { runner, calls } = createFakeGlabRunner((args) => { + if (args[0] === "auth") { + if (merged) throw new Error("GitLab /user returned 403"); + return { stdout: "", stderr: "" }; + } + if (args[0] === "config") return { stdout: "", stderr: "" }; + if (args.at(-1)?.includes("/discussions?")) return httpJson([]); + return httpJson({ iid: 1, state: merged ? "merged" : "opened", sha: "head", web_url: url }); + }); + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe(url, { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }); + await vi.advanceTimersByTimeAsync(25); + merged = true; + await vi.advanceTimersByTimeAsync(10_000); + expect(errors).toEqual([]); + expect(snapshots.map(({ state }) => state)).toEqual(["open", "merged"]); + expect(calls.filter(({ args }) => args[0] === "auth")).toEqual([]); + } finally { + service.dispose?.(); + } + }); + + it.each(["401 Unauthorized", "403 Forbidden", "404 Not Found"])( + "uses a valid bare profile after the port profile returns %s", + async (denial) => { + vi.useFakeTimers(); + const host = "gitlab.example.org"; + const authority = `${host}:3443`; + const url = `https://${authority}/team/app/-/merge_requests/1`; + const { runner, calls } = createFakeGlabRunner((args, options) => { + if (args[0] === "config") { + return { stdout: args[2] === "api_host" ? authority : "", stderr: "" }; + } + if (options.envOverlay?.GITLAB_HOST === authority) { + return httpJson({ message: denial }, {}, denial); + } + if (args.at(-1)?.includes("/discussions?")) return httpJson([]); + return httpJson({ iid: 1, state: "opened", sha: "valid-profile", web_url: url }); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe(url, { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([]); + expect(snapshots[0]?.headSha).toBe("valid-profile"); + expect( + calls + .filter(({ args }) => args[0] === "api") + .map(({ envOverlay }) => envOverlay?.GITLAB_HOST), + ).toEqual([authority, host, host]); + } finally { + service.dispose?.(); + } + }, + ); + + it("lets an admitted poll finish when the MR read leaves one request", async () => { + vi.useFakeTimers(); + const now = 1_700_000_000_000; + vi.setSystemTime(now); + const url = "https://gitlab.com/team/app/-/merge_requests/1"; + const { runner, calls } = createFakeGlabRunner((args) => { + if (args.at(-1)?.includes("/discussions?")) return httpJson([]); + return httpJson( + { iid: 1, state: "opened", sha: "head", web_url: url }, + { "RateLimit-Remaining": "1", "RateLimit-Reset": String(now / 1000 + 90) }, + ); + }); + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + try { + service.changeRequests!.observe(url, { + async snapshot(snapshot) { + snapshots.push(snapshot); + }, + error: (error) => errors.push(error), + }); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([]); + expect(snapshots).toHaveLength(1); + expect(calls).toHaveLength(2); + await vi.advanceTimersByTimeAsync(89_974); + expect(calls).toHaveLength(2); + await vi.advanceTimersByTimeAsync(1); + expect(calls).toHaveLength(4); + } finally { + service.dispose?.(); + } + }); + + it.each([2, 3])("lets each watch finish across quota windows with %i requests", async (quota) => { + vi.useFakeTimers(); + const start = 1_700_000_000_000; + vi.setSystemTime(start); + const url = "https://gitlab.com/team/app/-/merge_requests"; + let window = -1; + let requests = 0; + const { runner } = createFakeGlabRunner((args) => { + const currentWindow = Math.floor((Date.now() - start) / 60_000); + if (window !== currentWindow) { + window = currentWindow; + requests = 0; + } + requests += 1; + if (requests > quota) { + return httpJson({}, { "Retry-After": "60" }, "429 Too Many Requests"); + } + const endpoint = args.at(-1)!; + if (endpoint.includes("/discussions?")) return httpJson([]); + const iid = Number(endpoint.at(-1)); + return httpJson({ + iid, + state: iid === 2 ? "merged" : "opened", + sha: "head", + web_url: `${url}/${iid}`, + }); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: string[] = []; + const failures: number[] = []; + try { + for (const iid of [1, 2]) { + service.changeRequests!.observe(`${url}/${iid}`, { + async snapshot(snapshot) { + snapshots.push(`${iid}:${snapshot.state}`); + }, + error() { + failures.push(iid); + }, + }); + } + await vi.advanceTimersByTimeAsync(25); + expect(snapshots).toEqual(quota === 3 ? ["1:open"] : []); + expect(failures).toEqual(quota === 3 ? [2] : [1, 2]); + await vi.advanceTimersByTimeAsync(quota === 3 ? 60_000 : 120_000); + expect(snapshots).toEqual(["1:open", "2:merged"]); + } finally { + service.dispose?.(); + } + }); + + it("stops an overlapping poll before it starts another GitLab API request during a pause", async () => { + vi.useFakeTimers(); + const url = "https://gitlab.com/team/app/-/merge_requests"; + let releaseMr!: () => void; + const firstMr = new Promise((resolve) => { + releaseMr = resolve; + }); + const { runner, calls } = createFakeGlabRunner(async (args) => { + const endpoint = args.at(-1)!; + if (endpoint.endsWith("/merge_requests/1")) { + await firstMr; + return httpJson({ iid: 1, state: "opened", sha: "head", web_url: `${url}/1` }); + } + if (endpoint.endsWith("/merge_requests/2")) { + return httpJson({}, { "Retry-After": "90" }, "429 Too Many Requests"); + } + return httpJson([]); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + const observer = { + async snapshot(snapshot: ChangeRequestSnapshot) { + snapshots.push(snapshot); + }, + error: (error: unknown) => errors.push(error), + }; + try { + service.changeRequests!.observe(`${url}/1`, observer); + service.changeRequests!.observe(`${url}/2`, observer); + await vi.advanceTimersByTimeAsync(25); + releaseMr(); + await vi.advanceTimersByTimeAsync(0); + expect(errors).toHaveLength(2); + expect(snapshots).toEqual([]); + expect(calls.map(({ args }) => args.at(-1))).toEqual([ + "projects/team%2Fapp/merge_requests/1", + "projects/team%2Fapp/merge_requests/2", + ]); + } finally { + service.dispose?.(); + } + }); + + it("rereads the profile's subfolder when the installer changes it", async () => { + vi.useFakeTimers(); + let folder = "gitlab"; + const mrUrl = () => `https://gitlab.example.org/${folder}/team/app/-/merge_requests/1`; + const { runner } = createFakeGlabRunner((args) => { + if (args[0] === "auth") return { stdout: "", stderr: "" }; + if (args[0] === "config") + return { stdout: args[2] === "subfolder" ? folder : "", stderr: "" }; + const endpoint = args.at(-1)!; + if (endpoint === "projects/team%2Fapp/merge_requests/1") { + return httpJson({ iid: 1, state: "opened", web_url: mrUrl(), sha: folder }); + } + if (endpoint.startsWith("projects/team%2Fapp/merge_requests/1/discussions?")) { + return httpJson([]); + } + return httpJson({ message: "Project not found" }, {}, "404 Not Found"); + }); + const service = createGitLabService({ runner, resolveGlabPath: async () => "glab" }); + const snapshots: ChangeRequestSnapshot[] = []; + const errors: unknown[] = []; + const observer = { + async snapshot(snapshot: ChangeRequestSnapshot) { + snapshots.push(snapshot); + }, + error: (error: unknown) => errors.push(error), + }; + try { + const release = service.changeRequests!.observe(mrUrl(), observer); + await vi.advanceTimersByTimeAsync(25); + release(); + folder = "new"; + service.changeRequests!.observe(mrUrl(), observer); + await vi.advanceTimersByTimeAsync(25); + expect(errors).toEqual([]); + expect(snapshots.map(({ headSha }) => headSha)).toEqual(["gitlab", "new"]); + } finally { + service.dispose?.(); + } + }); + + it.each(["bare", "bare with API host override", "port"])( + "watches a custom-port instance with a %s CLI profile", + async (profile) => { + vi.useFakeTimers(); + const mrUrl = "https://gitlab.example.org:3443/team/app/-/merge_requests/1"; + const bare = "gitlab.example.org"; + const authority = `${bare}:3443`; + const profileHost = profile === "port" ? authority : bare; + const encoded = encodeURIComponent("team/app"); + const states = ["opened", "locked", "merged"] as const; + let poll = 0; + + const { runner } = createFakeGlabRunner((args, options) => { + if (args[0] === "config") { + const override = + profile === "bare with API host override" && options.envOverlay?.GITLAB_API_HOST !== ""; + const apiHost = + args[2] === "api_host" && (override || args.at(-1) === profileHost) ? authority : ""; + return { stdout: apiHost, stderr: "" }; + } + const expected = + profile === "port" ? ["api", "-i"] : ["api", "--hostname", profileHost, "-i"]; + expect(args.slice(0, -1)).toEqual(expected); + expect(options.envOverlay?.GITLAB_HOST).toBe(profileHost); + const endpoint = args.at(-1) ?? ""; + if (endpoint === `projects/${encoded}/merge_requests/1`) { + const state = states[Math.min(poll, states.length - 1)]!; + poll += 1; + return httpJson({ + iid: 1, + state, + merged_at: state === "merged" ? "2026-11-20T08:00:00Z" : null, + sha: "sha-merge", + web_url: mrUrl, + head_pipeline: null, + }); + } + if (endpoint.includes("/discussions")) { + return httpJson([], { "X-Next-Page": "" }); + } + throw new Error(`Unexpected: ${args.join(" ")}`); + }); + + const service = createGitLabService({ + runner, + resolveGlabPath: async () => "glab", + watchPollMs: 10_000, + }); + const probedHosts: string[] = []; + const forgeResolver = createForgeResolver({ + probeForge: async (host) => { + probedHosts.push(host); + return host === profileHost ? "gitlab" : null; + }, + createService: (forge) => (forge === "gitlab" ? service : null), + }); + const router = createChangeRequestRouter({ forgeResolver }); + const snapshots: ChangeRequestSnapshot[] = []; + try { + const resolved = await router.resolve(`${mrUrl}/diffs`); + expect(resolved).toEqual({ forge: "gitlab", url: mrUrl }); + expect(probedHosts).toEqual(profile === "port" ? [authority] : [authority, bare]); + + const release = router.source(resolved.forge)!.observe(resolved.url, { + async snapshot(s) { + snapshots.push(s); + }, + error(err) { + throw err; + }, + }); + await vi.advanceTimersByTimeAsync(25); + await vi.advanceTimersByTimeAsync(10_000); + await vi.advanceTimersByTimeAsync(10_000); + expect(snapshots.map((s) => s.state)).toEqual(["open", "open", "merged"]); + release(); + } finally { + forgeResolver.dispose(); + } + }, + ); +}); diff --git a/packages/server/src/services/gitlab-change-requests.ts b/packages/server/src/services/gitlab-change-requests.ts new file mode 100644 index 0000000000..65d67bae18 --- /dev/null +++ b/packages/server/src/services/gitlab-change-requests.ts @@ -0,0 +1,917 @@ +import { z } from "zod"; +import { normalizeHost } from "@getpaseo/protocol/git-remote"; +import { GITLAB_ACTIVE_PIPELINE_STATUS_SET } from "@getpaseo/protocol/gitlab-pipeline"; +import { + ChangeRequestRateLimitError, + createPerHostChangeRequestPoller, + parseCliHttpResponse, + parseRetryAfterMs, + type PerHostReadContext, +} from "./change-request-poller.js"; +import type { ChangeRequestSnapshot, ChangeRequestSource } from "./change-request-source.js"; + +const GITLAB_CLOUD_HOST = "gitlab.com"; +const GITLAB_PAGE_SIZE = 100; +const GITLAB_MR_TABS = new Set(["diffs", "commits", "pipelines", "builds", "reports"]); + +const checkUrlSchema = z.url({ protocol: /^https?$/ }); + +const gitLabWatchPipelineSchema = z + .object({ + id: z.number(), + project_id: z.number().nullable().optional(), + sha: z.string().nullable().optional(), + status: z.string(), + web_url: z.string().nullable().optional(), + }) + .passthrough(); + +const gitLabWatchMrSchema = z + .object({ + iid: z.number(), + state: z.string(), + sha: z.string().nullable().optional(), + diff_refs: z + .object({ + head_sha: z.string().nullable().optional(), + }) + .passthrough() + .nullable() + .optional(), + web_url: checkUrlSchema, + target_project_id: z.number().nullable().optional(), + merged_at: z.string().nullable().optional(), + head_pipeline: gitLabWatchPipelineSchema.nullable().optional(), + }) + .passthrough(); + +const gitLabWatchJobSchema = z + .object({ + id: z.number(), + name: z.string(), + status: z.string(), + allow_failure: z.boolean().optional(), + web_url: z.string().nullable().optional(), + }) + .passthrough(); + +const gitLabWatchNoteAuthorSchema = z + .object({ + username: z.string().optional(), + name: z.string().optional(), + }) + .passthrough(); + +const gitLabWatchNoteSchema = z + .object({ + id: z.number(), + body: z.string().nullable().optional(), + system: z.boolean().optional(), + type: z.string().nullable().optional(), + position: z.unknown().nullable().optional(), + author: gitLabWatchNoteAuthorSchema.nullable().optional(), + }) + .passthrough(); + +const gitLabWatchDiscussionSchema = z + .object({ + id: z.string(), + notes: z.array(gitLabWatchNoteSchema).optional().default([]), + }) + .passthrough(); + +const gitLabWatchApprovalsSchema = z + .object({ + approvals_required: z.number().nullable().optional(), + approvals_left: z.number().nullable().optional(), + }) + .passthrough(); + +type GitLabWatchMr = z.infer; +type GitLabWatchJob = z.infer; +type GitLabWatchNote = z.infer; +type GitLabWatchDiscussion = z.infer; +type GitLabWatchApprovals = z.infer; + +interface GitLabMergeRequestIdentity { + protocol: "http:" | "https:"; + host: string; + port: string; + hostWithPort: string; + projectPath: string; + iid: number; +} + +function invalidGitLabUrl(): never { + throw new Error("Enter a GitLab merge request URL."); +} + +function parseGitLabUrlOrigin(input: string): { + protocol: "http:" | "https:"; + host: string; + port: string; + hostWithPort: string; + pathname: string; +} { + const trimmed = input.trim(); + if (!trimmed || /\s/.test(trimmed)) invalidGitLabUrl(); + let parsed: URL; + try { + parsed = new URL(trimmed); + } catch { + invalidGitLabUrl(); + } + const protocol = parsed.protocol.toLowerCase(); + if ((protocol !== "https:" && protocol !== "http:") || parsed.username || parsed.password) { + invalidGitLabUrl(); + } + const host = normalizeHost(parsed.hostname); + if (!host || (host === GITLAB_CLOUD_HOST && protocol !== "https:")) { + invalidGitLabUrl(); + } + return { + protocol: protocol as "http:" | "https:", + host, + port: parsed.port, + hostWithPort: parsed.port ? `${host}:${parsed.port}` : host, + pathname: parsed.pathname, + }; +} + +function decodeGitLabProjectPath(rawSegments: string[]): string { + if (rawSegments.length < 2) invalidGitLabUrl(); + const decodedSegments: string[] = []; + for (const segment of rawSegments) { + let decoded: string; + try { + decoded = decodeURIComponent(segment); + } catch { + invalidGitLabUrl(); + } + const invalidSegment = + !decoded || + decoded === "." || + decoded === ".." || + decoded === "-" || + decoded.includes("/") || + decoded.includes("\\"); + if (invalidSegment) invalidGitLabUrl(); + decodedSegments.push(decoded); + } + return decodedSegments.join("/"); +} + +function parseGitLabMergeRequestUrl(input: string): GitLabMergeRequestIdentity { + const origin = parseGitLabUrlOrigin(input); + const rawSegments = origin.pathname.split("/").filter((segment) => segment.length > 0); + if (rawSegments.length < 4) invalidGitLabUrl(); + + // Strip an optional browser tab suffix (e.g. /diffs, /commits, /pipelines). + if ( + rawSegments.length >= 5 && + GITLAB_MR_TABS.has(rawSegments[rawSegments.length - 1]!.toLowerCase()) && + /^\d+$/.test(rawSegments[rawSegments.length - 2]!) + ) { + rawSegments.pop(); + } + + const iidRaw = rawSegments.pop(); + const resource = rawSegments.pop(); + if (!iidRaw || !/^\d+$/.test(iidRaw) || resource?.toLowerCase() !== "merge_requests") { + invalidGitLabUrl(); + } + const iid = Number(iidRaw); + if (!Number.isInteger(iid) || iid < 1 || iid > 2_147_483_647) { + invalidGitLabUrl(); + } + + // Modern GitLab URLs place `-` before `merge_requests`; legacy URLs omit it. + if (rawSegments[rawSegments.length - 1] === "-") { + rawSegments.pop(); + } + + return { + protocol: origin.protocol, + host: origin.host, + port: origin.port, + hostWithPort: origin.hostWithPort, + projectPath: decodeGitLabProjectPath(rawSegments), + iid, + }; +} + +export function resolveGitLabChangeRequest(input: string): string { + const { protocol, hostWithPort, projectPath, iid } = parseGitLabMergeRequestUrl(input); + return `${protocol}//${hostWithPort}/${projectPath}/-/merge_requests/${iid}`; +} + +export function gitlabChangeRequestHost(url: string): string { + return parseGitLabMergeRequestUrl(url).hostWithPort; +} + +export function describeGitLabChangeRequest(url: string): string { + const { host, port, hostWithPort, projectPath, iid } = parseGitLabMergeRequestUrl(url); + const project = + host === GITLAB_CLOUD_HOST && !port ? projectPath : `${hostWithPort}/${projectPath}`; + return `${project} !${iid}`; +} + +function mapGitLabMrState(mr: GitLabWatchMr): ChangeRequestSnapshot["state"] { + const state = mr.state.toLowerCase(); + // GitLab reports "locked" while a merge is actively in progress; treat it as + // open so polling continues until GitLab transitions to "merged" (or "closed"). + if (state === "opened" || state === "locked") return "open"; + if (state === "merged" || mr.merged_at != null) return "merged"; + return "closed"; +} + +function mapGitLabPipelineState( + status: string | null | undefined, +): ChangeRequestSnapshot["checksState"] { + if (!status) return "neutral"; + const normalized = status.toLowerCase(); + if (normalized === "success" || normalized === "passed") return "passed"; + if (normalized === "failed") return "failed"; + if (normalized === "manual" || GITLAB_ACTIVE_PIPELINE_STATUS_SET.has(normalized)) { + return "pending"; + } + return "neutral"; +} + +function mapGitLabJobState(job: GitLabWatchJob): ChangeRequestSnapshot["checks"][number]["state"] { + const raw = job.status.toLowerCase(); + const allowFailure = job.allow_failure ?? false; + if (raw === "failed") return allowFailure ? "neutral" : "failed"; + if (raw === "success" || raw === "passed") return "passed"; + if (raw === "manual") return allowFailure ? "neutral" : "pending"; + if (GITLAB_ACTIVE_PIPELINE_STATUS_SET.has(raw)) return "pending"; + return "neutral"; +} + +function toWatchCheck( + job: GitLabWatchJob, + prefix: "job" | "bridge", +): ChangeRequestSnapshot["checks"][number] { + const url = checkUrlSchema.safeParse(job.web_url).data; + return { + id: `${prefix}:${job.id}`, + name: job.name, + state: mapGitLabJobState(job), + ...(url ? { url } : {}), + }; +} + +type ApprovalNoteAction = "approved" | "unapproved" | "reset"; + +interface ParsedApprovalNote { + id: string; + action: ApprovalNoteAction; + actor: string | null; + url: string; +} + +function parseGitLabApprovalNote( + note: GitLabWatchNote, + mrWebUrl: string, +): ParsedApprovalNote | null { + if (note.system !== true) return null; + const body = note.body?.trim().toLowerCase() ?? ""; + if (!body) return null; + + let action: ApprovalNoteAction | null = null; + if (body === "approved this merge request" || body.startsWith("approved this merge request")) { + action = "approved"; + } else if ( + body === "unapproved this merge request" || + body.startsWith("unapproved this merge request") || + body.startsWith("removed approval") || + body.startsWith("revoked approval") + ) { + action = "unapproved"; + } else if (body.startsWith("reset approvals")) { + action = "reset"; + } + if (!action) return null; + + const actor = note.author?.username?.trim() || note.author?.name?.trim() || null; + return { + id: `approval-note:${note.id}`, + action, + actor, + url: `${mrWebUrl}#note_${note.id}`, + }; +} + +interface ApprovalCount { + given: number; + required: number; +} + +function summarizeApprovalCount(approvals: GitLabWatchApprovals): ApprovalCount | null { + const { approvals_required: required, approvals_left: remaining } = approvals; + if (required == null || remaining == null) return null; + return { given: Math.max(0, required - remaining), required }; +} + +function formatApprovalCount(summary: ApprovalCount | null): string { + if (!summary || summary.required <= 0) return ""; + return ` (${summary.given}/${summary.required} approvals)`; +} + +function formatApprovalActor(actor: string | null): string { + if (!actor) return ""; + return actor.startsWith("@") ? actor : `@${actor}`; +} + +function formatApprovalNoteMessage( + note: ParsedApprovalNote, + summary: ApprovalCount | null, +): string { + const actor = formatApprovalActor(note.actor); + const count = formatApprovalCount(summary); + if (note.action === "approved") { + return `Merge request approved${actor ? ` by ${actor}` : ""}${count}: ${note.url}`; + } + if (note.action === "unapproved") { + return `Merge request approval removed${actor ? ` by ${actor}` : ""}${count}: ${note.url}`; + } + return `Merge request approvals reset${actor ? ` by ${actor}` : ""}${count}: ${note.url}`; +} + +interface ParsedDiscussionItems { + humanActivity: ChangeRequestSnapshot["activity"]; + approvalNotes: ParsedApprovalNote[]; +} + +function parseDiscussionItems( + discussions: GitLabWatchDiscussion[], + mrWebUrl: string, +): ParsedDiscussionItems { + const humanActivity: ChangeRequestSnapshot["activity"] = []; + const approvalNotes: ParsedApprovalNote[] = []; + + for (const discussion of discussions) { + for (const note of discussion.notes) { + if (note.system === true) { + const approvalNote = parseGitLabApprovalNote(note, mrWebUrl); + if (approvalNote) approvalNotes.push(approvalNote); + continue; + } + const isInline = note.position != null || note.type === "DiffNote"; + humanActivity.push({ + id: isInline ? `inline:${note.id}` : `comment:${note.id}`, + kind: isInline ? "inline comment" : "comment", + url: `${mrWebUrl}#note_${note.id}`, + }); + } + } + + return { humanActivity, approvalNotes }; +} + +function resolvePipelineProjectId( + pipeline: z.infer, + mr: GitLabWatchMr, + encodedProject: string, +): string { + if (pipeline.project_id != null) return String(pipeline.project_id); + if (mr.target_project_id != null) return String(mr.target_project_id); + return encodedProject; +} + +function pipelineFailureCheck( + pipeline: z.infer, +): ChangeRequestSnapshot["checks"][number] { + const url = checkUrlSchema.safeParse(pipeline.web_url).data; + return { + id: `pipeline:${pipeline.id}`, + name: `Pipeline #${pipeline.id}`, + state: "failed", + ...(url ? { url } : {}), + }; +} + +function createRateLimitError( + host: string, + nowMs: number, + headers: ReadonlyMap, +): ChangeRequestRateLimitError { + const resetAtMs = parseRetryAfterMs(headers, nowMs, { + forceOnMissing: true, + defaultPauseMs: 60_000, + })!; + return new ChangeRequestRateLimitError({ host, resetAtMs }); +} + +function extractRateLimitFromFailedCommand( + host: string, + nowMs: number, + status: number | null, + headers: ReadonlyMap, + text: string, +): ChangeRequestRateLimitError | null { + if (status !== null) { + return status === 429 ? createRateLimitError(host, nowMs, headers) : null; + } + if (!/\b(429|too many requests|rate limit)\b/i.test(text)) return null; + return createRateLimitError(host, nowMs, headers); +} + +export interface CreateGitLabChangeRequestSourceOptions { + runCommand( + args: string[], + options: { cwd: string; envOverlay?: Record }, + ): Promise<{ stdout: string; stderr: string }>; + isAuthError(error: unknown): boolean; + isCommandError(error: unknown): error is Error & { stderr: string; stdout?: string }; + createAuthError(stderr: string): Error; + createCommandError(params: { + args: string[]; + cwd: string; + exitCode: number | null; + stderr: string; + stdout?: string; + }): Error; + now?: () => number; + pollIntervalMs?: number; + initialDelayMs?: number; +} + +export function createGitLabChangeRequestSource( + options: CreateGitLabChangeRequestSourceOptions, +): ChangeRequestSource & { dispose(): void } { + const now = options.now ?? Date.now; + + async function readConfig( + host: string, + key: string, + envOverlay: Record = {}, + ): Promise { + const config = await options.runCommand(["config", "get", key, "--host", host], { + cwd: process.cwd(), + envOverlay: { GITLAB_HOST: host, ...envOverlay }, + }); + return config.stdout.trim(); + } + + async function preferredProfileHost(identity: GitLabMergeRequestIdentity): Promise { + const { host, hostWithPort } = identity; + if (hostWithPort === host) return host; + + // An API host override applies to either profile. Use the configured value + // to decide which profile to try first, then check access through the MR. + const exactProfile = await readConfig(hostWithPort, "api_host", { GITLAB_API_HOST: "" }); + return exactProfile ? hostWithPort : host; + } + + async function readApiPrefix(profileHost: string): Promise { + if (profileHost === GITLAB_CLOUD_HOST) return ""; + const trimSlashes = /^\/+|\/+$/g; + let prefix = (await readConfig(profileHost, "subfolder")).replace(trimSlashes, ""); + if (!prefix) { + const apiHost = await readConfig(profileHost, "api_host"); + if (apiHost) { + const apiUrl = new URL(apiHost.includes("://") ? apiHost : `https://${apiHost}`); + prefix = apiUrl.pathname.replace(trimSlashes, ""); + } + } + return decodeURI(prefix); + } + + function parseJsonResponseBody(params: { + args: string[]; + cwd: string; + body: string; + schema: z.ZodType; + }): T { + let json: unknown; + try { + json = JSON.parse(params.body); + } catch { + throw options.createCommandError({ + args: params.args, + cwd: params.cwd, + exitCode: null, + stderr: `glab did not return valid JSON (${params.body.length} bytes)`, + }); + } + const validated = params.schema.safeParse(json); + if (!validated.success) { + throw options.createCommandError({ + args: params.args, + cwd: params.cwd, + exitCode: null, + stderr: `glab JSON did not match the expected schema: ${validated.error.message}`, + }); + } + return validated.data; + } + + function checkHttpStatus(params: { + host: string; + args: string[]; + cwd: string; + status: number | null; + headers: ReadonlyMap; + body: string; + rawStdout: string; + rawStderr: string; + pauseHost(untilMs: number): void; + }): void { + // Only decide rate limits from the HTTP status code on parsed responses so + // ordinary MR titles, comments, or IIDs mentioning "429" or "rate limit" + // never trip a false rate-limit pause. + if (params.status === null) return; + if (params.status === 429) { + const error = createRateLimitError(params.host, now(), params.headers); + params.pauseHost(error.resetAtMs); + throw error; + } + if (params.status === 401) { + throw options.createAuthError(params.body || params.rawStderr || "401 Unauthorized"); + } + if (params.status < 200 || params.status >= 300) { + throw options.createCommandError({ + args: params.args, + cwd: params.cwd, + exitCode: null, + stderr: `HTTP ${params.status}: ${params.body || params.rawStderr}`.trim(), + stdout: params.rawStdout, + }); + } + } + + async function apiGet(params: { + host: string; + endpoint: string; + schema: z.ZodType; + poll: PerHostReadContext; + }): Promise<{ data: T; headers: ReadonlyMap }> { + if (!params.poll.isObserved()) throw new Error("Change request observation ended"); + params.poll.assertHostAvailable(); + // glab api rejects a port in --hostname. GITLAB_HOST selects that profile. + const args = params.host.includes(":") + ? ["api", "-i", params.endpoint] + : ["api", "--hostname", params.host, "-i", params.endpoint]; + const cwd = process.cwd(); + let result: { stdout: string; stderr: string }; + try { + result = await options.runCommand(args, { + cwd, + envOverlay: { GITLAB_HOST: params.host }, + }); + } catch (error) { + if (options.isCommandError(error)) { + const parsedError = error.stdout ? parseCliHttpResponse(error.stdout) : null; + const rateLimit = extractRateLimitFromFailedCommand( + params.host, + now(), + parsedError?.status ?? null, + parsedError?.headers ?? new Map(), + `${error.stderr}\n${error.stdout ?? ""}`, + ); + if (rateLimit) { + params.poll.pauseHost(rateLimit.resetAtMs); + throw rateLimit; + } + } + throw error; + } + + const parsed = parseCliHttpResponse(result.stdout); + checkHttpStatus({ + host: params.host, + args, + cwd, + status: parsed.status, + headers: parsed.headers, + body: parsed.body, + rawStdout: result.stdout, + rawStderr: result.stderr, + pauseHost: params.poll.pauseHost, + }); + + const proactivePause = parseRetryAfterMs(parsed.headers, now()); + if (proactivePause !== null) { + params.poll.deferPollsUntil(proactivePause); + } + + const data = parseJsonResponseBody({ args, cwd, body: parsed.body, schema: params.schema }); + return { data, headers: parsed.headers }; + } + + async function apiGetAllPages(params: { + host: string; + endpoint: string; + itemSchema: z.ZodType; + poll: PerHostReadContext; + }): Promise<{ items: T[]; pagesRead: number }> { + const items: T[] = []; + const schema = z.array(params.itemSchema); + const seenPages = new Set([1]); + let page = 1; + let pagesRead = 0; + + while (true) { + if (!params.poll.isObserved()) { + return { items, pagesRead }; + } + const sep = params.endpoint.includes("?") ? "&" : "?"; + const response = await apiGet({ + host: params.host, + endpoint: `${params.endpoint}${sep}per_page=${GITLAB_PAGE_SIZE}&page=${page}`, + schema, + poll: params.poll, + }); + pagesRead += 1; + items.push(...response.data); + + const nextPageHeader = response.headers.get("x-next-page"); + if (nextPageHeader !== undefined) { + const trimmed = nextPageHeader.trim(); + if (!trimmed) break; + const nextPage = Number(trimmed); + if (!Number.isInteger(nextPage) || nextPage <= 0 || seenPages.has(nextPage)) { + throw new Error("GitLab returned an invalid pagination header."); + } + seenPages.add(nextPage); + page = nextPage; + } else { + if (response.data.length < GITLAB_PAGE_SIZE) break; + page += 1; + seenPages.add(page); + } + } + + return { items, pagesRead }; + } + + function commandErrorStatus(error: unknown): number | null { + return options.isCommandError(error) && error.stdout + ? parseCliHttpResponse(error.stdout).status + : null; + } + + async function readMergeRequestMetadata(params: { + host: string; + identity: GitLabMergeRequestIdentity; + endpoint: string; + poll: PerHostReadContext; + }): Promise { + const { data: mr } = await apiGet({ + host: params.host, + endpoint: params.endpoint, + schema: gitLabWatchMrSchema, + poll: params.poll, + }); + const address = parseGitLabMergeRequestUrl(mr.web_url); + const expected = params.identity; + if (address.protocol !== expected.protocol || address.hostWithPort !== expected.hostWithPort) { + throw new Error( + `GitLab returned a merge request from a different web origin: ${address.protocol}//${address.hostWithPort}`, + ); + } + if (address.projectPath.toLowerCase() !== expected.projectPath.toLowerCase()) { + throw new Error("GitLab returned a merge request from a different project."); + } + if (address.iid !== expected.iid || mr.iid !== expected.iid) { + throw new Error("GitLab returned a different merge request number."); + } + return mr; + } + + function pipelineDetailsUnavailable(error: unknown): boolean { + const status = commandErrorStatus(error); + return status === 403 || status === 404; + } + + async function readPipelineChecks(params: { + host: string; + identity: GitLabMergeRequestIdentity; + mrEndpoint: string; + encodedProject: string; + mr: GitLabWatchMr; + headSha: string; + poll: PerHostReadContext; + }): Promise { + const pipeline = params.mr.head_pipeline; + if (!pipeline) return []; + + const pipelineProject = resolvePipelineProjectId(pipeline, params.mr, params.encodedProject); + let jobsResult: { items: GitLabWatchJob[]; pagesRead: number }; + try { + jobsResult = await apiGetAllPages({ + host: params.host, + endpoint: `projects/${pipelineProject}/pipelines/${pipeline.id}/jobs`, + itemSchema: gitLabWatchJobSchema, + poll: params.poll, + }); + } catch (error) { + if (!pipelineDetailsUnavailable(error)) throw error; + // The MR can expose pipeline status even when its jobs are restricted. + return mapGitLabPipelineState(pipeline.status) === "failed" + ? [pipelineFailureCheck(pipeline)] + : []; + } + if (!params.poll.isObserved()) { + throw new Error("Change request observation ended"); + } + + const checks = jobsResult.items.map((job) => toWatchCheck(job, "job")); + let fetchedBridges = false; + if ( + mapGitLabPipelineState(pipeline.status) === "failed" && + !checks.some((check) => check.state === "failed") + ) { + fetchedBridges = true; + await appendFailedBridgeOrPipelineChecks({ + host: params.host, + pipelineProject, + pipeline, + checks, + poll: params.poll, + }); + } + + // Guard against headSha or head_pipeline shifting while paginating check pages. + if (jobsResult.pagesRead > 1 || fetchedBridges) { + const recheck = await readMergeRequestMetadata({ + host: params.host, + identity: params.identity, + endpoint: params.mrEndpoint, + poll: params.poll, + }); + const recheckSha = recheck.sha || recheck.diff_refs?.head_sha || ""; + if ( + recheckSha !== params.headSha || + recheck.head_pipeline?.id !== pipeline.id || + recheck.head_pipeline?.status !== pipeline.status + ) { + throw new Error("The merge request head or pipeline changed during pagination."); + } + } + return checks; + } + + async function appendFailedBridgeOrPipelineChecks(params: { + host: string; + pipelineProject: string; + pipeline: z.infer; + checks: ChangeRequestSnapshot["checks"]; + poll: PerHostReadContext; + }): Promise { + try { + const bridgesResult = await apiGetAllPages({ + host: params.host, + endpoint: `projects/${params.pipelineProject}/pipelines/${params.pipeline.id}/bridges`, + itemSchema: gitLabWatchJobSchema, + poll: params.poll, + }); + for (const bridge of bridgesResult.items) { + params.checks.push(toWatchCheck(bridge, "bridge")); + } + } catch (error) { + if (!pipelineDetailsUnavailable(error)) throw error; + } + if (!params.checks.some((check) => check.state === "failed")) { + params.checks.push(pipelineFailureCheck(params.pipeline)); + } + } + + async function readOptionalApprovalCount(params: { + host: string; + endpoint: string; + poll: PerHostReadContext; + }): Promise { + try { + const { data: approvals } = await apiGet({ + host: params.host, + endpoint: params.endpoint, + schema: gitLabWatchApprovalsSchema, + poll: params.poll, + }); + return summarizeApprovalCount(approvals); + } catch { + // The approval system note is complete without an API count. + return null; + } + } + + async function readInitialMergeRequest( + identity: GitLabMergeRequestIdentity, + poll: PerHostReadContext, + ) { + async function readUsing(host: string) { + const { projectPath, iid } = identity; + const prefix = await readApiPrefix(host); + const relativePath = + prefix && projectPath.startsWith(`${prefix}/`) + ? projectPath.slice(prefix.length + 1) + : projectPath; + const encodedProject = encodeURIComponent( + relativePath.includes("/") ? relativePath : projectPath, + ); + const mrEndpoint = `projects/${encodedProject}/merge_requests/${iid}`; + const mr = await readMergeRequestMetadata({ host, identity, endpoint: mrEndpoint, poll }); + return { host, encodedProject, mrEndpoint, mr }; + } + + const preferred = await preferredProfileHost(identity); + try { + return await readUsing(preferred); + } catch (error) { + const status = commandErrorStatus(error); + const accessDenied = options.isAuthError(error) || status === 403 || status === 404; + if (preferred === identity.host || !accessDenied) throw error; + return readUsing(identity.host); + } + } + + async function readMergeRequest(context: PerHostReadContext): Promise { + const identity = parseGitLabMergeRequestUrl(context.url); + const { iid } = identity; + const { host, encodedProject, mrEndpoint, mr } = await readInitialMergeRequest( + identity, + context, + ); + const headSha = mr.sha || mr.diff_refs?.head_sha || ""; + if (!headSha) { + throw new Error(`GitLab merge request !${iid} did not include a head commit SHA.`); + } + + const checks = await readPipelineChecks({ + host, + identity, + mrEndpoint, + encodedProject, + mr, + headSha, + poll: context, + }); + const discussionsResult = await apiGetAllPages({ + host, + endpoint: `projects/${encodedProject}/merge_requests/${iid}/discussions`, + itemSchema: gitLabWatchDiscussionSchema, + poll: context, + }); + if (!context.isObserved()) { + throw new Error("Change request observation ended"); + } + + const { humanActivity, approvalNotes } = parseDiscussionItems( + discussionsResult.items, + mr.web_url, + ); + const known = context.knownActivity(); + const hasNewApprovalNote = + known !== undefined && approvalNotes.some((note) => !known.has(note.id)); + // Fetch /approvals only to enrich a newly observed approval system note with + // current counts — keeping seenActivity as the single persisted owner of + // approval transition baselines. + const approvalCount = hasNewApprovalNote + ? await readOptionalApprovalCount({ + host, + endpoint: `projects/${encodedProject}/merge_requests/${iid}/approvals`, + poll: context, + }) + : null; + + const activity: ChangeRequestSnapshot["activity"] = [ + ...humanActivity, + ...approvalNotes.map((note) => ({ + id: note.id, + kind: "approval" as const, + url: note.url, + message: formatApprovalNoteMessage(note, approvalCount), + })), + ]; + + return { + state: mapGitLabMrState(mr), + headSha, + checksState: mapGitLabPipelineState(mr.head_pipeline?.status), + checks, + reviewDecision: null, + activity, + }; + } + + const poller = createPerHostChangeRequestPoller({ + hostOf: gitlabChangeRequestHost, + read: readMergeRequest, + now, + pollIntervalMs: options.pollIntervalMs, + initialDelayMs: options.initialDelayMs, + }); + + return { + resolve: resolveGitLabChangeRequest, + describe: describeGitLabChangeRequest, + observe(url, callbacks) { + return poller.observe(url, callbacks); + }, + dispose() { + poller.dispose(); + }, + }; +} diff --git a/packages/server/src/services/gitlab-service.ts b/packages/server/src/services/gitlab-service.ts index 25737e321e..6f65a127a3 100644 --- a/packages/server/src/services/gitlab-service.ts +++ b/packages/server/src/services/gitlab-service.ts @@ -60,6 +60,7 @@ import { CHECK_TRAIT_WARNING, } from "@getpaseo/protocol/check-traits"; import { GITLAB_ACTIVE_PIPELINE_STATUS_SET } from "@getpaseo/protocol/gitlab-pipeline"; +import { createGitLabChangeRequestSource } from "./gitlab-change-requests.js"; import { isGitLabStatusFacts, type GitLabStatusFacts } from "./gitlab-facts.js"; const GLAB_ENV = { @@ -112,6 +113,9 @@ export interface CreateGitLabServiceOptions { runner?: GlabCommandRunner; resolveGlabPath?: () => Promise; resolveRemoteUrl?: (cwd: string) => Promise; + now?: () => number; + watchPollMs?: number; + watchInitialDelayMs?: number; } const GitLabPipelineSchema = z @@ -925,19 +929,37 @@ export function createGitLabService(options: CreateGitLabServiceOptions = {}): F const resolveGlab = createCachedCliPathResolver(options.resolveGlabPath ?? resolveGlabPath); const resolveRemoteUrl = options.resolveRemoteUrl ?? defaultResolveRemoteUrl; - async function run(args: string[], runOptions: GlabCommandRunnerOptions): Promise { + async function runRaw( + args: string[], + runOptions: GlabCommandRunnerOptions, + ): Promise { const glabPath = await resolveGlab(); if (!glabPath) { throw new GlabCliMissingError(); } try { - const result = await runner(args, runOptions); - return result.stdout.trim(); + return await runner(args, runOptions); } catch (error) { throw glabCliRunner.normalizeError(error, { args, cwd: runOptions.cwd }); } } + async function run(args: string[], runOptions: GlabCommandRunnerOptions): Promise { + const result = await runRaw(args, runOptions); + return result.stdout.trim(); + } + + const changeRequests = createGitLabChangeRequestSource({ + runCommand: runRaw, + isAuthError: (error) => error instanceof GlabAuthenticationError, + isCommandError: (error): error is GlabCommandError => error instanceof GlabCommandError, + createAuthError: (stderr) => new GlabAuthenticationError({ stderr }), + createCommandError: (params) => new GlabCommandError(params), + now: options.now, + pollIntervalMs: options.watchPollMs, + initialDelayMs: options.watchInitialDelayMs, + }); + // Centralize parse + validate so a malformed or empty glab payload surfaces as // a classified GlabCommandError (with args + cwd) instead of a raw // SyntaxError/ZodError that bypasses the Glab* error classification. @@ -1486,6 +1508,12 @@ export function createGitLabService(options: CreateGitLabServiceOptions = {}): F return { success: true }; }, + changeRequests, + invalidate(_input: { cwd: string }): void {}, + + dispose(): void { + changeRequests.dispose(); + }, }; }