mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-06 07:38:47 +01:00
fix: keep admission responsibility reads on its transaction
This commit is contained in:
1 parent
ee199bdbaf
commit
30eea0dac1
2 files changed
+57
-55
No files matched your search
@@ -3331,48 +3331,47 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
|
||||
? (await instanceSettings.getExperimental()).enableIsolatedWorkspaces
|
||||
: false;
|
||||
let operatorResponsibleUserId: string | null = opts.manualUserWake ? opts.requestedByActorId! : null;
|
||||
let queuedResponsibleUserIdPromise: Promise<string> | null = null;
|
||||
const resolveQueuedResponsibleUserId = () => {
|
||||
if (operatorResponsibleUserId) return Promise.resolve(operatorResponsibleUserId);
|
||||
queuedResponsibleUserIdPromise ??= (async () => {
|
||||
const queuedIssueContext = issueId
|
||||
? await getIssueExecutionContext(agent.companyId, issueId)
|
||||
: null;
|
||||
const queuedRoutineEnvContext = await getRoutineEnvForExecutionIssue(
|
||||
agent.companyId,
|
||||
queuedIssueContext,
|
||||
);
|
||||
const queuedResponsibleUserId =
|
||||
await resolveResponsibleUserIdForRunSeed({
|
||||
// Resolve on the admission transaction: its issue lock must not await a
|
||||
// checkout from the root pool, or a competing admission can exhaust it.
|
||||
const resolveQueuedResponsibleUserId = async (queryOwner: Db) => {
|
||||
if (operatorResponsibleUserId) return operatorResponsibleUserId;
|
||||
const queuedIssueContext = issueId
|
||||
? await getIssueExecutionContext(agent.companyId, issueId, queryOwner)
|
||||
: null;
|
||||
const queuedRoutineEnvContext = await getRoutineEnvForExecutionIssue(
|
||||
agent.companyId,
|
||||
queuedIssueContext,
|
||||
queryOwner,
|
||||
);
|
||||
const queuedResponsibleUserId =
|
||||
await resolveResponsibleUserIdForRunSeed({
|
||||
companyId: agent.companyId,
|
||||
contextSnapshot: enrichedContextSnapshot,
|
||||
issueContext: queuedIssueContext,
|
||||
routineEnvContext: queuedRoutineEnvContext,
|
||||
requestedByActorType: opts.requestedByActorType ?? null,
|
||||
requestedByActorId: opts.requestedByActorId ?? null,
|
||||
source,
|
||||
triggerDetail,
|
||||
}, queryOwner);
|
||||
if (!queuedResponsibleUserId) {
|
||||
throw new HttpError(
|
||||
422,
|
||||
"Unable to resolve responsible user for heartbeat run dispatch",
|
||||
{
|
||||
code: "responsible_user_unresolved",
|
||||
agentId,
|
||||
companyId: agent.companyId,
|
||||
contextSnapshot: enrichedContextSnapshot,
|
||||
issueContext: queuedIssueContext,
|
||||
routineEnvContext: queuedRoutineEnvContext,
|
||||
requestedByActorType: opts.requestedByActorType ?? null,
|
||||
requestedByActorId: opts.requestedByActorId ?? null,
|
||||
issueId: issueId ?? null,
|
||||
source,
|
||||
triggerDetail,
|
||||
});
|
||||
if (!queuedResponsibleUserId) {
|
||||
throw new HttpError(
|
||||
422,
|
||||
"Unable to resolve responsible user for heartbeat run dispatch",
|
||||
{
|
||||
code: "responsible_user_unresolved",
|
||||
agentId,
|
||||
companyId: agent.companyId,
|
||||
issueId: issueId ?? null,
|
||||
source,
|
||||
triggerDetail,
|
||||
wakeReason: readNonEmptyString(
|
||||
enrichedContextSnapshot.wakeReason,
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
return queuedResponsibleUserId;
|
||||
})();
|
||||
return queuedResponsibleUserIdPromise;
|
||||
wakeReason: readNonEmptyString(
|
||||
enrichedContextSnapshot.wakeReason,
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
return queuedResponsibleUserId;
|
||||
};
|
||||
|
||||
const budgetBlock = await budgets.getInvocationBlock(
|
||||
@@ -5171,7 +5170,7 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
|
||||
invocationSource: source,
|
||||
triggerDetail,
|
||||
status: "queued",
|
||||
responsibleUserId: await resolveQueuedResponsibleUserId(),
|
||||
responsibleUserId: await resolveQueuedResponsibleUserId(tx as unknown as Db),
|
||||
wakeupRequestId: wakeupRequest.id,
|
||||
retryOfRunId: failedChatRetry
|
||||
? durableRequest!.failedRunRetry!.failedRunId
|
||||
@@ -5489,7 +5488,7 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
|
||||
invocationSource: source,
|
||||
triggerDetail,
|
||||
status: "queued",
|
||||
responsibleUserId: await resolveQueuedResponsibleUserId(),
|
||||
responsibleUserId: await resolveQueuedResponsibleUserId(tx as unknown as Db),
|
||||
wakeupRequestId: wakeupRequest.id,
|
||||
contextSnapshot: enrichedContextSnapshot,
|
||||
sessionIdBefore: sessionBefore,
|
||||
|
||||
@@ -2918,8 +2918,8 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
return byCompany;
|
||||
}
|
||||
|
||||
async function getIssueExecutionContext(companyId: string, issueId: string) {
|
||||
return db
|
||||
async function getIssueExecutionContext(companyId: string, issueId: string, queryOwner: Db = db) {
|
||||
return queryOwner
|
||||
.select({
|
||||
chatCommunicationGuidance: chatConversations.communicationGuidance,
|
||||
chatAssignedAgentId: chatEndpoints.assignedAgentId,
|
||||
@@ -3040,6 +3040,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
async function getRoutineEnvForExecutionIssue(
|
||||
companyId: string,
|
||||
issueContext: { originKind: string | null; originId: string | null; originRunId: string | null } | null,
|
||||
queryOwner: Db = db,
|
||||
) {
|
||||
if (
|
||||
!issueContext ||
|
||||
@@ -3050,7 +3051,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
}
|
||||
|
||||
const routineRun = issueContext.originRunId
|
||||
? await db
|
||||
? await queryOwner
|
||||
.select({
|
||||
routineRevisionId: routineRuns.routineRevisionId,
|
||||
responsibleUserId: routineRuns.responsibleUserId,
|
||||
@@ -3067,7 +3068,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
: null;
|
||||
|
||||
if (routineRun?.routineRevisionId) {
|
||||
const revision = await db
|
||||
const revision = await queryOwner
|
||||
.select({
|
||||
snapshot: routineRevisions.snapshot,
|
||||
responsibleUserId: routineRevisions.responsibleUserId,
|
||||
@@ -3096,7 +3097,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
}
|
||||
}
|
||||
|
||||
const routine = await db
|
||||
const routine = await queryOwner
|
||||
.select({
|
||||
env: routines.env,
|
||||
responsibleUserId: routines.responsibleUserId,
|
||||
@@ -3117,8 +3118,8 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveCompanyDefaultResponsibleUserId(companyId: string) {
|
||||
const company = await db
|
||||
async function resolveCompanyDefaultResponsibleUserId(companyId: string, queryOwner: Db = db) {
|
||||
const company = await queryOwner
|
||||
.select({ defaultResponsibleUserId: companies.defaultResponsibleUserId })
|
||||
.from(companies)
|
||||
.where(eq(companies.id, companyId))
|
||||
@@ -3128,7 +3129,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
);
|
||||
if (explicitDefault) return explicitDefault;
|
||||
|
||||
const owner = await db
|
||||
const owner = await queryOwner
|
||||
.select({ userId: companyMemberships.principalId })
|
||||
.from(companyMemberships)
|
||||
.where(
|
||||
@@ -3144,7 +3145,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (owner?.userId) return owner.userId;
|
||||
|
||||
const firstUser = await db
|
||||
const firstUser = await queryOwner
|
||||
.select({ userId: companyMemberships.principalId })
|
||||
.from(companyMemberships)
|
||||
.where(
|
||||
@@ -3163,9 +3164,10 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
async function resolveParentIssueResponsibleUserId(
|
||||
companyId: string,
|
||||
parentId: string | null | undefined,
|
||||
queryOwner: Db = db,
|
||||
) {
|
||||
if (!parentId) return null;
|
||||
const parent = await db
|
||||
const parent = await queryOwner
|
||||
.select({
|
||||
responsibleUserId: issues.responsibleUserId,
|
||||
createdByUserId: issues.createdByUserId,
|
||||
@@ -3201,7 +3203,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
source?: WakeupOptions["source"] | null;
|
||||
triggerDetail?: WakeupOptions["triggerDetail"] | null;
|
||||
existingRunResponsibleUserId?: string | null;
|
||||
}) {
|
||||
}, queryOwner: Db = db) {
|
||||
const contextResponsibleUserId = readNonEmptyString(
|
||||
input.contextSnapshot.responsibleUserId,
|
||||
);
|
||||
@@ -3221,7 +3223,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
messageIds.length &&
|
||||
!input.contextSnapshot.retryOfRunId
|
||||
) {
|
||||
const messages = await db
|
||||
const messages = await queryOwner
|
||||
.select({
|
||||
id: issueComments.id,
|
||||
authorUserId: issueComments.authorUserId,
|
||||
@@ -3246,7 +3248,7 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
}
|
||||
const retryOfRunId = readNonEmptyString(input.contextSnapshot.retryOfRunId);
|
||||
if (retryOfRunId) {
|
||||
const [origin] = await db
|
||||
const [origin] = await queryOwner
|
||||
.select({ responsibleUserId: heartbeatRuns.responsibleUserId })
|
||||
.from(heartbeatRuns)
|
||||
.where(
|
||||
@@ -3268,11 +3270,12 @@ export function createHeartbeatRunPreparation(db: Db) {
|
||||
const parentResponsibleUserId = await resolveParentIssueResponsibleUserId(
|
||||
input.companyId,
|
||||
input.issueContext?.parentId,
|
||||
queryOwner,
|
||||
);
|
||||
if (parentResponsibleUserId) return parentResponsibleUserId;
|
||||
if (!input.issueContext && requestedUserId) return requestedUserId;
|
||||
input.contextSnapshot.executionIdentityCause = "company_default";
|
||||
return resolveCompanyDefaultResponsibleUserId(input.companyId);
|
||||
return resolveCompanyDefaultResponsibleUserId(input.companyId, queryOwner);
|
||||
}
|
||||
|
||||
async function resolveResponsibleUserIdForRun(input: {
|
||||
|
||||
Reference in new issue
Block a user