fix(auth): preserve identity across connection request handoffs

Continue accepted connection requests as the verified recipient and retain their original audit parent. Keep declined and automatically closed requests on the source identity. Preserve the latest accepted retry context, ordinary preparation retries, and saved approval origins.

Verification: 209 focused local tests passed. Final-head CI passed, Greptile and both Paperclip reviewers rated the change 5/5, and all review threads are resolved.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-11-18 10:45:55 -06:00
1 parent 2a43fe3e34
commit 875e4d375e
6 files changed
+301 -11

No files matched your search

@@ -57,8 +57,10 @@ export async function wakeConnectionIntentAfterResolution(
mutation: "interaction",
wakeReason: "issue_commented",
source: "connection_intent.resolved",
...(input.actorType === "system" && input.loaded.interaction.addresseeUserId
? { responsibleUserId: input.loaded.interaction.addresseeUserId, connectionIntentResolution: "existing_connection" }
// Automatic retirement observes access; it does not accept work as the
// addressee. Identity initialization retains the request's source context.
...(input.actorType === "system"
? { connectionIntentResolution: "existing_connection" }
: {}),
...(interactionResolvedAt
? { interactionResolvedAt }
+45 -6
View File
@@ -77,6 +77,20 @@ export async function explicitOperatorRunIdentity(
export type RunIdentityContext = typeof runIdentityContexts.$inferSelect;
/** Only the recipient's completed connection action authorizes a handoff. */
function acceptedConnectionRecipient(interaction: Pick<typeof issueThreadInteractions.$inferSelect,
"kind" | "status" | "resolvedByAgentId" | "resolvedByUserId" | "addresseeUserId" | "result"
> | undefined): string | null {
return interaction?.kind === "connection_intent"
&& interaction.status === "accepted"
&& !interaction.resolvedByAgentId
&& interaction.resolvedByUserId === interaction.addresseeUserId
&& interaction.result?.outcome === "connected"
&& "connectionId" in interaction.result
&& typeof interaction.result.connectionId === "string"
? interaction.addresseeUserId : null;
}
function assertRunTaskIdentity(run: {
nativeIssueId?: string | null;
issueId?: string | null;
@@ -270,13 +284,19 @@ export async function initializeRunIdentity(
retryOrigin = origin.identityContextId;
}
}
const [parent] = input.parentRunId
const snapshotRetryRunId = run.contextSnapshot?.retryOfRunId;
const retryRunId = run.retryOfRunId ?? (
typeof snapshotRetryRunId === "string" ? snapshotRetryRunId.trim() || null : null
);
const isRetry = retryRunId !== null || retryOrigin !== null;
const parentRunId = retryRunId ?? input.parentRunId;
const [parent] = parentRunId
? await tx
.select()
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, input.parentRunId),
eq(heartbeatRuns.id, parentRunId),
eq(heartbeatRuns.companyId, input.companyId),
),
)
@@ -294,8 +314,20 @@ export async function initializeRunIdentity(
),
)
: [];
const retryContextId = retryOrigin ?? (isRetry ? parent?.activeIdentityContextId : null);
// A preparation failure can precede identity initialization. An ordinary
// retry retains its stored person when available, or the caller's existing
// responsible-user resolution. It never replays a connection handoff.
const uninitializedRetryUserId = isRetry && !retryContextId
? parent?.responsibleUserId : null;
if (!operatorIdentity && isRetry && !retryContextId && interaction?.kind === "connection_intent")
throw forbidden("Originating execution identity is unavailable");
// An ordinary approval keeps its saved origin when preparation failed
// before the previous run accepted an identity. A stored retry context
// still wins after initialization or later accepted steering.
const parentId = operatorIdentity ? null : (
retryOrigin ?? interaction?.sourceIdentityContextId ?? input.parentContextId ?? parent?.activeIdentityContextId
retryContextId ?? interaction?.sourceIdentityContextId
?? (isRetry ? null : input.parentContextId ?? parent?.activeIdentityContextId)
);
const [origin] = parentId
? await tx
@@ -311,13 +343,20 @@ export async function initializeRunIdentity(
: [];
if (parentId && !origin)
throw forbidden("Originating execution identity is unavailable");
const connectionRecipient = acceptedConnectionRecipient(interaction);
const identity = operatorIdentity
? { responsibleUserId: operatorIdentity.actorId, cause: operatorIdentity.cause }
: !isRetry && connectionRecipient && origin?.responsibleUserId !== connectionRecipient
? { responsibleUserId: connectionRecipient, cause: "connection_handoff" }
: {
responsibleUserId: origin ? origin.responsibleUserId : uninitializedRetryUserId ?? input.responsibleUserId,
cause: origin?.cause === "company_default" ? "company_default" : input.cause,
};
let current = await append(tx, {
companyId: input.companyId,
runId: input.runId,
responsibleUserId: operatorIdentity?.actorId ?? (origin ? origin.responsibleUserId : input.responsibleUserId),
...identity,
parentContextId: origin?.id ?? null,
cause:
operatorIdentity ? operatorIdentity.cause : origin?.cause === "company_default" ? "company_default" : input.cause,
correlationId: "dispatch",
});
const ids = [...new Set(input.messageIds ?? [])];