Files
PaperClipAI/server/src/services/run-identity.ts
T
DottaandPaperclip 5f0da562db fix(server): wake chat lifecycle milestones from committed changes (#17606)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Connected chats show safe notices when a run starts, waits,
finishes, or fails.
> - A recurring scan projected these notices even when no eligible work
existed.
> - Eligibility depends on committed run state, conversation lineage,
and current authority.
> - Existing database rows can reconstruct that work after restart.
> - This PR wakes a scoped worker from those writes and removes
lifecycle polling.
> - Native progress events keep their separate existing owner until the
next migration.

## Linked Issues or Issue Description

**What happened?**

The chat maintenance timer repeatedly scanned run lifecycle state. Empty
and settled instances still queried the database. A run-status event
alone cannot replace the scan: prompt receipts, continuation receipts,
identity changes, and restored authority can make a run eligible later.

**What did you expect?**

Committed changes wake projection. Startup reconstructs eligible work. A
completed pass stops until another relevant change arrives. Failed work
retains recovery without scanning settled history forever.

**Steps to reproduce**

Configure an automatic chat endpoint. Finish its work, or leave it
unused. Observe recurring lifecycle projection queries from the chat
maintenance callback.

Stacked on #17592. Related foundation: #15625. This PR owns lifecycle
milestones only.

## What Changed

- Preserve upstream coalesced Cloud membership writes together with
locked membership restoration and its committed chat notification.

- Signal milestone eligibility from the real Slack account-link
transaction.

- Register the lifecycle worker with the existing delivery coordinator.
Remove lifecycle projection from the periodic chat callback. Keep native
progress in a separately named lane and preserve the explicit combined
API.
- Reconstruct company/issue scopes from the existing selector. Drain
bounded batches with company fairness. Retry failures, retain writes
through drain/resume, and disarm settled work.
- Signal from supported run, presentation, interaction, receipt,
lineage, conversation, recovery, and authority writers. Preserve their
transaction ownership, including nested savepoints and uncertain
settlement. No new table is added.
- Preserve provider authorization, authored-final suppression,
responsible-user continuity, deduplication, the 24-hour selection
window, and raw-output restrictions. Keep provider interpretation in its
owner.
- Preserve PostgreSQL timestamp precision in lifecycle pagination.
Rejected submillisecond candidates must not repeat the same page and
starve later notices.
- Document producer coverage and alternate-writer dispositions in
`doc/plans/2026-10-25-chat-run-milestone-sleep-audit.md`.

## Verification

Published head: `1d056f68c5b1005933b9e12de8745bf1c4cbeeca`, targeting
master after #17592 merged at
`7d75ae0569e41786640cf09647ae8849411d2f22`. A normal merge integrates
the actual landed parent. The entire 77-path milestone patch is
binary-identical to the locally verified preparation across that
ancestry reconciliation; both unrelated master paths are preserved. The
four earlier producer/test integration conflicts retain scoped milestone
and voice notices, current onboarding completion admission, and expanded
identity query/read-scope coverage.

- **330 distinct selected cases pass on the verified unchanged patch:**
305 across 14 complete suites (all five milestone worker suites, run
identity, board claim, Cloud provisioning, auth session routes, Cloud
actor authentication, stale-lock recovery, GitHub credentials, project
Git authorization, and app admission wiring), plus 25 selected real chat
producer cases. The remaining 1,590 chat integration cases were skipped,
not passed.
- The real producer cases cover saved milestone discovery, GitHub and
Slack identity changes, account-link rollback/reconfirmation, ordinary
ingress reactivation, post-close action/delivery receipt settlement, all
ten authority-restoration paths including Cloud synchronization, and
changed native review metadata.
- Worker coverage verifies outer commit, rollback, uncertain-write
recovery, restart, bounded batching/fairness, timestamp precision,
retries, drain/resume, and settled SQL quiet. Authorization and existing
publication deduplication remain with their owning services.
- Server production typechecking, build, and module-boundary checks
pass. The tests above were executed separately. Production typechecking
excludes `src/__tests__` and does not replace those runs. Each database
owner uses its own newly created and app-migrated PostgreSQL fixture.
- [Current-parent acceptance and preserved failure
ledger](https://github.com/paperclipai/paperclip/blob/1d056f68c5b1005933b9e12de8745bf1c4cbeeca/doc/plans/2026-10-25-chat-run-milestone-sleep-audit.md#current-parent-integration-and-acceptance-october-28)
records commands, counts, producer scope, conflict dispositions, and
limits. Earlier combined-stack counts and failures keep their original
revision scope; local acceptance does not erase old CI failures.
- No new migration, queue table, dependency, workflow, or shared
scheduler contract is introduced. Parent migration 0397, bounded consent
recovery, voice cleanup/retention repairs, and runner checkpoint
restoration remain present.
- **Exact-head Greptile is 5/5 and all 55 current checks are clean.** No
Ponytail review is present, which is an explicitly authorized
nonblocker; no failing review is waived. Older reviews and parent checks
are not current passes. Actual failing Ponytail findings must be fixed;
an absent/stopped/paused turn without a verdict is an authorized
nonblocker. Active unfinished Ponytail uses its original request/start
timer, with a 25-minute monitor cutoff respecting the user's 30-minute
maximum.
- No complete local repository matrix, live provider tenant, external
alarm, physical host/database suspension, or fleet savings claim is
made. Native progress remains a separate following slice.

## Risks

- A future eligibility writer must notify the existing projection
boundary. Independent SQL writers or another active server process are
outside this process-local signal contract.
- Authority and lineage remain sensitive. The worker uses the existing
selectors and publication policy; it does not grant execution or
disclosure authority.
- Native progress and other publication maintenance still have recurring
work. This PR removes one reason, not the entire chat maintenance timer.
- Worker quiet does not prove database suspension, host sleep, or
external alarm coverage. Cloud sleep blockers remain unchanged.
- Merge eligibility requires passing current-head CI and Greptile 5/5
after this actual-master reconciliation. Subsequent stack slices retain
their own gates.

## Model Used

OpenAI Codex, GPT-6 family. The exact serving model ID and
context-window size are not exposed. Used repository inspection,
reasoning, shell execution, PostgreSQL tests, browser E2Es and GitHub
tools.

## 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 available in this session)
- [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 the documented focused 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>
2026-10-28 11:07:54 -05:00

661 lines
26 KiB
TypeScript

import { readTaskRetryIntent, readTaskRetryBoardMessageOrigin } from "./task-retry.js";
import { and, asc, desc, eq, getTableColumns, inArray, sql } from "drizzle-orm";
import {
agentWakeupRequests,
heartbeatRuns,
heartbeatRunEvents,
issueComments,
issueThreadInteractions,
issues,
runIdentityContexts,
type Db,
} from "@paperclipai/db";
import { conflict, forbidden } from "../errors.js";
import { notifyChatRunMilestones } from "./chat-run-milestone-notifications.js";
import { boundRunIssueId, boundRunIssueIdSql, runTaskContextColumn } from "./run-task-binding.js";
import { isUuidLike } from "@paperclipai/shared";
import { queuedCommentIdsFromRunContext, queuedCommentIdsFromWakePayload } from "./issue-queued-comment-queue.js";
/** Resolve an explicit click from persisted receipts, never caller context or message authors. */
export async function explicitOperatorRunIdentity(
executor: Pick<Db, "select">,
run: Pick<typeof heartbeatRuns.$inferSelect, "id" | "companyId" | "agentId" | "contextSnapshot" | "wakeupRequestId">,
) {
const [request] = run.wakeupRequestId ? await executor.select().from(agentWakeupRequests).where(and(
eq(agentWakeupRequests.id, run.wakeupRequestId), eq(agentWakeupRequests.companyId, run.companyId),
eq(agentWakeupRequests.agentId, run.agentId), eq(agentWakeupRequests.runId, run.id),
)) : [];
if (request?.payload?.manualUserWake === true) {
if (request.requestedByActorType !== "user" || !request.requestedByActorId) {
throw forbidden("Manual wake requires an authenticated user");
}
return { actorId: request.requestedByActorId, cause: "manual_user_wake" };
}
const prefix = "queued-comment-interrupt:";
if (!request?.idempotencyKey?.startsWith(prefix)) return null;
const queueId = request.idempotencyKey.slice(prefix.length);
// The key only locates a candidate. The consumed queue, actor, run, company,
// agent, task, and delivered messages must all independently agree.
if (!isUuidLike(queueId)) {
throw forbidden("Queued-message interrupt authority is unavailable");
}
const [receipt] = await executor.select().from(agentWakeupRequests).where(and(
eq(agentWakeupRequests.id, queueId), eq(agentWakeupRequests.companyId, run.companyId),
eq(agentWakeupRequests.agentId, run.agentId), eq(agentWakeupRequests.runId, run.id),
eq(agentWakeupRequests.status, "coalesced"),
));
const marker = receipt?.payload?.queuedCommentInterrupt;
const actorId = marker && typeof marker === "object" && "actorId" in marker ? marker.actorId : null;
const ids = queuedCommentIdsFromWakePayload(receipt?.payload);
const deliveredIds = queuedCommentIdsFromRunContext(run.contextSnapshot);
if (typeof actorId !== "string" || !actorId || !ids.length ||
receipt?.payload?.issueId !== run.contextSnapshot?.issueId ||
!ids.every(id => deliveredIds.includes(id)) ||
request.requestedByActorType !== "user" || request.requestedByActorId !== actorId) {
throw forbidden("Queued-message interrupt authority is unavailable");
}
return { actorId, cause: "queued_comment_interrupt" };
}
export type RunIdentityContext = typeof runIdentityContexts.$inferSelect;
type Executor = Pick<Db, "select" | "insert" | "update">;
/**
* Lock the task before the run, matching task mutation ordering. Identity
* operations do not change parent keys: NO KEY UPDATE still serializes writers
* and steering, while allowing audit inserts to check their foreign keys.
* FOR UPDATE can deadlock with an append that holds KEY SHARE on the run and
* then checks the task while identity capture holds the task and waits on the run.
*/
async function lockIdentityTask(
executor: Pick<Db, "select">,
companyId: string,
runId: string,
) {
const [run] = await executor
.select({
contextSnapshot: runTaskContextColumn,
nativeIssueId: heartbeatRuns.nativeIssueId,
issueId: heartbeatRuns.issueId,
})
.from(heartbeatRuns)
.where(
and(eq(heartbeatRuns.id, runId), eq(heartbeatRuns.companyId, companyId)),
);
const issueId = run?.nativeIssueId ?? run?.issueId ?? run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId;
const legacyIssueId = run?.contextSnapshot?.issueId ?? run?.contextSnapshot?.taskId;
if ([issueId, legacyIssueId].some(id => id !== undefined && id !== null &&
(typeof id !== "string" || !isUuidLike(id)))) {
throw forbidden("Run task identity is invalid");
}
if (typeof issueId === "string")
await executor
.select({ id: issues.id })
.from(issues)
.where(and(eq(issues.id, issueId), eq(issues.companyId, companyId)))
.for("no key update");
}
async function append(
executor: Executor,
input: {
companyId: string;
runId: string;
responsibleUserId: string | null;
messageId?: string | null;
parentContextId?: string | null;
cause: string;
correlationId: string;
status?: "accepted" | "pending";
},
) {
const [existing] = await executor
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.runId, input.runId),
eq(runIdentityContexts.correlationId, input.correlationId),
),
);
if (existing) return existing;
const [last] = await executor
.select()
.from(runIdentityContexts)
.where(eq(runIdentityContexts.runId, input.runId))
.orderBy(desc(runIdentityContexts.revision))
.limit(1);
const [created] = await executor
.insert(runIdentityContexts)
.values({
...input,
revision: (last?.revision ?? 0) + 1,
acceptedAt: input.status === "pending" ? null : new Date(),
})
.returning();
if (!created) throw new Error("Failed to persist run identity");
if (created.status === "accepted") await activate(executor, created);
return created;
}
async function activate(executor: Executor, context: RunIdentityContext) {
const [binding] = await executor.select({ issueId: boundRunIssueIdSql() })
.from(heartbeatRuns).where(and(
eq(heartbeatRuns.id, context.runId), eq(heartbeatRuns.companyId, context.companyId),
));
if (binding?.issueId) {
await notifyChatRunMilestones(executor, { companyId: context.companyId, issueId: binding.issueId });
}
const [run] = await executor
.update(heartbeatRuns)
.set({
responsibleUserId: context.responsibleUserId,
activeIdentityContextId: context.id,
updatedAt: new Date(),
})
.where(
and(
eq(heartbeatRuns.id, context.runId),
eq(heartbeatRuns.companyId, context.companyId),
),
)
.returning({
nativeIssueId: heartbeatRuns.nativeIssueId,
issueId: heartbeatRuns.issueId,
contextSnapshot: runTaskContextColumn,
});
const issueId = run ? boundRunIssueId(run) : null;
if (typeof issueId === "string")
await executor
.update(issues)
.set({ continuationIdentityContextId: context.id })
.where(
and(eq(issues.id, issueId), eq(issues.companyId, context.companyId)),
);
}
/** Called only for newly dispatched runs. Historical rows are never backfilled. */
export async function initializeRunIdentity(
db: Db,
input: {
companyId: string;
runId: string;
responsibleUserId: string | null;
messageIds?: string[];
issueId?: string | null;
interactionId?: string | null;
parentRunId?: string | null;
parentContextId?: string | null;
cause: string;
},
) {
return db.transaction(async (tx) => {
await lockIdentityTask(tx, input.companyId, input.runId);
const [run] = await tx
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.runId),
eq(heartbeatRuns.companyId, input.companyId),
),
)
.for("no key update");
if (!run) throw forbidden("Run identity does not belong to this company");
if (run.activeIdentityContextId) {
const [current] = await tx
.select()
.from(runIdentityContexts)
.where(eq(runIdentityContexts.id, run.activeIdentityContextId));
return current!;
}
const operatorIdentity = await explicitOperatorRunIdentity(tx, run);
let retryOrigin: string | null = null;
const retryId = run.contextSnapshot?.taskRetryRequestId;
if (typeof retryId === "string") {
const [receipt] = await tx.select().from(agentWakeupRequests).where(and(
eq(agentWakeupRequests.id, retryId), eq(agentWakeupRequests.companyId, input.companyId),
eq(agentWakeupRequests.agentId, run.agentId)));
const retry = readTaskRetryIntent(receipt?.payload);
if (run.contextSnapshot?.boardMessageRetry === true || retry?.boardMessageOrigin) {
if (!retry?.boardMessageOrigin) throw forbidden("This saved message can no longer be retried safely. Send a new message on the task to continue from saved work.");
const [dispatch] = run.wakeupRequestId ? await tx.select().from(agentWakeupRequests).where(and(
eq(agentWakeupRequests.id, run.wakeupRequestId), eq(agentWakeupRequests.companyId, input.companyId),
eq(agentWakeupRequests.agentId, run.agentId), eq(agentWakeupRequests.runId, run.id),
eq(agentWakeupRequests.status, "claimed"), eq(agentWakeupRequests.reason, "task_retry"),
eq(agentWakeupRequests.idempotencyKey, `task-retry-dispatch:${receipt!.id}`),
eq(agentWakeupRequests.requestedByActorType, "user"), eq(agentWakeupRequests.requestedByActorId, retry.actorId))) : [];
const [task] = input.issueId ? await tx.select({ id: issues.id }).from(issues).where(and(
eq(issues.id, input.issueId), eq(issues.companyId, input.companyId), eq(issues.assigneeAgentId, run.agentId),
eq(issues.executionRunId, run.id))) : [];
const origin = input.issueId && run.status === "running" && task && dispatch &&
[run.issueId, run.nativeIssueId, run.contextSnapshot?.issueId, run.contextSnapshot?.taskId]
.every(id => id === null || id === undefined || id === input.issueId) && receipt!.runId === run.id && receipt!.status === "coalesced" &&
receipt!.reason === "task_retry" && receipt!.requestedByActorType === "user" &&
run.contextSnapshot?.previousRunId === retry.sourceRunId && retry.generationId === receipt!.id && !retry.chatActionId && !retry.canonicalIntentId &&
receipt!.payload?.issueId === input.issueId && retry.actorId === receipt!.requestedByActorId &&
await readTaskRetryBoardMessageOrigin(tx as unknown as Db, { companyId: input.companyId, issueId: input.issueId,
agentId: run.agentId, sourceRunId: retry.sourceRunId, actorId: retry.actorId });
if (!origin || origin.commentId !== retry.boardMessageOrigin.commentId ||
origin.identityContextId !== retry.boardMessageOrigin.identityContextId) {
throw forbidden("This saved message can no longer be retried safely. Send a new message on the task to continue from saved work.");
}
retryOrigin = origin.identityContextId;
}
}
const [parent] = input.parentRunId
? await tx
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.parentRunId),
eq(heartbeatRuns.companyId, input.companyId),
),
)
: [];
const [interaction] =
input.interactionId && input.issueId
? await tx
.select()
.from(issueThreadInteractions)
.where(
and(
eq(issueThreadInteractions.id, input.interactionId),
eq(issueThreadInteractions.companyId, input.companyId),
eq(issueThreadInteractions.issueId, input.issueId),
),
)
: [];
const parentId = operatorIdentity ? null : (
retryOrigin ?? interaction?.sourceIdentityContextId ?? input.parentContextId ?? parent?.activeIdentityContextId
);
const [origin] = parentId
? await tx
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.id, parentId),
eq(runIdentityContexts.companyId, input.companyId),
eq(runIdentityContexts.status, "accepted"),
),
)
: [];
if (parentId && !origin)
throw forbidden("Originating execution identity is unavailable");
let current = await append(tx, {
companyId: input.companyId,
runId: input.runId,
responsibleUserId: operatorIdentity?.actorId ?? (origin ? origin.responsibleUserId : input.responsibleUserId),
parentContextId: origin?.id ?? null,
cause:
operatorIdentity ? operatorIdentity.cause : origin?.cause === "company_default" ? "company_default" : input.cause,
correlationId: "dispatch",
});
const ids = [...new Set(input.messageIds ?? [])];
const comments =
ids.length && input.issueId
? await tx
.select()
.from(issueComments)
.where(
and(
eq(issueComments.companyId, input.companyId),
eq(issueComments.issueId, input.issueId),
inArray(issueComments.id, ids),
),
)
: [];
for (const id of ids) {
const comment = comments.find((c) => c.id === id);
if (!comment?.authorUserId) continue;
current = await append(tx, {
companyId: input.companyId,
runId: input.runId,
responsibleUserId: operatorIdentity?.actorId ?? comment.authorUserId,
messageId: id,
parentContextId: current.id,
cause: operatorIdentity?.cause ?? "instruction",
correlationId: `message:${id}`,
});
}
return current;
});
}
/** Caller holds the task and run row locks, in that order. Reserve before delivery so acquisitions cannot guess. */
export async function prepareSteeredIdentity(
executor: Executor,
input: {
companyId: string;
runId: string;
messageId: string;
issueId: string;
source?: "comment" | "interaction";
},
) {
const [run] = await executor
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.runId),
eq(heartbeatRuns.companyId, input.companyId),
),
);
const [comment] = input.source === "interaction" ? [] : await executor
.select()
.from(issueComments)
.where(
and(
eq(issueComments.id, input.messageId),
eq(issueComments.companyId, input.companyId),
eq(issueComments.issueId, input.issueId),
),
);
const [interaction] = input.source === "interaction" ? await executor.select().from(issueThreadInteractions).where(and(
eq(issueThreadInteractions.id, input.messageId),
eq(issueThreadInteractions.companyId, input.companyId),
eq(issueThreadInteractions.issueId, input.issueId),
inArray(issueThreadInteractions.status, ["accepted", "answered", "rejected"]),
)) : [];
const responsibleUserId = interaction?.resolvedByUserId ?? comment?.authorUserId;
if (!run || !responsibleUserId)
throw forbidden("Steering requires an authenticated message author");
if (
run.status !== "running" ||
boundRunIssueId(run) !== input.issueId
) {
throw conflict("Steering targets a different or inactive execution");
}
return append(executor, {
companyId: input.companyId,
runId: input.runId,
responsibleUserId,
messageId: input.messageId,
parentContextId: run.activeIdentityContextId,
cause: "steering",
correlationId: `${input.source === "interaction" ? "interaction" : "message"}:${input.messageId}`,
status: "pending",
});
}
export async function reserveSteeredIdentity(
db: Db,
input: Parameters<typeof prepareSteeredIdentity>[1],
) {
return db.transaction(async (tx) => {
await lockIdentityTask(tx, input.companyId, input.runId);
const [run] = await tx
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.runId),
eq(heartbeatRuns.companyId, input.companyId),
),
)
.for("no key update");
// Processes started before the broker rollout keep their original environment.
if (!run?.activeIdentityContextId) return null;
const [pending] = await tx
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.runId, run.id),
eq(runIdentityContexts.status, "pending"),
),
)
.limit(1);
if (pending && pending.messageId !== input.messageId) {
if (await storedSteeringAcknowledgement(tx, pending))
await acceptSteeredIdentity(tx, pending);
else
throw conflict(
"A prior steering acknowledgement must be reconciled first",
);
}
const [existing] = await tx
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.runId, run.id),
eq(runIdentityContexts.messageId, input.messageId),
eq(runIdentityContexts.status, "accepted"),
),
);
if (existing) return existing;
const context = await prepareSteeredIdentity(tx, input);
if (context.status === "rejected") {
await tx
.update(runIdentityContexts)
.set({ status: "pending" })
.where(eq(runIdentityContexts.id, context.id));
return { ...context, status: "pending" };
}
return context;
});
}
export async function rejectSteeredIdentity(
db: Db,
context: RunIdentityContext,
) {
await db
.update(runIdentityContexts)
.set({ status: "rejected" })
.where(
and(
eq(runIdentityContexts.id, context.id),
eq(runIdentityContexts.status, "pending"),
),
);
}
/** Accept within the caller's task/run transaction; standalone recovery uses reconcileSteeredIdentity. */
export async function acceptSteeredIdentity(
executor: Executor,
context: RunIdentityContext,
) {
// A replay must not reactivate a historical identity after later instructions.
const [accepted] = await executor
.update(runIdentityContexts)
.set({ status: "accepted", acceptedAt: new Date() })
.where(
and(
eq(runIdentityContexts.id, context.id),
eq(runIdentityContexts.status, "pending"),
),
)
.returning();
if (accepted) await activate(executor, accepted);
}
const runIdentityFields = {
id: heartbeatRuns.id, companyId: heartbeatRuns.companyId, agentId: heartbeatRuns.agentId,
status: heartbeatRuns.status, responsibleUserId: heartbeatRuns.responsibleUserId,
activeIdentityContextId: heartbeatRuns.activeIdentityContextId,
};
export const runIdentityTaskFields = {
...runIdentityFields, nativeIssueId: heartbeatRuns.nativeIssueId, issueId: heartbeatRuns.issueId,
wakeupRequestId: heartbeatRuns.wakeupRequestId,
contextSnapshot: sql<Record<string, unknown> | null>`case when jsonb_typeof(${heartbeatRuns.contextSnapshot}) = 'object'
then jsonb_build_object('issueId', ${heartbeatRuns.contextSnapshot}->'issueId',
'taskId', ${heartbeatRuns.contextSnapshot}->'taskId',
'conversationSessionGeneration', ${heartbeatRuns.contextSnapshot}->'conversationSessionGeneration',
'paperclipSkillTest', ${heartbeatRuns.contextSnapshot}->'paperclipSkillTest') else null end`,
};
export const runIdentityBrokerFields = {
...runIdentityFields, invocationSource: heartbeatRuns.invocationSource,
contextSnapshot: sql<Record<string, unknown> | null>`case when jsonb_typeof(${heartbeatRuns.contextSnapshot}) = 'object'
then jsonb_build_object('issueId', ${heartbeatRuns.contextSnapshot}->'issueId',
'projectId', ${heartbeatRuns.contextSnapshot}->'projectId', 'routineId', ${heartbeatRuns.contextSnapshot}->'routineId',
'responsibleUserId', ${heartbeatRuns.contextSnapshot}->'responsibleUserId',
'responsible_user_id', ${heartbeatRuns.contextSnapshot}->'responsible_user_id',
'paperclipIssue', case when jsonb_typeof(${heartbeatRuns.contextSnapshot}->'paperclipIssue') = 'object'
then jsonb_build_object('id', ${heartbeatRuns.contextSnapshot}->'paperclipIssue'->'id',
'projectId', ${heartbeatRuns.contextSnapshot}->'paperclipIssue'->'projectId',
'responsibleUserId', ${heartbeatRuns.contextSnapshot}->'paperclipIssue'->'responsibleUserId',
'responsible_user_id', ${heartbeatRuns.contextSnapshot}->'paperclipIssue'->'responsible_user_id') else null end)
else null end`,
};
const runIdentityProjectFields = {
...runIdentityTaskFields, runtimeMode: heartbeatRuns.runtimeMode,
runnerProfileJson: sql<Record<string, unknown> | null>`case when jsonb_typeof(${heartbeatRuns.runnerProfileJson}) = 'object'
then case when ${heartbeatRuns.runnerProfileJson} ? 'nativeExecutionInput'
then jsonb_build_object('nativeExecutionInput', ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput')
else '{}'::jsonb end else null end`,
};
type FullIdentityRun = typeof heartbeatRuns.$inferSelect;
type IdentityRun = Pick<FullIdentityRun, keyof typeof runIdentityFields>;
type TaskIdentityRun = Pick<FullIdentityRun, keyof typeof runIdentityTaskFields>;
type BrokerIdentityRun = Pick<FullIdentityRun, keyof typeof runIdentityBrokerFields>;
type ProjectIdentityRun = Pick<FullIdentityRun, keyof typeof runIdentityProjectFields>;
export type RunIdentityReadScope = "github_credentials" | "request_authentication" | "identity" | "task_authorization" | "token_broker" | "project_execution";
export type RunIdentityCapture<Scope extends RunIdentityReadScope | undefined = undefined> = {
run: Scope extends "identity" ? IdentityRun : Scope extends "task_authorization" ? TaskIdentityRun :
Scope extends "token_broker" ? BrokerIdentityRun : Scope extends "project_execution" ? ProjectIdentityRun : FullIdentityRun;
context: RunIdentityContext | null;
};
export async function captureRunIdentity<Scope extends RunIdentityReadScope | undefined = undefined>(
db: Db,
input: { companyId: string; runId: string; agentId: string },
options?: { readScope: Scope },
): Promise<RunIdentityCapture<Scope>> {
// Lock acquisition serializes with steering delivery and its durable acknowledgement.
return db.transaction(async (tx) => {
await lockIdentityTask(tx, input.companyId, input.runId);
const [run] = await tx
.select(options?.readScope === "identity" ? runIdentityFields :
options?.readScope === "task_authorization" ? runIdentityTaskFields :
options?.readScope === "token_broker" ? runIdentityBrokerFields :
options?.readScope === "project_execution" ? runIdentityProjectFields :
options?.readScope === "github_credentials" || options?.readScope === "request_authentication" ? {
...getTableColumns(heartbeatRuns),
contextSnapshot: sql<Record<string, unknown> | null>`case when jsonb_typeof(${heartbeatRuns.contextSnapshot}) = 'object'
then jsonb_build_object('issueId', ${heartbeatRuns.contextSnapshot}->'issueId',
'taskId', ${heartbeatRuns.contextSnapshot}->'taskId', 'projectId', ${heartbeatRuns.contextSnapshot}->'projectId',
'executionPolicy', ${heartbeatRuns.contextSnapshot}->'executionPolicy') else null end`,
runnerProfileJson: sql<Record<string, unknown> | null>`null`,
resultJson: sql<Record<string, unknown> | null>`null`,
stdoutExcerpt: sql<string | null>`null`, stderrExcerpt: sql<string | null>`null`,
} : getTableColumns(heartbeatRuns))
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.runId),
eq(heartbeatRuns.companyId, input.companyId),
eq(heartbeatRuns.agentId, input.agentId),
),
)
.for("no key update");
if (!run || run.status !== "running")
throw forbidden(
"Credential acquisition requires this agent's active run",
);
const [pending] = await tx
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.runId, run.id),
eq(runIdentityContexts.status, "pending"),
),
)
.limit(1);
if (pending) {
const acknowledgement = await storedSteeringAcknowledgement(tx, pending);
if (!acknowledgement)
throw conflict(
"Message acceptance is being reconciled; retry credential acquisition",
);
await acceptSteeredIdentity(tx, pending);
run.activeIdentityContextId = pending.id;
run.responsibleUserId = pending.responsibleUserId;
}
const [context] = run.activeIdentityContextId
? await tx
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.id, run.activeIdentityContextId),
eq(runIdentityContexts.runId, run.id),
),
)
: [];
return { run, context: context ?? null } as RunIdentityCapture<Scope>;
});
}
export async function listRunIdentityContexts(
db: Db,
companyId: string,
runId: string,
) {
return db
.select()
.from(runIdentityContexts)
.where(
and(
eq(runIdentityContexts.companyId, companyId),
eq(runIdentityContexts.runId, runId),
),
)
.orderBy(asc(runIdentityContexts.revision));
}
/** Late native acknowledgements settle reservations even when the HTTP caller timed out. */
export async function reconcileSteeredIdentity(
db: Db,
context: RunIdentityContext,
) {
await db.transaction(async (tx) => {
await lockIdentityTask(tx, context.companyId, context.runId);
const [run] = await tx
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, context.runId),
eq(heartbeatRuns.companyId, context.companyId),
),
)
.for("no key update");
if (!run) return;
await acceptSteeredIdentity(tx, context);
});
}
/** Only events validated and persisted by the native control-plane transport count. */
export async function storedSteeringAcknowledgement(
executor: Pick<Db, "select">,
context: Pick<RunIdentityContext, "companyId" | "runId" | "messageId">,
) {
if (!context.messageId) return null;
const [receipt] = await executor
.select({ payload: heartbeatRunEvents.payload })
.from(heartbeatRunEvents)
.where(
and(
eq(heartbeatRunEvents.companyId, context.companyId),
eq(heartbeatRunEvents.runId, context.runId),
eq(heartbeatRunEvents.eventType, "item.completed"),
sql`${heartbeatRunEvents.sourceEventId} is not null`,
sql`${heartbeatRunEvents.payload}->'prpEvent'->'payload'->>'kind' = 'steering_acknowledgement'`,
sql`${heartbeatRunEvents.payload}->'prpEvent'->>'itemId' like ${`%:steer:${context.messageId}`}`,
),
)
.limit(1);
const event = receipt?.payload?.prpEvent as { turnId?: string } | undefined;
return typeof event?.turnId === "string" ? { turnId: event.turnId } : null;
}