{"record":{"id":"5ba97bb18ae42d5e","repo":"Hmbown/CodeWhale","slug":"codewhale-emitted-more-than-one-terminal-metadata-receipt","errorCode":null,"errorMessage":"Codewhale emitted more than one terminal metadata receipt","messagePattern":"Codewhale emitted more than one terminal metadata receipt","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"integrations/verifiers-codewhale/codewhale_harness/harness.py","lineNumber":373,"sourceCode":"            raise RuntimeError(\n                f\"Codewhale stream-json line {line_number} was not valid JSON\"\n            ) from error\n        if not isinstance(event, dict):\n            raise RuntimeError(\n                f\"Codewhale stream-json line {line_number} was not an object\"\n            )\n        if event.get(\"schema\") != STREAM_SCHEMA or event.get(\n            \"schema_version\"\n        ) != STREAM_SCHEMA_VERSION:\n            raise RuntimeError(\"Codewhale stream-json schema did not match v0.9.1\")\n        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,","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/integrations/verifiers-codewhale/codewhale_harness/harness.py#L355-L391","documentation":"The Codewhale verifier harness parses the agent's stream-json output in `_parse_stream_receipt` and requires exactly one terminal metadata receipt. This RuntimeError is raised when a second `metadata` event with `receipt_kind == \"terminal\"` appears after one was already captured, i.e. the stream contains duplicate terminal receipts, which would make the attested terminal ambiguous.","triggerScenarios":"`launch()` runs a Codewhale binary whose stream-json emits two or more metadata events that both carry `receipt_kind: \"terminal\"`; the loop at harness.py:373 sees `terminal is not None` on the second occurrence and raises.","commonSituations":"Running a stale or patched Codewhale binary that emits a terminal receipt per turn instead of per session; wrapping the agent so its output is concatenated (e.g. retry/restart inside the stream) so two receipts appear in one stream; a hand-crafted or mocked stream for tests containing duplicated metadata lines.","solutions":["Inspect the stream-json output and find both metadata events; determine why the binary emitted two terminal receipts","Upgrade or rebuild the Codewhale binary to the version the harness expects (one terminal receipt per stream)","If retries/restarts occur, restart the stream parse per attempt instead of feeding events from multiple runs into one `_parse_stream_receipt` call","Fix any test fixture or mock stream so it contains exactly one terminal metadata event before the single done event"],"exampleFix":"// before (fixture emits two receipts)\n{\"type\":\"metadata\",\"meta\":{\"receipt_kind\":\"terminal\",...}}\n{\"type\":\"metadata\",\"meta\":{\"receipt_kind\":\"terminal\",...}}\n{\"type\":\"done\"}\n// after (exactly one terminal receipt, immediately before done)\n{\"type\":\"metadata\",\"meta\":{\"receipt_kind\":\"terminal\",...}}\n{\"type\":\"done\"}","handlingStrategy":"validation","validationCode":"meta_events = [e for e in stream if e.get(\"type\") == \"metadata\" and isinstance(e.get(\"meta\"), dict) and e[\"meta\"].get(\"receipt_kind\") == \"terminal\"]\nassert len(meta_events) == 1, f\"expected 1 terminal receipt, got {len(meta_events)}\"","typeGuard":"def is_terminal_metadata(event) -> bool:\n    meta = event.get(\"meta\") if isinstance(event, dict) else None\n    return isinstance(meta, dict) and meta.get(\"receipt_kind\") == \"terminal\"","tryCatchPattern":"try:\n    receipt = _parse_stream_receipt(events)\nexcept RuntimeError as e:\n    if \"more than one terminal\" in str(e):\n        log.error(\"duplicate terminal receipt; check binary version/stream concatenation\")\n    raise","preventionTips":["Pin the Codewhale binary version the harness was built against","Never concatenate stream-json from multiple runs before parsing","Keep test fixtures to the canonical [.., metadata, done] shape","Validate stream shape with a linter/fixture check before harness runs"],"tags":["python","stream-parsing","protocol-violation","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"}