fix(api): wrap audit log writes inside their parent transactions

Prevents mutations from committing without an audit trail if the
auditLog.create call fails after the main write already succeeded.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 16:40:10 +02:00
parent a01f99561d
commit 3c0179fcec
25 changed files with 758 additions and 656 deletions
@@ -19,7 +19,7 @@ export function createToolContext(
},
): ToolContext {
const userRole = options?.userRole ?? SystemRole.ADMIN;
const mergedDb = {
const mergedDb: Record<string, unknown> = {
...defaultDbDefaults,
...db,
blueprint: {
@@ -27,6 +27,9 @@ export function createToolContext(
...(db.blueprint as Record<string, unknown> | undefined),
},
};
if (!mergedDb["$transaction"]) {
mergedDb["$transaction"] = vi.fn(async (fn: (tx: unknown) => unknown) => fn(mergedDb));
}
return {
db: mergedDb as ToolContext["db"],
userId: "user_1",