refactor(server): move the release half of the deferred wake state machine into a wake-queue module (#13132)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The heartbeat service releases deferred issue wakes and promotes the
next run
> - The release logic sat in a large service function, which made its
decisions and database effects hard to test
> - A promotion race could reopen an issue without creating the promoted
run
> - Company checks did not protect every read and write, and some
readers used a second transaction connection
> - This pull request moves the release logic into a layered wake-queue
module and closes these race and company-scope defects
> - The benefit is clearer decisions, safer writes, and focused tests
while existing callers keep the same entry point

## Linked Issues or Issue Description

Refs: #10195

## What Changed

- Move the release half of deferred issue execution from `heartbeat.ts`
into `server/src/modules/wake-queue/`.
- Add pure policy decisions with table-driven tests.
- Claim a wake before the reopen write and advance to the next wake when
the claim fails.
- Add company predicates to guarded reads and writes.
- Pass the transaction-scoped issue snapshot to reader ports.
- Keep `releaseIssueExecutionAndPromote` as the public wrapper.

## Verification

- Run `pnpm check:module-boundaries`.
- Run `pnpm exec tsc --noEmit` inside `server/` and compare the result
with the known baseline.
- Run the wake-queue unit and adapter tests in continuous integration.
- Check the promotion-claim ordering, guarded writes, transaction-scoped
reads, and cross-company outcomes.
- Search the pull request diff for internal issue identifiers.

## Risks

- The refactor changes the transaction path for deferred wake release.
- A stale or lost wake claim now skips that wake and continues with the
next queued wake.
- The public wrapper keeps its name and signature, which limits caller
risk.
- Continuous integration must confirm the full server test suite and
build.

## Model Used

OpenAI Codex, GPT-5, exact runtime model version not exposed, context
window not exposed, with shell, Git, and GitHub tool use.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Nicky LeachandPaperclip authored and GitHub committed 2026-09-09 21:01:26 -07:00
1 parent 018ca5daaf
commit 6dd48cad43
14 files changed
+3896 -1574

No files matched your search

File diff suppressed because it is too large. Load diff
+48 -4
View File
@@ -5638,7 +5638,7 @@ export function issueService(db: Db) {
return row;
}
return {
const service = {
clearExecutionRunIfTerminal,
clearCheckoutRunIfTerminal,
addStopRelayCommentIfNeeded,
@@ -7783,6 +7783,7 @@ export function issueService(db: Db) {
blockedByIssueIds?: string[];
actorAgentId?: string | null;
actorUserId?: string | null;
companyGuard?: string;
},
dbOrTx: any = db,
postCommitActivityPublications?: ActivityPublication[],
@@ -7792,10 +7793,18 @@ export function issueService(db: Db) {
const activityPublications = postCommitActivityPublications ?? ownedActivityPublications;
const ownedPostCommitActions: IssuePostCommitAction[] = [];
const queuedPostCommitActions = postCommitActions ?? ownedPostCommitActions;
// A caller that supplies `companyGuard` gets the company added to
// every read, lock, and write predicate below. A check before this
// call is not a boundary: `issues.company_id` can change between
// that check and this write, so the predicate must carry the
// company itself.
const idPredicate = data.companyGuard !== undefined
? and(eq(issues.id, id), eq(issues.companyId, data.companyGuard))
: eq(issues.id, id);
const existing = await dbOrTx
.select()
.from(issues)
.where(eq(issues.id, id))
.where(idPredicate)
.then((rows: Array<typeof issues.$inferSelect>) => rows[0] ?? null);
if (!existing) return null;
@@ -7804,6 +7813,7 @@ export function issueService(db: Db) {
blockedByIssueIds,
actorAgentId,
actorUserId,
companyGuard,
...issueData
} = data;
const isolatedWorkspacesEnabled = (await instanceSettings.getExperimental()).enableIsolatedWorkspaces;
@@ -7955,7 +7965,7 @@ export function issueService(db: Db) {
const receiptExisting = await tx
.select()
.from(issues)
.where(eq(issues.id, id))
.where(idPredicate)
.for("update")
.then((rows: Array<typeof issues.$inferSelect>) => rows[0] ?? null);
if (!receiptExisting) return null;
@@ -7994,7 +8004,7 @@ export function issueService(db: Db) {
const updated = await tx
.update(issues)
.set(patch)
.where(eq(issues.id, id))
.where(idPredicate)
.returning()
.then((rows: Array<typeof issues.$inferSelect>) => rows[0] ?? null);
if (!updated) return null;
@@ -9535,4 +9545,38 @@ export function issueService(db: Db) {
}));
},
};
type IssueServiceApi = typeof service & {
updateForCompany: (
id: string,
companyId: string,
data: Parameters<typeof service.update>[1],
dbOrTx?: any,
postCommitActivityPublications?: ActivityPublication[],
postCommitActions?: IssuePostCommitAction[],
) => ReturnType<typeof service.update>;
};
const serviceApi = service as IssueServiceApi;
// A company-scoped wrapper around `update`. It passes the company as a
// guard on every read, lock, and write predicate, so a caller with only
// a company id and an issue id cannot update an issue in another company.
serviceApi.updateForCompany = async (
id,
companyId,
data,
dbOrTx = db,
postCommitActivityPublications,
postCommitActions,
) => {
return service.update(
id,
{ ...data, companyGuard: companyId },
dbOrTx,
postCommitActivityPublications,
postCommitActions,
);
};
return serviceApi;
}