Skip to content

The open-source app everyone uses to manage agents at work

fix: prevent duplicate task creation and recovery loops (#9648)

Dotta53f09cb818b9
diff --git a/.agents/skills/pr-gardening/SKILL.md b/.agents/skills/pr-gardening/SKILL.md
index 925ca6db55..5819c3d595 100644
--- a/.agents/skills/pr-gardening/SKILL.md
+++ b/.agents/skills/pr-gardening/SKILL.md
@@ -64,6 +64,19 @@ For every candidate, the script re-fetches the current head SHA and records:
Verdicts are `ready`, `needs_gardening`, or `report_only` for drafts. Always rerun this stage after any wake or claim that a PR was fixed. Never trust issue comments as proof of readiness.
+## Follow-up Create-PR Task Deduplication
+
+If gardening decides a branch needs a follow-up task to create a single pull request, deduplicate before creating anything.
+
+For each branch, process one branch at a time and do this serially:
+
+1. Search open Paperclip issues for the exact branch name with statuses `backlog`, `todo`, `in_progress`, `in_review`, and `blocked`.
+2. Inspect matching issue titles, descriptions, and recent comments for an equivalent open "create PR from this branch" task for the same branch.
+3. If an equivalent open task exists, reuse it: add a concise comment with the current PR/head/reason context and link it from the gardening issue or blocker list. Do not create another task.
+4. Only if no equivalent open task exists, create exactly one follow-up task for that branch.
+
+Never fan out follow-up task creation in parallel. Do not issue concurrent `POST /api/companies/:companyId/issues` calls for create-PR tasks. After P1's issue-create idempotency support is available, every create-PR follow-up task creation must include `idempotencyKey: "pr-gardening:create-pr:{branch}"`, where `{branch}` is the exact branch name.
+
## Stage C — Comment on Originating Issues
Skip this stage in `--dry-run` mode and for `ready` or `report_only` entries.
diff --git a/packages/db/src/migrations/0176_issue_create_idempotency_key_expiry.sql b/packages/db/src/migrations/0176_issue_create_idempotency_key_expiry.sql
new file mode 100644
index 0000000000..e75cf2805f
--- /dev/null
+++ b/packages/db/src/migrations/0176_issue_create_idempotency_key_expiry.sql
@@ -0,0 +1,2 @@
+CREATE INDEX IF NOT EXISTS "issue_create_idempotency_keys_company_created_at_idx"
+ ON "issue_create_idempotency_keys" USING btree ("company_id", "created_at");
diff --git a/packages/db/src/migrations/meta/_journal.json b/packages/db/src/migrations/meta/_journal.json
index 0e0c76170c..2e32c8fb8a 100644
--- a/packages/db/src/migrations/meta/_journal.json
+++ b/packages/db/src/migrations/meta/_journal.json
@@ -1219,6 +1219,13 @@
"when": 1784210755027,
"tag": "0175_nested_skill_folders",
"breakpoints": true
+ },
+ {
+ "idx": 176,
+ "version": "7",
+ "when": 1784211956161,
+ "tag": "0176_issue_create_idempotency_key_expiry",
+ "breakpoints": true
}
]
}
diff --git a/packages/db/src/schema/issue_create_idempotency_keys.ts b/packages/db/src/schema/issue_create_idempotency_keys.ts
index c428d9943d..8a4f918e1f 100644
--- a/packages/db/src/schema/issue_create_idempotency_keys.ts
+++ b/packages/db/src/schema/issue_create_idempotency_keys.ts
@@ -17,5 +17,9 @@ export const issueCreateIdempotencyKeys = pgTable(
table.idempotencyKey,
),
issueIdx: index("issue_create_idempotency_keys_issue_idx").on(table.issueId),
+ companyCreatedAtIdx: index("issue_create_idempotency_keys_company_created_at_idx").on(
+ table.companyId,
+ table.createdAt,
+ ),
}),
);
diff --git a/server/src/__tests__/issue-assigned-backlog-contract-routes.test.ts b/server/src/__tests__/issue-assigned-backlog-contract-routes.test.ts
index 52e8224fc2..ac80ef32d5 100644
--- a/server/src/__tests__/issue-assigned-backlog-contract-routes.test.ts
+++ b/server/src/__tests__/issue-assigned-backlog-contract-routes.test.ts
@@ -178,7 +178,7 @@ describe("assigned backlog creation contract", () => {
}));
mockIssueService.create.mockImplementation(async (_companyId: string, data: Record<string, unknown>) =>
makeIssue({
- id: "issue-1",
+ id: String(data.id),
title: String(data.title),
status: String(data.status),
assigneeAgentId: data.assigneeAgentId as string | null | undefined,
@@ -326,7 +326,7 @@ describe("assigned backlog creation contract", () => {
expect.anything(),
expect.objectContaining({
action: "issue.created",
- entityId: "issue-1",
+ entityId: expect.any(String),
details: expect.objectContaining({
status: "backlog",
statusDefaulted: false,
diff --git a/server/src/__tests__/issue-create-deduplication-routes.test.ts b/server/src/__tests__/issue-create-deduplication-routes.test.ts
index 3958a6dd28..9a7701cba5 100644
--- a/server/src/__tests__/issue-create-deduplication-routes.test.ts
+++ b/server/src/__tests__/issue-create-deduplication-routes.test.ts
@@ -19,7 +19,10 @@ import {
import { actorMiddleware } from "../middleware/auth.js";
import { errorHandler } from "../middleware/index.js";
import { issueRoutes } from "../routes/issues.js";
-import { issueService } from "../services/issues.js";
+import {
+ ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_DAYS,
+ issueService,
+} from "../services/issues.js";
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
@@ -112,6 +115,45 @@ describeEmbeddedPostgres("issue create deduplication routes", () => {
expect(await db.select().from(issueCreateIdempotencyKeys)).toHaveLength(1);
});
+ it("expires old idempotency keys before replay lookup", async () => {
+ const companyId = await seedCompany();
+ const parent = await seedParent(companyId);
+ const app = createApp();
+ const oldIssueId = randomUUID();
+ const idempotencyKey = "run-1:expired-retry";
+ const expiredCreatedAt = new Date(
+ Date.now() - (ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_DAYS + 1) * 24 * 60 * 60 * 1000,
+ );
+ await db.insert(issues).values({
+ id: oldIssueId,
+ companyId,
+ parentId: parent.id,
+ title: "Expired retry target",
+ status: "todo",
+ priority: "medium",
+ });
+ await db.insert(issueCreateIdempotencyKeys).values({
+ companyId,
+ idempotencyKey,
+ issueId: oldIssueId,
+ createdAt: expiredCreatedAt,
+ });
+
+ const recreated = await request(app)
+ .post(`/api/companies/${companyId}/issues`)
+ .send({ parentId: parent.id, title: "Expired retry creates new work", idempotencyKey })
+ .expect(201);
+
+ const rows = await db.select().from(issueCreateIdempotencyKeys);
+ expect(recreated.body.id).not.toBe(oldIssueId);
+ expect(rows).toHaveLength(1);
+ expect(rows[0]).toMatchObject({
+ companyId,
+ idempotencyKey,
+ issueId: recreated.body.id,
+ });
+ });
+
it("returns a recent open sibling whose normalized title matches", async () => {
const companyId = await seedCompany();
const parent = await seedParent(companyId);
diff --git a/server/src/__tests__/productivity-review-service.test.ts b/server/src/__tests__/productivity-review-service.test.ts
index 9dbf784923..12b3bdd18c 100644
--- a/server/src/__tests__/productivity-review-service.test.ts
+++ b/server/src/__tests__/productivity-review-service.test.ts
@@ -385,6 +385,62 @@ describeEmbeddedPostgres("productivity review service", () => {
expect(await listProductivityReviews(seeded.companyId)).toHaveLength(4);
});
+ it("uses review creation order for no-action streak windows", async () => {
+ const now = new Date("2026-04-28T12:00:00.000Z");
+ const seeded = await seedAssignedIssue();
+ await insertRuns({
+ companyId: seeded.companyId,
+ agentId: seeded.coderId,
+ issueId: seeded.issueId,
+ count: DEFAULT_PRODUCTIVITY_REVIEW_NO_COMMENT_STREAK_RUNS,
+ now,
+ });
+ const reviewWindows = [
+ { hoursAgo: 96, updatedAt: new Date(now.getTime() - 95 * 60 * 60 * 1000) },
+ { hoursAgo: 72, updatedAt: new Date(now.getTime() - 7 * 60 * 60 * 1000) },
+ { hoursAgo: 48, updatedAt: new Date(now.getTime() - 47 * 60 * 60 * 1000) },
+ ].map((window, index) => {
+ const createdAt = new Date(now.getTime() - window.hoursAgo * 60 * 60 * 1000);
+ return {
+ id: randomUUID(),
+ companyId: seeded.companyId,
+ title: `Productivity review ordered window ${index + 1}`,
+ status: "done" as const,
+ priority: "high" as const,
+ originKind: PRODUCTIVITY_REVIEW_ORIGIN_KIND,
+ originId: seeded.issueId,
+ originFingerprint: `productivity-review:${seeded.issueId}`,
+ parentId: seeded.issueId,
+ issueNumber: index + 2,
+ identifier: `${seeded.issuePrefix}-${index + 2}`,
+ createdAt,
+ updatedAt: window.updatedAt,
+ };
+ });
+ const middleReviewCreatedAt = reviewWindows[1]!.createdAt;
+ await db.insert(issues).values(reviewWindows);
+ await db.insert(activityLog).values({
+ companyId: seeded.companyId,
+ actorType: "agent",
+ actorId: seeded.coderId,
+ agentId: seeded.coderId,
+ action: "issue.updated",
+ entityType: "issue",
+ entityId: seeded.issueId,
+ createdAt: new Date(middleReviewCreatedAt.getTime() + 60_000),
+ });
+
+ const result = await productivityReviewService(db).reconcileProductivityReviews({
+ now,
+ companyId: seeded.companyId,
+ thresholds: { maxConsecutiveNoActionReviews: 1 },
+ });
+
+ expect(result.created).toBe(0);
+ expect(result.noActionSuppressed).toBe(1);
+ expect(await listProductivityReviews(seeded.companyId)).toHaveLength(3);
+ });
+
it("does not count cancelled productivity reviews toward the creation cap", async () => {
const now = new Date("2026-04-28T12:00:00.000Z");
const seeded = await seedAssignedIssue();
diff --git a/server/src/services/issues.ts b/server/src/services/issues.ts
index 6184683d01..3130570c1c 100644
--- a/server/src/services/issues.ts
+++ b/server/src/services/issues.ts
@@ -134,6 +134,9 @@ const ISSUE_COMMENT_RUN_LOG_DERIVATION_MAX_LOG_BYTES = 2_000_000;
const ISSUE_COMMENT_RUN_LOG_DERIVATION_CHUNK_BYTES = 256_000;
const ISSUE_COMMENT_RUN_LOG_DERIVATION_END_SLACK_MS = 60_000;
const ISSUE_COMMENT_RUN_LOG_DERIVATION_MAX_PARALLEL_READS = 8;
+export const ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_DAYS = 7;
+const ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_MS = ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_DAYS * 24 * 60 * 60 * 1000;
+const ISSUE_CREATE_IDEMPOTENCY_KEY_CLEANUP_BATCH_SIZE = 500;
const DELETED_ISSUE_COMMENT_BODY = "";
const ISSUE_WAKE_DIAGNOSTICS_ACTIVITY_ACTIONS = ["issue.tree_hold_wakeup_deferred"] as const;
@@ -6152,6 +6155,19 @@ export function issueService(db: Db) {
let existingIssue: typeof issues.$inferSelect | undefined;
let deduplicationReason: "idempotency_key" | "recent_open_title" | null = null;
if (idempotencyKey) {
+ const idempotencyKeyRetentionCutoff = new Date(Date.now() - ISSUE_CREATE_IDEMPOTENCY_KEY_RETENTION_MS);
+ await tx.execute(sql`
+ delete from ${issueCreateIdempotencyKeys}
+ where ${issueCreateIdempotencyKeys.id} in (
+ select ${issueCreateIdempotencyKeys.id}
+ from ${issueCreateIdempotencyKeys}
+ where ${issueCreateIdempotencyKeys.companyId} = ${companyId}
+ and ${issueCreateIdempotencyKeys.createdAt} < ${idempotencyKeyRetentionCutoff.toISOString()}::timestamptz
+ order by ${issueCreateIdempotencyKeys.createdAt} asc, ${issueCreateIdempotencyKeys.id} asc
+ limit ${ISSUE_CREATE_IDEMPOTENCY_KEY_CLEANUP_BATCH_SIZE}
+ )
+ `);
+
[existingIssue] = await tx
.select()
.from(issueCreateIdempotencyKeys)
diff --git a/server/src/services/productivity-review.ts b/server/src/services/productivity-review.ts
index 8301ea6e08..e694f2e222 100644
--- a/server/src/services/productivity-review.ts
+++ b/server/src/services/productivity-review.ts
@@ -333,7 +333,7 @@ export function productivityReviewService(db: Db, deps?: { enqueueWakeup?: Enque
visibleIssueCondition(),
),
)
- .orderBy(desc(issues.updatedAt), desc(issues.id))
+ .orderBy(desc(issues.createdAt), desc(issues.id))
.limit(thresholds.maxConsecutiveNoActionReviews);
const earliestReviewCreatedAt = completedReviews.at(-1)?.createdAt;