mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 12:50:37 +01:00
Integrate current master for Photon sleep delivery
* commit '20ccf03676c2b70e3865e632780cc95f9aaaa051': fix(workspaces): settle closed remote workspaces after one confirmed destroy (#21616) perf(chat): scope GitHub review check run hints to the issue and throttle company-wide rebuilds (#21610)
This commit is contained in:
commit
60564f5ec7
6 files changed
+245
-19
No files matched your search
@@ -82,6 +82,7 @@ import { githubChatReviewService } from "../services/chat-github-reviews.js";
|
||||
import { resolveGitHubReviewInstructions } from "../services/chat-channels/github/review-instructions.js";
|
||||
import { githubResponseCommentService } from "../services/chat-github-response-comments.js";
|
||||
import { GITHUB_REVIEW_CHECK_MAX_HEAD_POLL_MS, githubReviewCheckService } from "../services/chat-github-checks.js";
|
||||
import { GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS } from "../services/chat-channels/github/review-check-work.js";
|
||||
import { githubAutomaticReviewEvent } from "../services/chat-github-events.js";
|
||||
import { githubBotToolsForSession, syncGitHubBotTools } from "../services/chat-github-tools.js";
|
||||
import { buildPaperclipRuntimeMcpServers } from "../services/heartbeat/run-preparation.js";
|
||||
@@ -7774,12 +7775,13 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
|
||||
};
|
||||
const window = async (seconds: number) => {
|
||||
const before = { statements: queries.length, pulls: pulls(),
|
||||
states: queries.filter(query => query.includes('from "chat_github_reviews"')).length };
|
||||
states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length };
|
||||
for (let second = 0; second < seconds; second++) await pass();
|
||||
return { statements: queries.length - before.statements, pulls: pulls() - before.pulls,
|
||||
states: queries.filter(query => query.includes('from "chat_github_reviews"')).length - before.states };
|
||||
states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length - before.states };
|
||||
};
|
||||
return { ...f, pass, window, pulls, restart: () => { task = register(); }, now: () => clock };
|
||||
const states = () => queries.filter(query => query.includes('from "chat_github_reviews" where')).length;
|
||||
return { ...f, pass, window, pulls, states, restart: () => { task = register(); }, now: () => clock };
|
||||
}
|
||||
|
||||
it("backs off remote head polls while an unresolved GitHub review check cannot change", async () => {
|
||||
@@ -7815,6 +7817,48 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => {
|
||||
expect((await db.select().from(chatGitHubReviews).where(eq(chatGitHubReviews.id, f.review.id)))[0].state).toBe("superseded");
|
||||
}, 120_000);
|
||||
|
||||
it("rebuilds GitHub review checks for company-wide hints at most once per interval", async () => {
|
||||
const f = await unresolvedReviewCheckPasses();
|
||||
await f.window(400);
|
||||
const reads = async (hint: string) => {
|
||||
const before = f.states();
|
||||
await f.pass(hint);
|
||||
return f.states() - before;
|
||||
};
|
||||
const company = JSON.stringify({ companyId: f.companyId });
|
||||
const repeated = [];
|
||||
for (let index = 0; index < 10; index++) repeated.push(await reads(company));
|
||||
expect(repeated).toEqual([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
for (let index = 0; index < GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS / 1000 - 10; index++) await f.pass();
|
||||
expect(await reads(company)).toBe(1);
|
||||
expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1);
|
||||
expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1);
|
||||
expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: randomUUID() }))).toBe(0);
|
||||
expect(await reads(JSON.stringify({ companyId: randomUUID() }))).toBe(0);
|
||||
}, 120_000);
|
||||
|
||||
it("publishes a run change for a GitHub review check at once from its issue milestone", async () => {
|
||||
const f = await unresolvedReviewCheckPasses();
|
||||
await f.window(400);
|
||||
await f.pass(JSON.stringify({ companyId: f.companyId }));
|
||||
const hints: string[] = [];
|
||||
const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.chatGitHubReviewChecks, (event, settled) => {
|
||||
if (event === "settled") hints.push(...(settled ?? []));
|
||||
});
|
||||
onTestFinished(unsubscribe);
|
||||
await db.transaction(async tx => {
|
||||
await tx.update(chatGitHubReviews).set({ runId: f.run.id }).where(eq(chatGitHubReviews.id, f.review.id));
|
||||
await notifyChatRunMilestones(tx, { companyId: f.companyId, issueId: f.review.issueId, runId: f.run.id });
|
||||
});
|
||||
await expect.poll(() => hints.length, { timeout: 5000 }).toBeGreaterThan(0);
|
||||
expect(hints.map(hint => JSON.parse(hint))).toContainEqual({ companyId: f.companyId, issueId: f.review.issueId });
|
||||
const writes = f.writes.length;
|
||||
for (const hint of hints) await f.pass(hint);
|
||||
expect(f.writes).toHaveLength(writes + 1);
|
||||
expect(f.writes.at(-1)).toMatchObject({ status: "in_progress" });
|
||||
expect(await f.action()).toMatchObject({ status: "received", result: { state: "running", unchangedPolls: 0 } });
|
||||
}, 120_000);
|
||||
|
||||
async function reviewProjectionFixture(cancelFirst = true) {
|
||||
const f = await reviewBotFixture();
|
||||
const deliveryId = randomUUID();
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
agents, companies, createDb, environmentLeases, environments, executionWorkspaceSnapshots, executionWorkspaces,
|
||||
heartbeatRuns, issues, projects, type Db,
|
||||
} from "@paperclipai/db";
|
||||
import { readRemoteWorkspaceAuthority } from "@paperclipai/shared";
|
||||
import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js";
|
||||
import { executionWorkspaceService } from "../services/execution-workspaces.js";
|
||||
import { remoteTerminationReceipt } from "../services/remote-execution-termination.js";
|
||||
import { registerTerminalWorkspaceWork, TERMINAL_WORKSPACE_RETRY_MS } from "../services/terminal-workspace-work.js";
|
||||
import { meterDatabaseStatements } from "./helpers/database-statement-meter.js";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
||||
import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
|
||||
|
||||
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_TERMINAL_WORKSPACE_IDLE_TEST_DATABASE_URL");
|
||||
const support = external ? { supported: true } : await getEmbeddedPostgresTestSupport();
|
||||
const DAY = 86_400_000;
|
||||
const realSetTimeout = globalThis.setTimeout;
|
||||
const realSleep = (ms: number) => new Promise(resolve => realSetTimeout(resolve, ms));
|
||||
|
||||
(support.supported ? describe : describe.skip)("terminal workspace sweep at idle", () => {
|
||||
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | undefined;
|
||||
let db: Db;
|
||||
let meter: ReturnType<typeof meterDatabaseStatements>;
|
||||
beforeAll(async () => {
|
||||
if (!external) temporary = await startEmbeddedPostgresTestDatabase("terminal-workspace-idle-");
|
||||
const url = external ?? temporary!.connectionString;
|
||||
db = createDb(url);
|
||||
db.$client.options.onnotice = () => {};
|
||||
meter = meterDatabaseStatements(db, url);
|
||||
}, 120_000);
|
||||
afterEach(() => { vi.useRealTimers(); });
|
||||
afterAll(async () => { meter?.uninstall(); await db?.$client.end({ timeout: 1 }); await temporary?.cleanup(); });
|
||||
|
||||
async function seedCompany() {
|
||||
const companyId = randomUUID(), projectId = randomUUID(), agentId = randomUUID(), environmentId = randomUUID();
|
||||
await db.insert(companies).values({ id: companyId, name: "Idle", issuePrefix: `I${companyId.slice(0, 6)}` });
|
||||
await db.insert(projects).values({ id: projectId, companyId, name: "Project", status: "in_progress" });
|
||||
await db.insert(agents).values({ id: agentId, companyId, name: "Agent", role: "engineer", status: "idle", adapterType: "process" });
|
||||
await db.insert(environments).values({ id: environmentId, name: `Sandbox ${environmentId}`, driver: "sandbox" });
|
||||
return { companyId, projectId, agentId, environmentId };
|
||||
}
|
||||
|
||||
async function seedWorkspace(f: Awaited<ReturnType<typeof seedCompany>>, kind: "open" | "held" | "cooldown" | "done" | "remote" | "remote_unsaved") {
|
||||
const old = new Date(Date.now() - 10 * DAY);
|
||||
const id = randomUUID(), issueId = randomUUID(), snapshotId = randomUUID();
|
||||
const location = { environmentId: f.environmentId, leaseId: randomUUID(), providerLeaseId: `sandbox-${randomUUID()}`, remoteCwd: "/workspace" };
|
||||
const remote = kind === "remote" || kind === "remote_unsaved";
|
||||
let runId: string | null = null;
|
||||
if (kind === "held") {
|
||||
runId = randomUUID();
|
||||
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "running" });
|
||||
}
|
||||
await db.insert(executionWorkspaces).values({ id, companyId: f.companyId, projectId: f.projectId,
|
||||
mode: remote ? "isolated_workspace" : "shared_workspace", strategyType: "project_primary", name: `${kind} ${id}`,
|
||||
status: remote ? "archived" : "active", closedAt: remote ? old : null, providerType: remote ? "remote" : "local_fs",
|
||||
cwd: `/missing/${id}`, createdAt: old, updatedAt: old,
|
||||
metadata: remote ? { remoteWorkspace: { schema: "paperclip.remote-workspace.v1", epoch: 1, snapshotId,
|
||||
source: { kind: "empty" }, location, writerRunId: null, dirty: false } } : null });
|
||||
await db.insert(issues).values({ id: issueId, companyId: f.companyId, projectId: f.projectId, title: kind,
|
||||
status: kind === "open" ? "in_progress" : "done", executionWorkspaceId: id, executionRunId: runId,
|
||||
completedAt: kind === "open" ? null : kind === "cooldown" ? new Date(Date.now() - 3_600_000) : old, updatedAt: old });
|
||||
await db.update(executionWorkspaces).set({ sourceIssueId: issueId, updatedAt: old }).where(eq(executionWorkspaces.id, id));
|
||||
if (kind === "remote") await db.insert(executionWorkspaceSnapshots).values({ id: snapshotId, companyId: f.companyId,
|
||||
executionWorkspaceId: id, sourceOperationId: randomUUID(), generation: 1, epoch: 1, status: "committed" });
|
||||
return { id, location };
|
||||
}
|
||||
|
||||
function destroyedLease(input: { companyId: string; leaseId: string; providerLeaseId: string; environmentId: string }) {
|
||||
const lease = { id: input.leaseId, companyId: input.companyId, heartbeatRunId: randomUUID(), provider: "daytona",
|
||||
providerLeaseId: input.providerLeaseId, environmentId: input.environmentId };
|
||||
return { ...lease, status: "released", cleanupStatus: "success", releasedAt: new Date(),
|
||||
metadata: { remoteExecutionTermination: remoteTerminationReceipt(lease, { providerLeaseId: input.providerLeaseId, state: "destroyed" }) } };
|
||||
}
|
||||
|
||||
it("settles after one cleanup of each closed remote workspace and inspects nothing on its own writes", async () => {
|
||||
const f = await seedCompany();
|
||||
const remote: string[] = [];
|
||||
for (const kind of ["open", "open", "held", "cooldown", "done", "remote", "remote", "remote_unsaved"] as const) {
|
||||
for (let index = 0; index < 5; index++) {
|
||||
const workspace = await seedWorkspace(f, kind);
|
||||
if (kind === "remote") remote.push(workspace.id);
|
||||
}
|
||||
}
|
||||
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
|
||||
const destroyCalls: string[] = [];
|
||||
const service = executionWorkspaceService(db, {
|
||||
destroyRemoteWorkspaceLease: async input => { destroyCalls.push(input.workspaceId); return destroyedLease(input) as never; },
|
||||
workspaceReaperCooldownDays: 1, workspaceCompactionDelayMs: 3_600_000,
|
||||
terminalWorkspaceInspectionBackoffMs: TERMINAL_WORKSPACE_RETRY_MS,
|
||||
});
|
||||
const sweeps: Array<{ targeted: boolean; checked: number }> = [];
|
||||
let inFlight = 0;
|
||||
const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError: () => {} });
|
||||
const worker = registerTerminalWorkspaceWork(coordinator, { ...service,
|
||||
sweepTerminalWorkspaces: async (limit, signal, options) => {
|
||||
inFlight += 1;
|
||||
try {
|
||||
const result = await service.sweepTerminalWorkspaces(limit, signal, options);
|
||||
sweeps.push({ targeted: Boolean(options?.targets), checked: result.checked });
|
||||
return result;
|
||||
} finally { inFlight -= 1; }
|
||||
} }, () => true);
|
||||
const idle = async (seconds: number) => {
|
||||
for (let second = 0; second < seconds; second += 10) {
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
do { await realSleep(10); await vi.advanceTimersByTimeAsync(1); } while (inFlight > 0);
|
||||
}
|
||||
};
|
||||
try {
|
||||
await idle(120);
|
||||
expect(new Set(destroyCalls)).toEqual(new Set(remote));
|
||||
expect(destroyCalls.length).toBe(remote.length);
|
||||
const before = { statements: meter.read().statements, sweeps: sweeps.length, destroys: destroyCalls.length };
|
||||
await idle(30 * 60);
|
||||
vi.useRealTimers();
|
||||
const window = sweeps.slice(before.sweeps);
|
||||
expect(destroyCalls.length).toBe(before.destroys);
|
||||
expect(window.filter(sweep => sweep.targeted)).toEqual([]);
|
||||
expect(meter.read().statements - before.statements).toBeLessThan(5_000);
|
||||
for (const id of remote) {
|
||||
const [row] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, id));
|
||||
expect(readRemoteWorkspaceAuthority(row!.metadata)?.location).toBeNull();
|
||||
}
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
await worker.stop(); await coordinator.stop();
|
||||
}
|
||||
}, 600_000);
|
||||
|
||||
it("clears the location of an already destroyed lease without calling the provider again", async () => {
|
||||
const f = await seedCompany();
|
||||
const workspace = await seedWorkspace(f, "remote");
|
||||
const runId = randomUUID();
|
||||
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "succeeded" });
|
||||
const lease = { id: workspace.location.leaseId, companyId: f.companyId, heartbeatRunId: runId, provider: "daytona",
|
||||
providerLeaseId: workspace.location.providerLeaseId };
|
||||
await db.insert(environmentLeases).values({ ...lease, environmentId: f.environmentId, executionWorkspaceId: workspace.id,
|
||||
status: "released", cleanupStatus: "success", releasedAt: new Date(),
|
||||
metadata: { remoteExecutionTermination: remoteTerminationReceipt(lease, { providerLeaseId: lease.providerLeaseId, state: "destroyed" }) } });
|
||||
const destroy = vi.fn();
|
||||
const service = executionWorkspaceService(db, { destroyRemoteWorkspaceLease: destroy as never, workspaceReaperCooldownDays: 1 });
|
||||
expect(await service.sweepTerminalWorkspaces(50, undefined, { targets: { workspaceIds: [workspace.id], issueIds: [] } }))
|
||||
.toMatchObject({ checked: 1, archived: 1 });
|
||||
expect(destroy).not.toHaveBeenCalled();
|
||||
const [row] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, workspace.id));
|
||||
expect(readRemoteWorkspaceAuthority(row!.metadata)?.location).toBeNull();
|
||||
expect(await service.sweepTerminalWorkspaces(50, undefined, { targets: { workspaceIds: [workspace.id], issueIds: [] } }))
|
||||
.toMatchObject({ checked: 0 });
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,21 @@
|
||||
import { and, asc, eq, gt, or, sql } from "drizzle-orm";
|
||||
import { chatActions, chatEndpointLeases, type Db } from "@paperclipai/db";
|
||||
import { chatActions, chatEndpointLeases, chatGitHubReviews, type Db } from "@paperclipai/db";
|
||||
import type { GitHubReviewEventContext } from "@paperclipai/shared";
|
||||
import type { createDeliveryWorkCoordinator } from "../../delivery-work-coordinator.js";
|
||||
import { DELIVERY_QUEUES, notifyDeliveryWork } from "../../delivery-work-notifications.js";
|
||||
import { registerScopedDeadlineWork } from "../../scoped-deadline-work.js";
|
||||
import { githubReviewCheckState } from "./review-check-state.js";
|
||||
|
||||
type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number };
|
||||
type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number; issueId?: string };
|
||||
type Entry = Scope & { id: string; dueAt: number };
|
||||
|
||||
export const GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS = 30_000;
|
||||
|
||||
export function githubReviewCheckReconstructionIntervalMs(scope: Scope | undefined) {
|
||||
return scope && !scope.endpointId && !scope.repositoryId && !scope.number && !scope.issueId
|
||||
? GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS : 0;
|
||||
}
|
||||
|
||||
export async function notifyGitHubReviewChecks(tx: object, scope: Scope) {
|
||||
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify(scope));
|
||||
}
|
||||
@@ -19,8 +26,14 @@ export function registerGitHubReviewCheckWork(
|
||||
dispatch: (id: string, canContinue: () => boolean) => Promise<unknown>,
|
||||
canRun: () => boolean,
|
||||
) {
|
||||
const reviewIssueId = sql<string | null>`(select r.issue_id from ${chatGitHubReviews} r
|
||||
where r.endpoint_id = ${chatActions.endpointId}
|
||||
and r.repository_id = ${chatActions.payload}->'event'->>'repositoryId'
|
||||
and r.pull_number = (${chatActions.payload}->'event'->>'pullNumber')::integer
|
||||
and r.head_sha = ${chatActions.payload}->'event'->>'headSha'
|
||||
order by r.created_at desc limit 1)`;
|
||||
async function page(scope?: Scope, cursor?: string, actionId?: string) {
|
||||
return db.select({ action: chatActions,
|
||||
return db.select({ action: chatActions, reviewIssueId,
|
||||
leaseExpiresAt: sql<string | null>`(select max(l.expires_at) from ${chatEndpointLeases} l
|
||||
where l.company_id = ${chatActions.companyId} and l.endpoint_id = ${chatActions.endpointId}
|
||||
and l.lease_key = 'github-publication:' || (${chatActions.payload}->'event'->>'repositoryId')
|
||||
@@ -31,6 +44,7 @@ export function registerGitHubReviewCheckWork(
|
||||
scope?.endpointId ? eq(chatActions.endpointId, scope.endpointId) : undefined,
|
||||
scope?.repositoryId ? sql`${chatActions.payload}->'event'->>'repositoryId' = ${scope.repositoryId}` : undefined,
|
||||
scope?.number ? sql`${chatActions.payload}->'event'->>'pullNumber' = ${String(scope.number)}` : undefined,
|
||||
scope?.issueId ? sql`${reviewIssueId} = ${scope.issueId}` : undefined,
|
||||
cursor ? gt(chatActions.id, cursor) : undefined, actionId ? eq(chatActions.id, actionId) : undefined,
|
||||
)).orderBy(asc(chatActions.id)).limit(100);
|
||||
}
|
||||
@@ -45,14 +59,16 @@ export function registerGitHubReviewCheckWork(
|
||||
}
|
||||
if (row.leaseExpiresAt) dueAt = Math.max(dueAt, Date.parse(row.leaseExpiresAt));
|
||||
return { id: action.id, companyId: action.companyId, endpointId: action.endpointId,
|
||||
repositoryId: event.repositoryId, number: event.pullNumber, dueAt };
|
||||
repositoryId: event.repositoryId, number: event.pullNumber, issueId: row.reviewIssueId ?? undefined, dueAt };
|
||||
}
|
||||
return registerScopedDeadlineWork(coordinator, {
|
||||
queue: DELIVERY_QUEUES.chatGitHubReviewChecks,
|
||||
decodeScope: (hint: string): Scope | undefined => hint === "*" ? undefined : JSON.parse(hint),
|
||||
matches: (value: Entry, scope?: Scope) => !scope || (value.companyId === scope.companyId &&
|
||||
(!scope.endpointId || value.endpointId === scope.endpointId) &&
|
||||
(!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number)),
|
||||
(!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number) &&
|
||||
(!scope.issueId || value.issueId === scope.issueId)),
|
||||
reconstructionIntervalMs: githubReviewCheckReconstructionIntervalMs,
|
||||
load: async (scope?: Scope, signal?: AbortSignal) => {
|
||||
const entries: Entry[] = [];
|
||||
let cursor: string | undefined;
|
||||
|
||||
@@ -19,7 +19,7 @@ export async function notifyChatRunMilestones(tx: object, scope: ChatRunMileston
|
||||
await notifyNativeResponsePresentation(tx, { companyId: scope.companyId, issueId: scope.issueId,
|
||||
runId: scope.issueId ? undefined : scope.runId });
|
||||
}
|
||||
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId }));
|
||||
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId }));
|
||||
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubTerminalResponses, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId }));
|
||||
await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatRunMilestones, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId }));
|
||||
// Lifecycle and coarse native progress share lineage and authority. Extend
|
||||
|
||||
@@ -2285,6 +2285,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
isWriteTarget: (fresh: FenceableWorkspaceRow) => boolean;
|
||||
onSkip: () => T;
|
||||
skipLog?: { event: string; message: string };
|
||||
sweepOwned?: boolean;
|
||||
write: (context: { tx: DbTransaction; fresh: FenceableWorkspaceRow }) => Promise<T>;
|
||||
}): Promise<T> {
|
||||
return withExecutionWorkspaceLifecycleLock(db, input.workspaceId, async (tx) => {
|
||||
@@ -2317,7 +2318,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
}
|
||||
return input.onSkip();
|
||||
}
|
||||
await measureRunPhase("workspace.notification", () => notifyTerminalWorkspaceWork(tx, `workspace:${input.workspaceId}`));
|
||||
if (!input.sweepOwned) await measureRunPhase("workspace.notification", () => notifyTerminalWorkspaceWork(tx, `workspace:${input.workspaceId}`));
|
||||
return input.write({ tx, fresh });
|
||||
});
|
||||
}
|
||||
@@ -2337,11 +2338,12 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
// snapshot.
|
||||
async function clearReopenPendingConsumptionUnderLock(
|
||||
workspaceId: string,
|
||||
options: { expectedGeneration: number; requireStaleSinceBefore?: Date },
|
||||
options: { expectedGeneration: number; requireStaleSinceBefore?: Date; sweepOwned?: boolean },
|
||||
): Promise<boolean> {
|
||||
return fenceLifecycleGenerationWrite<boolean>({
|
||||
workspaceId,
|
||||
expectedGeneration: options.expectedGeneration,
|
||||
sweepOwned: options.sweepOwned,
|
||||
isWriteTarget: (fresh) => {
|
||||
if (!metadataHasReopenPendingConsumption(fresh.metadata)) return false;
|
||||
if (options.requireStaleSinceBefore) {
|
||||
@@ -2622,6 +2624,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
return fenceLifecycleGenerationWrite<{ cleaned: boolean; warnings: string[]; skippedReopened?: boolean }>({
|
||||
workspaceId: workspace.id,
|
||||
expectedGeneration: capturedGeneration,
|
||||
sweepOwned: true,
|
||||
isWriteTarget: (fresh) => isClosedExecutionWorkspaceStatus(fresh.status),
|
||||
skipLog: {
|
||||
event: "execution_workspace.cleanup_skipped",
|
||||
@@ -2753,6 +2756,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
return fenceLifecycleGenerationWrite<boolean>({
|
||||
workspaceId: input.workspaceId,
|
||||
expectedGeneration: input.capturedGeneration,
|
||||
sweepOwned: true,
|
||||
isWriteTarget: (fresh) => isClosedExecutionWorkspaceStatus(fresh.status),
|
||||
skipLog: {
|
||||
event: "execution_workspace.cleanup_failed_write_skipped",
|
||||
@@ -3798,7 +3802,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
const expirationTree = usingIssueIds ? issueTree.filter(issue => usingIssueIds.has(issue.id)) : issueTree;
|
||||
if (!sourceIssue || expirationTree.some(issue => !TERMINAL_ISSUE_STATUSES.has(issue.status))) {
|
||||
if (reopenPending) {
|
||||
await clearReopenPendingConsumptionUnderLock(workspace.id, {
|
||||
await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true,
|
||||
expectedGeneration: readExecutionWorkspaceLifecycleGeneration(
|
||||
workspace.metadata as Record<string, unknown> | null,
|
||||
),
|
||||
@@ -4055,7 +4059,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
// a later terminal cycle can archive the workspace again. Pass the
|
||||
// generation from this snapshot, so the clear skips a newer reopen that
|
||||
// raised the generation and installed its own fence after this read.
|
||||
await clearReopenPendingConsumptionUnderLock(workspace.id, {
|
||||
await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true,
|
||||
expectedGeneration: readExecutionWorkspaceLifecycleGeneration(
|
||||
workspace.metadata as Record<string, unknown> | null,
|
||||
),
|
||||
@@ -4127,7 +4131,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic
|
||||
// the rebuilt worktree. Pass this snapshot's generation and re-confirm
|
||||
// staleness against the fresh row under the lock, so a newer reopen that
|
||||
// raised the generation or refreshed the timestamp keeps its live fence.
|
||||
const cleared = await clearReopenPendingConsumptionUnderLock(workspace.id, {
|
||||
const cleared = await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true,
|
||||
expectedGeneration: readExecutionWorkspaceLifecycleGeneration(
|
||||
workspace.metadata as Record<string, unknown> | null,
|
||||
),
|
||||
|
||||
@@ -87,12 +87,21 @@ export async function cleanupRemoteWorkspaceAllocation(input: {
|
||||
if (!persistence.ready || !persistence.authority) throw new Error(persistence.reason ?? "remote_workspace_saved_state_required");
|
||||
const location = persistence.authority.location;
|
||||
if (location) {
|
||||
const released = await input.destroyLease({ companyId: workspace.companyId, workspaceId: workspace.id,
|
||||
leaseId: location.leaseId, environmentId: location.environmentId, providerLeaseId: location.providerLeaseId });
|
||||
if (!released || !hasRemoteTerminationReceipt(released)
|
||||
|| (released.metadata?.remoteExecutionTermination as { state?: unknown } | undefined)?.state !== "destroyed") {
|
||||
throw new Error("remote_workspace_cleanup_unconfirmed");
|
||||
const destroyed = (lease: Parameters<typeof hasRemoteTerminationReceipt>[0] | null | undefined) => Boolean(lease && hasRemoteTerminationReceipt(lease)
|
||||
&& (lease.metadata?.remoteExecutionTermination as { state?: unknown } | undefined)?.state === "destroyed");
|
||||
const [stored] = await input.db.select().from(environmentLeases).where(and(
|
||||
eq(environmentLeases.companyId, workspace.companyId), eq(environmentLeases.id, location.leaseId),
|
||||
eq(environmentLeases.environmentId, location.environmentId), eq(environmentLeases.providerLeaseId, location.providerLeaseId),
|
||||
));
|
||||
if (!destroyed(stored)) {
|
||||
const released = await input.destroyLease({ companyId: workspace.companyId, workspaceId: workspace.id,
|
||||
leaseId: location.leaseId, environmentId: location.environmentId, providerLeaseId: location.providerLeaseId });
|
||||
if (!destroyed(released)) throw new Error("remote_workspace_cleanup_unconfirmed");
|
||||
}
|
||||
await input.db.update(executionWorkspaces)
|
||||
.set({ metadata: sql`jsonb_set(${executionWorkspaces.metadata}, '{remoteWorkspace,location}', 'null'::jsonb)`, updatedAt: new Date() })
|
||||
.where(and(eq(executionWorkspaces.companyId, workspace.companyId), eq(executionWorkspaces.id, workspace.id),
|
||||
sql`${executionWorkspaces.metadata}->'remoteWorkspace'->'location' = ${JSON.stringify(location)}::jsonb`));
|
||||
}
|
||||
return { cleaned: true, warnings: [] as string[] };
|
||||
}
|
||||
Reference in new issue
Block a user