Compare commits

...
Author SHA1 Message Date
Mohamed Boudra 02e2c5f3ec fix(app): record a host eviction only after its delete commits
Eviction marked the host as evicted before deleteHost ran. If storage aborted
that delete, the rows stayed stored but the mark remained. A malformed-row
repair running behind it trusted the mark: it skipped the checkpoint update
yet still deleted the row, leaving a workspace cursor with no workspace. The
next refresh then resumed past that workspace.

The mark is now set after the delete commits, so it always matches storage.
A failed eviction leaves no mark, and the repair updates the row and its
cursor together.
2026-11-20 23:36:50 +07:00
Mohamed Boudra cf0c4243a3 fix(app): apply the eviction rule to malformed-row repairs
Repairing a malformed directory row reads the checkpoint, drops the affected
cursor, and writes the rest back. That write went straight to the row store
through queueOperation, outside the eviction check in the regular persist
path. If a write evicted the same host while the repair was reading, the
repair stored a checkpoint for a host with no directory rows, the state this
branch exists to prevent.

All three repair writes now go through one helper that runs in the serialized
writer and drops the checkpoint upsert when the host has been evicted since.
The deletes still apply.
2026-11-20 23:27:50 +07:00
Mohamed Boudra 1f28cde1a9 fix(app): advance directory cursors from live updates only after a refresh
A live update moved the directory cursor whenever no refresh was in flight,
including when no refresh had ever completed on the current subscriptions.
If the first sync after discarding a version 1 checkpoint failed, the next
project update saved a cursor at its own sequence with only that project
stored. The retry asked for changes after it, the daemon had none, and the
other projects stayed missing across restarts. The previous commit closed
this window during the cache load only; a failed refresh reopened it.

A live event's sequence covers everything before it only when a refresh
completed on the subscriptions that delivered it, which is what
satisfiedDemandSource already records. Live events now update rows either
way but move the cursor only in that state, so a cursor never skips changes
the client did not receive. Once a refresh completes, live updates advance
the cursor as before.

The sidebar recovery e2e fixture unwrapped the checkpoint only when it was
version 1, so the version 2 writer made it abort before damaging the cache.
It now reads either envelope and stores the damage as version 1, the format
this branch stops trusting, so the test fails against the previous cache
module and passes here.
2026-11-20 23:19:42 +07:00
Mohamed Boudra a138bea3ee fix(app): hold workspace updates that arrive while the directory cache loads
A project update received while the cached directory was being read went
straight into the replica. It advanced the project cursor before any refresh
had established a baseline, and saved that cursor to the cache. On an install
damaged before the eviction fix, that write replaced the version 1 checkpoint
with a version 2 one before the reader could reject it. The refresh then asked
for project changes after the live update, the daemon had none, and projects
missing from the cache stayed missing across restarts.

Agents already open a transaction before the cache load, so their updates are
recorded and applied over the refreshed snapshot. Workspaces now do the same.
An update during the load also stops the cached cursors being adopted, so the
refresh fetches full snapshots, which is the recovery a damaged cache needs.
2026-11-20 22:52:28 +07:00
Mohamed Boudra d328f0fbbf fix(app): restore a host's workspaces after its cached directory is evicted
A host could keep a current directory checkpoint in the replica cache with
none of the rows it described. The daemon then answered "no changes" since
that cursor, and the sidebar had no projects to place cached workspaces
under, so every workspace for that host vanished and stayed missing across
restarts.

The cache already refused mutations for an evicted host, but it checked at
the moment a mutation was queued. Eviction happens later, inside the
serialized write, so anything queued before the eviction ran was still
written, checkpoint included. The check moves into materializePendingChanges
so it runs in the same serialized step that evicts, which also covers an
eviction that lands while an earlier persist is still indexing the store.

Checking at queue time was also losing good data: once a host was
invalidated, every mutation was dropped, including updates accepted between
a reconnect's baseline replacement and its write. Those are kept now,
because the baseline is in the same pending batch.

Version 1 checkpoints could have been written by that path, so they are no
longer trusted. A reader discards them and keeps the cached rows, so the
sidebar still paints from cache and each host re-syncs its directory once.
Later launches resume incrementally.

Both the race and the recovery live in the shared cache, so this covers iOS,
Android, web, and Electron. It does not address two tabs or windows sharing
one IndexedDB database, where another connection evicts the host.
2026-11-20 22:39:05 +07:00
paseo-bot[bot] c853f98b90 fix(server): wait for restored checkout status on concurrent reads (#10304)
Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
2026-11-20 12:45:08 +00:00
Mohamed Boudra c3da87ee4c Support Gitea, Forgejo, and Codeberg change request watches (#10280)
* feat(server): add Gitea, Forgejo, and Codeberg support for change request watches

Implement the Gitea-family ChangeRequestSource using cwd-free tea login list and tea api --login -i requests, inspecting HTTP status codes directly on stdout/stderr rather than treating exit code 0 alone as success.

Reuse the shared per-host bounded poller for concurrency limits, 429 Retry-After pauses, and exponential backoff. Combine commit statuses and Actions tasks (including approval-held runs via need_approval and cross-repo waiting shadows) with mid-pagination headSha validation, and paginate comments, reviews, and inline review comments with bounded fan-out and quiet-poll caching.

* fix(server): address review findings on Gitea watch pagination, check sharing, and runs fetch

- Read /pulls/:index/reviews/:id/comments in a single request instead of a page loop because Gitea ignores page/limit on that endpoint and omits X-Total-Count/Link headers.
- Paginate /commits/:sha/status using the X-Total-Count response header instead of body total_count (which Gitea sets to the page length).
- Extract shared pure Gitea check schemas, shadow detection, rerun selection, and tea login matching into gitea-checks.ts imported by both gitea-service.ts and gitea-change-requests.ts.
- Remove the redundant /actions/runs fetch from the watch adapter because watch checks carry no action_required trait and approval-held tasks/pending shadows already map to pending.

* fix(server): preserve Gitea watch identity, checks, and backoff

* fix(server): route Gitea watches by base URL and retain complete updates

* fix(server): reconcile Gitea checks by job and refresh watch logins

* fix(server): validate Gitea and Forgejo Actions job responses

* fix(server): classify canceled native Actions statuses as neutral

* fix(server): drain failed Gitea polls before releasing host slots

* fix(server): let admitted Gitea polls finish with low quota

* fix(server): normalize Gitea watch URLs and refresh inline replies

* test(server): cover late Gitea rate limits across recovery windows
2026-11-20 18:41:41 +07:00
13 changed files with 3316 additions and 310 deletions
+7 -2
View File
@@ -582,12 +582,17 @@ host, kinds, and ids. Opening a cached workspace uses exact workspace and projec
directory scan. One invalid row is deleted and returned as a miss without affecting other rows.
An invalid directory row and its affected checkpoint cursor are repaired in one transaction, so a
later launch cannot accept a checkpoint for a partial baseline. Directory changes and their
checkpoint are also applied in one transaction. Reads wait for accepted changes to reach storage,
checkpoint are also applied in one transaction. A stored checkpoint carries a version. When a writer
bug could have stored a checkpoint without its directory, bump the version: older checkpoints are
rejected on read, cached rows still paint, and each host re-syncs its directory once. Reads wait for
accepted changes to reach storage,
then retry only when a requested row changes during the read. Unrelated agent streams do not
invalidate the read. Directory baseline replacements and host removal or renaming still invalidate
the affected reads.
The cache is capped at 32 MiB and evicts whole hosts in least-recently-written order. Budget
The cache is capped at 32 MiB and evicts whole hosts in least-recently-written order. After an
eviction, only a baseline replacement stores that host's directory again. Mutations queued around
the eviction are dropped when they are written, so a checkpoint never outlives its rows. Budget
bookkeeping may scan opaque row sizes during a deferred write, never during host registry startup or
before a requested cache row can paint. The row store is not encrypted. A cached timeline can contain
source code, prompts, and tool output; encrypted-at-rest storage is a separate security decision.
@@ -206,13 +206,14 @@ async function simulateDamagedLegacyDirectoryCache(
return;
}
const stored = JSON.parse(checkpoint.payload);
// Accept either writer so this fixture also reproduces on the unfixed client.
const cursors = stored.version === 1 ? stored.cursors : stored;
if (!cursors.workspaces?.generation) {
// Read the cursors from whichever version wrote them, then store them as version 1:
// the format whose writer could keep a checkpoint after evicting the rows it describes.
const cursors = typeof stored.version === "number" ? stored.cursors : stored;
if (!cursors?.workspaces?.generation) {
transaction.abort();
return;
}
store.put({ ...checkpoint, payload: JSON.stringify(cursors) });
store.put({ ...checkpoint, payload: JSON.stringify({ version: 1, cursors }) });
for (const row of rows) {
if (
row.serverId === serverId &&
@@ -1,4 +1,4 @@
import { expect, type Page } from "@playwright/test";
import { expect, test, type Page } from "@playwright/test";
import { openChangesPanel } from "./branch-switcher";
export async function restoreArchivedWorkspace(page: Page): Promise<void> {
@@ -32,7 +32,11 @@ export async function expectCommittedFile(page: Page, filename: string): Promise
}
export async function expectCommittedFileAfterReload(page: Page, filename: string): Promise<void> {
await expectCommittedFile(page, filename);
await page.reload();
await expectCommittedFile(page, filename);
await test.step("See the committed file immediately after Restore", async () => {
await expectCommittedFile(page, filename);
});
await test.step("Reload and see the same committed file", async () => {
await page.reload();
await expectCommittedFile(page, filename);
});
}
@@ -10,7 +10,10 @@ import type {
DaemonClient,
WorkspaceLabelListPayload,
} from "@getpaseo/client/internal/daemon-client";
import type { SessionOutboundMessage } from "@getpaseo/protocol/messages";
import type {
SessionOutboundMessage,
WorkspaceDescriptorPayload,
} from "@getpaseo/protocol/messages";
import {
normalizeProjectDescriptor,
normalizeWorkspaceDescriptor,
@@ -1667,6 +1670,187 @@ it("fills every cached workspace beneath live updates received during the SQLite
database.close();
});
/**
* A host whose cache was damaged before the eviction fix: workspaces survived, project rows did not,
* and a version 1 checkpoint still claims the project directory is current. The daemon holds
* projects A and B; renaming A is its update at `renameSeq`.
*/
async function createDamagedLegacyProjectDirectory(serverId: string, renameSeq: number) {
serverIds.add(serverId);
const { cache, database, storage, reopen, holdRead } = createSqliteCache();
cache.setHosts([serverId]);
const workspacePayload = (id: string, projectId: string): WorkspaceDescriptorPayload => ({
id,
projectId,
projectDisplayName: projectId,
projectRootPath: `/repo/${projectId}`,
workspaceDirectory: `/repo/${projectId}`,
projectKind: "git",
workspaceKind: "local_checkout",
name: id,
status: "done",
statusEnteredAt: null,
activityAt: null,
archivingAt: null,
diffStat: null,
scripts: [],
});
const project = (projectId: string, projectDisplayName: string) => ({
projectId,
projectDisplayName,
projectRootPath: `/repo/${projectId}`,
projectKind: "git" as const,
});
const workspaceEntries = [workspacePayload("wa", "A"), workspacePayload("wb", "B")];
cache.replaceDirectoryBaseline(serverId, {
agents: new Map(),
workspaces: new Map(
workspaceEntries.map((entry) => [entry.id, normalizeWorkspaceDescriptor(entry)]),
),
projects: new Map(),
});
await cache.flush();
await storage.apply({
deletes: [],
upserts: [
{
serverId,
kind: "checkpoint",
id: "singleton",
payload: JSON.stringify({
version: 1,
cursors: { projects: { generation: "g", afterSeq: renameSeq - 1 } },
}),
},
],
});
const reopened = reopen();
reopened.setHosts([serverId]);
const client = new FakeDirectoryClient();
let failNextProjectRequest = false;
client.listProjects = async (options?: unknown): Promise<ProjectListResult> => {
client.lastProjectOptions = options;
if (failNextProjectRequest) {
failNextProjectRequest = false;
throw new Error("Request timed out");
}
const resumesAfterRename =
(options as { sync?: { afterSeq?: number } } | undefined)?.sync?.afterSeq === renameSeq;
return resumesAfterRename
? {
requestId: "projects",
projects: [],
sync: { generation: "g", headSeq: renameSeq, mode: "changes", removals: [] },
}
: {
requestId: "projects",
projects: [project("A", "Renamed"), project("B", "B")],
sync: { generation: "g", headSeq: renameSeq, mode: "snapshot", removals: [] },
};
};
client.fetchWorkspaces = async (): Promise<WorkspaceFetchResult> => ({
requestId: "workspaces",
entries: workspaceEntries,
emptyProjects: [],
pageInfo: { hasMore: false, nextCursor: null, prevCursor: null },
sync: { generation: "g", headSeq: 2, mode: "snapshot", removals: [] },
});
const directory = new DirectorySync(
serverId,
{
onAgentStoppedRunning: () => {},
markAgentLoading: () => {},
markAgentReady: () => {},
markAgentError: () => {},
},
reopened,
);
const store = useSessionStore.getState();
store.initializeSession(serverId, client as unknown as DaemonClient, 1);
store.updateSessionServerInfo(serverId, {
serverId,
hostname: null,
version: "test",
features: { workspaceMultiplicity: true, directorySync: true, projectList: true },
});
directory.connectionChanged({
client: client as unknown as DaemonClient,
status: "online",
source: { clientGeneration: 1, connectionEpoch: 1 },
});
return {
client,
directory,
holdRead,
failNextProjectRequest: () => {
failNextProjectRequest = true;
},
renameProjectA: () =>
client.emit({
type: "project.update",
payload: {
kind: "upsert",
project: project("A", "Renamed"),
generation: "g",
seq: renameSeq,
},
}),
sessionProjectIds: () =>
[...useSessionStore.getState().sessions[serverId].projects.keys()].sort(),
persistedProjectIds: async () => {
await reopened.flush();
const restarted = reopen();
restarted.setHosts([serverId]);
return [...(await restarted.readDirectory(serverId)).projects.keys()].sort();
},
close: async () => {
directory.dispose();
await reopened.flush();
database.close();
},
};
}
it("re-syncs every project when a project update lands while an untrusted cache loads", async () => {
const damaged = await createDamagedLegacyProjectDirectory("untrusted-checkpoint-race", 6);
try {
let renamed = false;
damaged.holdRead(async () => {
if (renamed) return;
renamed = true;
damaged.renameProjectA();
});
damaged.directory.setDemand({}, true);
await damaged.directory.refreshDemand();
expect(renamed).toBe(true);
expect(damaged.client.lastProjectOptions).toEqual({ sync: {} });
expect(damaged.sessionProjectIds()).toEqual(["A", "B"]);
expect(await damaged.persistedProjectIds()).toEqual(["A", "B"]);
} finally {
await damaged.close();
}
});
it("re-syncs every project when a project update lands after the first sync fails", async () => {
const damaged = await createDamagedLegacyProjectDirectory("failed-first-sync", 3);
try {
damaged.failNextProjectRequest();
damaged.directory.setDemand({}, true);
await expect(damaged.directory.refreshDemand()).rejects.toThrow("Request timed out");
damaged.renameProjectA();
await damaged.directory.refreshDemand();
expect(damaged.client.lastProjectOptions).toEqual({ sync: {} });
expect(damaged.sessionProjectIds()).toEqual(["A", "B"]);
expect(await damaged.persistedProjectIds()).toEqual(["A", "B"]);
} finally {
await damaged.close();
}
});
function createWorkspaceLabelDirectory(serverId: string) {
const { client, directory } = createDirectory(serverId, { workspaceLabels: true });
const store = useSessionStore.getState();
@@ -288,7 +288,7 @@ export class DirectorySync {
this.revision += 1;
if (this.agentTransactions.record(source, delta)) return;
this.agents.applyDelta(delta);
this.noteLiveCursor("agents", delta);
if (this.extendsRefreshedBaseline(source)) this.noteLiveCursor("agents", delta);
this.persistCheckpoint();
}
@@ -300,7 +300,7 @@ export class DirectorySync {
this.workspaceRevision += 1;
if (this.workspaceTransactions.record(source, delta)) return;
this.applyWorkspaceDelta(delta);
if (delta.kind !== "script_status")
if (delta.kind !== "script_status" && this.extendsRefreshedBaseline(source))
this.noteLiveCursor(
"projectId" in delta || "project" in delta ? "projects" : "workspaces",
delta,
@@ -308,6 +308,20 @@ export class DirectorySync {
this.persistCheckpoint();
}
/**
* A live event's sequence covers everything before it only when a refresh completed on the
* subscriptions that delivered it. Before that (first sync still pending or failed) or after
* they lapse, the client may have missed earlier changes, so the event updates rows but leaves
* the cursor where the next refresh can still fetch what was missed.
*/
private extendsRefreshedBaseline(source: DirectorySourceToken): boolean {
const refreshed = this.satisfiedDemandSource;
return (
refreshed?.clientGeneration === source.clientGeneration &&
refreshed.connectionEpoch === source.connectionEpoch
);
}
private observeDirectoryEvents(): void {
if (this.eventSubscription) return;
const { client, source } = this.requireOnline();
@@ -566,24 +580,16 @@ export class DirectorySync {
input: { subscribe?: boolean } | undefined,
loadDirectoryCache: boolean,
): Promise<void> {
if (loadDirectoryCache) await this.loadCachedDirectory();
if (loadDirectoryCache) {
this.primeWorkspaceTransactionForCacheLoad();
await this.loadCachedDirectory();
}
const onlineConnection = this.getOnlineConnection();
if (!onlineConnection) return;
const { client, source } = onlineConnection;
const transaction = this.workspaceTransactions.begin(source, () => {
const baseline = this.workspaces.snapshot();
return {
workspaces: new Map(baseline.workspaces),
projects: new Map(baseline.projects),
// Cache hydration can advance the live replica while these requests await responses.
// Every page must use the cursors belonging to the maps captured here.
requestCursors: { ...this.cursors },
syncCursors: {},
syncModes: {},
touchedWorkspaceIds: new Set(),
touchedProjectIds: new Set(),
};
});
const transaction = this.workspaceTransactions.begin(source, () =>
this.createWorkspaceRefreshSnapshot(),
);
try {
await this.waitForSessionMetadata(client, source);
const serverInfo = useSessionStore.getState().sessions[this.serverId]?.serverInfo;
@@ -807,6 +813,31 @@ export class DirectorySync {
}));
}
// Updates that arrive before a refresh completes have no trusted baseline to advance from.
// Holding them keeps their cursor out of the cache until the refresh they are applied over.
private primeWorkspaceTransactionForCacheLoad(): void {
const connection = this.getOnlineConnection();
if (!connection) return;
this.workspaceTransactions.begin(connection.source, () =>
this.createWorkspaceRefreshSnapshot(),
);
}
private createWorkspaceRefreshSnapshot(): WorkspaceRefreshSnapshot {
const baseline = this.workspaces.snapshot();
return {
workspaces: new Map(baseline.workspaces),
projects: new Map(baseline.projects),
// Cache hydration can advance the live replica while these requests await responses.
// Every page must use the cursors belonging to the maps captured here.
requestCursors: { ...this.cursors },
syncCursors: {},
syncModes: {},
touchedWorkspaceIds: new Set(),
touchedProjectIds: new Set(),
};
}
private supportsDirectorySync(): boolean {
return (
useSessionStore.getState().sessions[this.serverId]?.serverInfo?.features?.directorySync ===
@@ -37,6 +37,9 @@ class MemoryStorage implements ReplicaRowStore {
onRead: (() => void) | null = null;
/** Throws once a read goes past this count, so a non-terminating read loop fails the test. */
readLimit: number | null = null;
/** Awaited before the store lists every row or deletes a host, so a test can interleave there. */
beforeReadAll: (() => Promise<void>) | null = null;
beforeDeleteHost: (() => Promise<void>) | null = null;
private key(row: Pick<ReplicaRow, "serverId" | "kind" | "id">): string {
return `${row.serverId}:${row.kind}:${row.id}`;
@@ -68,6 +71,7 @@ class MemoryStorage implements ReplicaRowStore {
}
async readAll(): Promise<ReplicaHostRows[]> {
await this.beforeReadAll?.();
const hosts = new Map<string, ReplicaRow[]>();
for (const row of this.rows.values()) {
const rows = hosts.get(row.serverId) ?? [];
@@ -91,6 +95,7 @@ class MemoryStorage implements ReplicaRowStore {
}
async deleteHost(serverId: string): Promise<void> {
await this.beforeDeleteHost?.();
for (const [key, row] of this.rows) if (row.serverId === serverId) this.rows.delete(key);
}
@@ -436,40 +441,49 @@ describe("ReplicaCache", () => {
await expectLegacyWorkspaceRefresh(cached);
});
it("rejects pre-baseline-fix checkpoints without discarding cached rows or timelines", async () => {
const storage = new MemoryStorage();
const writer = createCache(storage);
const baseline = directory();
writer.replaceDirectoryBaseline(SERVER_ID, baseline);
writer.commitTimeline(SERVER_ID, "agent-1", timeline());
await writer.flush();
await storage.apply({
deletes: [],
upserts: [
{
serverId: SERVER_ID,
kind: "checkpoint",
id: "singleton",
payload: JSON.stringify(baseline.checkpoint),
},
],
});
it.each([
["before the baseline fix", (checkpoint: DirectoryCheckpoint): unknown => checkpoint],
[
"before the eviction fix",
(checkpoint: DirectoryCheckpoint): unknown => ({ version: 1, cursors: checkpoint }),
],
])(
"rejects checkpoints written %s without discarding cached rows or timelines",
async (_era, legacyPayload) => {
const storage = new MemoryStorage();
const writer = createCache(storage);
const baseline = directory();
writer.replaceDirectoryBaseline(SERVER_ID, baseline);
writer.commitTimeline(SERVER_ID, "agent-1", timeline());
await writer.flush();
await storage.apply({
deletes: [],
upserts: [
{
serverId: SERVER_ID,
kind: "checkpoint",
id: "singleton",
payload: JSON.stringify(legacyPayload(baseline.checkpoint)),
},
],
});
const reader = createCache(storage);
const restored = await reader.readDirectory(SERVER_ID);
expect(restored.checkpoint).toBeUndefined();
expect([...restored.workspaces.keys()]).toEqual([...baseline.workspaces.keys()]);
expect([...restored.projects.keys()]).toEqual([...baseline.projects.keys()]);
expect([...restored.agents.keys()]).toEqual([...baseline.agents.keys()]);
expect(await reader.readTimeline(SERVER_ID, "agent-1")).toEqual(timeline());
expect(await storage.read(SERVER_ID, ["checkpoint"])).toEqual([]);
const reader = createCache(storage);
const restored = await reader.readDirectory(SERVER_ID);
expect(restored.checkpoint).toBeUndefined();
expect([...restored.workspaces.keys()]).toEqual([...baseline.workspaces.keys()]);
expect([...restored.projects.keys()]).toEqual([...baseline.projects.keys()]);
expect([...restored.agents.keys()]).toEqual([...baseline.agents.keys()]);
expect(await reader.readTimeline(SERVER_ID, "agent-1")).toEqual(timeline());
expect(await storage.read(SERVER_ID, ["checkpoint"])).toEqual([]);
reader.replaceDirectoryBaseline(SERVER_ID, baseline);
await reader.flush();
expect((await createCache(storage).readDirectory(SERVER_ID)).checkpoint).toEqual(
baseline.checkpoint,
);
});
reader.replaceDirectoryBaseline(SERVER_ID, baseline);
await reader.flush();
expect((await createCache(storage).readDirectory(SERVER_ID)).checkpoint).toEqual(
baseline.checkpoint,
);
},
);
it("does nothing until an owner explicitly commits data", async () => {
const storage = new MemoryStorage();
@@ -1102,6 +1116,113 @@ describe("ReplicaCache", () => {
).toEqual(["agent", "checkpoint", "project", "workspace"]);
});
it.each(["beforeReadAll", "beforeDeleteHost"] as const)(
"stores no checkpoint for a host evicted after its update was queued (%s)",
async (pausePoint) => {
const storage = new MemoryStorage();
const previousSession = new ReplicaCache(storage, noLegacyCleanup);
previousSession.setHosts([SERVER_ID, "other-host"]);
previousSession.replaceDirectoryBaseline(SERVER_ID, directory());
previousSession.replaceDirectoryBaseline("other-host", directory());
await previousSession.flush();
const cache = new ReplicaCache(storage, { ...noLegacyCleanup, maxBytes: 8_192 });
cache.setHosts([SERVER_ID, "other-host"]);
const paused = deferred();
const resume = deferred();
storage[pausePoint] = async () => {
storage[pausePoint] = null;
paused.resolve();
await resume.promise;
};
// Larger than the whole budget, so this persist evicts both hosts.
cache.commitTimeline(SERVER_ID, "agent-1", timeline("x".repeat(16_384)));
const eviction = cache.flush();
await paused.promise;
cache.commitDirectoryMutations(SERVER_ID, [], { agents: { generation: "g", afterSeq: 13 } });
resume.resolve();
await eviction;
await cache.flush();
const restored = await createCache(storage).readDirectory(SERVER_ID);
expect(restored.agents.size).toBe(0);
expect(restored.checkpoint).toBeUndefined();
},
);
it("stores no checkpoint for a host evicted while a malformed row is repaired", async () => {
const { storage } = await cacheWithoutBackgroundVisibility();
const reader = createCache(storage, 8_192);
storage.onRead = () => {
if (!storage.reads.at(-1)?.kinds.includes("checkpoint")) return;
storage.onRead = null;
// Larger than the whole budget, so this persist evicts the host before the repair writes.
reader.commitTimeline(SERVER_ID, "agent-1", timeline("x".repeat(16_384)));
void reader.flush();
};
expect(await reader.readWorkspace(SERVER_ID, "workspace-1")).toBeUndefined();
await reader.flush();
const restored = await createCache(storage).readDirectory(SERVER_ID);
expect(restored.projects.size).toBe(0);
expect(restored.checkpoint).toBeUndefined();
});
it("repairs a malformed row's cursor when the overlapping eviction fails", async () => {
const { storage } = await cacheWithoutBackgroundVisibility();
const reader = createCache(storage, 8_192);
storage.onRead = () => {
if (!storage.reads.at(-1)?.kinds.includes("checkpoint")) return;
storage.onRead = null;
storage.beforeDeleteHost = async () => {
storage.beforeDeleteHost = null;
throw new Error("Eviction aborted");
};
reader.commitTimeline(SERVER_ID, "agent-1", timeline("x".repeat(16_384)));
void reader.flush();
};
expect(await reader.readWorkspace(SERVER_ID, "workspace-1")).toBeUndefined();
// A newer timeline fits the budget, so no later eviction clears the checkpoint.
reader.commitTimeline(SERVER_ID, "agent-1", timeline());
await reader.flush();
const restored = await createCache(storage).readDirectory(SERVER_ID);
expect(restored.workspaces.size).toBe(0);
expect(restored.checkpoint).toEqual({
agents: { generation: "g", afterSeq: 12 },
projects: { generation: "g", afterSeq: 4 },
});
});
it("keeps an update queued behind the baseline that rebuilds an evicted host", async () => {
const storage = new MemoryStorage();
const cache = new ReplicaCache(storage, { ...noLegacyCleanup, maxBytes: 2_500 });
cache.setHosts([SERVER_ID, "other-host"]);
const baseline = directory();
cache.replaceDirectoryBaseline(SERVER_ID, baseline);
await cache.flush();
cache.replaceDirectoryBaseline("other-host", baseline);
await cache.flush();
expect((await cache.readDirectory(SERVER_ID)).agents.size).toBe(0);
const renamed = { ...agent(), title: "Renamed agent" };
cache.replaceDirectoryBaseline(SERVER_ID, baseline);
cache.commitDirectoryMutations(
SERVER_ID,
[{ kind: "agent", type: "upsert", id: renamed.id, value: renamed }],
{ agents: { generation: "g", afterSeq: 13 } },
);
await cache.flush();
cache.commitDirectoryMutations(SERVER_ID, [], { agents: { generation: "g", afterSeq: 14 } });
await cache.flush();
const restored = await createCache(storage).readDirectory(SERVER_ID);
expect(restored.checkpoint).toEqual({ agents: { generation: "g", afterSeq: 14 } });
expect(restored.agents.get(renamed.id)?.title).toBe("Renamed agent");
});
it("runs legacy cleanup once when storage is first used", async () => {
const storage = new MemoryStorage();
const cache = new ReplicaCache(storage, {
+34 -20
View File
@@ -397,15 +397,19 @@ const DirectoryCheckpointSchema = z.strictObject({
agents: DirectoryCursorSchema.optional(),
});
// Old checkpoints could describe a baseline that an overlapping refresh had discarded.
// Revalidate directory contents once without evicting cached rows or timelines.
// A checkpoint is trusted only when this version wrote it. Earlier writers could store one for a
// directory the cache no longer held: unversioned ones after an overlapping refresh, version 1
// after an eviction. Rejecting them revalidates directory contents once without evicting cached
// rows or timelines.
const DIRECTORY_CHECKPOINT_VERSION = 2;
const StoredDirectoryCheckpointSchema = z.strictObject({
version: z.literal(1),
version: z.literal(DIRECTORY_CHECKPOINT_VERSION),
cursors: DirectoryCheckpointSchema,
});
function serializeDirectoryCheckpoint(cursors: DirectoryCheckpoint) {
return { version: 1 as const, cursors };
return { version: DIRECTORY_CHECKPOINT_VERSION, cursors };
}
function deserializeDirectoryCheckpoint(payload: string): DirectoryCheckpoint {
@@ -968,6 +972,7 @@ export class ReplicaCache {
private pendingUpserts = new Map<string, StructuredReplicaUpsert>();
private pendingDeletes = new Map<string, ReplicaRowKey>();
private pendingBaselines = new Set<string>();
// Hosts whose stored directory was evicted. Only a baseline stores their directory again.
private readonly invalidatedHosts = new Set<string>();
private readonly maxBytes: number;
private totalBytes = 0;
@@ -1109,11 +1114,7 @@ export class ReplicaCache {
private async deleteInvalidRow(row: ReplicaRow): Promise<void> {
const invalidEntity = directoryEntityForRow(row);
if (!invalidEntity) {
const changes = { upserts: [], deletes: [row] } satisfies ReplicaRowChanges;
await this.queueOperation(async () => {
await this.rowStore.apply(changes);
this.applyStoredChanges(changes);
});
await this.applyRepair(row.serverId, { upserts: [], deletes: [row] });
return;
}
@@ -1144,10 +1145,7 @@ export class ReplicaCache {
]
: [],
};
await this.queueOperation(async () => {
await this.rowStore.apply(changes);
this.applyStoredChanges(changes);
});
await this.applyRepair(row.serverId, changes);
}
private async repairInvalidDirectoryRows(
@@ -1170,9 +1168,16 @@ export class ReplicaCache {
]
: [],
};
await this.queueOperation(async () => {
await this.rowStore.apply(changes);
this.applyStoredChanges(changes);
await this.applyRepair(serverId, changes);
}
// A repair is queued behind writes that can evict its host. If one did, the host has no
// directory left for the repaired checkpoint to describe, so only the deletes still apply.
private applyRepair(serverId: string, changes: ReplicaRowChanges): Promise<void> {
return this.queueOperation(async () => {
const applied = this.invalidatedHosts.has(serverId) ? { ...changes, upserts: [] } : changes;
await this.rowStore.apply(applied);
this.applyStoredChanges(applied);
});
}
@@ -1182,7 +1187,6 @@ export class ReplicaCache {
checkpoint?: DirectoryCheckpoint,
): void {
if (!this.activeServerIds.has(serverId)) return;
if (this.invalidatedHosts.has(serverId)) return;
for (const mutation of mutations) {
if (mutation.type === "delete") {
this.queueEntityDelete(serverId, mutation.kind, mutation.id);
@@ -1412,9 +1416,17 @@ export class ReplicaCache {
}
private materializePendingChanges(pending: PendingReplicaChanges): ReplicaRowChanges {
const deletes = new Map(pending.deletes.map((row) => [pendingRowKey(row), row]));
// Decided here, inside the serialized write, because eviction happens there too. A mutation
// queued before its host was evicted would otherwise store a checkpoint without its directory.
const isStorable = (row: ReplicaRowKey): boolean =>
row.kind === "timeline" ||
!this.invalidatedHosts.has(row.serverId) ||
pending.baselines.has(row.serverId);
const deletes = new Map(
pending.deletes.filter(isStorable).map((row) => [pendingRowKey(row), row]),
);
const upserts: ReplicaRow[] = [];
for (const upsert of pending.upserts) {
for (const upsert of pending.upserts.filter(isStorable)) {
const row = this.materializeUpsert(upsert);
if (row) upserts.push(row);
else deletes.set(pendingRowKey(upsert), upsert);
@@ -1563,8 +1575,10 @@ export class ReplicaCache {
projectedTotal -= bytes;
projectedBytes.delete(serverId);
evicted.add(serverId);
this.invalidatedHosts.add(serverId);
if (this.storedRows.has(serverId)) await this.rowStore.deleteHost(serverId);
// Only after the delete commits: a failed eviction leaves the rows stored, and writers that
// check this set must keep their checkpoint in step with those rows.
this.invalidatedHosts.add(serverId);
this.removeStoredHost(serverId);
}
@@ -2404,6 +2404,46 @@ describe("WorkspaceGitServiceImpl with workspace folders that no longer exist",
await service.dispose();
});
test("a snapshot read during restoration waits for fresh checkout facts", async () => {
const workspace = await createTemporaryFolder();
const git = createFolderBackedGit();
const started = createDeferred<void>();
const release = createDeferred<void>();
let holdRestoredRead = false;
const service = createService({
...git.deps,
getCheckoutSnapshotFacts: async (cwd: string) => {
if (holdRestoredRead) {
started.resolve();
await release.promise;
}
return git.deps.getCheckoutSnapshotFacts(cwd);
},
});
const subscription = service.registerWorkspace({ cwd: workspace }, vi.fn());
await waitForObservationToSettle(service, 1);
await rm(workspace, { recursive: true, force: true });
service.onWorkspaceStateMayHaveChanged(workspace);
await vi.advanceTimersByTimeAsync(1_000);
await waitForRefreshesToSettle(service);
expect((await service.getSnapshot(workspace)).git.isGit).toBe(false);
await advanceThroughDegradedPolls(service);
await waitForObservationToSettle(service, 0);
await mkdir(workspace);
holdRestoredRead = true;
const restoringRead = service.getSnapshot(workspace);
await started.promise;
const concurrentRead = service.getSnapshot(workspace);
release.resolve();
expect((await restoringRead).git.isGit).toBe(true);
expect((await concurrentRead).git.isGit).toBe(true);
subscription.unsubscribe();
await service.dispose();
});
test("a resumed workspace rebuilds observation from fresh facts, not the non-git facts cached while its folder was gone", async () => {
const workspace = await createTemporaryFolder();
const git = createFolderBackedGit();
@@ -838,9 +838,10 @@ export class WorkspaceGitServiceImpl implements WorkspaceGitService {
cwd = resolve(cwd);
const request = this.normalizeRefreshRequest(options, "getSnapshot");
const target = this.ensureWorkspaceTarget(cwd);
// The snapshot cached while the folder was gone can say "not git". Resume
// first, so a restored folder is read again instead of served from it.
if (target.folderMissing) {
// Restoration clears folderMissing before the fresh snapshot is ready.
if (target.folderRestorePromise) {
await target.folderRestorePromise;
} else if (target.folderMissing) {
await this.resumeWorkspaceIfFolderRestored(target);
}
if (!request.force && target.latestSnapshot) {
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,936 @@
import pLimit from "p-limit";
import { z } from "zod";
import { normalizeHost } from "@getpaseo/protocol/git-remote";
import {
ChangeRequestRateLimitError,
createPerHostChangeRequestPoller,
type PerHostReadContext,
parseCliHttpResponse,
parseRetryAfterMs,
} from "./change-request-poller.js";
import type { ChangeRequestSnapshot, ChangeRequestSource } from "./change-request-source.js";
import {
findTeaLoginNameForBaseUrl,
getGiteaActionTaskName,
GITEA_ACTION_MAX_PAGES,
GITEA_ACTION_PAGE_LIMIT,
GiteaActionsListResponseSchema,
GiteaCombinedCommitStatusSchema,
latestGiteaActionTasksByWorkflow,
parseGiteaActionTasks,
type GiteaActionTask,
type GiteaCommitStatus,
} from "./gitea-checks.js";
const GITEA_CLOUD_HOSTS = new Set(["gitea.com", "codeberg.org"]);
const GITEA_PR_TABS = new Set(["commits", "files", "checks"]);
const GITEA_PAGE_SIZE = GITEA_ACTION_PAGE_LIMIT;
const GITEA_REVIEW_COMMENT_CONCURRENCY = 3;
const GITEA_REVIEW_COMMENTS_TTL_MS = 5 * 60_000;
const GITEA_ACTION_RUN_LOOKUP_LIMIT = 5;
const checkUrlSchema = z.url({ protocol: /^https?$/ });
const giteaWatchPrSchema = z
.object({
number: z.number(),
state: z.string(),
merged: z.boolean().optional(),
merged_at: z.string().nullable().optional(),
html_url: z.string().optional(),
head: z
.object({
sha: z.string(),
})
.passthrough(),
})
.passthrough();
const giteaIssueCommentSchema = z
.object({
id: z.number(),
html_url: z.string().optional(),
type: z.string().optional(),
})
.passthrough();
const giteaReviewSchema = z
.object({
id: z.number(),
state: z.string(),
comments_count: z.number().optional().default(0),
submitted_at: z.string().nullable().optional(),
updated_at: z.string().nullable().optional(),
html_url: z.string().optional(),
})
.passthrough();
const giteaReviewCommentSchema = z
.object({
id: z.number(),
html_url: z.string().optional(),
})
.passthrough();
const giteaActionJobSchema = z.object({
id: z.number(),
name: z.string(),
status: z.string(),
conclusion: z.string().nullable().optional(),
html_url: z.string().nullable().optional(),
});
const giteaActionJobsPageSchema = z.union([
z.object({
jobs: z
.array(giteaActionJobSchema)
.nullable()
.transform((jobs) => jobs ?? []),
total_count: z.number(),
}),
// Forgejo returns the complete job list as an array.
z.array(giteaActionJobSchema),
]);
type GiteaActionJob = z.infer<typeof giteaActionJobSchema>;
type GiteaWatchPr = z.infer<typeof giteaWatchPrSchema>;
type GiteaReview = z.infer<typeof giteaReviewSchema>;
interface GiteaPullRequestIdentity {
baseUrl: string;
displayBase: string | null;
owner: string;
repo: string;
number: number;
}
function invalidGiteaUrl(): never {
throw new Error("Enter a Gitea, Forgejo, or Codeberg pull request URL.");
}
function parseGiteaUrlOrigin(input: string): {
protocol: "http:" | "https:";
host: string;
port: string;
hostWithPort: string;
pathname: string;
} {
const trimmed = input.trim();
if (!trimmed || /\s/.test(trimmed)) invalidGiteaUrl();
let parsed: URL;
try {
parsed = new URL(trimmed);
} catch {
invalidGiteaUrl();
}
const protocol = parsed.protocol.toLowerCase();
if ((protocol !== "https:" && protocol !== "http:") || parsed.username || parsed.password) {
invalidGiteaUrl();
}
const host = normalizeHost(parsed.hostname);
if (!host || (GITEA_CLOUD_HOSTS.has(host) && protocol !== "https:")) {
invalidGiteaUrl();
}
return {
protocol: protocol as "http:" | "https:",
host,
port: parsed.port,
hostWithPort: parsed.port ? `${host}:${parsed.port}` : host,
pathname: parsed.pathname,
};
}
function decodeRepoSegment(raw: string): string {
let decoded: string;
try {
decoded = decodeURIComponent(raw);
} catch {
invalidGiteaUrl();
}
if (
!decoded ||
decoded === "." ||
decoded === ".." ||
decoded.includes("/") ||
decoded.includes("\\")
) {
invalidGiteaUrl();
}
return decoded;
}
function parseGiteaPullRequestUrl(input: string): GiteaPullRequestIdentity {
const origin = parseGiteaUrlOrigin(input);
const segments = origin.pathname.split("/").filter(Boolean);
if (
segments.length >= 5 &&
GITEA_PR_TABS.has(segments.at(-1)!.toLowerCase()) &&
/^\d+$/.test(segments.at(-2)!)
) {
segments.pop();
}
const rawNumber = segments.pop();
const number = Number(rawNumber);
const resource = segments.pop();
const repo = segments.pop();
const owner = segments.pop();
if (
resource?.toLowerCase() !== "pulls" ||
!owner ||
!repo ||
!rawNumber ||
!/^\d+$/.test(rawNumber) ||
!Number.isInteger(number) ||
number < 1 ||
number > 2_147_483_647
) {
invalidGiteaUrl();
}
const path = segments.length ? `/${segments.join("/")}` : "";
const displayBase =
!path && !origin.port && GITEA_CLOUD_HOSTS.has(origin.host)
? null
: `${origin.hostWithPort}${path}`;
return {
baseUrl: `${origin.protocol}//${origin.hostWithPort}${path}`,
displayBase,
owner: decodeRepoSegment(owner),
repo: decodeRepoSegment(repo),
number,
};
}
export function resolveGiteaChangeRequest(input: string): string {
const { baseUrl, owner, repo, number } = parseGiteaPullRequestUrl(input);
return `${baseUrl}/${owner.toLowerCase()}/${repo.toLowerCase()}/pulls/${number}`;
}
export function describeGiteaChangeRequest(url: string): string {
const { displayBase, owner, repo, number } = parseGiteaPullRequestUrl(url);
const project = displayBase ? `${displayBase}/${owner}/${repo}` : `${owner}/${repo}`;
return `${project} #${number}`;
}
function mapGiteaPrState(pr: GiteaWatchPr): ChangeRequestSnapshot["state"] {
if (pr.merged === true || pr.merged_at != null) return "merged";
const normalized = pr.state.toLowerCase();
if (normalized === "open") return "open";
if (normalized === "merged") return "merged";
return "closed";
}
function mapCommitStatusToWatchState(
status: string,
): ChangeRequestSnapshot["checks"][number]["state"] {
switch (status.toLowerCase()) {
case "success":
return "passed";
case "failure":
case "error":
case "warning":
return "failed";
case "pending":
case "running":
return "pending";
default:
return "neutral";
}
}
function isCanceledActionStatus(status: GiteaCommitStatus): boolean {
// Actions writes a generic failure state on cancellation, with a distinct
// description in the native job's commit status.
const description = status.description ?? "";
return description === "Has been cancelled" || description.startsWith("Canceled after ");
}
function mapActionTaskToWatchState(
status: string,
): ChangeRequestSnapshot["checks"][number]["state"] {
switch (status.toLowerCase()) {
case "success":
return "passed";
case "failure":
case "failed":
case "error":
return "failed";
case "cancelled":
case "canceled":
case "skipped":
return "neutral";
default:
return "pending";
}
}
function computeAggregateChecksState(
checks: ChangeRequestSnapshot["checks"],
): ChangeRequestSnapshot["checksState"] {
if (checks.length === 0) return "neutral";
if (checks.some((check) => check.state === "failed")) return "failed";
if (checks.some((check) => check.state === "pending")) return "pending";
if (checks.some((check) => check.state === "passed")) return "passed";
return "neutral";
}
function nativeActionUrl(value: string | null | undefined, origin: string) {
if (!value) return null;
try {
const url = new URL(value, origin);
if (url.origin !== origin) return null;
const match = /^(.*\/actions\/runs\/(\d+))(\/jobs\/\d+)?\/?$/.exec(url.pathname);
if (!match) return null;
const [, run, runId, job] = match;
return { run: run!, runId: runId!, jobUrl: job ? `${origin}${run}${job}` : null };
} catch {
return null;
}
}
function combineWatchChecks(
commitStatuses: GiteaCommitStatus[],
actionTasks: GiteaActionTask[],
jobsByRun: ReadonlyMap<string, GiteaActionJob[]>,
origin: string,
): ChangeRequestSnapshot["checks"] {
const checks: ChangeRequestSnapshot["checks"] = [];
const actionJobs = [...jobsByRun.values()].flat();
const coveredJobUrls = new Set(
actionJobs.map((job) => nativeActionUrl(job.html_url, origin)?.jobUrl),
);
for (const status of commitStatuses) {
const jobUrl = nativeActionUrl(status.target_url, origin)?.jobUrl;
if (jobUrl && coveredJobUrls.has(jobUrl)) continue;
const url = checkUrlSchema.safeParse(status.target_url).data;
checks.push({
id: `status:${status.id}`,
name: status.context || `status-${status.id}`,
state:
jobUrl && isCanceledActionStatus(status)
? "neutral"
: mapCommitStatusToWatchState(status.status),
...(url ? { url } : {}),
});
}
for (const job of actionJobs) {
const url = checkUrlSchema.safeParse(job.html_url).data;
checks.push({
id: `action-job:${job.id}`,
name: job.name,
state: mapActionTaskToWatchState(job.conclusion || job.status),
...(url ? { url } : {}),
});
}
const seenTaskIds = new Set<number>();
for (const task of actionTasks) {
if (seenTaskIds.has(task.id)) continue;
const address = nativeActionUrl(task.url, origin);
if (address && jobsByRun.has(address.run)) continue;
seenTaskIds.add(task.id);
const url = checkUrlSchema.safeParse(task.url).data;
checks.push({
id: `action:${task.id}`,
name: getGiteaActionTaskName(task),
state: mapActionTaskToWatchState(task.status),
...(url ? { url } : {}),
});
}
return checks;
}
function hasNextLinkHeader(headers: ReadonlyMap<string, string>): boolean {
const link = headers.get("link");
return link ? /rel=["']?next["']?/i.test(link) : false;
}
function parseTotalCountHeader(headers: ReadonlyMap<string, string>): number | null {
const raw = headers.get("x-total-count");
if (!raw) return null;
const parsed = Number(raw);
return Number.isInteger(parsed) && parsed >= 0 ? parsed : null;
}
function isSubmittedGiteaReview(review: GiteaReview): boolean {
const state = review.state.trim().toUpperCase();
return state !== "PENDING" && state !== "REQUEST_REVIEW";
}
function parseTeaHttpStreams(
stdout: string,
stderr: string,
): {
status: number | null;
headers: Map<string, string>;
body: string;
} {
const fromStdout = parseCliHttpResponse(stdout);
if (fromStdout.status !== null) {
return fromStdout;
}
const fromStderr = parseCliHttpResponse(stderr);
if (fromStderr.status !== null) {
return { status: fromStderr.status, headers: fromStderr.headers, body: stdout.trim() };
}
return { status: null, headers: new Map(), body: stdout.trim() };
}
class TeaHttpStatusError extends Error {
readonly status: number;
constructor(status: number, message: string) {
super(message);
this.name = "TeaHttpStatusError";
this.status = status;
}
}
function isOptionalEndpointStatus(error: unknown): boolean {
return (
error instanceof TeaHttpStatusError &&
(error.status === 403 || error.status === 404 || error.status === 501)
);
}
type TeaApiGet = <T>(
endpoint: string,
schema: z.ZodType<T>,
) => Promise<{ data: T; headers: ReadonlyMap<string, string> }>;
interface TeaPollApi {
get: TeaApiGet;
all<T extends readonly unknown[]>(
reads: T,
): Promise<{ -readonly [Key in keyof T]: Awaited<T[Key]> }>;
}
interface CachedReviewComments {
count: number;
fetchedAt: number;
items: ChangeRequestSnapshot["activity"];
}
export interface CreateGiteaChangeRequestSourceOptions {
runCommand(
args: string[],
options: { cwd: string; envOverlay?: Record<string, string> },
): Promise<{ stdout: string; stderr: 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 createGiteaChangeRequestSource(
options: CreateGiteaChangeRequestSourceOptions,
): ChangeRequestSource & { dispose(): void } {
const now = options.now ?? Date.now;
const cachedReviewCommentsByUrl = new Map<string, Map<number, CachedReviewComments>>();
const reviewCommentLimit = pLimit(GITEA_REVIEW_COMMENT_CONCURRENCY);
async function resolveLogin(baseUrl: string): Promise<string> {
const result = await options.runCommand(["login", "list", "-o", "json"], {
cwd: process.cwd(),
});
const loginName = findTeaLoginNameForBaseUrl(result.stdout, baseUrl);
if (!loginName) {
throw options.createAuthError(`No tea login configured for ${baseUrl}`);
}
return loginName;
}
function parseJsonBody<T>(params: {
args: string[];
cwd: string;
body: string;
schema: z.ZodType<T>;
}): T {
let data: unknown;
try {
data = JSON.parse(params.body);
} catch {
throw options.createCommandError({
args: params.args,
cwd: params.cwd,
exitCode: null,
stderr: `tea did not return valid JSON (${params.body.length} bytes)`,
});
}
const parsed = params.schema.safeParse(data);
if (!parsed.success) {
throw options.createCommandError({
args: params.args,
cwd: params.cwd,
exitCode: null,
stderr: `tea JSON did not match the expected schema: ${parsed.error.message}`,
});
}
return parsed.data;
}
function bindTeaApi(context: PerHostReadContext, loginName: string): TeaPollApi {
let active = true;
const get: TeaApiGet = async (endpoint, schema) => {
if (!active || !context.isObserved()) throw new Error("Change request observation ended");
context.assertHostAvailable();
const args = ["api", "--login", loginName, "-i", endpoint];
const cwd = process.cwd();
const result = await options.runCommand(args, { cwd });
const parsed = parseTeaHttpStreams(result.stdout, result.stderr);
const nowMs = now();
// tea api can exit 0 on an HTTP failure, so always check the status line.
if (parsed.status === null) {
throw options.createCommandError({
args,
cwd,
exitCode: 0,
stderr: "tea api -i did not return an HTTP status line",
stdout: result.stdout,
});
}
if (parsed.status === 429) {
const resetAtMs = parseRetryAfterMs(parsed.headers, nowMs, {
forceOnMissing: true,
defaultPauseMs: 60_000,
})!;
context.pauseHost(resetAtMs);
throw new ChangeRequestRateLimitError({ host: context.host, resetAtMs });
}
if (parsed.status === 401) {
throw options.createAuthError(parsed.body || "401 Unauthorized");
}
if (parsed.status < 200 || parsed.status >= 300) {
throw new TeaHttpStatusError(
parsed.status,
`Gitea API returned HTTP ${parsed.status} for ${endpoint}: ${parsed.body}`.trim(),
);
}
const proactivePause = parseRetryAfterMs(parsed.headers, nowMs);
if (proactivePause !== null) context.deferPollsUntil(proactivePause);
const data = parseJsonBody({ args, cwd, body: parsed.body, schema });
return { data, headers: parsed.headers };
};
return {
get,
all(reads) {
return Promise.all(reads).catch(async (error: unknown) => {
// Stop admitting reads from this poll and hold its host slot until
// requests already sent (including late rate limits) settle.
active = false;
await Promise.allSettled(reads);
throw error;
});
},
};
}
async function teaApiGetAllPages<T>(params: {
get: TeaApiGet;
endpoint: string;
itemSchema: z.ZodType<T>;
}): Promise<T[]> {
const items: T[] = [];
const schema = z.array(params.itemSchema);
let minPageSize = 1;
for (let page = 1; ; page += 1) {
const sep = params.endpoint.includes("?") ? "&" : "?";
const { data, headers } = await params.get(
`${params.endpoint}${sep}page=${page}&limit=${GITEA_PAGE_SIZE}`,
schema,
);
items.push(...data);
if (headers.has("link")) {
if (hasNextLinkHeader(headers)) continue;
break;
}
const totalCount = parseTotalCountHeader(headers);
if (totalCount !== null) {
// Gitea can cap limit below the requested value and filter pending
// reviews after pagination. The largest page is a lower bound on
// the number of records advanced on every page.
minPageSize = Math.max(minPageSize, data.length);
if (page * minPageSize < totalCount) continue;
break;
}
if (data.length < GITEA_PAGE_SIZE) break;
}
return items;
}
async function readCommitStatuses(params: {
get: TeaApiGet;
repoPrefix: string;
headSha: string;
isObserved(): boolean;
}): Promise<{ statuses: GiteaCommitStatus[]; pagesRead: number }> {
const statuses: GiteaCommitStatus[] = [];
const seenIds = new Set<number>();
let page = 1;
let pagesRead = 0;
while (true) {
if (!params.isObserved()) return { statuses, pagesRead };
const response = await params.get(
`${params.repoPrefix}/commits/${encodeURIComponent(params.headSha)}/status?limit=${GITEA_PAGE_SIZE}&page=${page}`,
GiteaCombinedCommitStatusSchema,
);
pagesRead += 1;
const pageStatuses = response.data.statuses;
if (pageStatuses.length === 0) break;
for (const status of pageStatuses) {
if (seenIds.has(status.id)) {
throw new Error("Gitea commit status pages overlap. Retry the poll.");
}
seenIds.add(status.id);
statuses.push(status);
}
// Gitea sets body `total_count` to the current page length; the full total
// lives only in the `X-Total-Count` response header.
const headerTotal = parseTotalCountHeader(response.headers);
if (headerTotal !== null) {
if (statuses.length >= headerTotal) break;
page += 1;
continue;
}
if (hasNextLinkHeader(response.headers) || pageStatuses.length >= GITEA_PAGE_SIZE) {
page += 1;
continue;
}
break;
}
return { statuses, pagesRead };
}
async function readBoundedActionTasks(params: {
get: TeaApiGet;
repoPrefix: string;
isObserved(): boolean;
}): Promise<{ items: unknown[]; pagesRead: number }> {
const items: unknown[] = [];
let fetched = 0;
let pagesRead = 0;
for (let page = 1; page <= GITEA_ACTION_MAX_PAGES; page += 1) {
if (!params.isObserved()) return { items, pagesRead };
let response: { data: z.infer<typeof GiteaActionsListResponseSchema> };
try {
response = await params.get(
`${params.repoPrefix}/actions/tasks?limit=${GITEA_PAGE_SIZE}&page=${page}`,
GiteaActionsListResponseSchema,
);
} catch (error) {
if (page === 1 && isOptionalEndpointStatus(error)) {
return { items: [], pagesRead: 0 };
}
throw error;
}
pagesRead += 1;
const runs = response.data.workflow_runs;
if (runs.length === 0) break;
fetched += runs.length;
items.push(...runs);
if (response.data.total_count !== undefined && fetched >= response.data.total_count) break;
}
return { items, pagesRead };
}
async function readActionJobs(params: {
get: TeaApiGet;
origin: string;
repoPrefix: string;
tasks: GiteaActionTask[];
statuses: GiteaCommitStatus[];
}): Promise<Map<string, GiteaActionJob[]>> {
// Commit statuses identify a job URL. Tasks identify a run, so only a job
// list can establish coverage when both sources report the same run.
const statusRuns = new Set<string>();
for (const status of params.statuses) {
const address = nativeActionUrl(status.target_url, params.origin);
if (address?.jobUrl) statusRuns.add(address.run);
}
const runs = new Map<string, string>();
for (const task of params.tasks) {
const address = nativeActionUrl(task.url, params.origin);
if (address && statusRuns.has(address.run)) runs.set(address.run, address.runId);
}
const jobsByRun = new Map<string, GiteaActionJob[]>();
for (const [run, runId] of [...runs].slice(0, GITEA_ACTION_RUN_LOOKUP_LIMIT)) {
const endpoint = `${params.repoPrefix}/actions/runs/${runId}/jobs`;
const jobs = new Map<number, GiteaActionJob>();
for (let page = 1; page <= GITEA_ACTION_MAX_PAGES; page += 1) {
try {
const { data } = await params.get(
`${endpoint}?limit=${GITEA_PAGE_SIZE}&page=${page}`,
giteaActionJobsPageSchema,
);
const items = Array.isArray(data) ? data : data.jobs;
const totalCount = Array.isArray(data) ? data.length : data.total_count;
if (!items.length) break;
// The browser run number and API ID differ on some versions. Keep
// the original sources if any job belongs to another run.
if (items.some((job) => nativeActionUrl(job.html_url, params.origin)?.run !== run)) {
break;
}
for (const job of items) jobs.set(job.id, job);
if (jobs.size >= totalCount) {
jobsByRun.set(run, [...jobs.values()]);
break;
}
} catch (error) {
if (page === 1 && isOptionalEndpointStatus(error)) break;
throw error;
}
}
}
return jobsByRun;
}
async function readChecks(params: {
api: TeaPollApi;
origin: string;
repoPrefix: string;
prEndpoint: string;
headSha: string;
isObserved(): boolean;
}): Promise<ChangeRequestSnapshot["checks"]> {
const { get, all } = params.api;
const [statusResult, tasksResult] = await all([
readCommitStatuses({ ...params, get }),
readBoundedActionTasks({ ...params, get }),
]);
if (!params.isObserved()) {
throw new Error("Change request observation ended");
}
const matchingTasks = parseGiteaActionTasks(tasksResult.items).filter(
(task) => task.head_sha === params.headSha,
);
const latestTasks = latestGiteaActionTasksByWorkflow(matchingTasks);
const jobsByRun = await readActionJobs({
...params,
get,
tasks: latestTasks,
statuses: statusResult.statuses,
});
if (statusResult.pagesRead > 1 || tasksResult.pagesRead > 1 || jobsByRun.size > 0) {
const { data: recheck } = await get(params.prEndpoint, giteaWatchPrSchema);
if (recheck.head.sha !== params.headSha) {
throw new Error("The pull request head changed during pagination.");
}
}
return combineWatchChecks(statusResult.statuses, latestTasks, jobsByRun, params.origin);
}
async function readActivity(params: {
url: string;
api: TeaPollApi;
repoPrefix: string;
number: number;
prWebUrl: string;
terminal: boolean;
knownActivity(): ReadonlySet<string> | undefined;
isObserved(): boolean;
}): Promise<ChangeRequestSnapshot["activity"]> {
const { get, all } = params.api;
const [comments, reviews] = await all([
teaApiGetAllPages({
get,
endpoint: `${params.repoPrefix}/issues/${params.number}/comments`,
itemSchema: giteaIssueCommentSchema,
}),
teaApiGetAllPages({
get,
endpoint: `${params.repoPrefix}/pulls/${params.number}/reviews`,
itemSchema: giteaReviewSchema,
}),
]);
if (!params.isObserved()) {
throw new Error("Change request observation ended");
}
const activity: ChangeRequestSnapshot["activity"] = [];
for (const comment of comments) {
if (comment.type !== undefined && comment.type !== "comment") continue;
activity.push({
id: `comment:${comment.id}`,
kind: "comment",
url: comment.html_url || `${params.prWebUrl}#issuecomment-${comment.id}`,
});
}
const submittedReviews = reviews.filter(isSubmittedGiteaReview);
for (const review of submittedReviews) {
activity.push({
id: `review:${review.id}`,
kind: "review",
url: review.html_url || `${params.prWebUrl}#pullrequestreview-${review.id}`,
});
}
const known = params.knownActivity();
let reviewCache = cachedReviewCommentsByUrl.get(params.url);
if (!reviewCache) {
reviewCache = new Map<number, CachedReviewComments>();
cachedReviewCommentsByUrl.set(params.url, reviewCache);
}
const staleBefore = now() - GITEA_REVIEW_COMMENTS_TTL_MS;
const reviewsNeedingFetch = submittedReviews.filter((review) => {
if (review.comments_count <= 0) return false;
const cached = reviewCache.get(review.id);
if (
!params.terminal &&
cached &&
cached.fetchedAt > staleBefore &&
cached.count === review.comments_count &&
cached.items.every((item) => known?.has(item.id))
) {
return false;
}
return true;
});
// Gitea's review-comments endpoint returns all comments for a review in one
// response and ignores page/limit, so fetch it once without a pagination loop.
const fetchedGroups = await all(
reviewsNeedingFetch.map((review) =>
reviewCommentLimit(async () => {
if (!params.isObserved()) {
return { reviewId: review.id, count: review.comments_count, items: [] };
}
const { data: inlineComments } = await get(
`${params.repoPrefix}/pulls/${params.number}/reviews/${review.id}/comments`,
z.array(giteaReviewCommentSchema),
);
const items: ChangeRequestSnapshot["activity"] = inlineComments.map((comment) => ({
id: `inline:${comment.id}`,
kind: "inline comment",
url: comment.html_url || `${params.prWebUrl}#issuecomment-${comment.id}`,
}));
return { reviewId: review.id, count: review.comments_count, items };
}),
),
);
if (!params.isObserved()) {
throw new Error("Change request observation ended");
}
const fetchedAt = now();
for (const group of fetchedGroups) {
reviewCache.set(group.reviewId, { count: group.count, fetchedAt, items: group.items });
}
for (const review of submittedReviews) {
if (review.comments_count <= 0) continue;
const cached = reviewCache.get(review.id);
if (cached) {
activity.push(...cached.items);
}
}
return activity;
}
async function readPullRequest(context: PerHostReadContext): Promise<ChangeRequestSnapshot> {
const identity = parseGiteaPullRequestUrl(context.url);
const { baseUrl, owner, repo, number } = identity;
const loginName = await resolveLogin(baseUrl);
const api = bindTeaApi(context, loginName);
const repoPrefix = `repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`;
const prEndpoint = `${repoPrefix}/pulls/${number}`;
const { data: pr } = await api.get(prEndpoint, giteaWatchPrSchema);
if (!pr.head.sha) {
throw new Error(`Gitea pull request #${number} did not include a head commit SHA.`);
}
const prWebUrl = pr.html_url || context.url;
const address = parseGiteaPullRequestUrl(prWebUrl);
if (
address.baseUrl !== baseUrl ||
address.owner.toLowerCase() !== owner.toLowerCase() ||
address.repo.toLowerCase() !== repo.toLowerCase() ||
address.number !== number ||
pr.number !== number
) {
throw new Error("Tea returned a pull request that does not match the watch URL.");
}
const checks = await readChecks({
api,
origin: context.host,
repoPrefix,
prEndpoint,
headSha: pr.head.sha,
isObserved: context.isObserved,
});
const state = mapGiteaPrState(pr);
const activity = await readActivity({
url: context.url,
api,
repoPrefix,
number,
prWebUrl,
terminal: state !== "open",
knownActivity: context.knownActivity,
isObserved: context.isObserved,
});
return {
state,
headSha: pr.head.sha,
checksState: computeAggregateChecksState(checks),
checks,
reviewDecision: null,
activity,
};
}
const poller = createPerHostChangeRequestPoller({
hostOf: (url) => new URL(url).origin,
read: readPullRequest,
now,
pollIntervalMs: options.pollIntervalMs,
initialDelayMs: options.initialDelayMs,
});
return {
resolve: resolveGiteaChangeRequest,
describe: describeGiteaChangeRequest,
observe(url, callbacks) {
const release = poller.observe(url, callbacks);
return () => {
cachedReviewCommentsByUrl.delete(url);
release();
};
},
dispose() {
cachedReviewCommentsByUrl.clear();
poller.dispose();
},
};
}
@@ -0,0 +1,253 @@
import { z } from "zod";
import { normalizeHost } from "@getpaseo/protocol/git-remote";
// 50 is the page-size cap Gitea enforces on list endpoints; five pages bounds
// the sweep to the 250 most recent Actions tasks.
export const GITEA_ACTION_PAGE_LIMIT = 50;
export const GITEA_ACTION_MAX_PAGES = 5;
const GITEA_RELATIVE_URL_ORIGIN = "https://gitea.invalid";
export const GiteaLoginSchema = z
.object({
name: z.string().optional(),
url: z.string().optional(),
ssh_host: z.string().optional(),
})
.passthrough();
export const GiteaCommitStatusSchema = z
.object({
id: z.number(),
status: z.string(),
target_url: z.string().nullable().optional(),
description: z.string().nullable().optional(),
url: z.string().nullable().optional(),
context: z.string().optional().default(""),
creator: z.unknown().nullable().optional(),
})
.passthrough();
export const GiteaCombinedCommitStatusSchema = z
.object({
state: z.string().optional().default(""),
sha: z.string().optional(),
total_count: z.number().optional(),
statuses: z
.array(GiteaCommitStatusSchema)
.nullable()
.optional()
.transform((value) => value ?? []),
})
.passthrough();
export const GiteaActionTaskSchema = z
.object({
id: z.number(),
name: z.string().nullable().optional(),
head_branch: z.string().nullable().optional(),
head_sha: z.string(),
run_number: z.number().optional(),
event: z.string().nullable().optional(),
display_title: z.string().nullable().optional(),
status: z.string(),
need_approval: z.boolean().optional().default(false),
workflow_id: z.string().nullable().optional(),
url: z.string().nullable().optional(),
created_at: z.string().nullable().optional(),
run_started_at: z.string().nullable().optional(),
})
.passthrough();
// The item shape is validated per-task rather than as `z.array(GiteaActionTaskSchema)`
// so one malformed task (missing id/status/head_sha) drops only itself instead of
// throwing out the whole page and zeroing every Actions check for the PR.
export const GiteaActionsListResponseSchema = z
.object({
workflow_runs: z
.array(z.unknown())
.nullable()
.optional()
.transform((value) => value ?? []),
total_count: z.number().optional(),
})
.passthrough();
export type GiteaCommitStatus = z.infer<typeof GiteaCommitStatusSchema>;
export type GiteaCombinedCommitStatus = z.infer<typeof GiteaCombinedCommitStatusSchema>;
export type GiteaActionTask = z.infer<typeof GiteaActionTaskSchema>;
export function getGiteaActionTaskName(task: GiteaActionTask): string {
return task.name || task.display_title || task.workflow_id || `actions-${task.id}`;
}
export function getUrlOrigin(value: string | null | undefined): string | null {
if (!value) return null;
try {
return new URL(value).origin;
} catch {
return null;
}
}
export function getGiteaActionRunIdFromUrl(value: string | null | undefined): number | null {
if (!value) return null;
try {
const url = new URL(value, GITEA_RELATIVE_URL_ORIGIN);
const match = url.pathname.match(/\/actions\/runs\/(\d+)(?:\/jobs\/\d+)?\/?$/);
return match?.[1] ? Number.parseInt(match[1], 10) : null;
} catch {
return null;
}
}
function isNativeActionJobUrl(
targetUrl: string | null | undefined,
nativeOrigins: ReadonlySet<string>,
nativeRunIds: ReadonlySet<number>,
): boolean {
if (!targetUrl) return false;
try {
const url = new URL(targetUrl, GITEA_RELATIVE_URL_ORIGIN);
if (!/\/actions\/runs\/\d+\/jobs\/\d+\/?$/.test(url.pathname)) return false;
if (url.origin !== GITEA_RELATIVE_URL_ORIGIN && !nativeOrigins.has(url.origin)) return false;
const runId = getGiteaActionRunIdFromUrl(targetUrl);
return runId !== null && nativeRunIds.has(runId);
} catch {
return false;
}
}
// A "shadow" is the commit status Gitea synthesizes to mirror one of its own
// Actions jobs; keeping both would double-report every job. Detection is purely
// structural: Gitea always creates these statuses with a target_url of the form
// "<run link>/jobs/<job id>" (services/actions/commit_status.go upstream), so a
// status is a shadow exactly when its URL points at a job of a run we actually
// fetched from the Actions API. Anything else - external CI, URL-less statuses,
// contexts that merely look like "<workflow> / <job> (<event>)" - is kept.
export function isGiteaActionShadowStatus(
status: GiteaCommitStatus,
nativeOrigins: ReadonlySet<string>,
nativeRunIds: ReadonlySet<number>,
): boolean {
return isNativeActionJobUrl(status.target_url, nativeOrigins, nativeRunIds);
}
function getGiteaActionWorkflowIdentity(task: GiteaActionTask): string {
if (task.workflow_id) {
return `workflow\u0000${task.workflow_id}`;
}
// Instances that omit workflow_id (older Gitea/Forgejo payloads) still need
// reruns of the same job collapsed to the latest execution, so fall back to
// the job name before treating the task as its own group.
const name = task.name || task.display_title;
if (name) {
return `name\u0000${name}`;
}
return `task\u0000${getGiteaActionExecutionIdentity(task)}`;
}
function getGiteaActionExecutionIdentity(task: GiteaActionTask): string {
if (task.run_number !== undefined) return `run-number\u0000${task.run_number}`;
const runId = getGiteaActionRunIdFromUrl(task.url);
if (runId !== null) return `run-id\u0000${runId}`;
if (task.url) return `url\u0000${task.url}`;
return `task\u0000${task.id}`;
}
function parseGiteaActionTaskTime(task: GiteaActionTask): number {
const timestamp = task.created_at ?? task.run_started_at ?? null;
if (!timestamp) {
return 0;
}
const time = Date.parse(timestamp);
return Number.isNaN(time) ? 0 : time;
}
function compareGiteaActionTaskRecency(left: GiteaActionTask, right: GiteaActionTask): number {
const leftRunNumber = left.run_number ?? 0;
const rightRunNumber = right.run_number ?? 0;
if (leftRunNumber !== rightRunNumber) {
return leftRunNumber - rightRunNumber;
}
const leftTime = parseGiteaActionTaskTime(left);
const rightTime = parseGiteaActionTaskTime(right);
if (leftTime !== rightTime) {
return leftTime - rightTime;
}
return left.id - right.id;
}
export function parseGiteaActionTasks(rawTasks: unknown[]): GiteaActionTask[] {
const tasks: GiteaActionTask[] = [];
for (const raw of rawTasks) {
const parsed = GiteaActionTaskSchema.safeParse(raw);
if (parsed.success) {
tasks.push(parsed.data);
}
}
return tasks;
}
/**
* Keep all sibling tasks from the latest execution of each workflow.
* Per docs/forge-providers.md, ambiguous same-run tasks are preserved because
* display names are not stable job identities and `/actions/tasks` omits rerun
* attempt numbers.
*/
export function latestGiteaActionTasksByWorkflow(tasks: GiteaActionTask[]): GiteaActionTask[] {
const latestExecutions = new Map<string, GiteaActionTask>();
for (const task of tasks) {
const identity = getGiteaActionWorkflowIdentity(task);
const current = latestExecutions.get(identity);
if (!current || compareGiteaActionTaskRecency(current, task) < 0) {
latestExecutions.set(identity, task);
}
}
return tasks.filter((task) => {
const latest = latestExecutions.get(getGiteaActionWorkflowIdentity(task));
return (
latest !== undefined &&
getGiteaActionExecutionIdentity(task) === getGiteaActionExecutionIdentity(latest)
);
});
}
function parseTeaLogins(stdout: string): z.infer<typeof GiteaLoginSchema>[] {
try {
return z.array(GiteaLoginSchema).safeParse(JSON.parse(stdout)).data ?? [];
} catch {
return [];
}
}
export function findTeaLoginNameForBaseUrl(stdout: string, baseUrl: string): string | null {
const match = parseTeaLogins(stdout).find((login) => {
if (!login.name || !login.url) return false;
try {
const url = new URL(login.url);
return `${url.origin}${url.pathname.replace(/\/+$/, "")}` === baseUrl;
} catch {
return false;
}
});
return match?.name ?? null;
}
export function findTeaLoginNameForHost(stdout: string, host: string): string | null {
const targetHost = normalizeHost(host);
const match = parseTeaLogins(stdout).find((login) => {
const candidates = [login.ssh_host, login.name];
if (login.url) {
try {
candidates.push(new URL(login.url).hostname);
} catch {
// ignore malformed login url
}
}
return candidates.some((candidate) => candidate && normalizeHost(candidate) === targetHost);
});
return match?.name ?? null;
}
+44 -227
View File
@@ -61,6 +61,23 @@ import type {
SearchIssuesAndPrsOptions,
SearchResult,
} from "./forge-service.js";
import {
findTeaLoginNameForHost,
getGiteaActionRunIdFromUrl,
getGiteaActionTaskName,
getUrlOrigin,
GITEA_ACTION_MAX_PAGES,
GITEA_ACTION_PAGE_LIMIT,
GiteaActionsListResponseSchema,
GiteaCombinedCommitStatusSchema,
isGiteaActionShadowStatus,
latestGiteaActionTasksByWorkflow,
parseGiteaActionTasks,
type GiteaActionTask,
type GiteaCombinedCommitStatus,
type GiteaCommitStatus,
} from "./gitea-checks.js";
import { createGiteaChangeRequestSource } from "./gitea-change-requests.js";
import { isGiteaStatusFacts, type GiteaStatusFacts } from "./gitea-facts.js";
const TEA_ENV = {
@@ -77,10 +94,6 @@ const reviewCommentsLimit = pLimit(REVIEW_COMMENTS_FAN_OUT_CONCURRENCY);
// short cache, each poll re-derives the PR head SHA from scratch (branch name +
// PR lookup + PR view). The TTL keeps a new push visible within one poll cycle.
const CURRENT_PR_HEAD_SHA_CACHE_TTL_MS = 30_000;
// 50 is the page-size cap Gitea enforces on list endpoints; five pages bounds
// the sweep to the 250 most recent Actions tasks.
const GITEA_ACTION_PAGE_LIMIT = 50;
const GITEA_ACTION_MAX_PAGES = 5;
// PullRequestFlowAGit in forgejo models/issues/pull.go
const GITEA_PULL_REQUEST_FLOW_AGIT = 1;
@@ -142,6 +155,8 @@ export interface CreateGiteaServiceOptions {
resolveRemoteUrl?: (cwd: string) => Promise<string | null>;
resolveCurrentBranch?: (cwd: string) => Promise<string | null>;
now?: () => number;
watchPollMs?: number;
watchInitialDelayMs?: number;
}
/**
@@ -180,14 +195,6 @@ const GiteaIssueListItemSchema = z
})
.passthrough();
const GiteaLoginSchema = z
.object({
name: z.string().optional(),
url: z.string().optional(),
ssh_host: z.string().optional(),
})
.passthrough();
const GiteaUserSchema = z
.object({
login: z.string().optional(),
@@ -288,55 +295,6 @@ const GiteaCurrentPullRequestApiSchema = z
})
.passthrough();
const GiteaCommitStatusSchema = z
.object({
id: z.number(),
status: z.string(),
target_url: z.string().nullable().optional(),
description: z.string().nullable().optional(),
url: z.string().nullable().optional(),
context: z.string().optional().default(""),
creator: z.unknown().nullable().optional(),
})
.passthrough();
const GiteaCombinedCommitStatusSchema = z
.object({
state: z.string().optional().default(""),
sha: z.string().optional(),
total_count: z.number().optional(),
statuses: z.array(GiteaCommitStatusSchema).optional().default([]),
})
.passthrough();
const GiteaActionTaskSchema = z
.object({
id: z.number(),
name: z.string().nullable().optional(),
head_branch: z.string().nullable().optional(),
head_sha: z.string(),
run_number: z.number().optional(),
event: z.string().nullable().optional(),
display_title: z.string().nullable().optional(),
status: z.string(),
need_approval: z.boolean().optional().default(false),
workflow_id: z.string().nullable().optional(),
url: z.string().nullable().optional(),
created_at: z.string().nullable().optional(),
run_started_at: z.string().nullable().optional(),
})
.passthrough();
// The item shape is validated per-task rather than as `z.array(GiteaActionTaskSchema)`
// so one malformed task (missing id/status/head_sha) drops only itself instead of
// throwing out the whole page and zeroing every Actions check for the PR.
const GiteaActionsListResponseSchema = z
.object({
workflow_runs: z.array(z.unknown()).optional().default([]),
total_count: z.number().optional(),
})
.passthrough();
const GiteaActionRunPullRequestRefSchema = z
.object({
repo: z.object({ id: z.number().optional() }).passthrough().nullable().optional(),
@@ -410,9 +368,6 @@ type GiteaPrListItem = z.infer<typeof GiteaPrListItemSchema>;
type GiteaIssueListItem = z.infer<typeof GiteaIssueListItemSchema>;
type GiteaPullRequestView = z.infer<typeof GiteaPullRequestViewSchema>;
type GiteaCurrentPullRequestApi = z.infer<typeof GiteaCurrentPullRequestApiSchema>;
type GiteaCommitStatus = z.infer<typeof GiteaCommitStatusSchema>;
type GiteaCombinedCommitStatus = z.infer<typeof GiteaCombinedCommitStatusSchema>;
type GiteaActionTask = z.infer<typeof GiteaActionTaskSchema>;
type GiteaIssueComment = z.infer<typeof GiteaIssueCommentSchema>;
type GiteaReview = z.infer<typeof GiteaReviewSchema>;
type GiteaReviewComment = z.infer<typeof GiteaReviewCommentSchema>;
@@ -797,10 +752,6 @@ function deriveGiteaActionRequiredRunIds(runs: unknown[], sha: string): Set<numb
return runIds;
}
function getGiteaActionTaskName(task: GiteaActionTask): string {
return task.name || task.display_title || task.workflow_id || `actions-${task.id}`;
}
function toGiteaActionTaskCheck(task: GiteaActionTask): PullRequestCheck {
const rawStatus = task.status.toLowerCase();
return {
@@ -814,136 +765,6 @@ function toGiteaActionTaskCheck(task: GiteaActionTask): PullRequestCheck {
};
}
function getGiteaActionWorkflowIdentity(task: GiteaActionTask): string {
if (task.workflow_id) {
return `workflow\u0000${task.workflow_id}`;
}
// Instances that omit workflow_id (older Gitea/Forgejo payloads) still need
// reruns of the same job collapsed to the latest execution, so fall back to
// the job name before treating the task as its own group.
const name = task.name || task.display_title;
if (name) {
return `name\u0000${name}`;
}
return `task\u0000${getGiteaActionExecutionIdentity(task)}`;
}
function getGiteaActionExecutionIdentity(task: GiteaActionTask): string {
if (task.run_number !== undefined) return `run-number\u0000${task.run_number}`;
const runId = getGiteaActionRunIdFromUrl(task.url);
if (runId !== null) return `run-id\u0000${runId}`;
if (task.url) return `url\u0000${task.url}`;
return `task\u0000${task.id}`;
}
function parseGiteaActionTaskTime(task: GiteaActionTask): number {
const timestamp = task.created_at ?? task.run_started_at ?? null;
if (!timestamp) {
return 0;
}
const time = Date.parse(timestamp);
return Number.isNaN(time) ? 0 : time;
}
function compareGiteaActionTaskRecency(left: GiteaActionTask, right: GiteaActionTask): number {
const leftRunNumber = left.run_number ?? 0;
const rightRunNumber = right.run_number ?? 0;
if (leftRunNumber !== rightRunNumber) {
return leftRunNumber - rightRunNumber;
}
const leftTime = parseGiteaActionTaskTime(left);
const rightTime = parseGiteaActionTaskTime(right);
if (leftTime !== rightTime) {
return leftTime - rightTime;
}
return left.id - right.id;
}
function parseGiteaActionTasks(rawTasks: unknown[]): GiteaActionTask[] {
const tasks: GiteaActionTask[] = [];
for (const raw of rawTasks) {
const parsed = GiteaActionTaskSchema.safeParse(raw);
if (parsed.success) {
tasks.push(parsed.data);
}
}
return tasks;
}
function latestGiteaActionTasksByWorkflow(tasks: GiteaActionTask[]): GiteaActionTask[] {
const latestExecutions = new Map<string, GiteaActionTask>();
for (const task of tasks) {
const identity = getGiteaActionWorkflowIdentity(task);
const current = latestExecutions.get(identity);
if (!current || compareGiteaActionTaskRecency(current, task) < 0) {
latestExecutions.set(identity, task);
}
}
return tasks.filter((task) => {
const latest = latestExecutions.get(getGiteaActionWorkflowIdentity(task));
return (
latest !== undefined &&
getGiteaActionExecutionIdentity(task) === getGiteaActionExecutionIdentity(latest)
);
});
}
function getUrlOrigin(value: string | null | undefined): string | null {
if (!value) return null;
try {
return new URL(value).origin;
} catch {
return null;
}
}
const GITEA_RELATIVE_URL_ORIGIN = "https://gitea.invalid";
function getGiteaActionRunIdFromUrl(value: string | null | undefined): number | null {
if (!value) return null;
try {
const url = new URL(value, GITEA_RELATIVE_URL_ORIGIN);
const match = url.pathname.match(/\/actions\/runs\/(\d+)(?:\/jobs\/\d+)?\/?$/);
return match?.[1] ? Number.parseInt(match[1], 10) : null;
} catch {
return null;
}
}
function isNativeActionJobUrl(
targetUrl: string | null | undefined,
nativeOrigins: ReadonlySet<string>,
nativeRunIds: ReadonlySet<number>,
): boolean {
if (!targetUrl) return false;
try {
const url = new URL(targetUrl, GITEA_RELATIVE_URL_ORIGIN);
if (!/\/actions\/runs\/\d+\/jobs\/\d+\/?$/.test(url.pathname)) return false;
if (url.origin !== GITEA_RELATIVE_URL_ORIGIN && !nativeOrigins.has(url.origin)) return false;
const runId = getGiteaActionRunIdFromUrl(targetUrl);
return runId !== null && nativeRunIds.has(runId);
} catch {
return false;
}
}
// A "shadow" is the commit status Gitea synthesizes to mirror one of its own
// Actions jobs; keeping both would double-report every job. Detection is purely
// structural: Gitea always creates these statuses with a target_url of the form
// "<run link>/jobs/<job id>" (services/actions/commit_status.go upstream), so a
// status is a shadow exactly when its URL points at a job of a run we actually
// fetched from the Actions API. Anything else - external CI, URL-less statuses,
// contexts that merely look like "<workflow> / <job> (<event>)" - is kept.
function isGiteaActionShadowStatus(
status: GiteaCommitStatus,
nativeOrigins: ReadonlySet<string>,
nativeRunIds: ReadonlySet<number>,
): boolean {
return isNativeActionJobUrl(status.target_url, nativeOrigins, nativeRunIds);
}
function combineGiteaChecks(
commitStatuses: GiteaCommitStatus[],
actionTasks: GiteaActionTask[],
@@ -1219,32 +1040,6 @@ export async function probeGiteaHost(host: string): Promise<boolean> {
}
}
function findTeaLoginNameForHost(stdout: string, host: string): string | null {
let data: unknown;
try {
data = JSON.parse(stdout);
} catch {
return null;
}
const parsed = z.array(GiteaLoginSchema).safeParse(data);
if (!parsed.success) {
return null;
}
const target = host.toLowerCase();
const match = parsed.data.find((login) => {
const candidates = [login.ssh_host, login.name];
if (login.url) {
try {
candidates.push(new URL(login.url).hostname);
} catch {
// ignore malformed login url
}
}
return candidates.some((candidate) => candidate?.toLowerCase() === target);
});
return match?.name ?? null;
}
function hostHasLogin(stdout: string, host: string): boolean {
return findTeaLoginNameForHost(stdout, host) !== null;
}
@@ -1409,19 +1204,35 @@ export function createGiteaService(options: CreateGiteaServiceOptions = {}): For
const now = options.now ?? Date.now;
const currentPrHeadShaCache = new Map<string, { sha: string; expiresAt: number }>();
async function run(args: string[], runOptions: TeaCommandRunnerOptions): Promise<string> {
async function runRaw(
args: string[],
runOptions: TeaCommandRunnerOptions,
): Promise<TeaCommandResult> {
const teaPath = await resolveTea();
if (!teaPath) {
throw new TeaCliMissingError();
}
try {
const result = await runner(args, runOptions);
return result.stdout.trim();
return await runner(args, runOptions);
} catch (error) {
throw teaCliRunner.normalizeError(error, { args: redactTeaArgs(args), cwd: runOptions.cwd });
}
}
async function run(args: string[], runOptions: TeaCommandRunnerOptions): Promise<string> {
const result = await runRaw(args, runOptions);
return result.stdout.trim();
}
const changeRequests = createGiteaChangeRequestSource({
runCommand: runRaw,
createAuthError: (stderr) => new TeaAuthenticationError({ stderr }),
createCommandError: (params) => new TeaCommandError(params),
now,
pollIntervalMs: options.watchPollMs,
initialDelayMs: options.watchInitialDelayMs,
});
function runJsonParse<T>(
args: string[],
runOptions: TeaCommandRunnerOptions,
@@ -2359,6 +2170,8 @@ export function createGiteaService(options: CreateGiteaServiceOptions = {}): For
return notSupported("disablePullRequestAutoMerge");
},
changeRequests,
invalidate(input: { cwd: string }): void {
const prefix = `${input.cwd}:`;
for (const key of currentPrHeadShaCache.keys()) {
@@ -2367,5 +2180,9 @@ export function createGiteaService(options: CreateGiteaServiceOptions = {}): For
}
}
},
dispose(): void {
changeRequests.dispose();
},
};
}