Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(workflow): plan a squeezed automatic edge first before failing on a shared corridor

tt-a1i4659d979778c
diff --git a/archify/renderers/workflow/workflow-compiler.mjs b/archify/renderers/workflow/workflow-compiler.mjs
index 42587f5a..5ce9f205 100644
--- a/archify/renderers/workflow/workflow-compiler.mjs
+++ b/archify/renderers/workflow/workflow-compiler.mjs
@@ -2285,6 +2285,14 @@ function validateReadablePinnedGeometry() {
validateReadableRouteControls(edge);
pathFor(edge);
}
+ // Routing order feedback: an automatic edge that an earlier automatic edge
+ // forced into a shared corridor or crossing is planned first on a retry.
+ for (const index of asArray(layoutFeedback.routeFirst)) {
+ // Diagnostics name source indexes; the canonical edge order differs.
+ const edge = workflow.edges.find((candidate) => sourceIndexes.edges.get(candidate) === index);
+ if (!edge || !nodes.has(edge.from) || !nodes.has(edge.to) || !independentAutomaticRoute(edge)) continue;
+ pathFor(edge);
+ }
for (const edge of workflow.edges) {
if (!nodes.has(edge.from) || !nodes.has(edge.to)) continue;
validateReadableRouteControls(edge);
@@ -5076,11 +5084,58 @@ function feedbackFailure(request) {
return compilerFailure('readable-v2', diagnostics, message);
}
+const ROUTE_ORDER_CODES = new Set(['composition/ambiguous-corridor', 'composition/proper-crossing']);
+const MAX_ROUTE_ORDER_RETRIES = 3;
+
+// Automatic routes are planned in source order, each against those already
+// placed, so an earlier edge can take the only clear corridor of a later one
+// (a long cross-lane branch dropping between two same-lane neighbours). When
+// the readable result fails only because two automatic edges share a corridor
+// or cross, plan the later edge first and keep the retry only if it removes
+// errors; the document itself is never changed.
+function errorCount(result) {
+ return result.ok ? 0 : result.diagnostics.filter((diagnostic) => diagnostic.severity !== 'warning').length;
+}
+function routeOrderRetryIndex(result, layoutFeedback) {
+ if (result.ok || !Array.isArray(result.diagnostics)) return null;
+ const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity !== 'warning');
+ if (!errors.length || !errors.every((diagnostic) => ROUTE_ORDER_CODES.has(diagnostic.code))) return null;
+ const tried = new Set(asArray(layoutFeedback.routeFirst));
+ for (const diagnostic of errors) {
+ const indexes = [diagnostic.subject?.index, diagnostic.evidence?.otherRelationship?.index]
+ .filter(Number.isInteger);
+ if (indexes.length !== 2) continue;
+ const later = Math.max(...indexes);
+ if (!tried.has(later)) return later;
+ }
+ return null;
+}
+function compileWithRouteOrderFeedback(options) {
+ const first = compileWorkflowInternal(options);
+ if (options.workflow?.schema_version !== 2) return first;
+ let best = first;
+ let current = first;
+ let layoutFeedback = options.layoutFeedback;
+ for (let retry = 0; retry < MAX_ROUTE_ORDER_RETRIES && errorCount(best) > 0; retry += 1) {
+ const index = routeOrderRetryIndex(current, layoutFeedback);
+ if (index === null) break;
+ layoutFeedback = { ...layoutFeedback, routeFirst: [...asArray(layoutFeedback.routeFirst), index] };
+ try {
+ current = compileWorkflowInternal({ ...options, layoutFeedback });
+ } catch (error) {
+ if (error instanceof WorkflowLayoutFeedback) break;
+ throw error;
+ }
+ if (errorCount(current) < errorCount(best)) best = current;
+ }
+ return best;
+}
+
function compileWorkflowWithFeedback({ workflow, qualityProfile, sourceEvidence, discoverFixes = true } = {}) {
let layoutFeedback = {};
for (let attempt = 0; attempt <= MAX_READABLE_LAYOUT_FEEDBACK_ROUNDS; attempt += 1) {
try {
- return compileWorkflowInternal({
+ return compileWithRouteOrderFeedback({
workflow,
qualityProfile,
sourceEvidence,
diff --git a/test/fixtures/workflow-first-draft/agent-tool-call.workflow.json b/test/fixtures/workflow-first-draft/agent-tool-call.workflow.json
new file mode 100644
index 00000000..1bfda0ab
--- /dev/null
+++ b/test/fixtures/workflow-first-draft/agent-tool-call.workflow.json
@@ -0,0 +1,199 @@
+{
+ "schema_version": 2,
+ "diagram_type": "workflow",
+ "meta": {
+ "title": "AI Agent Tool Call",
+ "locale": "en",
+ "quality_profile": "showcase",
+ "output": "agent-tool-call.html"
+ },
+ "lanes": [
+ {
+ "id": "user_lane",
+ "label": "User"
+ },
+ {
+ "id": "agent_lane",
+ "label": "Agent"
+ },
+ {
+ "id": "policy_lane",
+ "label": "Approval",
+ "variant": "exception"
+ },
+ {
+ "id": "exec_lane",
+ "label": "Execution"
+ }
+ ],
+ "mainPath": [
+ "req",
+ "surface",
+ "plan",
+ "route",
+ "gate",
+ "exec",
+ "provider",
+ "reply"
+ ],
+ "nodes": [
+ {
+ "id": "req",
+ "lane": "user_lane",
+ "col": 0,
+ "type": "external",
+ "label": "User Request",
+ "sublabel": "asks the agent"
+ },
+ {
+ "id": "surface",
+ "lane": "user_lane",
+ "col": 1,
+ "type": "frontend",
+ "label": "Chat Surface",
+ "sublabel": "thread + context"
+ },
+ {
+ "id": "reply",
+ "lane": "user_lane",
+ "col": 5,
+ "type": "frontend",
+ "label": "Final Result",
+ "sublabel": "shown to user"
+ },
+ {
+ "id": "plan",
+ "lane": "agent_lane",
+ "col": 1,
+ "type": "backend",
+ "label": "Planner",
+ "sublabel": "decide next step"
+ },
+ {
+ "id": "route",
+ "lane": "agent_lane",
+ "col": 2,
+ "type": "backend",
+ "label": "Tool Router",
+ "sublabel": "pick tool + check risk"
+ },
+ {
+ "id": "gate",
+ "lane": "policy_lane",
+ "col": 2,
+ "type": "security",
+ "label": "Human Approval",
+ "sublabel": "approve or deny"
+ },
+ {
+ "id": "blocked",
+ "lane": "policy_lane",
+ "col": 3,
+ "type": "security",
+ "label": "Blocked",
+ "sublabel": "denied, waiting"
+ },
+ {
+ "id": "exec",
+ "lane": "exec_lane",
+ "col": 3,
+ "type": "backend",
+ "label": "Tool Execution",
+ "sublabel": "run the call"
+ },
+ {
+ "id": "provider",
+ "lane": "exec_lane",
+ "col": 4,
+ "type": "external",
+ "label": "External Provider",
+ "sublabel": "third-party API"
+ },
+ {
+ "id": "trace",
+ "lane": "exec_lane",
+ "col": 5,
+ "type": "database",
+ "label": "Trace Store",
+ "sublabel": "results + context"
+ }
+ ],
+ "edges": [
+ {
+ "id": "e_req",
+ "from": "req",
+ "to": "surface",
+ "label": "message"
+ },
+ {
+ "id": "e_plan",
+ "from": "surface",
+ "to": "plan",
+ "label": "prompt"
+ },
+ {
+ "id": "e_route",
+ "from": "plan",
+ "to": "route",
+ "label": "tool intent"
+ },
+ {
+ "id": "e_auto",
+ "from": "route",
+ "to": "exec",
+ "label": "no approval needed",
+ "role": "branch"
+ },
+ {
+ "id": "e_gate",
+ "from": "route",
+ "to": "gate",
+ "label": "needs approval",
+ "variant": "security"
+ },
+ {
+ "id": "e_approved",
+ "from": "gate",
+ "to": "exec",
+ "label": "approved",
+ "variant": "emphasis"
+ },
+ {
+ "id": "e_denied",
+ "from": "gate",
+ "to": "blocked",
+ "label": "denied",
+ "role": "error",
+ "variant": "security"
+ },
+ {
+ "id": "e_retry",
+ "from": "blocked",
+ "to": "plan",
+ "label": "retry",
+ "role": "return",
+ "variant": "dashed"
+ },
+ {
+ "id": "e_call",
+ "from": "exec",
+ "to": "provider",
+ "label": "API call"
+ },
+ {
+ "id": "e_result",
+ "from": "provider",
+ "to": "reply",
+ "label": "result",
+ "role": "return"
+ },
+ {
+ "id": "e_record",
+ "from": "provider",
+ "to": "trace",
+ "label": "record",
+ "role": "async",
+ "variant": "dashed"
+ }
+ ]
+}
diff --git a/test/workflow-route-order.test.mjs b/test/workflow-route-order.test.mjs
new file mode 100644
index 00000000..481b1c10
--- /dev/null
+++ b/test/workflow-route-order.test.mjs
@@ -0,0 +1,41 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+import { compileWorkflow } from '../archify/renderers/workflow/workflow-compiler.mjs';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const cli = path.resolve(__dirname, '..', 'archify', 'bin', 'archify.mjs');
+// An unedited first draft: the long "no approval needed" branch from the agent
+// lane to the execution lane was planned before the same-lane "denied" edge
+// and took the gap between Human Approval and Blocked, so "denied" could only
+// detour through the same corridor (composition/ambiguous-corridor on dev).
+const fixture = path.join(__dirname, 'fixtures', 'workflow-first-draft', 'agent-tool-call.workflow.json');
+const load = () => JSON.parse(fs.readFileSync(fixture, 'utf8'));
+
+test('an automatic edge squeezed into a shared corridor is planned first instead of failing the draft', () => {
+ const validation = spawnSync(process.execPath, [cli, 'validate', 'workflow', fixture, '--quality', 'showcase', '--json'], { encoding: 'utf8' });
+ assert.equal(validation.status, 0, validation.stdout + validation.stderr);
+});
+
+test('route-order feedback is deterministic and leaves the document untouched', () => {
+ const workflow = load();
+ const before = JSON.stringify(workflow);
+ const first = compileWorkflow({ workflow });
+ const second = compileWorkflow({ workflow: load() });
+ assert.equal(first.ok, true, first.error);
+ assert.equal(JSON.stringify(workflow), before);
+ assert.deepEqual(first.receipt.edges, second.receipt.edges);
+});
+
+test('pinned geometry keeps its own diagnostic: only automatic edges are re-ordered', () => {
+ const workflow = load();
+ workflow.edges.find((edge) => edge.id === 'e_auto').via = [[480.8, 243], [480.8, 449], [539.2, 449]];
+ workflow.edges.find((edge) => edge.id === 'e_denied').via = [[480.8, 367], [480.8, 325], [539.2, 325]];
+ const pinned = compileWorkflow({ workflow });
+ assert.equal(pinned.ok, false);
+ assert.deepEqual(pinned.diagnostics.map((diagnostic) => diagnostic.code), ['workflow/explicit-pin-conflict']);
+});