mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-14 17:22:00 +01:00
## Thinking Path > - Paperclip manages work for AI agents. > - Retry checks whether the task can keep its existing workspace. > - A provider can complete checkout before Core receives the result. > - Recreating a completed worktree costs time and can change its base commit. > - Core uses the same ownership and writer checks for local and provider workspaces. > - The provider verifies its checkout before Core creates a fresh workspace. ## Linked Issues or Issue Description **What happened?** A retry can allocate a new provider worktree even though the original checkout completed. **Steps to reproduce** Complete workspace checkout through a storage provider. Fail Runner startup or lose the prepare reply. Retry the task. **Expected behavior** Reuse the same worktree when the provider confirms checkout and the prior run can no longer write to it. Related: #20865 addresses timeouts while the original prepare is in progress. This PR checks an earlier checkout when the user retries. ## What Changed - Check the prior task binding and writer state before asking a storage provider to verify a checkout. - Extend the existing prepare operation with an optional verification mode. Providers opt in through their manifest. - Return through normal workspace reuse after verification. Keep the fresh retry fallback when verification cannot establish checkout. ## Verification - Core workspace preparation, plugin workspace ownership, retry continuity, and SDK workspace-provider suites passed locally (213 tests). - Focused cases pass for provider verification, retry through heartbeat dispatch, and real local Git checkout verification with no plugin worker. The heartbeat test confirms that ordinary preparation and adapter dispatch keep the original workspace, branch, and base commit. - Core server typecheck passed before the final audit and timing edits. GitHub CI covers the final revision and broader regressions. ## Risks Provider verification adds one bounded call before a fresh retry. Older providers keep their current behavior. Verification must preserve the workspace, branch, and recorded base commit. The normal execution preparation checks still run before admission. No database migration is needed. ## Model Used OpenAI GPT-6 (Codex), with source inspection and local test execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either linked existing issues or described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal issue details - [x] I have run targeted tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
142 lines
6.8 KiB
TypeScript
142 lines
6.8 KiB
TypeScript
import { and, eq, notInArray } from "drizzle-orm";
|
|
import { pluginCompanySettings, plugins, type Db } from "@paperclipai/db";
|
|
import type { Environment, PaperclipPluginManifestV1, ProjectExecutionWorkspacePolicy, ProjectWorkspaceProviderConfiguration } from "@paperclipai/shared";
|
|
import { parseProjectExecutionWorkspacePolicy } from "./execution-workspace-policy.js";
|
|
import type { PluginWorkerManager } from "./plugin-worker-manager.js";
|
|
import { workspaceWorkerRefusal } from "./workspace-provider-failure.js";
|
|
import { conflict } from "../errors.js";
|
|
|
|
export function isWorkspaceProvider(manifest: PaperclipPluginManifestV1) {
|
|
return Boolean(manifest.workspaceProvider && manifest.capabilities.includes("workspaces.manage") &&
|
|
manifest.resourceLifecycle?.includes("project"));
|
|
}
|
|
|
|
type ProviderDb = Pick<Db, "select">;
|
|
export type WorkspaceWorkers = Pick<PluginWorkerManager, "call" | "getWorker">;
|
|
|
|
export async function requireWorkspaceProviderWorker(
|
|
db: ProviderDb,
|
|
workers: WorkspaceWorkers | undefined,
|
|
companyId: string,
|
|
pluginId: string,
|
|
/** Before a call, a ready provider whose worker is not running is a retryable refusal: no request is sent. */
|
|
beforeCall = false,
|
|
) {
|
|
const [plugin] = await db
|
|
.select()
|
|
.from(plugins)
|
|
.where(eq(plugins.id, pluginId));
|
|
const [settings] = await db
|
|
.select()
|
|
.from(pluginCompanySettings)
|
|
.where(
|
|
and(
|
|
eq(pluginCompanySettings.pluginId, pluginId),
|
|
eq(pluginCompanySettings.companyId, companyId),
|
|
),
|
|
);
|
|
const worker = workers?.getWorker(pluginId);
|
|
const refusal = beforeCall && workers && plugin?.status === "ready" && settings?.enabled !== false &&
|
|
isWorkspaceProvider(plugin.manifestJson) ? workspaceWorkerRefusal(pluginId, worker) : null;
|
|
if (refusal) throw refusal;
|
|
if (
|
|
!workers ||
|
|
!plugin ||
|
|
plugin.status !== "ready" ||
|
|
settings?.enabled === false ||
|
|
!isWorkspaceProvider(plugin.manifestJson) ||
|
|
!worker?.supportedMethods?.includes("workspaceOperation")
|
|
) {
|
|
throw conflict("The workspace storage plugin is unavailable");
|
|
}
|
|
return plugin;
|
|
}
|
|
|
|
export async function listProjectWorkspaceProviders(db: ProviderDb, companyId: string) {
|
|
const rows = await db.select({ plugin: plugins, enabled: pluginCompanySettings.enabled })
|
|
.from(plugins)
|
|
.leftJoin(pluginCompanySettings, and(eq(pluginCompanySettings.pluginId, plugins.id),
|
|
eq(pluginCompanySettings.companyId, companyId)))
|
|
.where(notInArray(plugins.status, ["disabled", "uninstalled"]));
|
|
return rows.filter(row => row.enabled !== false && isWorkspaceProvider(row.plugin.manifestJson))
|
|
.map(row => row.plugin);
|
|
}
|
|
|
|
type Provider = Awaited<ReturnType<typeof listProjectWorkspaceProviders>>[number];
|
|
|
|
export function resolveProjectWorkspaceProvider(
|
|
rawPolicy: unknown,
|
|
providers: Provider[],
|
|
hasGitSource: boolean,
|
|
): ProjectWorkspaceProviderConfiguration {
|
|
const policy = parseProjectExecutionWorkspacePolicy(rawPolicy);
|
|
const explicit = policy?.storageProviderPluginId !== undefined;
|
|
const pluginId = policy?.storageProviderPluginId ?? null;
|
|
const selected = pluginId ? providers.find(plugin => plugin.id === pluginId) : undefined;
|
|
let error: string | null = null;
|
|
const declaration = selected?.manifestJson.workspaceProvider;
|
|
const supplied = hasGitSource ? declaration?.defaultForProjects : undefined;
|
|
const effectivePolicy = pluginId ? parseProjectExecutionWorkspacePolicy({
|
|
enabled: true,
|
|
...(supplied ? { defaultMode: supplied.mode } : {}),
|
|
...policy,
|
|
storageProviderPluginId: pluginId,
|
|
...(supplied ? { workspaceStrategy: { type: supplied.strategy, ...policy?.workspaceStrategy } } : {}),
|
|
}) : policy;
|
|
if (pluginId && !selected) error = "The workspace storage plugin is unavailable";
|
|
if (declaration && effectivePolicy && hasGitSource && (
|
|
effectivePolicy.enabled === false ||
|
|
(effectivePolicy.defaultMode && !declaration.modes.includes(effectivePolicy.defaultMode as "isolated_workspace")) ||
|
|
(effectivePolicy.workspaceStrategy?.type && !declaration.strategies.includes(effectivePolicy.workspaceStrategy.type as "git_worktree"))
|
|
)) {
|
|
error = "The storage plugin does not support the explicit project workspace settings. Change the workspace mode or strategy, or select another storage provider in project configuration.";
|
|
}
|
|
return {
|
|
source: explicit ? "project" : "core",
|
|
pluginId,
|
|
displayName: selected?.manifestJson.displayName ?? (pluginId ? "Unavailable provider" : "Core / local"),
|
|
providers: providers.map(plugin => ({ id: plugin.id, displayName: plugin.manifestJson.displayName,
|
|
isDefault: Boolean(plugin.manifestJson.workspaceProvider?.defaultForProjects) })),
|
|
effectivePolicy,
|
|
error,
|
|
};
|
|
}
|
|
|
|
export function sourcelessProjectWorkspacePolicy(rawPolicy: unknown): ProjectExecutionWorkspacePolicy | null {
|
|
const policy = parseProjectExecutionWorkspacePolicy(rawPolicy);
|
|
if (policy?.storageProviderPluginId === undefined) return policy;
|
|
const { storageProviderPluginId: _storage, ...settings } = policy;
|
|
return Object.keys(settings).some(key => key !== "enabled") ? settings : null;
|
|
}
|
|
|
|
export async function effectiveProjectWorkspacePolicy(db: ProviderDb, companyId: string, rawPolicy: unknown, hasGitSource: boolean) {
|
|
const configuration = resolveProjectWorkspaceProvider(rawPolicy, await listProjectWorkspaceProviders(db, companyId), hasGitSource);
|
|
if (configuration.error) throw conflict(configuration.error);
|
|
return configuration.effectivePolicy;
|
|
}
|
|
|
|
export function projectStorageForEnvironment(
|
|
environment: Pick<Environment, "driver" | "config">,
|
|
providers: Array<Pick<Provider, "id" | "pluginKey" | "manifestJson">>,
|
|
): { pluginId: string | null } | null {
|
|
if (["local", "ssh", "sandbox"].includes(environment.driver)) {
|
|
return environment.driver === "sandbox" && environment.config.provider === "fake" ? null : { pluginId: null };
|
|
}
|
|
if (environment.driver !== "plugin") return null;
|
|
const plugin = providers.find(value => value.pluginKey === environment.config.pluginKey);
|
|
const driver = plugin?.manifestJson.environmentDrivers?.find(value => value.driverKey === environment.config.driverKey
|
|
&& value.kind !== "sandbox_provider" && value.supportsTasks === true);
|
|
return plugin && driver && plugin.manifestJson.capabilities.includes("environment.drivers.register")
|
|
? { pluginId: plugin.id } : null;
|
|
}
|
|
|
|
export function assertProjectStorageEnvironment(
|
|
provider: Pick<Provider, "id" | "pluginKey" | "manifestJson">,
|
|
environment: Pick<Environment, "driver" | "config" | "name">,
|
|
) {
|
|
if (projectStorageForEnvironment(environment, [provider])?.pluginId !== provider.id) {
|
|
throw conflict(`This project's workspace storage (${provider.manifestJson.displayName}) does not support the environment "${environment.name}". Use an agent in a compatible environment, or create a project for this environment.`,
|
|
{ code: "project_environment_incompatible", retryable: false });
|
|
}
|
|
}
|