mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Every master commit must pass Cloud readiness before Docker publishes it and staging deploys it > - Two server tests failed there on and off, so master stopped reaching staging > - Both failures came from test setup that leaked across suites, not from product behavior > - CI runs each shard against one shared PostgreSQL server, and each suite gets its own database on it > - This pull request removes both leaks at their cause and adds a guard for each > - The benefit is that Cloud readiness no longer fails on these two tests, and Docker publishing and staging deploys continue ## Linked Issues or Issue Description **What happened?** Cloud readiness failed on master in two server test shards. 1. `antigravity-swarm.db.test.ts` failed with `duplicate key value violates unique constraint "companies_issue_prefix_idx"`. 2. `git-workspace-expiration.test.ts` > "rolls back when new history appears after capture without changing files" returned `skipReason: "inspection_failed"` instead of `restore_point_contents_changed`. This failed on four attempts of one master commit. **Root cause** 1. The swarm suite and 20 other suites made company issue prefixes from four random hex characters (65,536 values). The swarm suite seeds about 65 companies in one database, so one run of that file had about a 3% chance of a collision. 2. `isolated-remote-workspace-source-lock.test.ts` and `native-workspace-finalizer.test.ts` ran `ALTER ROLE <current_user> SET idle_in_transaction_session_timeout = '1s'`. In CI all suites in a shard share one PostgreSQL server and one role. So every new session in every other suite also got a 1-second idle-in-transaction limit. The expiration test holds a transaction open while it runs Git commands. PostgreSQL closed that session, and the expiration recorded `CONNECTION_CLOSED` as `inspection_failed`. The lock suite never reset the setting. **Steps to reproduce** 1. Run the lock suite and then the expiration test on one shared test server (the same layout CI uses). 2. Before this change, 40 of 40 runs of the expiration test fail with `CONNECTION_CLOSED`. **Expected behavior** A suite's session settings apply only to its own database. Seeded companies never share an issue prefix. ## What Changed - Added `uniqueIssuePrefix(tag)`, a shared test helper that adds 48 random bits (12 uppercase hex characters) to a tag. - Changed all 24 company seeds that used four or fewer random characters (21 files) to use the helper. Updated one identifier assertion in `cases-routes.test.ts` for the longer prefix. - Changed the two suites from `ALTER ROLE ... SET` to `ALTER DATABASE <current_database()> SET`, so the 1-second limit applies only to their own disposable database. The finalizer suite still resets it after its test. - Added two guard tests: one proves the helper does not repeat across 70,000 prefixes, and one fails if any test file changes a setting for a whole role. ## Verification - Deterministic red for the Git expiration failure: one shared test server, lock suite first, then the expiration test 40 times. Before: 40 of 40 failed with `CONNECTION_CLOSED`. After: 40 of 40 passed, and 40 of 40 passed again with 12 CPU-burning processes running. - The same failure appeared in PR CI before the fix: with the full suite on one shared server, 17 runs of the looped expiration test failed with `CONNECTION_CLOSED`. - Deterministic red for the prefix generator: the old four-character generator gives 42,898 distinct prefixes from 70,000 calls. The new helper gives 70,000. - `antigravity-swarm.db.test.ts`: 30 of 30 runs passed with 12 CPU-burning processes running. - All 21 suites that changed seeds pass locally. The two changed lock suites pass (72 tests). ## Risks - Low risk. Only test files change. The product code is unchanged. - Longer test prefixes make test issue identifiers longer. One assertion that checked a four-character prefix is updated. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, file edits, GitHub CLI) and extended reasoning. ## 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 (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run 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 - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
5.4 KiB
TypeScript
80 lines
5.4 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { agents, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRuns, issues } from "@paperclipai/db";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
|
|
import { rejectedModelRetryRefusal } from "./model-rejected-retry.js";
|
|
import { uniqueIssuePrefix } from "../../__tests__/helpers/issue-prefix.js";
|
|
|
|
const support = await getEmbeddedPostgresTestSupport();
|
|
const REJECTED = "openrouter/paperclip-eval/unavailable";
|
|
const WORKING = "openrouter/deepseek/deepseek-v4.1-flash-1017";
|
|
|
|
describe.skipIf(!support.supported)("retrying a run whose model the provider rejected", () => {
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: ReturnType<typeof createDb>;
|
|
beforeAll(async () => {
|
|
database = await startEmbeddedPostgresTestDatabase("paperclip-model-rejected-retry-");
|
|
db = createDb(database.connectionString);
|
|
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
|
|
afterAll(async () => { await db?.$client.end(); await database?.cleanup(); });
|
|
|
|
async function seed(input: { agentModel: string; overrideModel?: string; errorCode?: string }) {
|
|
const [company] = await db.insert(companies).values({ name: "Model retry", issuePrefix: uniqueIssuePrefix("M") }).returning();
|
|
const [agent] = await db.insert(agents).values({ companyId: company!.id, name: "Morgan", adapterType: "paperclip_runner",
|
|
adapterConfig: { provider: "opencode", model: input.agentModel } }).returning();
|
|
const [issue] = await db.insert(issues).values({ companyId: company!.id, title: "Website task", status: "blocked", assigneeAgentId: agent!.id,
|
|
assigneeAdapterOverrides: input.overrideModel ? { adapterConfig: { model: input.overrideModel } } : null }).returning();
|
|
const [run] = await db.insert(heartbeatRuns).values({ companyId: company!.id, agentId: agent!.id, status: "failed", runtimeMode: "native",
|
|
runtimeModeResolvedAt: new Date(), nativeIssueId: issue!.id, errorCode: input.errorCode ?? "native_provider_model_rejected",
|
|
contextSnapshot: { issueId: issue!.id },
|
|
runnerProfileJson: { nativeExecutionInput: { provider: { kind: "opencode", model: REJECTED } } } }).returning();
|
|
return { agent: agent!, issue: issue!, run: run! };
|
|
}
|
|
|
|
const check = (f: Awaited<ReturnType<typeof seed>>) => rejectedModelRetryRefusal(db, {
|
|
failedRun: f.run, agent: f.agent, issue: f.issue });
|
|
|
|
it("refuses while the agent still selects the rejected model and names the agent setting", async () => {
|
|
const f = await seed({ agentModel: REJECTED });
|
|
expect(await check(f)).toEqual({
|
|
message: `The provider rejected the model "${REJECTED}", and it is still selected. Choose a supported model in Morgan's settings, then try again.`,
|
|
details: { code: "native_provider_model_still_unavailable", setting: "agent_model", rejectedModel: REJECTED, agentId: f.agent.id, issueId: f.issue.id },
|
|
});
|
|
});
|
|
|
|
it("names the task's model override when that override still selects the rejected model", async () => {
|
|
const f = await seed({ agentModel: WORKING, overrideModel: REJECTED });
|
|
expect((await check(f))?.details.setting).toBe("task_model_override");
|
|
});
|
|
|
|
it.each([
|
|
{ shape: "the agent now selects another model", agentModel: WORKING },
|
|
{ shape: "the task override selects another model", agentModel: REJECTED, overrideModel: WORKING },
|
|
{ shape: "the run failed for another reason", agentModel: REJECTED, errorCode: "adapter_failed" },
|
|
])("allows the retry when $shape", async ({ agentModel, overrideModel, errorCode }) => {
|
|
expect(await check(await seed({ agentModel, overrideModel, errorCode }))).toBeNull();
|
|
});
|
|
|
|
it("compares the model a new run would resolve, including provider defaults", async () => {
|
|
const f = await seed({ agentModel: "" });
|
|
await db.update(agents).set({ adapterConfig: { provider: "acpx", acpxAgent: "grok", model: "grok-build" } }).where(eq(agents.id, f.agent.id));
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: { nativeExecutionInput: { provider: { kind: "acpx", model: "grok-4.7" } } } }).where(eq(heartbeatRuns.id, f.run.id));
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agent.id));
|
|
expect((await rejectedModelRetryRefusal(db, { failedRun: f.run, agent: agent!, issue: f.issue }))?.details).toMatchObject({ setting: "agent_model", rejectedModel: "grok-4.7" });
|
|
});
|
|
|
|
it("refuses when an omitted model resolves to the provider default that was rejected", async () => {
|
|
const f = await seed({ agentModel: "" });
|
|
await db.update(agents).set({ adapterConfig: { provider: "acpx", acpxAgent: "claude" } }).where(eq(agents.id, f.agent.id));
|
|
await db.update(heartbeatRuns).set({ runnerProfileJson: { nativeExecutionInput: { provider: { kind: "acpx", model: "claude-opus-5-5" } } } }).where(eq(heartbeatRuns.id, f.run.id));
|
|
const [agent] = await db.select().from(agents).where(eq(agents.id, f.agent.id));
|
|
expect((await rejectedModelRetryRefusal(db, { failedRun: f.run, agent: agent!, issue: f.issue }))?.details.rejectedModel).toBe("claude-opus-5-5");
|
|
});
|
|
|
|
it("ignores another company's run with the same id shape", async () => {
|
|
const f = await seed({ agentModel: REJECTED });
|
|
const other = await seed({ agentModel: REJECTED });
|
|
expect(await rejectedModelRetryRefusal(db, { failedRun: f.run, agent: other.agent, issue: other.issue })).toBeNull();
|
|
});
|
|
});
|