@inkeep/agents-core 0.59.2 → 0.59.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/auth/auth-schema.d.ts +86 -86
  2. package/dist/auth/auth-validation-schemas.d.ts +137 -137
  3. package/dist/auth/auth.js +2 -2
  4. package/dist/auth/permissions.d.ts +9 -9
  5. package/dist/client-exports.d.ts +2 -2
  6. package/dist/client-exports.js +2 -2
  7. package/dist/constants/signoz-queries.d.ts +30 -106
  8. package/dist/constants/signoz-queries.js +55 -108
  9. package/dist/data-access/manage/credentialReferences.js +1 -1
  10. package/dist/data-access/manage/dataComponents.js +1 -1
  11. package/dist/data-access/manage/skills.d.ts +1 -1
  12. package/dist/data-access/manage/subAgentRelations.d.ts +6 -6
  13. package/dist/data-access/manage/triggers.d.ts +2 -2
  14. package/dist/data-access/runtime/apiKeys.d.ts +4 -4
  15. package/dist/data-access/runtime/apps.d.ts +4 -4
  16. package/dist/data-access/runtime/conversations.d.ts +11 -11
  17. package/dist/data-access/runtime/scheduledTriggerInvocations.d.ts +3 -3
  18. package/dist/data-access/runtime/tasks.d.ts +4 -4
  19. package/dist/data-access/runtime/triggerInvocations.d.ts +1 -1
  20. package/dist/db/manage/manage-schema.d.ts +361 -361
  21. package/dist/db/runtime/runtime-schema.d.ts +14 -14
  22. package/dist/db/runtime/test-runtime-client.js +1 -1
  23. package/dist/dolt/advisory-lock.d.ts +7 -0
  24. package/dist/dolt/advisory-lock.js +18 -0
  25. package/dist/dolt/branches-api.d.ts +19 -9
  26. package/dist/dolt/branches-api.js +58 -29
  27. package/dist/dolt/index.d.ts +7 -4
  28. package/dist/dolt/index.js +7 -4
  29. package/dist/dolt/merge.d.ts +41 -5
  30. package/dist/dolt/merge.js +161 -24
  31. package/dist/dolt/pk-map.d.ts +6 -0
  32. package/dist/dolt/pk-map.js +27 -0
  33. package/dist/dolt/ref-middleware.d.ts +2 -1
  34. package/dist/dolt/ref-middleware.js +18 -4
  35. package/dist/dolt/resolve-conflicts.d.ts +10 -0
  36. package/dist/dolt/resolve-conflicts.js +100 -0
  37. package/dist/dolt/schema-sync.js +17 -17
  38. package/dist/index.d.ts +10 -7
  39. package/dist/index.js +11 -8
  40. package/dist/utils/error.d.ts +51 -51
  41. package/dist/utils/index.d.ts +2 -2
  42. package/dist/utils/index.js +3 -3
  43. package/dist/utils/schema-conversion.d.ts +27 -1
  44. package/dist/utils/schema-conversion.js +68 -1
  45. package/dist/utils/work-app-mcp.js +4 -3
  46. package/dist/validation/dolt-schemas.d.ts +96 -3
  47. package/dist/validation/dolt-schemas.js +54 -2
  48. package/dist/validation/index.d.ts +2 -2
  49. package/dist/validation/index.js +2 -2
  50. package/dist/validation/schemas.d.ts +1425 -1425
  51. package/package.json +2 -2
@@ -1,36 +1,150 @@
1
+ import { getLogger } from "../utils/logger.js";
2
+ import { doltAddAndCommit } from "./commit.js";
3
+ import { createApiError } from "../utils/error.js";
4
+ import { managePkMap } from "./pk-map.js";
5
+ import { ResolutionValidationError, applyResolutions } from "./resolve-conflicts.js";
1
6
  import { doltCheckout } from "./branch.js";
2
7
  import { sql } from "drizzle-orm";
3
8
 
4
9
  //#region src/dolt/merge.ts
10
+ const logger = getLogger("dolt-merge");
11
+ const TIMESTAMP_COLUMNS = new Set(["created_at", "updated_at"]);
12
+ var MergeConflictError = class extends Error {
13
+ constructor(message, conflictCount, fromBranch, toBranch) {
14
+ super(message);
15
+ this.conflictCount = conflictCount;
16
+ this.fromBranch = fromBranch;
17
+ this.toBranch = toBranch;
18
+ this.name = "MergeConflictError";
19
+ }
20
+ };
21
+ function extractConflictCount(row) {
22
+ if (typeof row.conflicts === "number" || typeof row.conflicts === "string") return Number(row.conflicts);
23
+ const doltMerge$1 = row.dolt_merge;
24
+ if (Array.isArray(doltMerge$1)) return Number(doltMerge$1[2] ?? 0);
25
+ throw new Error(`Unexpected DOLT_MERGE result format: ${JSON.stringify(row)}`);
26
+ }
27
+ function isTimestampOnlyConflictRow(row, pkColumns) {
28
+ if (row.our_diff_type !== "modified" || row.their_diff_type !== "modified") return false;
29
+ const pkSet = new Set(pkColumns);
30
+ for (const key of Object.keys(row)) {
31
+ if (!key.startsWith("base_")) continue;
32
+ const col = key.slice(5);
33
+ if (col === "diff_type" || pkSet.has(col) || TIMESTAMP_COLUMNS.has(col)) continue;
34
+ if (String(row[`base_${col}`] ?? "") !== String(row[`our_${col}`] ?? "") || String(row[`base_${col}`] ?? "") !== String(row[`their_${col}`] ?? "")) return false;
35
+ }
36
+ return true;
37
+ }
38
+ function buildTimestampAutoResolution(tableName, row, pkColumns) {
39
+ const primaryKey = {};
40
+ for (const col of pkColumns) primaryKey[col] = String(row[`base_${col}`] ?? row[`our_${col}`] ?? row[`their_${col}`]);
41
+ return {
42
+ table: tableName,
43
+ primaryKey,
44
+ rowDefaultPick: (row.our_updated_at ? new Date(String(row.our_updated_at)) : /* @__PURE__ */ new Date(0)) >= (row.their_updated_at ? new Date(String(row.their_updated_at)) : /* @__PURE__ */ new Date(0)) ? "ours" : "theirs"
45
+ };
46
+ }
5
47
  /**
6
- * Merge another branch into the current branch
7
- * Returns merge status and handles conflicts by allowing commit with conflicts
48
+ * Merge a branch into the currently checked out branch.
49
+ *
50
+ * Runs inside an explicit transaction so that conflicts and
51
+ * constraint-violations are surfaced to the caller instead of being
52
+ * auto-rolled-back by Dolt's AUTOCOMMIT mode.
53
+ *
54
+ * If conflicts arise and `resolutions` are provided, they are applied
55
+ * and the merge is committed. If conflicts arise without resolutions
56
+ * (or with insufficient resolutions), the transaction is rolled back
57
+ * and a `MergeConflictError` is thrown.
8
58
  */
9
59
  const doltMerge = (db) => async (params) => {
10
- console.log("merging branch", params.fromBranch, "into", params.toBranch);
60
+ logger.info({
61
+ fromBranch: params.fromBranch,
62
+ toBranch: params.toBranch
63
+ }, "Merging branch");
11
64
  await doltCheckout(db)({ branch: params.toBranch });
12
65
  const toHead = (await db.execute(sql`SELECT HASHOF('HEAD') as hash`)).rows[0]?.hash;
13
- const args = [`'${params.fromBranch}'`];
66
+ const args = [`'${params.fromBranch.replace(/'/g, "''")}'`];
14
67
  if (params.noFastForward) args.push("'--no-ff'");
15
- if (params.message) args.push("'-m'", `'${params.message.replace(/'/g, "''")}'`);
16
- if (params.author) args.push("'--author'", `'${params.author.name} <${params.author.email}>'`);
17
- const firstRow = (await db.execute(sql.raw(`SELECT DOLT_MERGE(${args.join(", ")})`))).rows[0] ?? {};
18
- const mergeResult = typeof firstRow.conflicts === "number" || typeof firstRow.conflicts === "string" || firstRow.conflicts == null ? firstRow : Object.values(firstRow)[0] ?? {};
19
- const conflicts = Number(mergeResult.conflicts ?? 0);
20
- if (Number.isFinite(conflicts) && conflicts > 0) return {
21
- status: "conflicts",
22
- from: params.fromBranch,
23
- to: params.toBranch,
24
- toHead,
25
- hasConflicts: true
26
- };
27
- return {
28
- status: "success",
29
- from: params.fromBranch,
30
- to: params.toBranch,
31
- toHead,
32
- hasConflicts: false
33
- };
68
+ if (params.message) {
69
+ const cleanedMessage = params.message.replace(/'/g, "''");
70
+ args.push("'-m'", `'${cleanedMessage}'`);
71
+ }
72
+ const cleanedAuthor = params.author?.name?.replace(/'/g, "''");
73
+ const cleanedEmail = params.author?.email?.replace(/'/g, "''");
74
+ if (params.author) args.push("'--author'", `'${cleanedAuthor} <${cleanedEmail}>'`);
75
+ await db.execute(sql.raw("START TRANSACTION"));
76
+ let txFinalized = false;
77
+ try {
78
+ let result;
79
+ try {
80
+ result = await db.execute(sql.raw(`SELECT DOLT_MERGE(${args.join(", ")})`));
81
+ logger.info({ result }, "DOLT_MERGE result");
82
+ } catch (error) {
83
+ const cause = error?.cause;
84
+ logger.error({
85
+ message: error?.message,
86
+ code: cause?.code,
87
+ severity: cause?.severity,
88
+ detail: cause?.detail,
89
+ hint: cause?.hint,
90
+ query: error?.query,
91
+ fromBranch: params.fromBranch,
92
+ toBranch: params.toBranch
93
+ }, "Error merging branch");
94
+ throw error;
95
+ }
96
+ const conflicts = extractConflictCount(result.rows[0] ?? {});
97
+ if (Number.isFinite(conflicts) && conflicts > 0) {
98
+ const userResolutions = params.resolutions ?? [];
99
+ const autoResolutions = [];
100
+ const conflictTables = await doltConflicts(db)();
101
+ let manualConflicts = 0;
102
+ for (const ct of conflictTables) {
103
+ const tableConflicts = await doltTableConflicts(db)({ tableName: ct.table });
104
+ const pkColumns = managePkMap[ct.table] ?? [];
105
+ for (const row of tableConflicts) if (isTimestampOnlyConflictRow(row, pkColumns)) autoResolutions.push(buildTimestampAutoResolution(ct.table, row, pkColumns));
106
+ else manualConflicts++;
107
+ }
108
+ if (manualConflicts > 0 && userResolutions.length < manualConflicts) throw new MergeConflictError(manualConflicts > 0 && userResolutions.length === 0 ? "Merge has conflicts but no resolutions were provided." : `Resolutions provided (${userResolutions.length}) do not cover all conflicts (${manualConflicts}). All conflicts must be resolved.`, manualConflicts, params.fromBranch, params.toBranch);
109
+ const allResolutions = [...autoResolutions, ...userResolutions];
110
+ try {
111
+ await applyResolutions(db)(allResolutions);
112
+ } catch (error) {
113
+ if (error instanceof ResolutionValidationError) throw createApiError({
114
+ code: "bad_request",
115
+ message: `Invalid resolution: ${error.message}`
116
+ });
117
+ throw error;
118
+ }
119
+ await doltAddAndCommit(db)({
120
+ message: params.message ? `${params.message} (with conflict resolution)` : `Merge ${params.fromBranch} into ${params.toBranch} (with conflict resolution)`,
121
+ author: params.author
122
+ });
123
+ txFinalized = true;
124
+ return {
125
+ status: "success",
126
+ from: params.fromBranch,
127
+ to: params.toBranch,
128
+ toHead,
129
+ hasConflicts: true
130
+ };
131
+ }
132
+ await db.execute(sql.raw("COMMIT"));
133
+ txFinalized = true;
134
+ return {
135
+ status: "success",
136
+ from: params.fromBranch,
137
+ to: params.toBranch,
138
+ toHead,
139
+ hasConflicts: false
140
+ };
141
+ } finally {
142
+ if (!txFinalized) try {
143
+ await db.execute(sql.raw("ROLLBACK"));
144
+ } catch (rollbackError) {
145
+ logger.error({ error: rollbackError }, "Failed to rollback transaction");
146
+ }
147
+ }
34
148
  };
35
149
  /**
36
150
  * Abort a merge
@@ -70,6 +184,29 @@ const doltSchemaConflicts = (db) => async () => {
70
184
  return (await db.execute(sql`SELECT * FROM dolt_schema_conflicts`)).rows;
71
185
  };
72
186
  /**
187
+ * Preview merge conflicts without modifying the database (dry-run).
188
+ * Returns a summary of which tables have conflicts.
189
+ */
190
+ const doltPreviewMergeConflictsSummary = (db) => async (params) => {
191
+ const escapedBaseBranch = params.baseBranch.replace(/'/g, "''");
192
+ const escapedMergeBranch = params.mergeBranch.replace(/'/g, "''");
193
+ return (await db.execute(sql.raw(`SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS_SUMMARY('${escapedBaseBranch}', '${escapedMergeBranch}')`))).rows.map((row) => ({
194
+ table: row.table.replace("public.", ""),
195
+ numDataConflicts: Number(row.num_data_conflicts ?? 0),
196
+ numSchemaConflicts: Number(row.num_schema_conflicts ?? 0)
197
+ }));
198
+ };
199
+ /**
200
+ * Preview detailed merge conflicts for a specific table without modifying the database (dry-run).
201
+ * Returns the same column shape as dolt_conflicts_$table.
202
+ */
203
+ const doltPreviewMergeConflicts = (db) => async (params) => {
204
+ const escapedBaseBranch = params.baseBranch.replace(/'/g, "''");
205
+ const escapedMergeBranch = params.mergeBranch.replace(/'/g, "''");
206
+ const escapedTableName = params.tableName.replace(/'/g, "''");
207
+ return (await db.execute(sql.raw(`SELECT * FROM DOLT_PREVIEW_MERGE_CONFLICTS('${escapedBaseBranch}', '${escapedMergeBranch}', '${escapedTableName}')`))).rows;
208
+ };
209
+ /**
73
210
  * Resolve conflicts for a table using a strategy
74
211
  */
75
212
  const doltResolveConflicts = (db) => async (params) => {
@@ -78,4 +215,4 @@ const doltResolveConflicts = (db) => async (params) => {
78
215
  };
79
216
 
80
217
  //#endregion
81
- export { doltAbortMerge, doltConflicts, doltMerge, doltMergeStatus, doltResolveConflicts, doltSchemaConflicts, doltTableConflicts };
218
+ export { MergeConflictError, doltAbortMerge, doltConflicts, doltMerge, doltMergeStatus, doltPreviewMergeConflicts, doltPreviewMergeConflictsSummary, doltResolveConflicts, doltSchemaConflicts, doltTableConflicts };
@@ -0,0 +1,6 @@
1
+ //#region src/dolt/pk-map.d.ts
2
+ type PkMap = Record<string, string[]>;
3
+ declare const managePkMap: PkMap;
4
+ declare const isValidManageTable: (tableName: string) => boolean;
5
+ //#endregion
6
+ export { PkMap, isValidManageTable, managePkMap };
@@ -0,0 +1,27 @@
1
+ import { manage_schema_exports } from "../db/manage/manage-schema.js";
2
+ import { getTableConfig } from "drizzle-orm/pg-core";
3
+
4
+ //#region src/dolt/pk-map.ts
5
+ function buildPkMapFromSchema() {
6
+ const pkMap = {};
7
+ for (const value of Object.values(manage_schema_exports)) {
8
+ if (value == null || typeof value !== "object") continue;
9
+ let config;
10
+ try {
11
+ config = getTableConfig(value);
12
+ } catch {
13
+ continue;
14
+ }
15
+ if (!config?.name || !config.primaryKeys?.length) continue;
16
+ const pkColumns = config.primaryKeys[0]?.columns.map((col) => col.name);
17
+ if (pkColumns && pkColumns.length > 0) pkMap[config.name] = pkColumns;
18
+ }
19
+ return pkMap;
20
+ }
21
+ const managePkMap = buildPkMapFromSchema();
22
+ const isValidManageTable = (tableName) => {
23
+ return tableName in managePkMap;
24
+ };
25
+
26
+ //#endregion
27
+ export { isValidManageTable, managePkMap };
@@ -3,6 +3,7 @@ import { AgentsManageDatabaseClient } from "../db/manage/manage-client.js";
3
3
  import { Context, Next } from "hono";
4
4
 
5
5
  //#region src/dolt/ref-middleware.d.ts
6
+ declare function isMergeRoute(path: string): boolean;
6
7
  type RefContext = {
7
8
  resolvedRef?: ResolvedRef;
8
9
  };
@@ -79,4 +80,4 @@ declare const refMiddlewareFactory: (db: AgentsManageDatabaseClient, options?: R
79
80
  */
80
81
  declare const writeProtectionMiddlewareFactory: () => (c: Context, next: Next) => Promise<void>;
81
82
  //#endregion
82
- export { RefContext, RefMiddlewareOptions, createRefMiddleware, createWriteProtectionMiddleware, refMiddlewareFactory, writeProtectionMiddlewareFactory };
83
+ export { RefContext, RefMiddlewareOptions, createRefMiddleware, createWriteProtectionMiddleware, isMergeRoute, refMiddlewareFactory, writeProtectionMiddlewareFactory };
@@ -1,10 +1,14 @@
1
1
  import { getLogger } from "../utils/logger.js";
2
+ import { createApiError } from "../utils/error.js";
2
3
  import { isRefWritable, resolveRef } from "./ref-helpers.js";
3
4
  import { ensureBranchExists } from "./branch.js";
4
- import { createApiError } from "../utils/error.js";
5
5
 
6
6
  //#region src/dolt/ref-middleware.ts
7
7
  const logger = getLogger("ref-middleware");
8
+ const MERGE_ROUTE_PATTERN = /\/branches\/merge(\/preview)?$/;
9
+ function isMergeRoute(path) {
10
+ return MERGE_ROUTE_PATTERN.test(path);
11
+ }
8
12
  /**
9
13
  * Default tenant ID extractor - extracts from /tenants/{tenantId} path pattern
10
14
  */
@@ -43,8 +47,18 @@ const defaultExtractProjectId = (c) => {
43
47
  const createRefMiddleware = (db, options = {}) => {
44
48
  const { extractTenantId = defaultExtractTenantId, extractProjectId = defaultExtractProjectId, allowProjectIdFromBody = true } = options;
45
49
  return async (c, next) => {
46
- const ref = c.req.query("ref");
47
- const pathSplit = c.req.path.split("/");
50
+ let ref = c.req.query("ref");
51
+ const path = c.req.path;
52
+ const pathSplit = path.split("/");
53
+ if (!ref && isMergeRoute(path) && c.req.method === "POST") try {
54
+ const body = await c.req.json();
55
+ if (body && typeof body.targetBranch === "string") {
56
+ ref = body.targetBranch;
57
+ logger.debug({ ref }, "Extracted targetBranch from merge route body as ref");
58
+ }
59
+ } catch {
60
+ logger.debug({}, "Could not extract targetBranch from merge route body");
61
+ }
48
62
  const tenantId = extractTenantId(c);
49
63
  let projectId = extractProjectId(c);
50
64
  if (!projectId && allowProjectIdFromBody && [
@@ -214,4 +228,4 @@ const refMiddlewareFactory = createRefMiddleware;
214
228
  const writeProtectionMiddlewareFactory = createWriteProtectionMiddleware;
215
229
 
216
230
  //#endregion
217
- export { createRefMiddleware, createWriteProtectionMiddleware, refMiddlewareFactory, writeProtectionMiddlewareFactory };
231
+ export { createRefMiddleware, createWriteProtectionMiddleware, isMergeRoute, refMiddlewareFactory, writeProtectionMiddlewareFactory };
@@ -0,0 +1,10 @@
1
+ import { ConflictResolution } from "../validation/dolt-schemas.js";
2
+ import { AgentsManageDatabaseClient } from "../db/manage/manage-client.js";
3
+
4
+ //#region src/dolt/resolve-conflicts.d.ts
5
+ declare class ResolutionValidationError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ declare const applyResolutions: (db: AgentsManageDatabaseClient) => (resolutions: ConflictResolution[]) => Promise<void>;
9
+ //#endregion
10
+ export { ResolutionValidationError, applyResolutions };
@@ -0,0 +1,100 @@
1
+ import { isValidManageTable, managePkMap } from "./pk-map.js";
2
+ import { sql } from "drizzle-orm";
3
+
4
+ //#region src/dolt/resolve-conflicts.ts
5
+ var ResolutionValidationError = class extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = "ResolutionValidationError";
9
+ }
10
+ };
11
+ function toSqlLiteral(val) {
12
+ if (val === null || val === void 0) return "NULL";
13
+ if (typeof val === "object") return `'${JSON.stringify(val).replace(/'/g, "''")}'`;
14
+ return `'${String(val).replace(/'/g, "''")}'`;
15
+ }
16
+ const applyResolutions = (db) => async (resolutions) => {
17
+ const affectedTables = /* @__PURE__ */ new Set();
18
+ for (const resolution of resolutions) {
19
+ if (!isValidManageTable(resolution.table)) throw new ResolutionValidationError(`Invalid table name: ${resolution.table}`);
20
+ affectedTables.add(resolution.table);
21
+ const pkColumns = managePkMap[resolution.table];
22
+ if (!pkColumns) throw new ResolutionValidationError(`No PK columns found for table: ${resolution.table}`);
23
+ const hasColumnOverrides = resolution.columns && Object.keys(resolution.columns).length > 0;
24
+ if (resolution.rowDefaultPick === "ours" && !hasColumnOverrides) continue;
25
+ const conflictRow = await readConflictRow(db, resolution.table, resolution.primaryKey, pkColumns);
26
+ if (!conflictRow) throw new ResolutionValidationError(`No conflict found for table ${resolution.table} with PK ${JSON.stringify(resolution.primaryKey)}`);
27
+ const ourDiffType = conflictRow.our_diff_type;
28
+ const theirDiffType = conflictRow.their_diff_type;
29
+ if (resolution.rowDefaultPick === "theirs" && !hasColumnOverrides) await applyTheirsResolution(db, resolution.table, resolution.primaryKey, pkColumns, conflictRow, ourDiffType, theirDiffType);
30
+ else await applyMixedResolution(db, resolution.table, resolution.primaryKey, pkColumns, conflictRow, resolution.rowDefaultPick, resolution.columns ?? {});
31
+ }
32
+ for (const table of affectedTables) await db.execute(sql.raw(`SELECT DOLT_CONFLICTS_RESOLVE('--ours', '${table}')`));
33
+ };
34
+ async function readConflictRow(db, table, primaryKey, pkColumns) {
35
+ const whereClause = pkColumns.map((col) => {
36
+ const val = primaryKey[col];
37
+ if (val === void 0) throw new ResolutionValidationError(`Missing PK column ${col} for table ${table}`);
38
+ const escapedVal = val.replace(/'/g, "''");
39
+ return `(base_${col} = '${escapedVal}' OR (base_${col} IS NULL AND (our_${col} = '${escapedVal}' OR their_${col} = '${escapedVal}')))`;
40
+ }).join(" AND ");
41
+ return (await db.execute(sql.raw(`SELECT * FROM dolt_conflicts_${table} WHERE ${whereClause} LIMIT 1`))).rows[0] ?? null;
42
+ }
43
+ function getColumnNames(conflictRow, pkColumns) {
44
+ const theirPrefix = "their_";
45
+ const skipColumns = new Set([
46
+ "our_diff_type",
47
+ "their_diff_type",
48
+ "base_diff_type",
49
+ ...pkColumns.map((c) => `base_${c}`),
50
+ ...pkColumns.map((c) => `our_${c}`),
51
+ ...pkColumns.map((c) => `their_${c}`)
52
+ ]);
53
+ const columns = [];
54
+ for (const key of Object.keys(conflictRow)) if (key.startsWith(theirPrefix) && !skipColumns.has(key)) columns.push(key.slice(6));
55
+ return columns;
56
+ }
57
+ async function applyTheirsResolution(db, table, primaryKey, pkColumns, conflictRow, ourDiffType, theirDiffType) {
58
+ const pkWhere = pkColumns.map((col) => {
59
+ return `"${col}" = '${primaryKey[col]?.replace(/'/g, "''")}'`;
60
+ }).join(" AND ");
61
+ if (theirDiffType === "removed") {
62
+ await db.execute(sql.raw(`DELETE FROM "${table}" WHERE ${pkWhere}`));
63
+ return;
64
+ }
65
+ const columns = getColumnNames(conflictRow, pkColumns);
66
+ if (ourDiffType === "removed") {
67
+ const allCols = [...pkColumns, ...columns];
68
+ const values = allCols.map((col) => {
69
+ return toSqlLiteral(pkColumns.includes(col) ? primaryKey[col] : conflictRow[`their_${col}`]);
70
+ });
71
+ await db.execute(sql.raw(`INSERT INTO "${table}" (${allCols.map((c) => `"${c}"`).join(", ")}) VALUES (${values.join(", ")})`));
72
+ return;
73
+ }
74
+ const setClauses = columns.map((col) => {
75
+ const val = conflictRow[`their_${col}`];
76
+ return `"${col}" = ${toSqlLiteral(val)}`;
77
+ });
78
+ await db.execute(sql.raw(`UPDATE "${table}" SET ${setClauses.join(", ")} WHERE ${pkWhere}`));
79
+ }
80
+ async function applyMixedResolution(db, table, primaryKey, pkColumns, conflictRow, rowDefaultPick, columnOverrides) {
81
+ const ourDiffType = conflictRow.our_diff_type;
82
+ const theirDiffType = conflictRow.their_diff_type;
83
+ if (theirDiffType === "removed" || ourDiffType === "removed") {
84
+ if (rowDefaultPick === "ours") return;
85
+ await applyTheirsResolution(db, table, primaryKey, pkColumns, conflictRow, ourDiffType, theirDiffType);
86
+ return;
87
+ }
88
+ const columns = getColumnNames(conflictRow, pkColumns);
89
+ const pkWhere = pkColumns.map((col) => {
90
+ return `"${col}" = '${primaryKey[col]?.replace(/'/g, "''")}'`;
91
+ }).join(" AND ");
92
+ const setClauses = columns.map((col) => {
93
+ const val = conflictRow[`${(columnOverrides[col] ?? rowDefaultPick) === "theirs" ? "their_" : "our_"}${col}`];
94
+ return `"${col}" = ${toSqlLiteral(val)}`;
95
+ });
96
+ await db.execute(sql.raw(`UPDATE "${table}" SET ${setClauses.join(", ")} WHERE ${pkWhere}`));
97
+ }
98
+
99
+ //#endregion
100
+ export { ResolutionValidationError, applyResolutions };
@@ -1,5 +1,5 @@
1
1
  import { doltAddAndCommit, doltStatus } from "./commit.js";
2
- import { doltAbortMerge, doltMerge } from "./merge.js";
2
+ import { MergeConflictError, doltMerge } from "./merge.js";
3
3
  import { sql } from "drizzle-orm";
4
4
  import { createHash } from "node:crypto";
5
5
 
@@ -135,23 +135,26 @@ const syncSchemaFromMain = (db) => async (options = {}) => {
135
135
  error: "Cannot sync schema: uncommitted changes exist. Commit changes first or set autoCommitPending: true"
136
136
  };
137
137
  const mergeSchemaMessage = `Synced schema from ${SCHEMA_SOURCE_BRANCH}`;
138
- if ((await doltMerge(db)({
139
- fromBranch: SCHEMA_SOURCE_BRANCH,
140
- toBranch: currentBranch,
141
- message: mergeSchemaMessage,
142
- noFastForward: true,
143
- author: {
144
- name: "Schema Sync System",
145
- email: "system@inkeep.com"
146
- }
147
- })).status === "conflicts") {
148
- await doltAbortMerge(db)();
149
- return {
138
+ const schemaSyncAuthor = {
139
+ name: "Schema Sync System",
140
+ email: "system@inkeep.com"
141
+ };
142
+ try {
143
+ await doltMerge(db)({
144
+ fromBranch: SCHEMA_SOURCE_BRANCH,
145
+ toBranch: currentBranch,
146
+ message: mergeSchemaMessage,
147
+ noFastForward: true,
148
+ author: schemaSyncAuthor
149
+ });
150
+ } catch (error) {
151
+ if (error instanceof MergeConflictError) return {
150
152
  synced: false,
151
153
  hadDifferences: true,
152
154
  differences,
153
- error: "Schema merge produced conflicts that require manual resolution. Merge has been aborted."
155
+ error: "Schema merge produced conflicts that require manual resolution. Merge has been rolled back."
154
156
  };
157
+ throw error;
155
158
  }
156
159
  return {
157
160
  synced: true,
@@ -160,9 +163,6 @@ const syncSchemaFromMain = (db) => async (options = {}) => {
160
163
  mergeCommitHash: await getLatestCommitHash(db)()
161
164
  };
162
165
  } catch (error) {
163
- try {
164
- await doltAbortMerge(db)();
165
- } catch {}
166
166
  return {
167
167
  synced: false,
168
168
  hadDifferences: true,