fix(client): keep the request context when cloning a client

Merged
PO
8d ago
+109-6

Why

InfrahubClient.clone() returns a client without the original client's request_context. Generators, transforms and checks work with a clone of the client they receive, so the changes they save are sent without the caller's account, and Infrahub records them against the worker's account (opsmill/infrahub#11480).

What changed

flowchart LR
	C["client<br>request_context = caller"] -->|"clone()"| K["clone<br>request_context = caller"]

clone() now copies the request_context, on the async and sync clients. In both repositories, the only caller outside tests is InfrahubOperation, the base class of generators, transforms and checks.

Used by opsmill/infrahub#11504.

How to review

  1. 9df7c36 and c321fa7: tests.
  2. 7ad158d: fix, and the regenerated clone() reference.
  3. 4502535: changelog fragment.

How to test

uv run pytest tests/unit/sdk/test_client.py -k clone tests/unit/sdk/test_generator.py
  • test_clone_keeps_request_context: the clone has the same request_context.
  • test_generator_writes_carry_the_client_request_context: a mutation sent by a generator carries the caller's account.

Both fail without the fix. Also tested end to end on a live Infrahub instance through opsmill/infrahub#11504. The 3 unit tests that fail in tests/unit/ctl also fail on stable.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change)
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)

šŸ¤– Generated with Claude Code