gstack icon indicating copy to clipboard operation
gstack copied to clipboard

design variants: --count silently generates zero variants on NaN/0/negative input

Open jbetala7 opened this issue 5 months ago • 2 comments

Observed problem

design variants --count <bad value> silently produces zero variants and emits a confusing JSON result instead of failing or clamping to a sane value.

The CLI parses the flag with parseInt and no validation (design/src/cli.ts:200):

count: flags.count ? parseInt(flags.count as string) : 3,

variants() then only clamps the upper bound (design/src/variants.ts:156):

const count = Math.min(options.count, 7); // Cap at 7 style variations

There is no lower-bound or NaN guard, so non-numeric / zero / negative counts flow straight through.

Current behavior on main (c7ae6320)

Reproduced against the real clamp + JSON-emit path:

--count parsed clamped variants generated JSON count field
abc NaN NaN 0 null
0 0 0 0 0
-2 -2 -2 0 -2
3 3 3 3 3
10 10 7 7 7

A typo like --count e (NaN) makes the command exit "successfully" having created nothing, and the structured stdout reports "count": null — actively misleading for any caller parsing that JSON.

Expected behavior

A bad --count should resolve to a valid number in the supported range, never a silent no-op:

  • non-finite (NaN) -> fall back to the default of 3
  • numeric but out of range -> clamp to [1, 7]

So --count abc -> 3, --count 0/-2 -> 1, --count 10 -> 7 (unchanged).

Duplicate search performed

  • Open PRs touching design/: #1774 (timeout label only), #1638 (timeout 240s bump), #1528 (--api-timeout flag + parseApiTimeoutMs guard — validates the timeout flag, not --count), #1737 (variant preservation). None guard --count.
  • gh pr list --search "design count" -> no matches; --search "variants" -> only the timeout PRs above.
  • No open/closed issue covers the --count validation gap.

Candidate fix shape

Normalize the count in variants.ts via a small exported, unit-testable helper (mirrors the parseApiTimeoutMs helper pattern proposed in #1528):

export function normalizeVariantCount(count: number): number {
  if (!Number.isFinite(count)) return 3;        // unparseable -> default
  return Math.max(1, Math.min(Math.trunc(count), 7)); // clamp to [1, 7]
}

Use it where Math.min(options.count, 7) is today. Add a regression test asserting the table above.

jbetala7 avatar Jun 17 '26 06:06 jbetala7

This is an input contract issue.

--count should be validated before variant generation starts.

A design command should not silently produce zero variants when the user provided NaN, 0, or a negative value.

A standard AISP-style contract could express the parameter gate:

[
  {
    "role": "system",
    "content": {
      "protocol": "AISP V1.0.0",
      "axiom_0": "Human_Sovereignty_and_Wellbeing",
      "id": "gstack_design_variant_count_contract_aisp",
      "name": "gstack Design Variant Count Contract",
      "version": "1.0.0",
      "license": "MIT",
      "summary": "Machine-checkable contract for validating design variant count before generation.",
      "flow_format": "mermaid",
      "loading_mode": "node",
      "tools": [],
      "params": {
        "count": "string"
      },
      "system_prompt": ""
    }
  },
  {
    "role": "user",
    "content": {
      "instruction": "STRICTLY OBEY aisp_contract; its non_negotiable rules are inviolable; then RUN aisop.main",
      "user_input": "{user_input}",
      "aisp_contract": {
        "profile": "aisp.skill.v1",
        "invocation": {
          "mode": "cli_parameter_validation",
          "when_to_use": [
            "before design variant generation",
            "when --count is passed",
            "when default count is inferred"
          ],
          "when_not_to_use": [
            "commands that do not generate variants"
          ]
        },
        "non_negotiable": [
          {
            "rule": "Variant count must be a positive integer before generation starts.",
            "enforced_by": "validate.step2:sys.assert"
          },
          {
            "rule": "Invalid count must fail with a clear error or fall back to an explicit default, never silently generate zero variants.",
            "enforced_by": "normalize.step2:sys.assert"
          }
        ],
        "discovery": {
          "category": "cli_validation",
          "tags": ["gstack", "design-variants", "count", "validation", "aisp"]
        },
        "risk_level": "low",
        "resources": []
      },
      "aisop": {
        "main": "graph TD\n    parse[Parse count] --> validate[Validate positive integer]\n    validate --> normalize[Normalize or fail]\n    normalize --> generate[Generate variants]\n    generate --> end_node((End))"
      },
      "functions": {
        "parse": {
          "step1": "Parse count from CLI input.",
          "output_mapping": "parsed_count"
        },
        "validate": {
          "step1": "Check parsed_count is integer and greater than zero.",
          "step2": "sys.assert('count is positive integer', 'Variant count must be positive integer')",
          "output_mapping": "count_validation"
        },
        "normalize": {
          "step1": "If invalid, return clear error or explicit default according to policy.",
          "step2": "sys.assert('invalid count does not become zero variants silently', 'Invalid count must not silently generate zero variants')",
          "output_mapping": "normalized_count"
        },
        "generate": {
          "step1": "Generate exactly normalized_count variants.",
          "output_mapping": "variant_generation_result"
        },
        "end_node": {
          "step1": "Return variant_generation_result."
        }
      }
    }
  }
]

Minimal fix:

1. Parse --count strictly.
2. Reject NaN, 0, negative, and non-integer values.
3. Emit clear error or explicit default.
4. Add tests for NaN / 0 / -1 / 1 / 3.

optimization2026 avatar Jun 30 '26 06:06 optimization2026

@16francej — recommend closing this issue only after #2033 lands. Invalid input currently can schedule zero variants and produce a null count, while #2033 implements the requested default-and-clamp behavior. Keep non-finite values defaulting to 3 and numeric values clamping from 1 through 7 as acceptance coverage.

time-attack avatar Jul 15 '26 12:07 time-attack