Skip to content

Commit e051ba7

Browse files
yangzhang75claude
andcommitted
fix(workflow-form): rebuild the cards on compilation only when it changed something they show
A compilation result rebuilt every card on the Form View, including the compilation a card's own write triggers: the controls were redrawn under the reader's eyes (the row buttons flickered after every Move), the keyboard focus went with the replaced node, 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. The canvas's property panel redraws only when an operator's schema changes. Follow the panel. A compilation rebuilds the cards only when it changed something they show: a card's operator schema (the column enums compilation writes into it), the set of inputs (an operator deleted makes its card broken), or an operator value a card does not hold (a co-editor's edit). A card's own edit, written back or still pending, is not a reason. The result picker and the shown results still follow the graph on every compilation. And before any rebuild, from any source, a card's unwritten edit is written, so a rebuild can no longer lose one. Part of the Form View feature (apache#8011), from the apache#8439 refinements; found on the apache#8761 demo. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 2082021 commit e051ba7

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
@@ -1054,6 +1054,22 @@ describe("WorkflowFormComponent", () => {
10541054
expect(formBindingService.writeValue).toHaveBeenCalled();
10551055
});
10561056

1057+
it("keeps a still-set value when a control emits an absent value before an edit", () => {
1058+
build(formViewWorkflow).ngOnInit();
1059+
formBindingService.readValue.mockReturnValue("seed");
1060+
renderOne("n_hvg");
1061+
const card = component.rendered[0];
1062+
const key = card.resolved.binding.id;
1063+
vi.useFakeTimers();
1064+
1065+
card.model[key] = undefined;
1066+
card.form.addControl(key, new FormControl(null));
1067+
vi.advanceTimersByTime(FORM_DEBOUNCE_TIME_MS + 50);
1068+
vi.useRealTimers();
1069+
1070+
expect(formBindingService.writeValue).not.toHaveBeenCalled();
1071+
});
1072+
10571073
it("ignores an unchanged form emission", () => {
10581074
build(formViewWorkflow).ngOnInit();
10591075
formBindingService.readValue.mockReturnValue("seed");
@@ -1418,14 +1434,293 @@ describe("WorkflowFormComponent", () => {
14181434
});
14191435

14201436
describe("keeping the inputs in step with the workflow", () => {
1421-
it("rebuilds the inputs when compilation reports a new state", async () => {
1437+
// Put op-1 on the graph with one exposed property holding `value`, and build its card.
1438+
const renderCard = (id: string, value: unknown) => {
1439+
h.hasOperatorIds.add("op-1");
1440+
formBindingService.resolveFields.mockReturnValue([resolved(id, id, { value })]);
1441+
formBindingService.readValue.mockReturnValue(value);
1442+
(component as any).readConfig();
1443+
};
1444+
const compiled = async () => {
1445+
h.compilationChanged.next("Succeeded");
1446+
await new Promise(r => setTimeout(r, FORM_DEBOUNCE_TIME_MS + 50));
1447+
};
1448+
// Compilation wrote column enums into op-1's schema: the card was built from a schema without them.
1449+
const changeSchema = () => {
1450+
(component as any).dynamicSchemaService = {
1451+
getDynamicSchema: () => ({ jsonSchema: { properties: { n_hvg: { enum: ["a", "b"] } } } }),
1452+
};
1453+
};
1454+
1455+
// A compilation changes nothing for most cards -- the one a card's own write triggers, for one.
1456+
// Rebuilding them anyway redrew every control, dropped the focus and lost a click made just
1457+
// before the rebuild; the canvas panel redraws only for a schema change, and so does the form.
1458+
it("leaves the cards alone when compilation changed nothing they show, as the canvas panel does", async () => {
14221459
build(formViewWorkflow).ngOnInit();
1460+
renderCard("n_hvg", "2000");
1461+
const card = component.rendered[0];
14231462
const rebuild = vi.spyOn(component as any, "readConfig");
1463+
const choices = vi.spyOn(component as any, "rebuildResultChoices");
14241464

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

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

14311726
it("rebuilds the inputs when a step is renamed, so each card's attribution follows", () => {
@@ -1441,11 +1736,12 @@ describe("WorkflowFormComponent", () => {
14411736

14421737
it("holds a rebuild while someone is typing and runs it once the focus leaves", async () => {
14431738
build(formViewWorkflow).ngOnInit();
1739+
renderCard("n_hvg", "2000");
14441740
const typing = vi.spyOn(component as any, "isTypingInTheForm").mockReturnValue(true);
14451741
const rebuild = vi.spyOn(component as any, "readConfig");
1742+
changeSchema();
14461743

1447-
h.compilationChanged.next("Succeeded");
1448-
await new Promise(r => setTimeout(r, FORM_DEBOUNCE_TIME_MS + 50));
1744+
await compiled();
14491745
expect(rebuild).not.toHaveBeenCalled();
14501746

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

0 commit comments

Comments
 (0)