forked from farhoodlabs/paperclip
0808b388ee
## Thinking Path > - Paperclip is a control plane for autonomous AI companies, where work must end with a clear disposition rather than ambiguous agent liveness. > - Recovery currently detects stalled or missing-next-step issues, but source issue recovery can become split across child recovery issues, blockers, and comments. > - That makes it harder for operators and agents to see who owns recovery and what exact action is needed on the original issue. > - Source-scoped recovery actions give the original issue a first-class active recovery state with owner, evidence, wake policy, and resolution outcome. > - This pull request adds the recovery-action data model, backend reconciliation and resolution APIs, and board UI indicators/actions. > - The benefit is clearer stalled-work recovery without losing source issue context or relying on comments as the liveness path. ## What Changed - Added the `issue_recovery_actions` schema, shared types/constants/validators, and an idempotent `0084_issue_recovery_actions` migration ordered after current `master` migrations. - Updated stranded/missing-disposition recovery to create source-scoped recovery actions, wake the recovery owner on the source issue, and avoid locking the source issue for recovery-action wakes. - Added API support for reading active recovery actions on issue detail/list surfaces and resolving them with restored, blocked, cancelled, or false-positive outcomes. - Require blocked recovery resolutions to have an unresolved first-class blocker, and removed the UI shortcut that could mark recovery blocked without a blocker selection path. - Surfaced recovery indicators/actions in the issue UI, blocker notices, active run panels, issue rows, and Storybook coverage. - Updated docs and focused tests for recovery semantics, ownership, races, stale comments, and UI behavior. ## Verification - `pnpm exec vitest run server/src/__tests__/issue-recovery-actions.test.ts server/src/__tests__/heartbeat-process-recovery.test.ts ui/src/components/IssueRecoveryActionCard.test.tsx ui/src/components/IssueBlockedNotice.test.tsx ui/src/api/issues.test.ts` — 5 files, 72 tests passed. - `pnpm --filter @paperclipai/shared typecheck` — passed. - `pnpm --filter @paperclipai/db typecheck` — passed, including migration numbering check. - `pnpm --filter @paperclipai/server typecheck` — passed. - `pnpm --filter @paperclipai/ui typecheck` — passed. - Follow-up verification after blocker-resolution guard: `pnpm exec vitest run server/src/__tests__/issue-recovery-actions.test.ts ui/src/components/IssueRecoveryActionCard.test.tsx ui/src/api/issues.test.ts` — 3 files, 27 tests passed. - Follow-up `pnpm --filter @paperclipai/server typecheck` — passed. - Follow-up `pnpm --filter @paperclipai/ui typecheck` — passed. - UI states are available in `ui/storybook/stories/source-issue-recovery.stories.tsx`; screenshot capture helper is `scripts/screenshot-recovery-card.cjs`. ## Risks - Medium: recovery behavior changes from child recovery issue ownership toward source-scoped actions, so operators may see stalled-work state in new places. - Migration risk is mitigated by using the next migration slot after `master` and making the table/constraints/index creation idempotent for anyone who previously applied the old branch-local `0082_dizzy_master_mold` migration. - Existing child recovery issue paths are still guarded for already-created recovery issues, but new source-scoped flows should be watched in CI and Greptile review. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5 coding agent, tool use enabled for shell, Git, GitHub, and local test execution. Context window not exposed by the runtime. ## 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 run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
178 lines
5.5 KiB
TypeScript
178 lines
5.5 KiB
TypeScript
import fs from "node:fs";
|
|
import net from "node:net";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { applyPendingMigrations, ensurePostgresDatabase } from "./client.js";
|
|
|
|
type EmbeddedPostgresInstance = {
|
|
initialise(): Promise<void>;
|
|
start(): Promise<void>;
|
|
stop(): Promise<void>;
|
|
};
|
|
|
|
type EmbeddedPostgresCtor = new (opts: {
|
|
databaseDir: string;
|
|
user: string;
|
|
password: string;
|
|
port: number;
|
|
persistent: boolean;
|
|
initdbFlags?: string[];
|
|
onLog?: (message: unknown) => void;
|
|
onError?: (message: unknown) => void;
|
|
}) => EmbeddedPostgresInstance;
|
|
|
|
export type EmbeddedPostgresTestSupport = {
|
|
supported: boolean;
|
|
reason?: string;
|
|
};
|
|
|
|
export type EmbeddedPostgresTestDatabase = {
|
|
connectionString: string;
|
|
cleanup(): Promise<void>;
|
|
};
|
|
|
|
let embeddedPostgresSupportPromise: Promise<EmbeddedPostgresTestSupport> | null = null;
|
|
|
|
const DEFAULT_PAPERCLIP_EMBEDDED_POSTGRES_PORT = 54329;
|
|
|
|
function getReservedTestPorts(): Set<number> {
|
|
const configuredPorts = [
|
|
DEFAULT_PAPERCLIP_EMBEDDED_POSTGRES_PORT,
|
|
Number.parseInt(process.env.PAPERCLIP_EMBEDDED_POSTGRES_PORT ?? "", 10),
|
|
...String(process.env.PAPERCLIP_TEST_POSTGRES_RESERVED_PORTS ?? "")
|
|
.split(",")
|
|
.map((value) => Number.parseInt(value.trim(), 10)),
|
|
];
|
|
return new Set(configuredPorts.filter((port) => Number.isInteger(port) && port > 0 && port <= 65535));
|
|
}
|
|
|
|
async function getEmbeddedPostgresCtor(): Promise<EmbeddedPostgresCtor> {
|
|
const mod = await import("embedded-postgres");
|
|
return mod.default as EmbeddedPostgresCtor;
|
|
}
|
|
|
|
async function getAvailablePort(): Promise<number> {
|
|
const reservedPorts = getReservedTestPorts();
|
|
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
const port = await new Promise<number>((resolve, reject) => {
|
|
const server = net.createServer();
|
|
server.unref();
|
|
server.on("error", reject);
|
|
server.listen(0, "127.0.0.1", () => {
|
|
const address = server.address();
|
|
if (!address || typeof address === "string") {
|
|
server.close(() => reject(new Error("Failed to allocate test port")));
|
|
return;
|
|
}
|
|
const { port } = address;
|
|
server.close((error) => {
|
|
if (error) reject(error);
|
|
else resolve(port);
|
|
});
|
|
});
|
|
});
|
|
|
|
if (!reservedPorts.has(port)) return port;
|
|
}
|
|
|
|
throw new Error(
|
|
`Failed to allocate embedded Postgres test port outside reserved Paperclip ports: ${[
|
|
...reservedPorts,
|
|
].join(", ")}`,
|
|
);
|
|
}
|
|
|
|
async function createEmbeddedPostgresTestInstance(tempDirPrefix: string) {
|
|
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), tempDirPrefix));
|
|
const port = await getAvailablePort();
|
|
const EmbeddedPostgres = await getEmbeddedPostgresCtor();
|
|
const instance = new EmbeddedPostgres({
|
|
databaseDir: dataDir,
|
|
user: "paperclip",
|
|
password: "paperclip",
|
|
port,
|
|
persistent: true,
|
|
initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"],
|
|
onLog: () => {},
|
|
onError: () => {},
|
|
});
|
|
|
|
return { dataDir, port, instance };
|
|
}
|
|
|
|
function cleanupEmbeddedPostgresTestDirs(dataDir: string) {
|
|
fs.rmSync(dataDir, { recursive: true, force: true });
|
|
}
|
|
|
|
function formatEmbeddedPostgresError(error: unknown): string {
|
|
if (error instanceof Error && error.message.length > 0) return error.message;
|
|
if (typeof error === "string" && error.length > 0) return error;
|
|
return "embedded Postgres startup failed";
|
|
}
|
|
|
|
async function probeEmbeddedPostgresSupport(): Promise<EmbeddedPostgresTestSupport> {
|
|
let dataDir: string | null = null;
|
|
let instance: EmbeddedPostgresInstance | null = null;
|
|
|
|
try {
|
|
const created = await createEmbeddedPostgresTestInstance(
|
|
"paperclip-embedded-postgres-probe-",
|
|
);
|
|
dataDir = created.dataDir;
|
|
instance = created.instance;
|
|
await instance.initialise();
|
|
await instance.start();
|
|
return { supported: true };
|
|
} catch (error) {
|
|
return {
|
|
supported: false,
|
|
reason: formatEmbeddedPostgresError(error),
|
|
};
|
|
} finally {
|
|
await instance?.stop().catch(() => {});
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
}
|
|
}
|
|
|
|
export async function getEmbeddedPostgresTestSupport(): Promise<EmbeddedPostgresTestSupport> {
|
|
if (!embeddedPostgresSupportPromise) {
|
|
embeddedPostgresSupportPromise = probeEmbeddedPostgresSupport();
|
|
}
|
|
return await embeddedPostgresSupportPromise;
|
|
}
|
|
|
|
export async function startEmbeddedPostgresTestDatabase(
|
|
tempDirPrefix: string,
|
|
): Promise<EmbeddedPostgresTestDatabase> {
|
|
let dataDir: string | null = null;
|
|
let instance: EmbeddedPostgresInstance | null = null;
|
|
|
|
try {
|
|
const created = await createEmbeddedPostgresTestInstance(tempDirPrefix);
|
|
dataDir = created.dataDir;
|
|
instance = created.instance;
|
|
const { port } = created;
|
|
await instance.initialise();
|
|
await instance.start();
|
|
|
|
const adminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/postgres`;
|
|
await ensurePostgresDatabase(adminConnectionString, "paperclip");
|
|
const connectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/paperclip`;
|
|
await applyPendingMigrations(connectionString);
|
|
|
|
return {
|
|
connectionString,
|
|
cleanup: async () => {
|
|
await instance?.stop().catch(() => {});
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
},
|
|
};
|
|
} catch (error) {
|
|
await instance?.stop().catch(() => {});
|
|
if (dataDir) cleanupEmbeddedPostgresTestDirs(dataDir);
|
|
throw new Error(
|
|
`Failed to start embedded PostgreSQL test database: ${formatEmbeddedPostgresError(error)}`,
|
|
);
|
|
}
|
|
}
|