Skip to content

test(sources): Codex event-stream contract (#1296) - #1404

Merged
Sinity merged 1 commit into
masterfrom
feature/test/codex-event-stream-contract-1296
May 20, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/test/codex-event-stream-contract-1296

Conversation

@Sinity

@Sinity Sinity commented May 20, 2026

Copy link
Copy Markdown
Owner

Summary

Adds a dedicated contract test module for the Codex on-disk streaming envelope shape — the realized form of OpenAI's response.delta / response.completed stream as persisted in Codex JSONL session files. Closes the corresponding known_gaps bullet in docs/plans/test-closure-matrix.yaml.

Problem

docs/plans/test-closure-matrix.yaml flagged sources.parsers.codex as having no dedicated event-stream contract test — coverage was property-shape only. The streaming envelope is the surface most likely to drift on an upstream Codex / OpenAI format change, and the existing parser tests under tests/unit/sources/test_parsers_codex.py do not pin: response_item message-vs-event dispatch, tool-call call_id pairing semantics across function_call / function_call_output, truncated stream behavior, or the unknown payload-type fallback that protects against silent drops when OpenAI introduces a new event class.

Solution

  • New module tests/unit/sources/test_codex_event_stream_contract.py with 22 contract assertions grouped into five concerns:
    1. Streaming envelope shape — response_item outer envelope, message-vs-event dispatch, unknown payload type surfaces as a provider event (canary for upstream changes).
    2. response.completed terminal state — multi-segment text concatenation, trailing token_count does not corrupt the prior message, terminal message timestamp drives conversation.updated_at.
    3. Tool-call streaming — function_call realizes as a single tool_use block; function_call_output produces a paired tool_result; cross-message call_id pairing.
    4. Stream resilience — truncated (missing function_call_output) keeps the orphan tool_use visible; out-of-order pairs survive; malformed records do not abort the stream; parse_stream and parse agree on truncated input.
    5. Cross-message references — second session_meta becomes parent + BranchType.CONTINUATION; turn_context cwd changes collected into working_directories; compacted event surfaced with has_replacement_history.
  • Three catalog JSONL fixtures under tests/data/codex_event_stream/:
    • text_only_stream.jsonl — session_meta + user/assistant text turn + trailing token_count.
    • tool_call_stream.jsonl — single function_call paired with function_call_output via call_id.
    • interleaved_stream.jsonl — multiple tool turns, mixed assistant messages, turn_context cwd changes, compaction event.
  • Removes the known_gaps bullet from the sources.parsers.codex row in docs/plans/test-closure-matrix.yaml and registers the new test as a representative test for the domain.

The tests pin the observable contract of the current parser (which is intentionally forgiving — it does not raise on truncation or reorder, it must not silently drop content). A future shift to "reject with typed error" would correctly fail these tests and force a deliberate update.

Verification

nix develop -c pytest tests/unit/sources/test_codex_event_stream_contract.py
============================== 22 passed in 8.23s ==============================

nix develop -c devtools verify --quick
"exit_code": 0

Ref #1296.

Adds a dedicated contract test module for the Codex on-disk streaming
envelope shape — the realized form of OpenAI's response.delta /
response.completed stream as persisted in Codex JSONL sessions.

Problem: docs/plans/test-closure-matrix.yaml flagged sources.parsers.codex
as having no dedicated event-stream contract test (coverage was
property-shape only). The streaming envelope is the most likely surface
to drift on an upstream format change and the existing parser tests do
not pin response_item dispatch, tool-call call_id pairing, truncated
stream behavior, or unknown payload-type fallback.

Solution: tests/unit/sources/test_codex_event_stream_contract.py with
22 contract assertions across five concerns — streaming envelope
shape, response.completed terminal-state semantics, tool-call streaming
(call_id pairing across function_call / function_call_output), stream
resilience (truncated / out-of-order / malformed), and cross-message
references (parent session, turn_context cwd, compaction). Three
catalog JSONL fixtures under tests/data/codex_event_stream/ cover
text-only, tool-call, and interleaved realistic shapes. Removes the
known_gaps bullet from the closure matrix.

Ref #1296.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 45 minutes and 10 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5143e219-9f1d-4c21-bd62-65804cffe83f

📥 Commits

Reviewing files that changed from the base of the PR and between 57cc03e and be9065a.

📒 Files selected for processing (5)
  • docs/plans/test-closure-matrix.yaml
  • tests/data/codex_event_stream/interleaved_stream.jsonl
  • tests/data/codex_event_stream/text_only_stream.jsonl
  • tests/data/codex_event_stream/tool_call_stream.jsonl
  • tests/unit/sources/test_codex_event_stream_contract.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/test/codex-event-stream-contract-1296

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit 80b8870 into master May 20, 2026
3 checks passed
@Sinity
Sinity deleted the feature/test/codex-event-stream-contract-1296 branch May 20, 2026 00:11
Sinity added a commit that referenced this pull request May 20, 2026
…oundtrip contract (#1297) (#1405)

## Summary

Adds dedicated contract tests for the Gemini AI Studio `chunkedPrompt`
parser
(`polylogue/sources/parsers/drive.py` + `drive_support*`) covering
chunks
decomposition, content_blocks emission, per-message `provider_meta`
roundtrip,
and conversation envelope metadata. Drops the corresponding `known_gaps`
bullet from `docs/plans/test-closure-matrix.yaml` for
`sources.parsers.gemini`.

## Problem

`docs/plans/test-closure-matrix.yaml` declared:

> Gemini chunkedPrompt message-extraction and metadata roundtrip lacks
> dedicated assertions; format detection only

`tests/unit/sources/test_parsers_drive.py` exercised dispatch and a few
helper
contracts, but the actual decomposition of `chunkedPrompt.chunks` into
normalized messages — including the `executableCode` /
`codeExecutionResult` / `isThought` / `safetyRatings` paths — had no
dedicated assertions. Metadata could regress silently.

## Solution

- New `tests/unit/sources/test_gemini_chunked_prompt_contract.py` with
29
  contract assertions across six classes:
  - `TestChunkedPromptEnvelopeShape` — chunks-to-messages decomposition,
    role guard, top-level `chunks` fallback, string-chunk handling.
  - `TestPerChunkContentBlocks` — `thinking` / `code` / `tool_result`
block emission from `isThought`, `executableCode`,
`codeExecutionResult`.
  - `TestMetadataRoundtrip` — `provider_meta.raw` preservation,
    top-level typed projections (`tokenCount`, `finishReason`,
    `thinkingBudget`, `isThought`, `safetyRatings`, `executableCode`,
    `codeExecutionResult`), plus a full parse → serialize → reparse loop
    that proves `raw` is sufficient to reconstruct identical messages.
  - `TestConversationLevelMetadata` — title source precedence (`title` →
    `displayName` → fallback id), `provider_conversation_id` derivation,
    envelope vs chunk-derived `createTime`/`updateTime`.
  - `TestPerChunkTimestamps` — per-chunk `createTime` propagation and
    envelope fallback.
- Three synthetic catalog payloads under
  `tests/data/gemini_chunked_prompt/`: `text_only_prompt.json`,
  `code_execution_prompt.json`, `multi_turn_prompt.json`.
- `docs/plans/test-closure-matrix.yaml`: drops the `known_gaps` bullet
for
  `sources.parsers.gemini` and lists the new test under
  `representative_tests`.

Mirrors the structural pattern established for the Codex event-stream
contract in #1296 / PR #1404.

## Verification

```
nix develop -c pytest tests/unit/sources/test_gemini_chunked_prompt_contract.py tests/unit/sources/test_parsers_drive.py -q
# 52 passed in 8.67s
nix develop -c devtools verify --quick
# all 21 steps ok (mypy, ruff, render-all, verify-closure-matrix, ...)
```

Closes #1297
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant