Skip to content

Commit 3726dd7

Browse files
yangzhang75claude
andauthored
fix(workflow-form): rebuild the cards on compilation only when it changed something they show (apache#8845)
### What changes were proposed in this PR? Closes apache#8844. Part of the Form View feature (apache#8011); from the apache#8439 refinements. Found on the apache#8761 demo (apache#8843). A compilation result rebuilt every card on the Form View, including the compilation a card's own write triggers. Three things followed from that, all visible with a Projection's drag-reorder list (apache#8761) and with any tick box: the controls were redrawn under the reader's eyes after every click (the row buttons flicker); the keyboard focus went with the replaced node, so a keyboard reader landed on the body about a second after every Move up / Move down; and a click made in the last 150 ms before the rebuild was lost, since the rebuild ended that card's pending debounced write and re-seeded the card from the operator, which still held the previous state (the row "jumps back"). The canvas's property panel redraws only when an operator's schema changes. The form now does the same: - A compilation rebuilds the cards only when it changed something they show: a card's operator schema (the column enums compilation writes into it, which turn an attribute box into a dropdown), the set of inputs (an operator deleted on the canvas makes its card broken; a property exposed or removed), or an operator value a card does not hold (a co-editor's edit), including the operator's other properties, which a card is seeded with as the context a custom widget reads to decide what it shows (the HuggingFace picker reads `task`): nothing but a rebuild re-seeds those, so a sibling changed on the canvas or by a co-editor would have left the card showing the old one (Copilot). Every property an input is bound to is left out of that context, this card's own and any other card's on the same step, since each is compared against the card that shows it: counting them as context too would make one card's edit a change to every other card of that step, and a form with two inputs from one step would rebuild wholesale on every compilation its own typing triggers -- the churn this PR exists to stop (Copilot). The keys are compared sorted, so a rewrite that only reorders them is not a change, and a card's own edit, written back or still on its way, is not a reason: the two agree once it is written. The result picker and the shown results still follow the graph on every compilation. - Before any rebuild, from any source, a card's unwritten edit is written, so a rebuild can no longer lose one. - An empty string and an absent value count as the same value in the comparison (formly's blank default for a property the operator never had), so such a card is not rebuilt on every compilation. ### Any related issues, documentation, discussions? Closes apache#8844. The drag-reorder list apache#8843 (apache#8761) brings to the form is where this is plain to see: every drop writes and compiles, and the rows redraw about a second later; a second drop made just before that redraw is undone. The cause is the form's own compile handling, present since the cards were first built from the compiled schema; on main alone a tick box goes through the same redraw, focus drop and lost second click, which take a keyboard or a stopwatch to notice. The Before/After below is therefore recorded on top of apache#8843 (before: apache#8843 alone; after: apache#8843 plus this); this PR does not depend on apache#8843 and applies to main as is. Known limitation, unchanged: a card still shows a co-editor's edit of the same property only after the rebuild that edit's compilation triggers (tracked as a follow-up). ### How was this PR tested? Unit tests (vitest), the form's spec: a compilation that changes nothing a card shows leaves the cards alone (same card objects) while the result picker is still refreshed; a changed operator schema, an operator gone from the graph, an operator value changed under a card (a co-editor's edit), another property of a card's operator changed (the widget context), a changed set of inputs (one exposed; a shown one turned broken) each rebuild; the same properties in another order do not, nor does a change to the card's own property while its edit is still on its way, nor one card's edit when two inputs come from the same step (both card objects survive it); an author's broken card is left alone; the card's own edit does not rebuild while pending nor once written; formly's blank and an absent value count as the same; an unwritten edit is written, once, before a rebuild from any source replaces the card; a read-only viewer's card has no write to flush; the typing hold still holds a needed rebuild until the focus leaves. Each guard was deletion-checked (every compilation rebuilding; the schema check, the context check, the exclusion of every bound property from it, that the exclusion covers the other cards of the step and not only this one, the context's key sort, the pending check, the broken-set check, the blank normalisation, the pre-rebuild flush, the pending mark, the pending reset and the result-picker refresh each removed; a broken card checked against a schema) and turned a named test red. eslint, prettier and the production (AOT) build pass; the full frontend suite passes; every statement, function and branch this PR adds is covered. Checked on a flag-on instance (main-demo; before = main, and main + apache#8843; after = the same plus this), a Projection's columns and its Drop Option exposed on the form. The tick box on main: 0.3 s after a tick the box still has the focus; 2.8 s later it is a new DOM node and the focus is on the body; two ticks 700 ms apart end in the state after the first (the second undone). With this change the box is the same node three seconds later, still focused, and both ticks land. The drag-reorder rows on main + apache#8843: after a drop the rows are replaced about a second later; with this change they are the same nodes, and the order still reaches a canvas tab open beside the form within a second, a drop made on the canvas panel still reaches the form (a value the card does not hold rebuilds it), and the saved workflow carries the order. #### Video **Before** https://github.com/user-attachments/assets/eac466c3-c0c9-4969-b39a-a6da41ec54ff **After** https://github.com/user-attachments/assets/3ecd6342-7d6b-405c-bd83-f45415c2bcba ### Was this PR authored or co-authored using generative AI tooling? Yes. Generated-by: Claude Code (Claude Fable 5.1, Anthropic). Co-authored with Claude, reviewed line by line by the author before submission. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 57faa6b commit 3726dd7

2 files changed

Lines changed: 470 additions & 34 deletions

File tree

‎frontend/src/app/workspace/component/workflow-form/workflow-form.component.spec.ts‎

Lines changed: 302 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,22 @@ describe("WorkflowFormComponent", () => {
10701070
expect(formBindingService.writeValue).toHaveBeenCalled();
10711071
});
10721072

1073+
it("keeps a still-set value when a control emits an absent value before an edit", () => {
1074+
build(formViewWorkflow).ngOnInit();
1075+
formBindingService.readValue.mockReturnValue("seed");
1076+
renderOne("n_hvg");
1077+
const card = component.rendered[0];
1078+
const key = card.resolved.binding.id;
1079+
vi.useFakeTimers();
1080+
1081+
card.model[key] = undefined;
1082+
card.form.addControl(key, new FormControl(null));
1083+
vi.advanceTimersByTime(FORM_DEBOUNCE_TIME_MS + 50);
1084+
vi.useRealTimers();
1085+
1086+
expect(formBindingService.writeValue).not.toHaveBeenCalled();
1087+
});
1088+
10731089
it("ignores an unchanged form emission", () => {
10741090
build(formViewWorkflow).ngOnInit();
10751091
formBindingService.readValue.mockReturnValue("seed");
@@ -1434,14 +1450,293 @@ describe("WorkflowFormComponent", () => {
14341450
});
14351451

14361452
describe("keeping the inputs in step with the workflow", () => {
1437-
it("rebuilds the inputs when compilation reports a new state", async () => {
1453+
// Put op-1 on the graph with one exposed property holding `value`, and build its card.
1454+
const renderCard = (id: string, value: unknown) => {
1455+
h.hasOperatorIds.add("op-1");
1456+
formBindingService.resolveFields.mockReturnValue([resolved(id, id, { value })]);
1457+
formBindingService.readValue.mockReturnValue(value);
1458+
(component as any).readConfig();
1459+
};
1460+
const compiled = async () => {
1461+
h.compilationChanged.next("Succeeded");
1462+
await new Promise(r => setTimeout(r, FORM_DEBOUNCE_TIME_MS + 50));
1463+
};
1464+
// Compilation wrote column enums into op-1's schema: the card was built from a schema without them.
1465+
const changeSchema = () => {
1466+
(component as any).dynamicSchemaService = {
1467+
getDynamicSchema: () => ({ jsonSchema: { properties: { n_hvg: { enum: ["a", "b"] } } } }),
1468+
};
1469+
};
1470+
1471+
// A compilation changes nothing for most cards -- the one a card's own write triggers, for one.
1472+
// Rebuilding them anyway redrew every control, dropped the focus and lost a click made just
1473+
// before the rebuild; the canvas panel redraws only for a schema change, and so does the form.
1474+
it("leaves the cards alone when compilation changed nothing they show, as the canvas panel does", async () => {
14381475
build(formViewWorkflow).ngOnInit();
1476+
renderCard("n_hvg", "2000");
1477+
const card = component.rendered[0];
14391478
const rebuild = vi.spyOn(component as any, "readConfig");
1479+
const choices = vi.spyOn(component as any, "rebuildResultChoices");
14401480

1441-
h.compilationChanged.next("Succeeded");
1442-
await new Promise(r => setTimeout(r, FORM_DEBOUNCE_TIME_MS + 50));
1481+
await compiled();
1482+
1483+
expect(rebuild).not.toHaveBeenCalled();
1484+
expect(component.rendered[0]).toBe(card);
1485+
// The result picker still follows the graph the compilation came from.
1486+
expect(choices).toHaveBeenCalledTimes(1);
1487+
});
1488+
1489+
it("rebuilds the inputs when compilation changed a card's operator schema (the column enums)", async () => {
1490+
build(formViewWorkflow).ngOnInit();
1491+
renderCard("n_hvg", "2000");
1492+
const rebuild = vi.spyOn(component as any, "readConfig");
1493+
changeSchema();
1494+
1495+
await compiled();
1496+
1497+
expect(rebuild).toHaveBeenCalledTimes(1);
1498+
});
1499+
1500+
it("rebuilds the inputs when a card's operator left the graph", async () => {
1501+
build(formViewWorkflow).ngOnInit();
1502+
renderCard("n_hvg", "2000");
1503+
const rebuild = vi.spyOn(component as any, "readConfig");
1504+
h.hasOperatorIds.delete("op-1");
1505+
1506+
await compiled();
1507+
1508+
expect(rebuild).toHaveBeenCalledTimes(1);
1509+
});
1510+
1511+
it("rebuilds the inputs when an operator's value changed under a card (a co-editor's edit)", async () => {
1512+
build(formViewWorkflow).ngOnInit();
1513+
renderCard("n_hvg", "2000");
1514+
const rebuild = vi.spyOn(component as any, "readConfig");
1515+
formBindingService.readValue.mockReturnValue("3000");
1516+
1517+
await compiled();
1518+
1519+
expect(rebuild).toHaveBeenCalledTimes(1);
1520+
});
1521+
1522+
// A card is seeded with its operator's other properties as widget context (the HuggingFace
1523+
// picker reads `task` to decide which models to load). Nothing but a rebuild re-seeds that, so
1524+
// a change the canvas or a co-editor makes to a sibling has to count as a change the card shows.
1525+
it("rebuilds the inputs when another property of a card's operator changed (the widget context)", async () => {
1526+
build(formViewWorkflow).ngOnInit();
1527+
h.graphOperators.push({
1528+
operatorID: "op-1",
1529+
operatorType: "HuggingFace",
1530+
operatorProperties: { n_hvg: "2000", task: "image-classification" },
1531+
});
1532+
renderCard("n_hvg", "2000");
1533+
const rebuild = vi.spyOn(component as any, "readConfig");
1534+
h.graphOperators[0].operatorProperties = { n_hvg: "2000", task: "text-generation" };
1535+
1536+
await compiled();
1537+
1538+
expect(rebuild).toHaveBeenCalledTimes(1);
1539+
});
1540+
1541+
// Two inputs from one step: each card's own property is the other card's sibling, so counting
1542+
// bound properties as context would make one card's edit rebuild the other -- and with it the
1543+
// card being edited, which is the churn this PR exists to stop (Copilot).
1544+
it("leaves both cards alone when one of two inputs from the same step is edited", async () => {
1545+
build(formViewWorkflow).ngOnInit();
1546+
h.hasOperatorIds.add("op-1");
1547+
h.graphOperators.push({
1548+
operatorID: "op-1",
1549+
operatorType: "Projection",
1550+
operatorProperties: { attributes: ["a"], isDrop: false },
1551+
});
1552+
h.formlyJsonschema.toFieldConfig = () => ({
1553+
fieldGroup: [
1554+
{ key: "attributes", props: { label: "Columns" } },
1555+
{ key: "isDrop", props: { label: "Drop" } },
1556+
],
1557+
});
1558+
formBindingService.resolveFields.mockReturnValue([
1559+
resolved("attributes", "attributes", { value: ["a"] }),
1560+
resolved("isDrop", "isDrop", { value: false }),
1561+
]);
1562+
formBindingService.readValue.mockImplementation((_op: string, key: string) =>
1563+
key === "attributes" ? ["a"] : false
1564+
);
1565+
(component as any).readConfig();
1566+
expect(component.rendered).toHaveLength(2);
1567+
const cards = [...component.rendered];
1568+
const rebuild = vi.spyOn(component as any, "readConfig");
1569+
1570+
// One card's edit reaches the operator, and the compilation it triggers comes back.
1571+
h.graphOperators[0].operatorProperties = { attributes: ["a", "b"], isDrop: false };
1572+
formBindingService.readValue.mockImplementation((_op: string, key: string) =>
1573+
key === "attributes" ? ["a", "b"] : false
1574+
);
1575+
cards[0].model["attributes"] = ["a", "b"];
1576+
1577+
await compiled();
14431578

1444-
expect(rebuild).toHaveBeenCalled();
1579+
expect(rebuild).not.toHaveBeenCalled();
1580+
expect(component.rendered[0]).toBe(cards[0]);
1581+
expect(component.rendered[1]).toBe(cards[1]);
1582+
});
1583+
1584+
it("leaves the cards alone when the operator's properties only came back in another order", async () => {
1585+
build(formViewWorkflow).ngOnInit();
1586+
h.graphOperators.push({
1587+
operatorID: "op-1",
1588+
operatorType: "HuggingFace",
1589+
operatorProperties: { n_hvg: "2000", task: "image-classification", device: "cpu" },
1590+
});
1591+
renderCard("n_hvg", "2000");
1592+
const rebuild = vi.spyOn(component as any, "readConfig");
1593+
// The same properties, the two siblings swapped: a rewrite that reorders keys is not a change.
1594+
h.graphOperators[0].operatorProperties = { device: "cpu", task: "image-classification", n_hvg: "2000" };
1595+
1596+
await compiled();
1597+
1598+
expect(rebuild).not.toHaveBeenCalled();
1599+
});
1600+
1601+
// Every bound property is compared against the card that shows it, so none of them is context:
1602+
// the card's own stays the card's to resolve while its edit is on its way.
1603+
it("leaves the card's own property out of the context, so a pending edit still wins", async () => {
1604+
build(formViewWorkflow).ngOnInit();
1605+
h.graphOperators.push({
1606+
operatorID: "op-1",
1607+
operatorType: "HuggingFace",
1608+
operatorProperties: { n_hvg: "2000", task: "image-classification" },
1609+
});
1610+
renderCard("n_hvg", "2000");
1611+
const rebuild = vi.spyOn(component as any, "readConfig");
1612+
component.rendered[0].pendingWrite = true;
1613+
h.graphOperators[0].operatorProperties = { n_hvg: "9999", task: "image-classification" };
1614+
formBindingService.readValue.mockReturnValue("9999");
1615+
1616+
await compiled();
1617+
1618+
expect(rebuild).not.toHaveBeenCalled();
1619+
});
1620+
1621+
// The graph throws for an operator that is gone, so the context is read behind a hasOperator
1622+
// guard. A card whose operator went is already a changed set of inputs, so this is the guard
1623+
// speaking for a state the rebuild check does not otherwise reach.
1624+
it("reads no context for an operator that is no longer on the graph", () => {
1625+
build(formViewWorkflow).ngOnInit();
1626+
1627+
expect((component as any).contextKeyFor("gone", "n_hvg")).toBe("[]");
1628+
});
1629+
1630+
it("takes formly's blank and an absent operator value as the same value", () => {
1631+
build(formViewWorkflow).ngOnInit();
1632+
renderCard("n_hvg", undefined);
1633+
const card = component.rendered[0];
1634+
1635+
card.model[card.resolved.binding.id] = "";
1636+
expect((component as any).cardsNeedRebuild()).toBe(false);
1637+
1638+
card.model[card.resolved.binding.id] = undefined;
1639+
formBindingService.readValue.mockReturnValue("");
1640+
expect((component as any).cardsNeedRebuild()).toBe(false);
1641+
});
1642+
1643+
it("rebuilds the inputs when the set of inputs changed (one more exposed)", async () => {
1644+
build(formViewWorkflow).ngOnInit();
1645+
renderCard("n_hvg", "2000");
1646+
const rebuild = vi.spyOn(component as any, "readConfig");
1647+
formBindingService.resolveFields.mockReturnValue([
1648+
resolved("n_hvg", "n_hvg", { value: "2000" }),
1649+
resolved("tags", "tags", { value: [] }),
1650+
]);
1651+
1652+
await compiled();
1653+
1654+
expect(rebuild).toHaveBeenCalledTimes(1);
1655+
});
1656+
1657+
it("rebuilds the inputs when a shown card's operator was deleted (the card turns broken)", async () => {
1658+
build(formViewWorkflow).ngOnInit();
1659+
component.authoring = true;
1660+
renderCard("n_hvg", "2000");
1661+
const rebuild = vi.spyOn(component as any, "readConfig");
1662+
// Same input, same id, same value on the operator; only its brokenReason is new.
1663+
formBindingService.resolveFields.mockReturnValue([
1664+
resolved("n_hvg", "n_hvg", { value: "2000", brokenReason: "The step was removed" }),
1665+
]);
1666+
1667+
await compiled();
1668+
1669+
expect(rebuild).toHaveBeenCalledTimes(1);
1670+
});
1671+
1672+
it("leaves an author's broken card alone: it has no schema to follow", async () => {
1673+
build(formViewWorkflow).ngOnInit();
1674+
component.authoring = true;
1675+
h.hasOperatorIds.add("op-1");
1676+
formBindingService.resolveFields.mockReturnValue([
1677+
resolved("n_hvg", "n_hvg", { value: "2000", brokenReason: "The step was removed" }),
1678+
]);
1679+
(component as any).readConfig();
1680+
const rebuild = vi.spyOn(component as any, "readConfig");
1681+
1682+
await compiled();
1683+
1684+
expect(rebuild).not.toHaveBeenCalled();
1685+
});
1686+
1687+
it("does not rebuild for the card's own edit still on its way to the operator, nor once it is written", () => {
1688+
build(formViewWorkflow).ngOnInit();
1689+
renderCard("n_hvg", "2000");
1690+
const card = component.rendered[0];
1691+
const key = card.resolved.binding.id;
1692+
vi.useFakeTimers();
1693+
1694+
// The first change marks the edit pending; the operator still holds the old value.
1695+
card.model[key] = "2500";
1696+
card.form.addControl(key, new FormControl("2500"));
1697+
card.form.markAsDirty();
1698+
expect(card.pendingWrite).toBe(true);
1699+
expect((component as any).cardsNeedRebuild()).toBe(false);
1700+
1701+
// Written after the debounce: the operator now holds it, so the two agree.
1702+
vi.advanceTimersByTime(FORM_DEBOUNCE_TIME_MS + 50);
1703+
vi.useRealTimers();
1704+
expect(formBindingService.writeValue).toHaveBeenCalledWith(card.resolved.binding, "2500");
1705+
expect(card.pendingWrite).toBe(false);
1706+
formBindingService.readValue.mockReturnValue("2500");
1707+
expect((component as any).cardsNeedRebuild()).toBe(false);
1708+
});
1709+
1710+
it("writes a card's unwritten edit before a rebuild replaces the card, so the rebuild cannot lose it", () => {
1711+
build(formViewWorkflow).ngOnInit();
1712+
renderCard("n_hvg", "2000");
1713+
const card = component.rendered[0];
1714+
const key = card.resolved.binding.id;
1715+
vi.useFakeTimers();
1716+
card.model[key] = "2500";
1717+
card.form.addControl(key, new FormControl("2500"));
1718+
card.form.markAsDirty();
1719+
expect(formBindingService.writeValue).not.toHaveBeenCalled();
1720+
1721+
// A rebuild from any source (a step renamed, the definition edited) before the debounce fires.
1722+
(component as any).readConfig();
1723+
vi.advanceTimersByTime(FORM_DEBOUNCE_TIME_MS + 50);
1724+
vi.useRealTimers();
1725+
1726+
expect(formBindingService.writeValue).toHaveBeenCalledTimes(1);
1727+
expect(formBindingService.writeValue).toHaveBeenCalledWith(card.resolved.binding, "2500");
1728+
expect(card.pendingWrite).toBe(false);
1729+
});
1730+
1731+
it("has no write to flush on a read-only viewer's card", () => {
1732+
build({ ...formViewWorkflow, readonly: true }).ngOnInit();
1733+
renderCard("n_hvg", "2000");
1734+
const card = component.rendered[0];
1735+
card.pendingWrite = true;
1736+
1737+
expect(card.flushWrite).toBeUndefined();
1738+
expect(() => (component as any).readConfig()).not.toThrow();
1739+
expect(formBindingService.writeValue).not.toHaveBeenCalled();
14451740
});
14461741

14471742
it("rebuilds the inputs when a step is renamed, so each card's attribution follows", () => {
@@ -1457,11 +1752,12 @@ describe("WorkflowFormComponent", () => {
14571752

14581753
it("holds a rebuild while someone is typing and runs it once the focus leaves", async () => {
14591754
build(formViewWorkflow).ngOnInit();
1755+
renderCard("n_hvg", "2000");
14601756
const typing = vi.spyOn(component as any, "isTypingInTheForm").mockReturnValue(true);
14611757
const rebuild = vi.spyOn(component as any, "readConfig");
1758+
changeSchema();
14621759

1463-
h.compilationChanged.next("Succeeded");
1464-
await new Promise(r => setTimeout(r, FORM_DEBOUNCE_TIME_MS + 50));
1760+
await compiled();
14651761
expect(rebuild).not.toHaveBeenCalled();
14661762

14671763
// The cursor leaves the field: the held rebuild runs, once. Held rather than dropped, or the

0 commit comments

Comments
 (0)