oc_chat
Loading...
Searching...
No Matches
worktree_env.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oc_chat/core/worktree_env.py
4
5# OCT-LINT: disable=tests_exist reason="Re-export shim plus the oct timing wiring; covered by test_245_engine_timing.py and test_257_dispatcher_seams.py"
6"""
7Per-worktree uv environments — moved to :mod:`oc_orchestrator.worktree_env`.
8
9Purpose
10-------
11Plan 270 P4. This module's code now lives in the standalone
12``oc_orchestrator`` package; this file keeps ``oc_chat.core.worktree_env``
13working for every importer that has always spelled it that way, and it
14carries the one piece of wiring oc_chat owns here: the ``oct`` timing
15ledger.
16
17It is a REBINDING, not a wrapper: the last statement replaces this
18module's entry in :data:`sys.modules` with the moved module itself, so
19``oc_chat.core.worktree_env`` and ``oc_orchestrator.worktree_env`` are
20one object. That is what keeps every private name reachable, every
21``monkeypatch`` effective across the seam, and every ``isinstance``
22true — a wrapper that merely re-exported public names would break all
23three quietly.
24
25The oct timing hook (plan 270 P4)
26---------------------------------
27The moved module treats ``oct`` as absent by default, which is what
28takes the toolchain out of the dispatcher package's import graph. oc_chat
29HAS ``oct``, so importing this path registers the live hook and the
30engine timing ledger fills exactly as it did before the split. The
31import is a STATEMENT rather than an ``importlib.import_module`` call:
32the plan-245 timing witness hides ``oct`` by patching
33``builtins.__import__``, which ``import_module`` bypasses.
34
35Responsibilities
36----------------
37- Register :func:`_oct_hook` as the moved module's default hook.
38- Bind the moved module to this path.
39
40Diagnostics
41-----------
42Domain: OC_CHAT.MAIN
43Levels:
44 L2 - lifecycle events
45 L3 - semantic details (an unknown hook target)
46 L4 - deep tracing (the rebinding)
47
48Contracts
49---------
50- Registering the hook is IDEMPOTENT and global to the process: the
51 moved module holds one default and the last registration wins. Only
52 this module and a test ever register one.
53- The hook never catches: each call site keeps its own failure policy,
54 and the timing edge's own ``except ImportError`` is what degrades a
55 missing ``oct`` to a null context.
56- Retirement rule (plan 270 P8): this path stays until an explicit
57 retirement plan retargets its importers. New code names
58 ``oc_orchestrator.worktree_env`` directly — and registers its own
59 hook if it wants timings.
60
61Dependencies
62------------
63- oc_diagnostics (structured debug logging via _dbg)
64- oc_orchestrator.worktree_env (the moved module this path binds)
65- oct.timing.ledger (lazy; timed_stage, optional - resolved through the hook this module installs)
66"""
67
68from __future__ import annotations
69
70import sys
71from typing import Any
72
73from oc_diagnostics import _dbg
74
75from oc_orchestrator import worktree_env as _moved
76
77
78def _oct_hook(target: str) -> Any:
79 """Resolve one member of ``oct`` for the moved module.
80
81 Byte-for-byte the pre-270 ``_default_oct_hook``, minus the
82 ``"package"`` target that plan 270 P1 retired when the venv repair
83 file moved into ``oc_diagnostics``. Nothing is caught here.
84 """
85 func = "_oct_hook"
86 if target == "timed_stage":
87 from oct.timing.ledger import timed_stage # noqa: PLC0415
88
89 return timed_stage
90 _dbg("OC_CHAT.MAIN", func, f"VALIDATION_ERROR: unknown target {target!r}", 3)
91 raise _moved.WorktreeEnvError(f"unknown oct hook target {target!r}")
92
93
94_moved.use_oct_hook(_oct_hook)
95
96_dbg("OC_CHAT.MAIN", "module", "worktree_env shim -> oc_orchestrator.worktree_env", 4)
97
98sys.modules[__name__] = _moved
Any _oct_hook(str target)