From 3b640bc4123d96e095c9049c9d84ae3bcfaea08d Mon Sep 17 00:00:00 2001 From: jaeone94 <89377375+jaeone94@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:52:25 +0900 Subject: [PATCH 1/2] feat: give DynamicGroup refusals their own rejection codes DynamicGroup count refusals used malformed_op (set_widget), apply_failed (add_node, named insert_workflow, definitions) or invalid_node_payload (positional insert_workflow), all shared with unrelated failures. Reject them as dynamic_group_count_out_of_range, and a count change under a declared widgets_values_form as dynamic_group_form_mismatch, so callers can count them by code. mint still throws a TypeError. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++-- docs/dynamic-groups.md | 4 +-- docs/multiplayer-schema.md | 9 +++--- src/applier.ts | 12 +++++--- src/dynamic-combos.ts | 4 +-- src/dynamic-groups.ts | 5 +++- test/dynamic-group.test.ts | 36 ++++++++++++++++++------ test/ka4-rejection-byte-identity.test.ts | 36 ++++++++++++++++++++++++ 8 files changed, 88 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5bdbf2c..527fff5 100644 --- a/README.md +++ b/README.md @@ -543,8 +543,10 @@ Rejection codes: `malformed_op`, `unknown_op`, `op_deferred`, `catalog_required`, `invalid_node_payload`, `unknown_widget`, `uncatalogued_widget_write`, `opaque_widgets`, `widget_out_of_range`, `input_slot_missing`, `output_slot_missing`, `not_a_subgraph`, -`interior_node_not_found`, `shared_definition_unforked`, `batch_aborted`, and -`apply_failed` for anything unexpected. Match on `code`, never on `message`. +`interior_node_not_found`, `shared_definition_unforked`, +`dynamic_group_count_out_of_range`, `dynamic_group_form_mismatch`, +`batch_aborted`, and `apply_failed` for anything unexpected. Match on `code`, +never on `message`. `uncatalogued_widget_write` means a NAME-KEYED widget write named a class the pinned catalog does not describe. `add_node` and `set_widget` (and `connect`'s diff --git a/docs/dynamic-groups.md b/docs/dynamic-groups.md index f7607a9..ac0b752 100644 --- a/docs/dynamic-groups.md +++ b/docs/dynamic-groups.md @@ -6,11 +6,11 @@ To regenerate `test/fixtures/dynamic-group.json` at that revision, load `tests/c `dynamic_groups` maps a row-count controller to `min`, `max`, relative `widgets`, and field `defaults`. Each row expands immediately after its controller under `..`. Remove any default rows already present in `widget_order` before expanding the saved count. -The catalog controls layout, not prompt validity. Saved rows above an advertised `max` are retained; CLI/core validation decides whether they can execute. Catalog bounds must be integers with `0 <= min <= max`. Counts must be integers from `min` to the allocation bound, and row-field names must address a row below that bound. The bound is shared by every group of an entry: `MAX_OVERFLOW_WIDGETS` divided by the total number of row fields across those groups. So all of an entry's rows together stay within `MAX_OVERFLOW_WIDGETS` slots, however many controllers the entry has. A count below `min` is refused because the frontend raises it to `min`, which would leave the document and canvas with different row counts. +The catalog controls layout, not prompt validity. Saved rows above an advertised `max` are retained; CLI/core validation decides whether they can execute. Catalog bounds must be integers with `0 <= min <= max`. Counts must be integers from `min` to the allocation bound, and row-field names must address a row below that bound. The bound is shared by every group of an entry: `MAX_OVERFLOW_WIDGETS` divided by the total number of row fields across those groups. So all of an entry's rows together stay within `MAX_OVERFLOW_WIDGETS` slots, however many controllers the entry has. A count below `min` is refused because the frontend raises it to `min`, which would leave the document and canvas with different row counts. A count outside this range is rejected `dynamic_group_count_out_of_range` on `set_widget` and on `add_node`/`insert_workflow` payloads, including their definitions; `mint` still throws a `TypeError`. A row-field name beyond the allocation bound is `unknown_widget`, like any other name the node does not have. Like dynamic-combo selections, count changes only change projection. Fields in hidden rows retain their name-keyed registers, and newly visible fields without a stored value show template defaults. This avoids receiver-side writes whose outcome depends on op arrival order (KA-2, KA-4). This is not a structural row operation: it does not remove a middle row, reindex surviving identities, or update connections and promotions. Agent row changes use whole-group replacement instead: the producer writes the count and every visible row field in one batch, keeping linked or promoted rows in place, so this package needs no structural operation. -An explicit `widgets_values_form` remains authoritative for names, occurrences, and projection order. A selector refresh of an array form keeps its declared row fields where the producer placed them. Supplied group metadata is used for count validation only: allocation bounds are checked, and a change from the addressed occurrence's stored count that requires replacing the declaration is refused before mutation. A catalog-free declaration cannot identify group controllers, so that path does not provide count safety. This PR does not introduce an op for changing producer-owned forms. +An explicit `widgets_values_form` remains authoritative for names, occurrences, and projection order. A selector refresh of an array form keeps its declared row fields where the producer placed them. Supplied group metadata is used for count validation only: allocation bounds are checked, and a change from the addressed occurrence's stored count that requires replacing the declaration is rejected `dynamic_group_form_mismatch` before mutation. A catalog-free declaration cannot identify group controllers, so that path does not provide count safety. This PR does not introduce an op for changing producer-owned forms. No wire key or op kind changes. New catalog content changes the catalog hash. Consumers must adopt this reader before receiving a DynamicGroup catalog; old documents remain tied to their original catalog and must be re-minted to adopt a new layout (KA-11, KA-12, FC-10). diff --git a/docs/multiplayer-schema.md b/docs/multiplayer-schema.md index 5342620..8fe4e58 100644 --- a/docs/multiplayer-schema.md +++ b/docs/multiplayer-schema.md @@ -2392,9 +2392,10 @@ A node carrying the field is **self-described**, and for it: statement for ARRAY forms only: a pinned dynamic-combo option child is also legal and a winning selector persists the recomputed realized order. Amendment A26 uses supplied `dynamic_groups` metadata only for count - validation: it checks the row-count bounds and refuses a change from the - addressed occurrence's stored count that would require replacing the - declaration; + validation: it refuses a count outside the row-count bounds + (`dynamic_group_count_out_of_range`) and a change from the addressed + occurrence's stored count that would require replacing the declaration + (`dynamic_group_form_mismatch`); - `project()` rebuilds `widgets_values` in the shape the producer serialized — an array of `order.length`, or the object in its original key order with each declared widget taking its live value and every other key its verbatim @@ -2600,4 +2601,4 @@ persists, so KA-11 does not require a layout bump or migration. An opt-in `dynamic_groups` catalog entry expands a saved row-count controller into indexed widget identities. These use the existing name-keyed registers; no op kind or on-document key changes. The new catalog hash requires coordinated reader adoption. Existing documents retain their original catalog and are re-minted to adopt the new layout. -Count writes change the projected layout, retaining hidden registers and applying fresh-row defaults only at read time. They do not delete rows or links. Explicit `widgets_values_form` declarations remain authoritative; supplied group metadata adds row-count bound validation (`min` to the allocation bound) and rejects a count change requiring a new declaration before mutation. This validation applies to the addressed occurrence in top-level, interior, and promoted writes. Without group metadata, the package cannot identify count fields; compatible producer/catalog/reader admission remains the caller's responsibility. See [DynamicGroup catalog support](dynamic-groups.md) for the producer contract, bounds, and fixture provenance. +Count writes change the projected layout, retaining hidden registers and applying fresh-row defaults only at read time. They do not delete rows or links. Explicit `widgets_values_form` declarations remain authoritative; supplied group metadata adds row-count bound validation (`min` to the allocation bound) and rejects a count change requiring a new declaration before mutation. This validation applies to the addressed occurrence in top-level, interior, and promoted writes. A count outside that range is rejected `dynamic_group_count_out_of_range`, whether it arrives as a `set_widget` value or as a saved count in an `add_node` or `insert_workflow` payload (including its definitions); a count change against a declared form is rejected `dynamic_group_form_mismatch`. Both are new rejection codes, so callers can count DynamicGroup refusals without parsing messages. A row-field name that no group row can address keeps `unknown_widget`, as any other unknown widget name does. Without group metadata, the package cannot identify count fields; compatible producer/catalog/reader admission remains the caller's responsibility. See [DynamicGroup catalog support](dynamic-groups.md) for the producer contract, bounds, and fixture provenance. diff --git a/src/applier.ts b/src/applier.ts index 8cca3e7..a02c891 100644 --- a/src/applier.ts +++ b/src/applier.ts @@ -155,7 +155,7 @@ import { type WireOp, } from "./types.js"; import { addInteriorLinkOrder, removeInteriorLinkOrder } from "./interior-link-order.js"; -import { groupCountRefusal, groupOf, isDynamicGroupField } from "./dynamic-groups.js"; +import { DynamicGroupCountError, groupCountRefusal, groupOf, isDynamicGroupField } from "./dynamic-groups.js"; import { optionOwnedWidgets, projectedLength, @@ -279,7 +279,10 @@ export function applyOps(doc: Y.Doc, ops: Op[], catalog?: WidgetCatalog, context }); } } catch (err) { - rejectRemainder(err, op, index); + // Raised from the shared layout code, which also serves mint and project. + rejectRemainder(err instanceof DynamicGroupCountError + ? new OpRejectedError("dynamic_group_count_out_of_range", err.message) + : err, op, index); return false; } return true; @@ -1150,6 +1153,7 @@ function applyInsertWorkflow(doc: Y.Doc, op: InsertWorkflowOp, catalog?: WidgetC nodeWrites.push([String(node.id), node.id, createNodeMap(node, declaresWidgetForm(node) ? undefined : widgetOrderForValues(entry, node.widgets_values))]); } catch (err) { + if (err instanceof DynamicGroupCountError) throw err; throw new OpRejectedError("invalid_node_payload", `insert_workflow(${node.type}): ${err instanceof Error ? err.message : String(err)}`); } } @@ -2028,11 +2032,11 @@ function validateWidgetOp(op: SetWidgetOp) { function validateDynamicGroupValue(node: Y.Map, catalog: WidgetCatalog | undefined, widget: string, occurrence: number, value: unknown): void { const entry = catalogEntry(catalog, String(node.get("type") ?? "")); const refusal = groupCountRefusal(entry, widget, value); - if (refusal) throw new OpRejectedError("malformed_op", refusal); + if (refusal) throw new DynamicGroupCountError(refusal); if (groupOf(entry, widget) && storedWidgetFormOf(node)) { const widgets = node.get("widgets"); if (widgets instanceof Y.Map && widgets.get(widgetStorageKey(widget, occurrence)) !== value) { - throw new OpRejectedError("malformed_op", "row count changes require an updated widgets_values_form declaration"); + throw new OpRejectedError("dynamic_group_form_mismatch", "row count changes require an updated widgets_values_form declaration"); } } } diff --git a/src/dynamic-combos.ts b/src/dynamic-combos.ts index c39698f..f9d553f 100644 --- a/src/dynamic-combos.ts +++ b/src/dynamic-combos.ts @@ -24,7 +24,7 @@ */ import * as Y from "yjs"; -import { assertDynamicGroupCatalog, fieldGroupOf, groupCountRefusal, groupLayout, groupOf, isDynamicGroupField } from "./dynamic-groups.js"; +import { assertDynamicGroupCatalog, DynamicGroupCountError, fieldGroupOf, groupCountRefusal, groupLayout, groupOf, isDynamicGroupField } from "./dynamic-groups.js"; import type { DynamicComboEntry, WidgetCatalogEntry } from "./types.js"; import { widgetOccurrenceAt, widgetStorageKey } from "./widget-identity.js"; @@ -137,7 +137,7 @@ export function widgetOrderForValues(entry: WidgetCatalogEntry | undefined, wv: for (const name of Object.keys(entry.dynamic_groups ?? {})) { if (!Object.hasOwn(named, name)) continue; const refusal = groupCountRefusal(entry, name, named[name]); - if (refusal) throw new TypeError(refusal); + if (refusal) throw new DynamicGroupCountError(refusal); } return expand(entry, combos ?? {}, (name) => (Object.hasOwn(named, name) ? named[name] : undefined)).order; } diff --git a/src/dynamic-groups.ts b/src/dynamic-groups.ts index aae8d99..0f1df95 100644 --- a/src/dynamic-groups.ts +++ b/src/dynamic-groups.ts @@ -1,6 +1,9 @@ import { MAX_OVERFLOW_WIDGETS } from "./limits.js"; import type { WidgetCatalogEntry } from "./types.js"; +/** A row count outside `min` to the allocation bound; `applyOps` rejects it `dynamic_group_count_out_of_range`. */ +export class DynamicGroupCountError extends TypeError {} + export function groupOf(entry: WidgetCatalogEntry | undefined, name: string) { const groups = entry?.dynamic_groups; return groups && Object.hasOwn(groups, name) ? groups[name] : undefined; @@ -122,7 +125,7 @@ export function groupLayout(entry: WidgetCatalogEntry, name: string, value: unkn if (!group) return undefined; const count = value === undefined ? group.min : value; const refusal = groupCountRefusal(entry, name, count); - if (refusal) throw new TypeError(refusal); + if (refusal) throw new DynamicGroupCountError(refusal); const order: string[] = []; const defaults = new Map(); defaults.set(name, group.min); diff --git a/test/dynamic-group.test.ts b/test/dynamic-group.test.ts index 58a2244..14a778a 100644 --- a/test/dynamic-group.test.ts +++ b/test/dynamic-group.test.ts @@ -2,7 +2,7 @@ import { readFileSync } from "node:fs"; import * as Y from "yjs"; import { describe, expect, it } from "vitest"; import { SCHEMA_VERSION as SCHEMA_VERSION_0310, project as project0310 } from "comfy-multi-player-v0310"; -import { SCHEMA_VERSION, applyOps, mint, project, type Op, type SetWidgetOp, type TopLevelSetWidgetOp, type WidgetCatalog, type WidgetCatalogEntry, type WorkflowJSON } from "../src/index.js"; +import { SCHEMA_VERSION, applyOps, mint, project, type Op, type SetWidgetOp, type TopLevelSetWidgetOp, type WidgetCatalog, type WidgetCatalogEntry, type WorkflowJSON, type WorkflowNode } from "../src/index.js"; import { canonicalize } from "./helpers.js"; import { assertRejectedWithoutMutation } from "./rejection-test-helpers.js"; @@ -123,7 +123,7 @@ describe("DynamicGroup catalog layout", () => { ...edit("loras", 1 - occurrence), widget_occurrence: occurrence, promoted: { value_index: occurrence, instance_path: ["1"], host_widgets_values: [1 - occurrence, 1 - occurrence, "tail"] }, } : { ...op, value: 1 - occurrence }; - assertRejectedWithoutMutation(workflow, invalid, "malformed_op", catalog, trailing); + assertRejectedWithoutMutation(workflow, invalid, "dynamic_group_form_mismatch", catalog, trailing); }); it.each([ @@ -252,7 +252,7 @@ describe("DynamicGroup catalog layout", () => { const workflow = structuredClone(composite.explicit_form_workflow); const before = structuredClone(workflow); assertRejectedWithoutMutation(workflow, composite.standalone_count_op, - "malformed_op", composite.catalog, { ...composite.op, op_id: "d".repeat(32) }); + "dynamic_group_form_mismatch", composite.catalog, { ...composite.op, op_id: "d".repeat(32) }); expect(workflow).toEqual(before); }); @@ -355,7 +355,7 @@ describe("DynamicGroup catalog layout", () => { }); it.each([-1, 1.5, "2", 1_000_000])("refuses unsafe row count %s before writing", (count) => { - assertRejectedWithoutMutation(cases.populated, edit("loras", count), "malformed_op", catalog, + assertRejectedWithoutMutation(cases.populated, edit("loras", count), "dynamic_group_count_out_of_range", catalog, edit("after", "must not run", 2)); }); @@ -372,7 +372,7 @@ describe("DynamicGroup catalog layout", () => { ] satisfies [string, WorkflowJSON, Op, Op][])("refuses %s counts below the group minimum", (_route, workflow, op, trailing) => { const minimumOne = structuredClone(catalog); minimumOne.types["DevToolsNodeWithDynamicGroup"]!.dynamic_groups!["loras"]!.min = 1; - assertRejectedWithoutMutation(workflow, op, "malformed_op", minimumOne, trailing); + assertRejectedWithoutMutation(workflow, op, "dynamic_group_count_out_of_range", minimumOne, trailing); }); it("refuses a saved count below the group minimum before minting", () => { @@ -404,7 +404,7 @@ describe("DynamicGroup catalog layout", () => { const workflow = structuredClone(cases.empty); delete workflow.nodes[0]!["widgets_values_named"]; workflow.nodes[0]!.widgets_values = [0, 0]; - assertRejectedWithoutMutation(workflow, edit("first", 2049), "malformed_op", sharedCatalog, edit("second", 1, 2)); + assertRejectedWithoutMutation(workflow, edit("first", 2049), "dynamic_group_count_out_of_range", sharedCatalog, edit("second", 1, 2)); assertRejectedWithoutMutation(workflow, edit("first.2048.x", 1), "unknown_widget", sharedCatalog, edit("second", 1, 2)); const doc = mint(workflow, sharedCatalog); @@ -420,6 +420,24 @@ describe("DynamicGroup catalog layout", () => { expect(() => mint(workflow, catalog)).toThrow(/row count/); }); + const definitionId = "11111111-1111-4111-8111-111111111111"; + it.each([ + ["add_node", (node: WorkflowNode) => ({ op: "add_node", node_id: 5, class_type: node.type, pos: [0, 0], node })], + ["insert_workflow", (node: WorkflowNode) => ({ op: "insert_workflow", workflow: { nodes: [node], links: [] } })], + ["definition insert_workflow", (node: WorkflowNode) => ({ op: "insert_workflow", workflow: { + nodes: [{ id: 6, type: definitionId, inputs: [], outputs: [] }], links: [], + definitions: { subgraphs: [{ id: definitionId, nodes: [node], links: [], inputs: [], outputs: [], widgets: [] }] }, + } })], + ] as const)("refuses positional and named %s payload counts outside the group range", (_label, payload) => { + const positional = ["head", -1, "A.safetensors", 1, true, "C.safetensors", 0.5, false, "tail"]; + for (const widgets_values of [positional, { before: "head", loras: -1, after: "tail" }]) { + const node: WorkflowNode = { id: 5, type: "DevToolsNodeWithDynamicGroup", inputs: [], outputs: [], widgets_values }; + const op = { ...payload(node), op_id: "c".repeat(32), actor: "agent:a", base_version: 1, stamp: [1, "agent:a"] } as unknown as Op; + assertRejectedWithoutMutation(cases.populated, op, "dynamic_group_count_out_of_range", catalog, + edit("after", "must not run", 2)); + } + }); + it.each([-1, 1.5, "2", 1_000_000])("refuses inactive named group count %s during mint", (count) => { const workflow = structuredClone(cases.empty); delete workflow.nodes[0]!["widgets_values_named"]; @@ -512,7 +530,7 @@ describe("DynamicGroup catalog layout", () => { expect(project(doc, catalog).nodes[0]!.widgets_values).toEqual([ "head", 2, "A.safetensors", 1, true, "C.safetensors", 0.7, false, "tail", ]); - assertRejectedWithoutMutation(workflow, edit("loras", 0), "malformed_op", catalog, + assertRejectedWithoutMutation(workflow, edit("loras", 0), "dynamic_group_form_mismatch", catalog, edit("after", "must not run", 2)); doc.destroy(); }); @@ -530,7 +548,7 @@ describe("DynamicGroup catalog layout", () => { host_widgets_values: ["head", count, "A.safetensors", 1, true, "C.safetensors", 0.5, false, "tail"], }, }; - assertRejectedWithoutMutation(workflow, op, "malformed_op", catalog, + assertRejectedWithoutMutation(workflow, op, count === 0 ? "dynamic_group_form_mismatch" : "dynamic_group_count_out_of_range", catalog, edit("after", "must not run", 2)); }); @@ -572,7 +590,7 @@ describe("DynamicGroup catalog layout", () => { "head", 2, "A.safetensors", 1, true, "C.safetensors", 0.7, false, "tail", ]); const invalid: Op = { ...edit("loras", -1), path: ["9", "1"], inner_widget: "loras" }; - assertRejectedWithoutMutation(workflow, invalid, "malformed_op", catalog, { ...op, op_id: "b".repeat(32) }); + assertRejectedWithoutMutation(workflow, invalid, "dynamic_group_count_out_of_range", catalog, { ...op, op_id: "b".repeat(32) }); doc.destroy(); }); }); diff --git a/test/ka4-rejection-byte-identity.test.ts b/test/ka4-rejection-byte-identity.test.ts index da9a5ee..8cc7a4e 100644 --- a/test/ka4-rejection-byte-identity.test.ts +++ b/test/ka4-rejection-byte-identity.test.ts @@ -700,12 +700,46 @@ function definitionConflictCase(): { doc: Y.Doc; op: Op; catalog?: WidgetCatalog }; } +const dynamicGroupFixture = JSON.parse(readFileSync(new URL("./fixtures/dynamic-group.json", import.meta.url), "utf8")) as { + catalog: WidgetCatalog; + cases: { populated: WorkflowJSON }; +}; + +/** `dynamic_group_count_out_of_range`: a DynamicGroup row count below the group minimum (A25). */ +function dynamicGroupCountCase(): { doc: Y.Doc; op: Op; catalog?: WidgetCatalog; code: string } { + const { catalog: groups, cases } = dynamicGroupFixture; + return { + doc: mint(cases.populated, groups), + op: { op: "set_widget", ...env(), node_id: 1, widget: "loras", value: -1 } as unknown as Op, + catalog: groups, + code: "dynamic_group_count_out_of_range", + }; +} + +/** `dynamic_group_form_mismatch`: a count change that would invalidate the node's declared `widgets_values_form` (A25). */ +function dynamicGroupFormCase(): { doc: Y.Doc; op: Op; catalog?: WidgetCatalog; code: string } { + const { catalog: groups, cases } = dynamicGroupFixture; + const workflow = structuredClone(cases.populated); + workflow.nodes[0]!.widgets_values_form = { order: [ + "before", "loras", "loras.0.lora_name", "loras.0.strength", "loras.0.enabled", + "loras.1.lora_name", "loras.1.strength", "loras.1.enabled", "after", + ] }; + return { + doc: mint(workflow, groups), + op: { op: "set_widget", ...env(), node_id: 1, widget: "loras", value: 1 } as unknown as Op, + catalog: groups, + code: "dynamic_group_form_mismatch", + }; +} + const FIXTURE_CASES = [ ["add_node without a catalog", catalogRequiredCase], ["promoted host write without a catalog", promotedCatalogRequiredCase], ["interior set_widget past the positional length", widgetOutOfRangeCase], ["interior set_widget into a definition two nodes instantiate", sharedDefinitionCase], ["define_subgraph whose root id collides with a nested definition", definitionConflictCase], + ["set_widget DynamicGroup count below the group minimum", dynamicGroupCountCase], + ["set_widget DynamicGroup count change under a declared form", dynamicGroupFormCase], ] as const; describe("KA-4: the rejection codes that need their own fixture", () => { @@ -756,6 +790,8 @@ const ALL_REJECTION_CODES = [ "interior_node_not_found", "shared_definition_unforked", "definition_conflict", + "dynamic_group_count_out_of_range", + "dynamic_group_form_mismatch", ] as const; // These require an already-consumed op_id or an intentionally deep payload; From b45ad96e1ac8f7ed1264241f8107d967767f8a11 Mon Sep 17 00:00:00 2001 From: jaeone94 <89377375+jaeone94@users.noreply.github.com> Date: Wed, 28 Oct 2026 01:40:58 +0900 Subject: [PATCH 2/2] fix: keep an impossible DynamicGroup catalog minimum apply_failed --- docs/dynamic-groups.md | 2 +- src/dynamic-groups.ts | 5 ++++- test/dynamic-group.test.ts | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/dynamic-groups.md b/docs/dynamic-groups.md index ac0b752..a49cc45 100644 --- a/docs/dynamic-groups.md +++ b/docs/dynamic-groups.md @@ -6,7 +6,7 @@ To regenerate `test/fixtures/dynamic-group.json` at that revision, load `tests/c `dynamic_groups` maps a row-count controller to `min`, `max`, relative `widgets`, and field `defaults`. Each row expands immediately after its controller under `..`. Remove any default rows already present in `widget_order` before expanding the saved count. -The catalog controls layout, not prompt validity. Saved rows above an advertised `max` are retained; CLI/core validation decides whether they can execute. Catalog bounds must be integers with `0 <= min <= max`. Counts must be integers from `min` to the allocation bound, and row-field names must address a row below that bound. The bound is shared by every group of an entry: `MAX_OVERFLOW_WIDGETS` divided by the total number of row fields across those groups. So all of an entry's rows together stay within `MAX_OVERFLOW_WIDGETS` slots, however many controllers the entry has. A count below `min` is refused because the frontend raises it to `min`, which would leave the document and canvas with different row counts. A count outside this range is rejected `dynamic_group_count_out_of_range` on `set_widget` and on `add_node`/`insert_workflow` payloads, including their definitions; `mint` still throws a `TypeError`. A row-field name beyond the allocation bound is `unknown_widget`, like any other name the node does not have. +The catalog controls layout, not prompt validity. Saved rows above an advertised `max` are retained; CLI/core validation decides whether they can execute. Catalog bounds must be integers with `0 <= min <= max`. Counts must be integers from `min` to the allocation bound, and row-field names must address a row below that bound. The bound is shared by every group of an entry: `MAX_OVERFLOW_WIDGETS` divided by the total number of row fields across those groups. So all of an entry's rows together stay within `MAX_OVERFLOW_WIDGETS` slots, however many controllers the entry has. A count below `min` is refused because the frontend raises it to `min`, which would leave the document and canvas with different row counts. A count outside this range is rejected `dynamic_group_count_out_of_range` on `set_widget` and on `add_node`/`insert_workflow` payloads, including their definitions; `mint` still throws a `TypeError`. A node with `widgets_values_form` keeps its declared values verbatim, so these payload and `mint` checks do not cover its counts. A catalog whose `min` exceeds the allocation bound is malformed and is rejected `apply_failed`. A row-field name beyond the allocation bound is `unknown_widget`, like any other name the node does not have. Like dynamic-combo selections, count changes only change projection. Fields in hidden rows retain their name-keyed registers, and newly visible fields without a stored value show template defaults. This avoids receiver-side writes whose outcome depends on op arrival order (KA-2, KA-4). This is not a structural row operation: it does not remove a middle row, reindex surviving identities, or update connections and promotions. Agent row changes use whole-group replacement instead: the producer writes the count and every visible row field in one batch, keeping linked or promoted rows in place, so this package needs no structural operation. diff --git a/src/dynamic-groups.ts b/src/dynamic-groups.ts index 0f1df95..7ac528c 100644 --- a/src/dynamic-groups.ts +++ b/src/dynamic-groups.ts @@ -97,7 +97,10 @@ export function assertDynamicGroupCatalog(entry: WidgetCatalogEntry): void { throw new TypeError(`DynamicGroup '${name}' must declare integer row bounds with 0 <= min <= max`); } } - for (const [name] of groups) { + for (const [name, group] of groups) { + if (group.min > maxRows(entry)) { + throw new TypeError(`DynamicGroup '${name}' minimum exceeds the widget allocation bound`); + } if (Object.hasOwn(entry.dynamic_combos ?? {}, name) || isDynamicGroupField(entry, name)) { throw new TypeError(`DynamicGroup controller '${name}' collides with another dynamic widget`); } diff --git a/test/dynamic-group.test.ts b/test/dynamic-group.test.ts index 14a778a..e6a672a 100644 --- a/test/dynamic-group.test.ts +++ b/test/dynamic-group.test.ts @@ -428,6 +428,8 @@ describe("DynamicGroup catalog layout", () => { nodes: [{ id: 6, type: definitionId, inputs: [], outputs: [] }], links: [], definitions: { subgraphs: [{ id: definitionId, nodes: [node], links: [], inputs: [], outputs: [], widgets: [] }] }, } })], + ["define_subgraph", (node: WorkflowNode) => ({ op: "define_subgraph", subgraph_id: definitionId, + subgraph_definition: { id: definitionId, nodes: [node], links: [], inputs: [], outputs: [], widgets: [] } })], ] as const)("refuses positional and named %s payload counts outside the group range", (_label, payload) => { const positional = ["head", -1, "A.safetensors", 1, true, "C.safetensors", 0.5, false, "tail"]; for (const widgets_values of [positional, { before: "head", loras: -1, after: "tail" }]) { @@ -438,6 +440,19 @@ describe("DynamicGroup catalog layout", () => { } }); + it("keeps a catalog minimum above the allocation bound apply_failed", () => { + const bigCatalog = structuredClone(catalog); + bigCatalog.types["Big"] = { + widget_order: ["count"], + dynamic_groups: { count: { min: 5000, max: 5000, widgets: ["x"], defaults: { x: 0 } } }, + }; + const node: WorkflowNode = { id: 5, type: "Big", inputs: [], outputs: [], widgets_values: [5000] }; + const op = { op: "add_node", node_id: 5, class_type: "Big", pos: [0, 0], node, + op_id: "d".repeat(32), actor: "agent:a", base_version: 1, stamp: [1, "agent:a"] } as unknown as Op; + assertRejectedWithoutMutation(cases.populated, op, "apply_failed", bigCatalog, edit("after", "must not run", 2)); + expect(() => mint({ ...cases.empty, nodes: [node] }, bigCatalog)).toThrow(/minimum exceeds the widget allocation bound/); + }); + it.each([-1, 1.5, "2", 1_000_000])("refuses inactive named group count %s during mint", (count) => { const workflow = structuredClone(cases.empty); delete workflow.nodes[0]!["widgets_values_named"];