test(server): check the agent row lock with FOR SHARE so lock checks cannot block each other (#21775)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Each master commit must pass Cloud readiness before its canary and
image publish
> - `agent-file-restore-locking.test.ts`, added in #21694, failed master
Cloud readiness run 43712563988 on 2026-11-15, so that commit's canary
did not publish
> - The test reported the agent row as locked during the download, but
its own sampler measured 0 ms of product lock time
> - The cause is in the test: its two lock checks used `FOR UPDATE
NOWAIT` on one pool and could block each other
> - This pull request checks the lock with `FOR SHARE NOWAIT`, which
still detects the product's `FOR UPDATE` but never conflicts with
another check
> - The benefit is that the test fails only when the product really
holds the lock

## Linked Issues or Issue Description

**What happened?**
`downloads a stale projection without holding the agent row lock` failed
in master Cloud readiness run 43712563988, attempt 2:
`expected [{ objectKey, agentRowLocked: true }] to deeply equal [{
objectKey, agentRowLocked: false }]`.
The test's metric line showed `lockedMs: 0`.

**Steps to reproduce**
1. Make each sampler check hold its `FOR UPDATE NOWAIT` lock for 50 ms
(to make the overlap certain).
2. Run `pnpm exec vitest run
src/__tests__/agent-file-restore-locking.test.ts -t "stale projection"`
in `server/`.
3. The test fails 3 of 3 times with `agentRowLocked: true` and
`lockedMs: 0`, the same as CI.

**Expected behavior**
Only a lock held by the product makes the check report "locked".

Refs #21694

## What Changed

- `agentRowLocked()` uses `FOR SHARE NOWAIT` instead of `FOR UPDATE
NOWAIT`. The product takes `FOR UPDATE` on this row
(`agent-file-store.ts`, `agent-instruction-revisions.ts`), which
conflicts with `FOR SHARE`. Two `FOR SHARE` checks do not conflict.
- New test: three concurrent checks all report unlocked, a `FOR UPDATE`
held on another connection is reported locked, and the row reads
unlocked again after release.

## Verification

- Red: with the forced 50 ms overlap and the old `FOR UPDATE` probe, the
test failed 3 of 3 runs with the CI error.
- Green: with the same forced overlap and `FOR SHARE`, 4 of 4 tests
passed in 3 of 3 runs.
- Final file (no forced overlap): `pnpm exec vitest run
src/__tests__/agent-file-restore-locking.test.ts` in `server/`: 5 of 5
tests passed in 5 of 5 runs.

## Risks

- Low risk. Test-only change. The new test makes sure the check still
detects a real `FOR UPDATE` lock.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with
tool use and code 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 (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
- [ ] 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>
This commit is contained in:
DottaandClaude Opus 5.5 authored and GitHub committed 2026-11-15 09:15:45 -06:00
1 parent 62033b9994
commit b9cf64f5bf
1 file changed
+26 -1
@@ -47,9 +47,10 @@ describe("agent file restore locking", () => {
const board = () => ({ type: "board" as const, userId, source: "session" as const });
const target = () => ({ companyId, agentId });
const lockCheckQuery = () => sql`select id from agents where id = ${agentId} for share nowait`;
async function agentRowLocked(): Promise<boolean> {
try {
await observer.transaction(async tx => { await tx.execute(sql`select id from agents where id = ${agentId} for update nowait`); });
await observer.transaction(async tx => { await tx.execute(lockCheckQuery()); });
return false;
} catch (error) {
const code = (error as { code?: string; cause?: { code?: string } }).code ?? (error as { cause?: { code?: string } }).cause?.code;
@@ -106,6 +107,30 @@ describe("agent file restore locking", () => {
await fs.rm(home, { recursive: true, force: true });
});
async function whileHolding(lock: (tx: Parameters<Parameters<typeof db.transaction>[0]>[0]) => Promise<unknown>, check: () => Promise<void>) {
let release!: () => void;
const released = new Promise<void>(resolve => { release = resolve; });
let holding!: () => void;
const held = new Promise<void>(resolve => { holding = resolve; });
const holder = db.transaction(async tx => { await lock(tx); holding(); await released; });
await Promise.race([held, holder]);
try { await check(); } finally { release(); }
await holder;
}
it("a lock check does not block another lock check", async () => {
await whileHolding(tx => tx.execute(lockCheckQuery()), async () => {
expect(await agentRowLocked()).toBe(false);
});
});
it("a lock check detects the product's agent row lock", async () => {
await whileHolding(tx => tx.select().from(agents).where(eq(agents.id, agentId)).for("update"), async () => {
expect(await agentRowLocked()).toBe(true);
});
expect(await agentRowLocked()).toBe(false);
});
it("downloads a stale projection without holding the agent row lock", async () => {
await loseLocalProjection();
objects.probe = agentRowLocked;