{"record":{"id":"348c54c8748f366d","repo":"Hmbown/CodeWhale","slug":"codewhale-terminal-metadata-did-not-immediately-precede-done","errorCode":null,"errorMessage":"Codewhale terminal metadata did not immediately precede done","messagePattern":"Codewhale terminal metadata did not immediately precede done","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"integrations/verifiers-codewhale/codewhale_harness/harness.py","lineNumber":384,"sourceCode":"        event_type = event.get(\"type\")\n        if event_type not in _EVENT_TYPES:\n            raise RuntimeError(\"Codewhale stream-json contained an unknown event type\")\n        counts[event_type] += 1\n        ordered_types.append(event_type)\n        if event_type == \"metadata\":\n            if terminal is not None:\n                raise RuntimeError(\"Codewhale emitted more than one terminal metadata receipt\")\n            meta = event.get(\"meta\")\n            if not isinstance(meta, dict) or meta.get(\"receipt_kind\") != \"terminal\":\n                raise RuntimeError(\"Codewhale metadata event was not a terminal receipt\")\n            terminal = _bounded_terminal(meta)\n\n    if terminal is None:\n        raise RuntimeError(\"Codewhale stream-json omitted terminal metadata\")\n    if counts[\"done\"] != 1 or not ordered_types or ordered_types[-1] != \"done\":\n        raise RuntimeError(\"Codewhale stream-json did not end with exactly one done event\")\n    if ordered_types[-2:-1] != [\"metadata\"]:\n        raise RuntimeError(\"Codewhale terminal metadata did not immediately precede done\")\n    for field in [\"binary_sha256\", \"prompt_sha256\"]:\n        if not isinstance(terminal.get(field), str) or not _SHA256.fullmatch(\n            terminal[field]\n        ):\n            raise RuntimeError(f\"Codewhale terminal receipt omitted a valid {field}\")\n    return {\n        \"schema\": STREAM_SCHEMA,\n        \"schema_version\": STREAM_SCHEMA_VERSION,\n        \"events\": dict(sorted(counts.items())),\n        \"terminal\": terminal,\n    }\n","sourceCodeStart":366,"sourceCodeEnd":396,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/integrations/verifiers-codewhale/codewhale_harness/harness.py#L366-L396","documentation":"The harness enforces an exact terminal sequence: the single `metadata` (terminal receipt) event must be the event immediately before the final `done` event. This error means the ordering was wrong — e.g. a non-metadata event sits between the terminal receipt and done, or metadata appears earlier in the stream.","triggerScenarios":"`ordered_types[-2:-1] != [\"metadata\"]` after the done checks pass (harness.py:384): the stream ends with done and contains a terminal metadata receipt, but something like a final item/text event was emitted between them.","commonSituations":"A binary or wrapper that flushes a final output item after the receipt; reordered async event emission; a fixture with events in the wrong order; concatenating streams from multiple runs.","solutions":["Inspect the captured event order and identify the event emitted between metadata and done","Fix the producing binary/wrapper to emit the terminal receipt last, immediately before done","Update test fixtures to the canonical order [.., \"metadata\", \"done\"]","If an extra final event is intentional, it must move before the metadata receipt"],"exampleFix":"// before\n{\"type\":\"metadata\",\"meta\":{...}}\n{\"type\":\"item\",\"text\":\"final\"}\n{\"type\":\"done\"}\n// after\n{\"type\":\"item\",\"text\":\"final\"}\n{\"type\":\"metadata\",\"meta\":{...}}\n{\"type\":\"done\"}","handlingStrategy":"validation","validationCode":"types = [e.get(\"type\") for e in events]\nassert types[-2:] == [\"metadata\", \"done\"], f\"terminal sequence is {types[-2:]}, expected ['metadata','done']\"","typeGuard":null,"tryCatchPattern":"try:\n    receipt = _parse_stream_receipt(events)\nexcept RuntimeError as e:\n    if \"immediately precede done\" in str(e):\n        log.error(\"event emitted between terminal receipt and done; reorder emission\")\n    raise","preventionTips":["Emit all output items before the terminal receipt","Keep the canonical order [.., metadata, done] in producers and fixtures","Avoid async emission that can reorder final events","Add an ordering assertion in producer tests"],"tags":["python","stream-parsing","event-ordering","verifier-harness"],"backgroundTag":"unexpected-response-shape","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}