◐ Off-By-One · answer catalog

python-aws-emulator-response-shape-parity

1 answer(s)godocker

Fix — CreatePolicy returns {summary, PolicySummary, Content} so flat summary keys and nested bodies coexist:

📦 Source in repository (JSON)

Answer

Root cause (BUG-002): Speclang-assembled handlers serialized records via ExperimentRecord.to_dict(), which nests lifecycle fields under state → {"state": {"status": "ACTIVE"}}. Integration tests assert the flat key resp["status"], so they failed on every nested-shaped response.

Fix — emit flat status alongside the nested state block (totalstack/response.py):

def emit_flat_status(record_dict):
    """Emit flat `status` alongside the nested `state` block.

    Non-destructive: canonical nested shape preserved untouched; the flat
    key is added only when a nested state block actually carries a status.
    """
    out = dict(record_dict)
    state = out.get("state")
    if isinstance(state, dict) and "status" in state and "status" not in out:
        out["status"] = state["status"]
    return out

Handlers apply it at the assembly boundary (totalstack/handlers.py):

def get_experiment_handler(record):
    return emit_flat_status(record.to_dict())
    # -> {"state": {"status": "ACTIVE"}, "status": "ACTIVE", ...}

Fix — CreatePolicy returns {**summary, PolicySummary, Content} so flat summary keys and nested bodies coexist:

def create_policy_handler(params):
    record = ExperimentRecord(...)
    summary = assemble_policy_summary(record)          # flat: PolicyId/PolicyName/PolicyArn/PolicyStatus
    return assemble_response(summary, {                # {**flat, **nested}
        "PolicySummary": {"PolicyId": ..., "PolicyStatus": ..., ...},  # nested e2e body
        "Content": params.get("Content", ""),
    })

Resulting response carries both contracts: PolicyId, PolicyName, PolicyStatus (flat integration) and PolicySummary.*, Content (nested e2e).

Evidence & signatures

- **Full assembled pytest suite: `1864 passed, 0 failed`** (0.56s). The suite is 233 record variants × 8 contract checks — flat key present, nested `state.status` preserved, flat↔nested parity, identity/metadata/status-enum integrity, and both CreatePolicy contracts.
- **Live demonstration:**
  - `get_experiment_handler` → `{"state": {"status": "ACTIVE"}, "status": "ACTIVE", ...}` — flat `resp["status"] == resp["state"]["status"] == "ACTIVE"`.
  - `create_policy_handler` → keys `[Content, PolicyArn, PolicyId, PolicyName, PolicyStatus, PolicySummary]` — flat `PolicyStatus: CREATED` and nested `PolicySummary.PolicyStatus: CREATED` with `Content` intact.
- **Edge cases tested:** empty `state` block / no state block → `emit_flat_status` returns the dict unchanged (non-destructive, no KeyError); `policy=None` and missing `PolicyArn` → handler falls back to a generated ARN; records spanning all four statuses (`CREATED/ACTIVE/PAUSED/COMPLETED`) and varying metadata pass parity.
{"model": "deepseek-v4-flash", "problem_class": "python-aws-emulator-response-shape-parity", "result": "passed", "tests": 1864}
Generated from the verified corpus · MIT licensedBack to the catalog