perf(native-runtime): stop re-surfacing settled controller namespace attention (#21675)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The native runtime releases owners of failed runs. When the owner's
controller is in a process space that Paperclip cannot verify, it shows
controller namespace attention on the task instead.
> - On a hosted instance with zero active runs, this writer made about
21% of all work signals. It inserted a new active attention row for the
same task about every 26 seconds.
> - The tasks were already done or cancelled. Recovery resolved each row
as `source_terminal`. The active slot was then free, so the next
owner-release pass inserted a new row and published a graph recovery
signal again. The loop did not stop.
> - A live task had the same defect: after the fresh-continuation
fallback resolved its attention, the next pass made the same incident
again.
> - This pull request stops the writer from surfacing attention for
finished tasks. It also stops the writer from making an incident again
when that run authority was resolved after its lease expired. Those
owners are no longer release candidates and do not set the release
deadline.
> - The benefit is that an idle instance does not write or publish
anything for these runs. Attention still shows once for a live task.

## Linked Issues or Issue Description

**What happened?**
The failed-run owner release (`settleFailedRunOwner`, expired-owner
path) called `surfaceNativeControllerNamespaceAttention` for every
expired foreign owner that had no active attention. It did this even
when the source task was `done` or `cancelled`. Recovery resolved that
attention as `source_terminal`. The next pass found no active attention
and inserted a new board row. Each insert published
`notifyIssueGraphRecovery` and the other execution-control signals. The
stranded-task check then ran again for the task graph. A hosted instance
collected about 45,000 rows on 13 finished tasks in this way.

**Expected behavior**
A pass that changes nothing writes nothing and publishes nothing. A
finished task never gets controller namespace attention. A live task
gets one incident for each exact run authority. A resolved incident
stays resolved until the authority changes.

**Steps to reproduce**
1. Make a native run that failed, with an `observed` coordinator whose
foreign lease has expired and whose controller host is not recorded.
2. Mark the task done or cancelled, or let the fresh-continuation
fallback resolve the attention of a live task.
3. Run the execution-finalization worker with the issue recovery workers
for 30 minutes.
4. On master, each task gets about 40 to 60 new attention rows. The
writer publishes thousands of signals.

## What Changed

- `surfaceNativeControllerNamespaceAttention` (shared helper): before it
creates attention, it checks for a resolved incident for this run that
was resolved at or after the owner's current lease expiry. If one
exists, it writes nothing.
- New `settledNativeControllerNamespaceAttention` SQL predicate. It is
shared by the helper, the release candidate page, and the release
deadline query, so all three use the same rule.
- `settleFailedRunOwner` (expired-owner path) does not surface attention
when the task is `done` or `cancelled`. The restart-recovery claim path
has the same guard.
- `releaseAbandonedNativeOwners` and `nextAbandonedNativeOwnerReleaseAt`
skip foreign owners whose task is finished or whose incident is settled.
A pass that has nothing to do selects nothing, and no immediate deadline
is set again. Local owners keep the normal proof-based release.
- The owner release proofs did not change. A foreign owner is still
never released without proof.
- Tests:
`server/src/__tests__/native-failed-run-owner-release-idle.test.ts`
(real PostgreSQL, real coordinator and workers, fake clock, statement
and signal counters) and three new cases in
`native-failed-run-owner-release.test.ts`.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/native-failed-run-owner-release-idle.test.ts
src/services/native-runtime/native-failed-run-owner-release.test.ts
src/services/native-runtime/native-restart-recovery.test.ts
src/services/native-runtime/native-restart-recovery-isolation.test.ts`:
80 tests pass.
- Red on master, green with this change. The test seeds one done task
and one cancelled task (each with an earlier resolved `source_terminal`
incident) and one live task, then runs 30 idle minutes:
- master: 126 attention inserts, 249 attention writes, 7,024 signals,
8,025 statements.
- this branch: 1 attention insert (the live task, surfaced once), 2
attention writes (that insert and the fallback's resolution), 36
signals, 350 statements. All of this is startup work. Nothing repeats.
- After the fresh continuation settles a live task's attention, six
unrelated finalization wakes over 30 minutes:
- master: 3 attention inserts, 90 signals, 551 statements, and the
release deadline stays due.
- this branch: 0 attention writes, 0 run writes, 6 signals (the test's
own wakes), 153 statements, and the release deadline is null.
- New unit cases: a done or cancelled task never surfaces, and surfaces
once when it is reopened. A settled incident stays settled over repeated
passes. A renewed authority (a new lease expiry) surfaces one new
incident.
- The three new unit cases fail on master and pass on this branch.
- `tsc --noEmit` for `server` passes.

## Risks

- Low risk. The change only removes writes. It does not weaken a release
proof, an authorization, a budget, a pause, or an approval check.
- An incident that a person resolved or dismissed after the lease
expired is not made again for the same authority. A renewed authority (a
later lease expiry) still makes a new incident.
- A superseded incident (status `cancelled`) still shows again when the
superseding action clears. This keeps the current behavior.
- The historical rows that the loop made are not removed. This change
only stops new rows.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits, test 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
- [x] I will address all Greptile and reviewer comments before
requesting merge

Refs #21621, #21616, #21617

🤖 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-14 17:45:02 -06:00
1 parent fc9e4cbbb3
commit 64c7122dae
4 files changed
+210 -4

No files matched your search

@@ -0,0 +1,134 @@
import { randomUUID } from "node:crypto";
import { setImmediate as immediate } from "node:timers/promises";
import { and, eq } from "drizzle-orm";
import { expect, it, vi } from "vitest";
import { agents, heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations, subscribeDatabaseWork } from "@paperclipai/db";
import { DELIVERY_QUEUES, notifyDeliveryWork } from "../services/delivery-work-notifications.js";
import { executionControlSchedule, registerExecutionControlWork } from "../services/execution-control-work.js";
import { reconcileAbandonedExecutionControl } from "../services/execution-control-reconciliation.js";
import { settleUnrecoverableExecutions } from "../services/execution-recovery-resolution.js";
import { nextAbandonedNativeOwnerReleaseAt, resetAbandonedNativeOwnerReleaseForTests } from "../services/native-runtime/native-failed-run-owner-release.js";
import { describeIssueRecovery, issueRecoveryFixture } from "./issue-recovery.test-fixture.js";
describeIssueRecovery("failed native runs held by an unverifiable foreign controller", () => {
const state = issueRecoveryFixture();
async function seedTask(f: { companyId: string; agentId: string }, status: "in_progress" | "done" | "cancelled") {
const db = state.db;
const issueId = randomUUID(), runId = randomUUID();
const leaseExpiresAt = new Date(Date.now() - 40 * 60_000);
await db.insert(issues).values({ id: issueId, companyId: f.companyId, title: `Native task ${status}`, status,
assigneeAgentId: f.agentId, workMode: "standard", executionRunId: null, checkoutRunId: null,
...(status === "done" ? { completedAt: new Date() } : status === "cancelled" ? { cancelledAt: new Date() } : {}) });
await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, nativeIssueId: issueId,
status: "failed", runtimeMode: "native", runtimeModeResolvedAt: new Date(), contextSnapshot: { issueId },
startedAt: new Date(Date.now() - 60 * 60_000), finishedAt: new Date(Date.now() - 50 * 60_000), errorCode: "setup_failed" });
await db.insert(nativeRunFinalizations).values({ runId, companyId: f.companyId, issueId, phase: "observed", attempt: 1,
leaseOwner: `foreign:${randomUUID()}`, leaseExpiresAt, controllerBootId: randomUUID(), controllerHostHash: null,
recoveryState: "awaiting_evidence" });
if (status !== "in_progress") {
await db.insert(issueRecoveryActions).values({ companyId: f.companyId, sourceIssueId: issueId, kind: "active_run_watchdog",
status: "resolved", ownerType: "board", returnOwnerAgentId: f.agentId, cause: "native_restart_recovery_blocked",
fingerprint: `native-controller-space:${runId}:${"0".repeat(64)}`, attemptCount: 0, outcome: "restored",
resolutionNote: "source_terminal", resolvedAt: new Date(leaseExpiresAt.getTime() - 60_000),
evidence: { runId, reason: "controller_process_space_unverified", executorStopEvidence: "unverified" },
nextAction: "The server running this task restarted. Paperclip continues the task in a fresh run from its saved work shortly." });
}
return { issueId, runId };
}
async function advance(ms: number) {
const until = Date.now() + ms;
while (Date.now() < until) {
await vi.advanceTimersByTimeAsync(Math.min(500, until - Date.now()));
await Promise.allSettled([...state.passes]); await immediate();
}
}
async function measure(work: () => Promise<unknown>) {
const statements: string[] = [], signals: string[] = [], prior = state.db.$client.options.debug;
const unsubscribes = Object.values(DELIVERY_QUEUES).map(queue => subscribeDatabaseWork(state.db, queue, event => {
if (event === "settled") signals.push(queue);
}));
state.db.$client.options.debug = (_connection, query) => { statements.push(query); };
try { await work(); } finally {
state.db.$client.options.debug = prior;
for (const unsubscribe of unsubscribes) unsubscribe();
}
return {
statements: statements.length,
attentionInserts: statements.filter(query => query.startsWith("insert into \"issue_recovery_actions\"")).length,
attentionWrites: statements.filter(query => /^(insert into|update) "issue_recovery_actions"/.test(query)).length,
runWrites: statements.filter(query => query.startsWith("update \"heartbeat_runs\"")).length,
signals: signals.length,
};
}
async function start() {
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
resetAbandonedNativeOwnerReleaseForTests();
await state.start({});
const schedule = executionControlSchedule(state.db);
await registerExecutionControlWork(state.coordinator, [
{ queue: DELIVERY_QUEUES.executionFinalization, run: () => reconcileAbandonedExecutionControl(state.db, new Date()),
next: schedule.finalization },
{ queue: DELIVERY_QUEUES.executionDisposition, run: () => settleUnrecoverableExecutions(state.db), next: schedule.disposition },
]).ready;
}
const rowsFor = (issueId: string) => state.db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId));
it("stays quiet after a live task's surfaced attention is settled", async () => {
const f = await state.fixture();
await state.db.update(agents).set({ status: "error" }).where(eq(agents.id, f.agentId));
const tasks = [await seedTask(f, "in_progress"), await seedTask(f, "in_progress"), await seedTask(f, "in_progress")];
await start();
for (let minute = 0; minute < 30; minute++) {
await advance(60_000);
const active = await state.db.select().from(issueRecoveryActions).where(and(
eq(issueRecoveryActions.companyId, f.companyId), eq(issueRecoveryActions.status, "active")));
const resolved = await state.db.select().from(issueRecoveryActions).where(and(
eq(issueRecoveryActions.companyId, f.companyId), eq(issueRecoveryActions.status, "resolved")));
if (active.length === 0 && resolved.length === tasks.length) break;
}
for (const task of tasks) {
expect(await rowsFor(task.issueId)).toEqual([expect.objectContaining({ status: "resolved",
resolutionNote: "continued_fresh_from_last_saved", fingerprint: expect.stringContaining(`native-controller-space:${task.runId}:`) })]);
}
expect(await nextAbandonedNativeOwnerReleaseAt(state.db)).toBeNull();
const idle = await measure(async () => {
for (let wake = 0; wake < 6; wake++) {
await state.db.transaction(tx => notifyDeliveryWork(tx, DELIVERY_QUEUES.executionFinalization));
await advance(5 * 60_000);
}
});
expect({ attentionWrites: idle.attentionWrites, runWrites: idle.runWrites, signals: idle.signals })
.toEqual({ attentionWrites: 0, runWrites: 0, signals: 6 });
expect(idle.statements).toBeLessThan(6 * 60);
expect(await state.db.select().from(issueRecoveryActions).where(and(eq(issueRecoveryActions.companyId, f.companyId),
eq(issueRecoveryActions.status, "active")))).toEqual([]);
}, 300_000);
it("never surfaces attention for finished tasks and surfaces a live task exactly once", async () => {
const f = await state.fixture();
await state.db.update(agents).set({ status: "error" }).where(eq(agents.id, f.agentId));
const done = await seedTask(f, "done");
const cancelled = await seedTask(f, "cancelled");
const live = await seedTask(f, "in_progress");
const idle = await measure(async () => {
await start();
await advance(30 * 60_000);
});
for (const task of [done, cancelled]) {
expect(await rowsFor(task.issueId)).toEqual([expect.objectContaining({ status: "resolved", resolutionNote: "source_terminal" })]);
}
const liveRows = await rowsFor(live.issueId);
expect(liveRows).toEqual([expect.objectContaining({ cause: "native_restart_recovery_blocked",
fingerprint: expect.stringContaining(`native-controller-space:${live.runId}:`),
evidence: expect.objectContaining({ runId: live.runId, reason: "controller_process_space_unverified" }) })]);
expect(idle.attentionInserts).toBe(1);
expect(await nextAbandonedNativeOwnerReleaseAt(state.db)).toBeNull();
expect(await state.db.select({ phase: nativeRunFinalizations.phase }).from(nativeRunFinalizations)
.where(eq(nativeRunFinalizations.runId, done.runId))).toEqual([{ phase: "observed" }]);
}, 300_000);
});
@@ -361,6 +361,50 @@ describe("failed native run owner release", () => {
expect(await nextAbandonedNativeOwnerReleaseAt(db)).toBeNull();
});
it.each(["done", "cancelled"] as const)("never surfaces namespace attention for a %s task until it is reopened", async status => {
const { runId, issueId } = await nativeRun({ status: "failed", coordinator: {
leaseOwner: `foreign:${randomUUID()}`, controllerHostHash: null, leaseExpiresAt: new Date(0) } });
await db.update(issues).set({ status }).where(eq(issues.id, issueId));
const before = await coordinator(runId);
expect(await nextAbandonedNativeOwnerReleaseAt(db)).toBeNull();
for (let pass = 0; pass < 3; pass++) {
expect(await committedWakes(DELIVERY_QUEUES.strandedAssignments, () => releaseAbandonedNativeOwners(db))).toBe(0);
}
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId))).toEqual([]);
expect(await coordinator(runId)).toEqual(before);
expect(await executionControlSchedule(db).finalization()).toBeNull();
await db.update(issues).set({ status: "in_progress" }).where(eq(issues.id, issueId));
expect(await nextAbandonedNativeOwnerReleaseAt(db)).not.toBeNull();
expect(await releaseAbandonedNativeOwners(db)).toBe(0);
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId))).toEqual([
expect.objectContaining({ status: "active", evidence: expect.objectContaining({ runId }) })]);
});
it("keeps a settled namespace incident settled and surfaces a renewed authority once", async () => {
const { runId, issueId } = await nativeRun({ status: "failed", coordinator: {
leaseOwner: `foreign:${randomUUID()}`, controllerHostHash: null, leaseExpiresAt: new Date(Date.now() - 60_000) } });
expect(await releaseAbandonedNativeOwners(db)).toBe(0);
const [surfaced] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId));
expect(surfaced).toMatchObject({ status: "active", evidence: { runId } });
await db.update(issueRecoveryActions).set({ status: "resolved", outcome: "restored", resolvedAt: new Date(),
resolutionNote: "source_terminal" }).where(eq(issueRecoveryActions.id, surfaced.id));
expect(await nextAbandonedNativeOwnerReleaseAt(db)).toBeNull();
for (let pass = 0; pass < 3; pass++) {
expect(await committedWakes(DELIVERY_QUEUES.strandedAssignments, () => releaseAbandonedNativeOwners(db))).toBe(0);
}
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId))).toHaveLength(1);
const renewedExpiry = new Date(Date.now() + 60_000);
await db.update(nativeRunFinalizations).set({ leaseExpiresAt: renewedExpiry }).where(eq(nativeRunFinalizations.runId, runId));
expect((await nextAbandonedNativeOwnerReleaseAt(db))?.getTime()).toBe(renewedExpiry.getTime());
const afterRenewal = new Date(renewedExpiry.getTime() + 1);
expect(await releaseAbandonedNativeOwners(db, afterRenewal)).toBe(0);
expect(await releaseAbandonedNativeOwners(db, afterRenewal)).toBe(0);
const rows = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, issueId));
expect(rows).toHaveLength(2);
expect(rows.find(row => row.id !== surfaced.id)).toMatchObject({ status: "active", evidence: { runId } });
expect(rows.find(row => row.id !== surfaced.id)?.fingerprint).not.toBe(surfaced.fingerprint);
});
it("keeps an unexpired owner blocking the stop check and schedules its expiry", async () => {
const leaseExpiresAt = new Date(Date.now() + 10 * 60_000);
const { runId } = await nativeRun({ status: "failed", errorCode: "setup_failed", error: LEASE_FAILURE,
@@ -15,7 +15,7 @@ import { notifyExecutionRunWork } from "../execution-control-notifications.js";
import { notifyNativeFinalizationWork } from "../native-finalization-notifications.js";
import { LEGACY_WORKSPACE_RECOVERY_KEY } from "../workspace-restore-recovery-state.js";
import { activeNativeExecutionRunIds } from "./native-active-runs.js";
import { currentNativeControllerIdentity, isCurrentNativeControllerProcess, surfaceNativeControllerNamespaceAttention, recordLegacyHostFallbackProof } from "./native-restart-recovery.js";
import { currentNativeControllerIdentity, isCurrentNativeControllerProcess, settledNativeControllerNamespaceAttention, surfaceNativeControllerNamespaceAttention, recordLegacyHostFallbackProof } from "./native-restart-recovery.js";
import { nativeControllerOwnerStamp, runProcessOwnerStamp } from "./native-owner-stamps.js";
/**
@@ -60,6 +60,13 @@ const unsettledRunEnvironmentLease = sql`exists (
/** A pending Stop belongs to native restart recovery's stop cleanup. */
const pendingNativeStop = sql`coalesce(${heartbeatRuns.resultJson}, '{}'::jsonb) #>> '{nativeCancellation,dispatchState}' = 'pending'`;
const quietNamespaceOwner = () => sql`(${settledNativeControllerNamespaceAttention({
companyId: nativeRunFinalizations.companyId, issueId: nativeRunFinalizations.issueId,
runId: nativeRunFinalizations.runId, leaseExpiresAt: nativeRunFinalizations.leaseExpiresAt,
})} or exists (select 1 from ${issues} source_task
where source_task.company_id = ${nativeRunFinalizations.companyId} and source_task.id = ${nativeRunFinalizations.issueId}
and source_task.status in ('done', 'cancelled')))`;
/**
* Rows the background pass may release once their lease expires. A pinned
* workspace restore belongs to observed-workspace preservation, which must stop
@@ -108,6 +115,7 @@ export async function nextAbandonedNativeOwnerReleaseAt(db: Db): Promise<Date |
where attention.company_id = ${nativeRunFinalizations.companyId}
and attention.source_issue_id = ${nativeRunFinalizations.issueId}
and attention.status in ('active', 'escalated'))`,
sql`not ${quietNamespaceOwner()}`,
)).orderBy(asc(nativeRunFinalizations.leaseExpiresAt)).limit(1);
const [row] = await db.select({ at: nativeRunFinalizations.leaseExpiresAt }).from(nativeRunFinalizations)
.innerJoin(heartbeatRuns, and(eq(heartbeatRuns.id, nativeRunFinalizations.runId),
@@ -245,7 +253,8 @@ async function settleFailedRunOwner(db: Db, release: Release, now: Date): Promis
if (!isCurrentNativeControllerProcess(coordinator)) {
const current = currentProcessSpaceHash();
if (current === "unknown" || coordinator.controllerHostHash !== current) {
if (!coordinator.leaseOwner?.startsWith("native-finalizer:") && !coordinator.leaseOwner?.startsWith("native-cleanup:")) {
if (!coordinator.leaseOwner?.startsWith("native-finalizer:") && !coordinator.leaseOwner?.startsWith("native-cleanup:")
&& task && !["done", "cancelled"].includes(task.status)) {
const [expiry] = await tx.select({ epoch: sql<string | null>`extract(epoch from ${nativeRunFinalizations.leaseExpiresAt})::numeric::text` })
.from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, run.id));
const { remoteProcess } = await readNativeRemoteExecutionOwnership(tx, { run, issueId: coordinator.issueId });
@@ -379,6 +388,7 @@ export async function releaseAbandonedNativeOwners(db: Db, now = new Date(), pag
// One bounded page per pass. The cursor rotates across passes, so rows that a
// live process still holds cannot starve stopped owners behind them; any
// remaining expired row keeps the finalization deadline due for the next pass.
const local = await localOwnerSpaceCondition();
const page = (after: string | null) => db.select({
runId: nativeRunFinalizations.runId,
leaseOwner: nativeRunFinalizations.leaseOwner,
@@ -388,6 +398,7 @@ export async function releaseAbandonedNativeOwners(db: Db, now = new Date(), pag
.innerJoin(heartbeatRuns, and(eq(heartbeatRuns.id, nativeRunFinalizations.runId),
eq(heartbeatRuns.companyId, nativeRunFinalizations.companyId)))
.where(and(abandonedOwnerCondition(), lte(nativeRunFinalizations.leaseExpiresAt, now),
sql`(coalesce(${local}, false) or not ${quietNamespaceOwner()})`,
after ? gt(nativeRunFinalizations.runId, after) : undefined))
.orderBy(asc(nativeRunFinalizations.runId))
.limit(pageSize + 1);
@@ -26,7 +26,7 @@ import {
recordNativeLocalProcessStopInTransaction,
} from "../native-local-process-stop.js";
import { randomUUID } from "node:crypto";
import { and, desc, eq, gt, inArray, isNotNull, isNull, lt, lte, ne, or, sql } from "drizzle-orm";
import { and, desc, eq, gt, inArray, isNotNull, isNull, lt, lte, ne, or, sql, type SQLWrapper } from "drizzle-orm";
import { alias } from "drizzle-orm/pg-core";
import type { Db } from "@paperclipai/db";
import {
@@ -1062,6 +1062,17 @@ async function endNamespaceUnverifiedRun(tx: Db, row: {
if (updatedRun) await preserveNativeFailedWorkspace(tx, updatedRun);
}
export function settledNativeControllerNamespaceAttention(owner: {
companyId: SQLWrapper | string; issueId: SQLWrapper | string; runId: SQLWrapper | string; leaseExpiresAt: SQLWrapper | string;
}) {
return sql`exists (select 1 from ${issueRecoveryActions} settled
where settled.company_id = ${owner.companyId}::uuid and settled.source_issue_id = ${owner.issueId}::uuid
and settled.evidence->>'runId' = ${owner.runId}::text
and settled.status = 'resolved' and settled.cause = 'native_restart_recovery_blocked'
and starts_with(settled.fingerprint, 'native-controller-space:' || ${owner.runId}::text || ':')
and settled.resolved_at >= ${owner.leaseExpiresAt}::timestamptz)`;
}
/** Shared by restart inspection and failed-owner cleanup under their existing
* issue/coordinator/run locks. Publishing attention never grants a new turn. */
export async function surfaceNativeControllerNamespaceAttention(db: Db, input: {
@@ -1085,6 +1096,12 @@ export async function surfaceNativeControllerNamespaceAttention(db: Db, input: {
const sameSource = existing?.cause === "native_restart_recovery_blocked"
&& existing.evidence.runId === input.run.id
&& ["controller_process_space_unverified", "remote_controller_process_space_unverified"].includes(String(existing.evidence.reason));
if (!existing && input.coordinator.leaseExpiresAt) {
const [settled] = await db.execute<{ settled: boolean }>(sql`select ${settledNativeControllerNamespaceAttention({
companyId: input.run.companyId, issueId: input.coordinator.issueId, runId: input.run.id,
leaseExpiresAt: input.coordinator.leaseExpiresAt.toISOString() })} as settled`);
if (settled?.settled) return;
}
if (!existing || (sameSource && existing.fingerprint !== fingerprint)) {
await attention.upsertSourceScoped({
companyId: input.run.companyId, sourceIssueId: input.coordinator.issueId,
@@ -1491,7 +1508,7 @@ export async function claimNativeRestartRecoveries(input: {
}
}
if (!takeover.allowed) {
if (namespaceAttentionNeeded) {
if (namespaceAttentionNeeded && !["done", "cancelled"].includes(row.issueStatus)) {
await surfaceNativeControllerNamespaceAttention(tx as unknown as Db, {
run: row.run, coordinator: row.coordinator, expiryEpoch: row.controllerLeaseExpiryEpoch, remoteProcess,
});