Skip to content

fix: Carry the correlation context into the derivation kernel - #5073

Merged
Sinity merged 1 commit into
masterfrom
fix/derivation-correlation
Sep 14, 2026
Merged

Sinity merged 1 commit into
masterfrom
fix/derivation-correlation

Conversation

@Sinity

@Sinity Sinity commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Summary

daemon.derivation.* events lost their run_id on exactly the rebuild path they exist to make legible.

Problem

DaemonConverger submits converge_derivations and _converge_selected_session_parts_sync into the bounded compute pool. BoundedComputeAdapter.__init__ builds that ThreadPoolExecutor eagerly — before any correlation bind — and ThreadPoolExecutor.submit does not copy contextvars. So every derivation event emitted from a pool thread carried no run_id, which is the single most useful correlation during a multi-hour rebuild: it is what lets you join a degraded derivation back to the run that produced it.

Found and reported by the daemon conversion worker, in a file it was scoped out of, with the note that no test would go red for it.

Solution

Wrap both submits in propagate().

propagate was typed Callable[..., object] -> Callable[..., object], which erased the result type of both asyncio.shield calls at these sites. Under that signature the cheapest way to satisfy the typechecker is to drop the wrapper rather than cast — and dropping it loses the correlation silently, which is how this class of defect reproduces itself. So propagate is now generic in both directions and wrapping is type-transparent.

ParamSpec/TypeVar rather than PEP 695 syntax, because mypy is configured at python_version = "3.11" while the devshell requires 3.14 free-threading.

Verification

devtools test tests/unit/test_structured_logging.py   # PASSED, exit 0
devtools verify --quick                                # exit 0, all 15 gates

The static half — that a wrapped submit keeps its result type — cannot be asserted at runtime, because ParamSpec erases to *args/**kwargs in the actual signature. An earlier version of the test asserted inspect.signature equality and was wrong; it failed, and the honest correction was to narrow the test rather than weaken the claim. devtools gate mypy is what enforces the static property: restoring the old signature fails typechecking on both asyncio.shield calls. The test now asserts only what is runtime-observable, and says so.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK

`DaemonConverger` submits `converge_derivations` and
`_converge_selected_session_parts_sync` into the bounded compute pool, whose
`ThreadPoolExecutor` is built eagerly in `BoundedComputeAdapter.__init__` --
before any correlation bind -- and which does not copy contextvars. Every
`daemon.derivation.*` event therefore lost its `run_id`, the single most useful
correlation during a multi-hour rebuild.

`propagate` was typed `Callable[..., object] -> Callable[..., object]`, which
erased the result type of both `asyncio.shield` calls at these sites. Under that
signature the cheapest way to satisfy the typechecker is to drop the wrapper
rather than cast -- and dropping it loses the correlation silently. Make
`propagate` generic in both directions so wrapping is type-transparent.

`ParamSpec`/`TypeVar` rather than PEP 695 syntax because mypy is configured at
`python_version = "3.11"`, though the devshell requires 3.14 free-threading.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 44 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 4639e42c-b2d7-4303-a034-028abc55461b

📥 Commits

Reviewing files that changed from the base of the PR and between c06465f and e53211d.

📒 Files selected for processing (3)
  • polylogue/daemon/convergence.py
  • polylogue/logging.py
  • tests/unit/test_structured_logging.py

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.

@Sinity
Sinity merged commit ab850e0 into master Sep 14, 2026
3 checks passed
@Sinity
Sinity deleted the fix/derivation-correlation branch September 14, 2026 12:03
Sinity added a commit that referenced this pull request Sep 14, 2026
Three `cast(...)` wrappers existed only to absorb `propagate`'s old
`Callable[..., object]` signature. #5073 made it generic, so all three are dead.

Removing them is not tidying. A `cast` sitting on a `propagate` call is the
visible residue of an annotation that made correct use cost more than incorrect
use: the next reader who hits a red mypy on a wrapped submit finds the
established workaround right there, and the cheaper move beside it is still to
drop the wrapper — which loses the correlation id silently. Deleting the casts
deletes the precedent.

The third, in `http.py`, was added during this branch's own conflict
resolution, by someone who had just been warned about exactly this pressure.
That is how quiet the failure mode is.

`cast` is no longer imported in `operation_runtime.py`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK
Sinity added a commit that referenced this pull request Sep 14, 2026
## Summary

Two tests grepped for prose sentences that the structured-logging wave
deleted. Both now assert the stable **event token** and its declared
fields.

## Which merged PR moved the ground

PRs #5072 (`5eb270707`, "Convert the remaining daemon modules to
structured
events") and #5073 (`ab850e0b7`, "Carry the correlation context into the
derivation kernel") retired prose logging in these modules.
`polylogue/logging.py:400` `emit()` writes structured records to its own
sinks and never touches stdlib logging, so `caplog.text` is empty by
construction -- these were failing on an assertion that can no longer be
true,
not on behaviour.

- `daemon/convergence_stages.py:308` now emits
`daemon.stage.check_failed`
(`reason="probe_failed_assuming_work"`), and the execute path reports
through
  `span.degraded("materialization_failed")`, whose terminal event is
  `daemon.stage.execute.degraded`.
- `daemon/lifecycle.py:287` now emits `daemon.lifecycle.signal_received`
  (`signal_name=SIGTERM`).

CLAUDE.md is explicit: "Do not enforce natural-language wording".

## Solution

-
`test_convergence_stage_reports_probe_and_materialization_failures_as_pending_work`:
  drop `caplog`, use `polylogue.logging.capture()`, and assert the exact
`(stage, outcome, reason, error_type)` tuple for each of the two events.
- `test_sigterm_read_only_daemon_records_forensics` reads the daemon
subprocess's own stream, which may render console or JSON. It now
selects
the line carrying `daemon.lifecycle.signal_received` and asserts it
names
  `SIGTERM`. The `"Current thread"` faulthandler assertion is unchanged.

Both assertions are on tokens and fields, which are the storage form;
the
human sentence was always a rendering concern.

## Verification

```
devtools test tests/unit/insights/test_delegation_work_evidence_materializer.py
     (receipt 20260914T163857Z-focused-test-3135387-125edb44)  -> green
devtools test tests/integration/test_daemon_resilience.py
     (receipt 20260914T165038Z-focused-test-3220954-2ff3e795)
  -> 7 passed / 7 collected, including
     test_sigterm_read_only_daemon_records_forensics
devtools verify --quick  -> exit 0, 15/15 gates
```

## Anti-vacuity

| Test | Mutation that makes it red |
|---|---|
| convergence stage, probe half | make `check`'s `except` return False,
or drop the `emit` |
| convergence stage, execute half | swallow the materialization
exception into a success; the `degraded`/`materialization_failed`
terminal event disappears |
| SIGTERM forensics | drop the `emit` from `install_signal_handlers` (no
line carries the token), or drop `faulthandler.dump_traceback` (no stack
dump) |

## Residuals

Test-only change. The integration test still depends on the daemon's
configured renderer emitting the token in either format; it does not pin
which
format, deliberately.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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