mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
fix(identity): read a provisioned agent key without an exclusive agent row lock (#21506)
Run setup reads an existing agent identity key under FOR KEY SHARE on the agent row instead of FOR UPDATE, so it no longer joins queued-run claim deadlocks. The lock still waits for an in-progress agent deletion. First provisioning uses the same lock with ON CONFLICT DO NOTHING. Co-Authored-By: paperclipbugbot <paperclipbugbot@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+129
-21
No files matched your search
@@ -42,30 +42,31 @@ export function agentIdentityService(db: Db) {
|
||||
|
||||
async ensureAgentIdentity(companyId: string, agentId: string) {
|
||||
return db.transaction(async (tx) => {
|
||||
// Lock the parent even when there is no identity row yet. This also
|
||||
// serializes deletion and works inside the agent-creation transaction.
|
||||
const [owner] = await tx.select({ id: agents.id }).from(agents)
|
||||
.where(and(eq(agents.companyId, companyId), eq(agents.id, agentId))).for("update");
|
||||
.where(and(eq(agents.companyId, companyId), eq(agents.id, agentId))).for("key share");
|
||||
if (!owner) throw notFound("Agent not found");
|
||||
let [row] = await tx.select().from(agentIdentityKeys).where(ownership(companyId, agentId));
|
||||
if (!row) {
|
||||
const keys = generateKeyPairSync("ed25519");
|
||||
const publicKeyPem = keys.publicKey.export({ type: "spki", format: "pem" }).toString();
|
||||
const keyId = fingerprint(keys.publicKey.export({ type: "spki", format: "der" }));
|
||||
const privateKeyPem = keys.privateKey.export({ type: "pkcs8", format: "pem" }).toString();
|
||||
const encrypted = await localEncryptedProvider.createSecret({ value: privateKeyPem });
|
||||
[row] = await tx.insert(agentIdentityKeys).values({
|
||||
companyId, agentId, algorithm: "Ed25519", keyId, publicKeyPem,
|
||||
privateKeyMaterial: encrypted.material,
|
||||
}).returning();
|
||||
await persistActivity(tx as unknown as Db, {
|
||||
companyId, actorType: "system", actorId: "system",
|
||||
action: "agent.identity.created", entityType: "agent", entityId: agentId,
|
||||
details: { algorithm: "Ed25519", keyId },
|
||||
});
|
||||
const [existing] = await tx.select().from(agentIdentityKeys).where(ownership(companyId, agentId));
|
||||
if (existing) return resolveIdentity(existing);
|
||||
const keys = generateKeyPairSync("ed25519");
|
||||
const publicKeyPem = keys.publicKey.export({ type: "spki", format: "pem" }).toString();
|
||||
const keyId = fingerprint(keys.publicKey.export({ type: "spki", format: "der" }));
|
||||
const privateKeyPem = keys.privateKey.export({ type: "pkcs8", format: "pem" }).toString();
|
||||
const encrypted = await localEncryptedProvider.createSecret({ value: privateKeyPem });
|
||||
const [created] = await tx.insert(agentIdentityKeys).values({
|
||||
companyId, agentId, algorithm: "Ed25519", keyId, publicKeyPem,
|
||||
privateKeyMaterial: encrypted.material,
|
||||
}).onConflictDoNothing({ target: agentIdentityKeys.agentId }).returning();
|
||||
if (!created) {
|
||||
const [winner] = await tx.select().from(agentIdentityKeys).where(ownership(companyId, agentId));
|
||||
if (!winner) throw new Error("Agent identity key belongs to another owner");
|
||||
return resolveIdentity(winner);
|
||||
}
|
||||
// Fail closed on corruption or a wrong master key; never replace a key.
|
||||
return resolveIdentity(row);
|
||||
await persistActivity(tx as unknown as Db, {
|
||||
companyId, actorType: "system", actorId: "system",
|
||||
action: "agent.identity.created", entityType: "agent", entityId: agentId,
|
||||
details: { algorithm: "Ed25519", keyId },
|
||||
});
|
||||
return resolveIdentity(created);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user