fix: field normalization, AI connection validation, autofix filter (#581) (#638)

- Normalize name→nodeName and id→nodeId for node-targeting operations in
  the Zod schema transform, so LLMs using natural field names no longer
  get "Node not found" errors
- Replace hardcoded ALL_CONNECTION_TYPES with dynamic iteration so AI
  sub-nodes (ai_outputParser, ai_document, ai_textSplitter, etc.) are
  not flagged as disconnected during save
- Add .catchall() to workflowConnectionSchema and extend connection
  reference validation to cover all connection types, not just main
- Fix filterOperationsByFixes ID-vs-name mismatch: typeversion-upgrade
  operations now include nodeName alongside nodeId, and the filter checks
  both fields

Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Romuald Członkowski
2026-03-15 14:32:14 +01:00
committed by GitHub
parent 65ab94deb2
commit f7a1cfe8bf
41 changed files with 657 additions and 154 deletions

View File

@@ -50,6 +50,9 @@ function getValidator(repository) {
}
return cachedValidator;
}
const NODE_TARGETING_OPERATIONS = new Set([
'updateNode', 'removeNode', 'moveNode', 'enableNode', 'disableNode'
]);
const workflowDiffSchema = zod_1.z.object({
id: zod_1.z.string(),
operations: zod_1.z.array(zod_1.z.object({
@@ -64,8 +67,8 @@ const workflowDiffSchema = zod_1.z.object({
target: zod_1.z.string().optional(),
from: zod_1.z.string().optional(),
to: zod_1.z.string().optional(),
sourceOutput: zod_1.z.string().optional(),
targetInput: zod_1.z.string().optional(),
sourceOutput: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform(String).optional(),
targetInput: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).transform(String).optional(),
sourceIndex: zod_1.z.number().optional(),
targetIndex: zod_1.z.number().optional(),
branch: zod_1.z.enum(['true', 'false']).optional(),
@@ -76,6 +79,19 @@ const workflowDiffSchema = zod_1.z.object({
settings: zod_1.z.any().optional(),
name: zod_1.z.string().optional(),
tag: zod_1.z.string().optional(),
id: zod_1.z.string().optional(),
}).transform((op) => {
if (NODE_TARGETING_OPERATIONS.has(op.type)) {
if (!op.nodeName && !op.nodeId && op.name) {
op.nodeName = op.name;
op.name = undefined;
}
if (!op.nodeId && op.id) {
op.nodeId = op.id;
op.id = undefined;
}
}
return op;
})),
validateOnly: zod_1.z.boolean().optional(),
continueOnError: zod_1.z.boolean().optional(),
@@ -167,11 +183,12 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
else {
return {
success: false,
saved: false,
error: 'Failed to apply diff operations',
operationsApplied: diffResult.operationsApplied,
details: {
errors: diffResult.errors,
warnings: diffResult.warnings,
operationsApplied: diffResult.operationsApplied,
applied: diffResult.applied,
failed: diffResult.failed
}
@@ -239,6 +256,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
if (!skipValidation) {
return {
success: false,
saved: false,
error: errorMessage,
details: {
errors: structureErrors,
@@ -247,7 +265,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
applied: diffResult.applied,
recoveryGuidance: recoverySteps,
note: 'Operations were applied but created an invalid workflow structure. The workflow was NOT saved to n8n to prevent UI rendering errors.',
autoSanitizationNote: 'Auto-sanitization runs on all nodes during updates to fix operator structures and add missing metadata. However, it cannot fix all issues (e.g., broken connections, branch mismatches). Use the recovery guidance above to resolve remaining issues.'
autoSanitizationNote: 'Auto-sanitization runs on modified nodes during updates to fix operator structures and add missing metadata. However, it cannot fix all issues (e.g., broken connections, branch mismatches). Use the recovery guidance above to resolve remaining issues.'
}
};
}
@@ -259,6 +277,58 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
}
try {
const updatedWorkflow = await client.updateWorkflow(input.id, diffResult.workflow);
let tagWarnings = [];
if (diffResult.tagsToAdd?.length || diffResult.tagsToRemove?.length) {
try {
const existingTags = Array.isArray(updatedWorkflow.tags)
? updatedWorkflow.tags.map((t) => typeof t === 'object' ? { id: t.id, name: t.name } : { id: '', name: t })
: [];
const allTags = await client.listTags();
const tagMap = new Map();
for (const t of allTags.data) {
if (t.id)
tagMap.set(t.name.toLowerCase(), t.id);
}
for (const tagName of (diffResult.tagsToAdd || [])) {
if (!tagMap.has(tagName.toLowerCase())) {
try {
const newTag = await client.createTag({ name: tagName });
if (newTag.id)
tagMap.set(tagName.toLowerCase(), newTag.id);
}
catch (createErr) {
tagWarnings.push(`Failed to create tag "${tagName}": ${createErr instanceof Error ? createErr.message : 'Unknown error'}`);
}
}
}
const currentTagIds = new Set();
for (const et of existingTags) {
if (et.id) {
currentTagIds.add(et.id);
}
else {
const resolved = tagMap.get(et.name.toLowerCase());
if (resolved)
currentTagIds.add(resolved);
}
}
for (const tagName of (diffResult.tagsToAdd || [])) {
const tagId = tagMap.get(tagName.toLowerCase());
if (tagId)
currentTagIds.add(tagId);
}
for (const tagName of (diffResult.tagsToRemove || [])) {
const tagId = tagMap.get(tagName.toLowerCase());
if (tagId)
currentTagIds.delete(tagId);
}
await client.updateWorkflowTags(input.id, Array.from(currentTagIds));
}
catch (tagError) {
tagWarnings.push(`Tag update failed: ${tagError instanceof Error ? tagError.message : 'Unknown error'}`);
logger_1.logger.warn('Tag operations failed (non-blocking)', tagError);
}
}
let finalWorkflow = updatedWorkflow;
let activationMessage = '';
try {
@@ -286,6 +356,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
logger_1.logger.error('Failed to activate workflow after update', activationError);
return {
success: false,
saved: true,
error: 'Workflow updated successfully but activation failed',
details: {
workflowUpdated: true,
@@ -303,6 +374,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
logger_1.logger.error('Failed to deactivate workflow after update', deactivationError);
return {
success: false,
saved: true,
error: 'Workflow updated successfully but deactivation failed',
details: {
workflowUpdated: true,
@@ -329,6 +401,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
}
return {
success: true,
saved: true,
data: {
id: finalWorkflow.id,
name: finalWorkflow.name,
@@ -341,7 +414,7 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
applied: diffResult.applied,
failed: diffResult.failed,
errors: diffResult.errors,
warnings: diffResult.warnings
warnings: mergeWarnings(diffResult.warnings, tagWarnings)
}
};
}
@@ -379,7 +452,9 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
return {
success: false,
error: 'Invalid input',
details: { errors: error.errors }
details: {
errors: error.errors.map(e => `${e.path.join('.')}: ${e.message}`)
}
};
}
logger_1.logger.error('Failed to update partial workflow', error);
@@ -389,6 +464,13 @@ async function handleUpdatePartialWorkflow(args, repository, context) {
};
}
}
function mergeWarnings(diffWarnings, tagWarnings) {
const merged = [
...(diffWarnings || []),
...tagWarnings.map(w => ({ operation: -1, message: w }))
];
return merged.length > 0 ? merged : undefined;
}
function inferIntentFromOperations(operations) {
if (!operations || operations.length === 0) {
return 'Partial workflow update';