|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import contextvars |
| 17 | +import threading |
| 18 | +import uuid |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from aiq.builder.intermediate_step_manager import IntermediateStepManager |
| 23 | +from aiq.builder.intermediate_step_manager import IntermediateStepPayload |
| 24 | +from aiq.builder.intermediate_step_manager import IntermediateStepState |
| 25 | +from aiq.builder.intermediate_step_manager import _current_open_step_id |
| 26 | + |
| 27 | +# --------------------------------------------------------------------------- # |
| 28 | +# Minimal stubs so the tests do not need the whole aiq code-base |
| 29 | +# --------------------------------------------------------------------------- # |
| 30 | + |
| 31 | + |
| 32 | +class _DummyStream(list): |
| 33 | + """Bare-bones Observable / Subject replacement.""" |
| 34 | + |
| 35 | + def on_next(self, value): # reactive push |
| 36 | + self.append(value) |
| 37 | + |
| 38 | + # simple subscribe: just call back synchronously |
| 39 | + def subscribe(self, on_next, on_error=None, on_complete=None): |
| 40 | + for v in self: |
| 41 | + on_next(v) |
| 42 | + return lambda: None # fake Subscription |
| 43 | + |
| 44 | + |
| 45 | +class _DummyFunction: # what active_function.get() returns |
| 46 | + |
| 47 | + def __init__(self, name="fn", fid=None, parent_name=None): |
| 48 | + self.function_name = name |
| 49 | + self.function_id = fid or str(uuid.uuid4()) |
| 50 | + self.parent_name = parent_name |
| 51 | + |
| 52 | + |
| 53 | +class _DummyContextState: |
| 54 | + """Only what IntermediateStepManager touches.""" |
| 55 | + |
| 56 | + def __init__(self): |
| 57 | + self.active_function = contextvars.ContextVar("active_function") |
| 58 | + self.active_function.set(_DummyFunction()) |
| 59 | + |
| 60 | + self.event_stream = contextvars.ContextVar("event_stream") |
| 61 | + self.event_stream.set(_DummyStream()) |
| 62 | + |
| 63 | + |
| 64 | +# --------------------------------------------------------------------------- # |
| 65 | +# Fixtures |
| 66 | +# --------------------------------------------------------------------------- # |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture() |
| 70 | +def mgr(): |
| 71 | + """Fresh manager + its stubbed context-state for each test.""" |
| 72 | + return IntermediateStepManager(context_state=_DummyContextState()) |
| 73 | + |
| 74 | + |
| 75 | +def _payload(step_id=None, state=IntermediateStepState.START, name="step", etype="LLM_START"): |
| 76 | + """Helper to create a payload with only the fields the manager uses.""" |
| 77 | + return IntermediateStepPayload( |
| 78 | + UUID=step_id or str(uuid.uuid4()), |
| 79 | + name=name, |
| 80 | + event_state=state, |
| 81 | + event_type=etype, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +# --------------------------------------------------------------------------- # |
| 86 | +# Tests |
| 87 | +# --------------------------------------------------------------------------- # |
| 88 | + |
| 89 | + |
| 90 | +def test_start_pushes_event_and_tracks_open_step(mgr): |
| 91 | + pay = _payload() |
| 92 | + mgr.push_intermediate_step(pay) |
| 93 | + |
| 94 | + # one event captured |
| 95 | + stream = mgr._context_state.event_stream.get() |
| 96 | + assert len(stream) == 1 |
| 97 | + # step now in outstanding dict |
| 98 | + assert pay.UUID in mgr._outstanding_start_steps |
| 99 | + |
| 100 | + |
| 101 | +def test_chunk_preserves_parent_id(mgr): |
| 102 | + pay = _payload() |
| 103 | + mgr.push_intermediate_step(pay) # START |
| 104 | + parent_id = _current_open_step_id.get() |
| 105 | + |
| 106 | + chunk = _payload(step_id=pay.UUID, state=IntermediateStepState.CHUNK) |
| 107 | + mgr.push_intermediate_step(chunk) |
| 108 | + |
| 109 | + # parent should still be the START id |
| 110 | + assert _current_open_step_id.get() == parent_id |
| 111 | + |
| 112 | + |
| 113 | +def test_end_same_context_restores_parent(mgr): |
| 114 | + pay = _payload() |
| 115 | + mgr.push_intermediate_step(pay) |
| 116 | + mgr.push_intermediate_step(_payload(step_id=pay.UUID, state=IntermediateStepState.END, etype="LLM_END")) |
| 117 | + |
| 118 | + # open-step removed, ContextVar back to parent (None) |
| 119 | + assert pay.UUID not in mgr._outstanding_start_steps |
| 120 | + |
| 121 | + |
| 122 | +def _end_in_thread(manager, payload): |
| 123 | + """Helper for cross-thread END.""" |
| 124 | + manager.push_intermediate_step(payload) |
| 125 | + |
| 126 | + |
| 127 | +def test_end_other_thread_no_token_error(mgr): |
| 128 | + pay = _payload() |
| 129 | + mgr.push_intermediate_step(pay) |
| 130 | + |
| 131 | + end_pay = _payload(step_id=pay.UUID, state=IntermediateStepState.END, etype="LLM_END") |
| 132 | + t = threading.Thread(target=_end_in_thread, args=(mgr, end_pay)) |
| 133 | + t.start() |
| 134 | + t.join() |
| 135 | + |
| 136 | + # still cleaned up |
| 137 | + assert pay.UUID not in mgr._outstanding_start_steps |
| 138 | + |
| 139 | + |
| 140 | +def test_mismatched_chunk_logs_warning(mgr, caplog): |
| 141 | + # CHUNK without START |
| 142 | + chunk = _payload(state=IntermediateStepState.CHUNK, etype="LLM_NEW_TOKEN") |
| 143 | + mgr.push_intermediate_step(chunk) |
| 144 | + |
| 145 | + assert "no matching start step" in caplog.text.lower() |
0 commit comments