fix(native-runtime): retry a native failure write after a brief lock timeout (#21494)

An interrupted native turn records its same-run resume in one transaction
under a 1 s lock_timeout. When another transaction briefly held a row that
the write needs (for example the agent row that the new recovery action
references), the insert failed with 55P03 and the whole write rolled back.
The run kept its dead lease, and the task needed reconciliation instead of
the recorded automatic resume.

Retry the whole failure write on transient lock contention with the
existing bounded helper. A rolled-back attempt has no effect, so a fresh
attempt is safe. The regression test holds the agent row until one attempt
is cancelled by its lock timeout, so only a retry can complete the write.

Co-Authored-By: Paperclip <Paperclip-Paperclip@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-13 14:02:04 -08:00
1 parent 0ead74b4f3
commit defed64af9
2 files changed
+118 -2

No files matched your search

@@ -1,6 +1,7 @@
import { currentControllerProcess } from "../controller-process-identity.js";
import { remoteRecoveryLeaseMismatch } from "./remote-recovery-lease-mismatch.js";
import { isGovernancePauseStop } from "../governance-pause-stop.js";
import { retryTransientLockContention } from "../transient-lock-contention.js";
import { nativeLeaseHolder, notifyNativeTokenLeaseClaimed } from "./native-token-lease-settlement.js";
import { captureRemoteNativeProviderArtifacts, shareNativeProviderArtifactCapture } from "./native-provider-artifact-capture.js";
import { NativeSessionDiagnostics, nativeSessionDiagnostics, type NativeDiagnosticLease } from "./native-session-diagnostics.js";
@@ -13101,7 +13102,11 @@ async function executePaperclipNativeSessionAttempt(
// transitions the run into "failed". Read after the transaction
// commits, so a rolled-back write never reports a false failure.
let terminalRunToReport: typeof heartbeatRuns.$inferSelect | null = null;
await input.db.transaction(async (tx) => {
// A lock timeout rolls this write back whole, so a fresh attempt is safe.
// Without it a briefly held row strands the run under its dead lease and
// the task needs reconciliation instead of its recorded recovery.
await retryTransientLockContention(() => input.db.transaction(async (tx) => {
terminalRunToReport = null;
await tx.execute(
sql`select set_config('statement_timeout', '15000', true), set_config('lock_timeout', '1000', true)`,
);
@@ -13496,7 +13501,7 @@ async function executePaperclipNativeSessionAttempt(
supersedeOnIdentityChange:
recoveryProjection.supersedeOnIdentityChange,
});
}).catch(async (failure: unknown) => {
})).catch(async (failure: unknown) => {
// Emit after rollback so the diagnostic sink cannot deadlock on the
// coordinator transaction. Reporting failure cannot change fencing.
if (failure instanceof NativePostResultFailureError) {