quapp-qibo 0.0.1.dev2__tar.gz → 0.0.1.dev3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/PKG-INFO +2 -2
  2. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/pyproject.toml +2 -2
  3. quapp_qibo-0.0.1.dev3/quapp_qibo/__init__.py +5 -0
  4. quapp_qibo-0.0.1.dev3/quapp_qibo/error_rules.py +80 -0
  5. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/device/qibo_device.py +46 -31
  6. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/provider/quapp_qibo_provider.py +9 -5
  7. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo.egg-info/PKG-INFO +2 -2
  8. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo.egg-info/SOURCES.txt +3 -1
  9. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo.egg-info/requires.txt +1 -1
  10. quapp_qibo-0.0.1.dev3/tests/test_error_logging.py +304 -0
  11. quapp_qibo-0.0.1.dev2/quapp_qibo/__init__.py +0 -1
  12. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/LICENSE +0 -0
  13. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/README.md +0 -0
  14. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/async_tasks/__init__.py +0 -0
  15. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/async_tasks/qibo_circuit_export_task.py +0 -0
  16. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/component/__init__.py +0 -0
  17. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/component/backend/__init__.py +0 -0
  18. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/component/backend/qibo_invocation.py +0 -0
  19. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/factory/__init__.py +0 -0
  20. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/factory/qibo_device_factory.py +0 -0
  21. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/factory/qibo_handler_factory.py +0 -0
  22. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/factory/qibo_provider_factory.py +0 -0
  23. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/handler/__init__.py +0 -0
  24. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/handler/invocation_handler.py +0 -0
  25. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/__init__.py +0 -0
  26. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/device/__init__.py +0 -0
  27. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/device/quapp_qibo_device.py +0 -0
  28. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo/model/provider/__init__.py +0 -0
  29. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo.egg-info/dependency_links.txt +0 -0
  30. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/quapp_qibo.egg-info/top_level.txt +0 -0
  31. {quapp_qibo-0.0.1.dev2 → quapp_qibo-0.0.1.dev3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quapp-qibo
3
- Version: 0.0.1.dev2
3
+ Version: 0.0.1.dev3
4
4
  Summary: Quapp common library supporting Quapp Platform for Quantum Computing
5
5
  Author-email: "CITYNOW Co. Ltd. " <corp@citynow.vn>
6
6
  License: The MIT License (MIT)
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3
19
19
  Requires-Python: <3.11,>=3.9
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE
22
- Requires-Dist: quapp-common==0.0.11.dev10
22
+ Requires-Dist: quapp-common==0.0.14.dev10
23
23
  Requires-Dist: qibo==0.3.1
24
24
  Requires-Dist: qibojit==0.1.13
25
25
  Provides-Extra: dev
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "quapp-qibo"
7
- version = "0.0.1.dev2"
7
+ version = "0.0.1.dev3"
8
8
  description = "Quapp common library supporting Quapp Platform for Quantum Computing"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "CITYNOW Co. Ltd. ", email = "corp@citynow.vn" }]
@@ -16,7 +16,7 @@ classifiers = [
16
16
  ]
17
17
  keywords = ["quapp", "quapp-qibo", "quantum"]
18
18
  dependencies = [
19
- "quapp-common==0.0.11.dev10",
19
+ "quapp-common==0.0.14.dev10",
20
20
  "qibo==0.3.1",
21
21
  "qibojit==0.1.13"
22
22
  ]
@@ -0,0 +1,5 @@
1
+ from .factory.qibo_handler_factory import QiboHandlerFactory
2
+
3
+ # Imported for the side effect: register_rules() has to run before the
4
+ # first job fails, so this import cannot be made lazy.
5
+ from . import error_rules as _error_rules # noqa: E402,F401
@@ -0,0 +1,80 @@
1
+ # Quapp Platform Project
2
+ # error_rules.py
3
+ # Copyright © CITYNOW Co. Ltd. All rights reserved.
4
+
5
+ """Qibo-specific error classification rules.
6
+
7
+ Registered into ``quapp_common.util.error_classifier`` when ``quapp_qibo`` is
8
+ imported, so ``quapp-common`` never has to depend on Qibo.
9
+
10
+ Matching is done on the class **NAME as a string**, never ``isinstance``, and
11
+ nothing from ``qibo`` is imported. Same reason as the Qiskit version:
12
+ ``import quapp_qibo`` must not demand the vendor SDK, and the classifier has to
13
+ work even when the SDK is absent.
14
+
15
+ Without any rule here the system still runs: the classifier falls back on the
16
+ phase, so the user just gets a generic ``EXECUTION_FAILED`` /
17
+ ``PROVIDER_INVOCATION_FAILED`` instead of a specific code.
18
+
19
+ The first two rules key off messages **this lib itself** raises -- checked
20
+ against the code, not guessed:
21
+
22
+ quapp_qibo/factory/qibo_provider_factory.py:23 Exception("Unsupported provider!")
23
+ quapp_qibo/factory/qibo_device_factory.py:29 Exception("Unsupported device!")
24
+ quapp_qibo/model/provider/quapp_qibo_provider.py:32
25
+ Exception('Unsupported device')
26
+
27
+ The last rule matches Qibo exception names. If upstream renames them the rule
28
+ becomes inert rather than broken -- the classifier just falls back on the phase.
29
+ """
30
+
31
+ from quapp_common.enum.error_category import ErrorCategory
32
+ from quapp_common.util.error_classifier import Rule, register_rules
33
+
34
+ DOC_DEVICE = 'errors/qibo-device'
35
+ DOC_CIRCUIT = 'errors/qibo-circuit'
36
+
37
+
38
+ def _message(exception) -> str:
39
+ return str(exception).lower()
40
+
41
+
42
+ def _name(exception) -> str:
43
+ return type(exception).__name__
44
+
45
+
46
+ register_rules(
47
+ # ------------------ raised by quapp-qibo itself (checked against code)
48
+ Rule(match=lambda e: 'unsupported device' in _message(e)
49
+ or 'unsupported provider' in _message(e),
50
+ category=ErrorCategory.PROVIDER_DEVICE,
51
+ code='QIBO_DEVICE_UNSUPPORTED',
52
+ resolution="This device or provider is not supported by Quapp's "
53
+ 'Qibo lib yet.',
54
+ steps=('Check the device selected for the job',
55
+ 'quapp-qibo currently serves QUAO_QUANTUM_SIMULATOR'),
56
+ doc_slug=DOC_DEVICE),
57
+
58
+ Rule(match=lambda e: 'invalid circuit type' in _message(e),
59
+ category=ErrorCategory.USER_CODE,
60
+ code='QIBO_INVALID_CIRCUIT_TYPE',
61
+ resolution='The circuit preparation function did not return a Qibo '
62
+ 'circuit.',
63
+ steps=('Check what your processing function returns',
64
+ 'The circuit must be a qibo.models.Circuit'),
65
+ doc_slug=DOC_CIRCUIT),
66
+
67
+ # ------------------------------------------------- Qibo's own exceptions
68
+ # Placed after the rules above: many Qibo exceptions inherit
69
+ # QiboException, but matching is by NAME so only these exact names land
70
+ # here.
71
+ Rule(match=lambda e: _name(e) in {'QiboException', 'RawBackendError',
72
+ 'BackendError'},
73
+ category=ErrorCategory.SDK_RUNTIME,
74
+ code='QIBO_SDK_ERROR',
75
+ resolution='Qibo failed while building or running the circuit. See '
76
+ 'the stack trace below.',
77
+ steps=('Read the rootCause to see which step Qibo rejected',
78
+ 'Check the qubit count and gate set the Qibo backend '
79
+ 'supports')),
80
+ )
@@ -8,14 +8,14 @@ from quapp_common.model.device.custom_device import CustomDevice
8
8
  from quapp_common.data.response.job_response import JobResponse
9
9
 
10
10
  from quapp_common.component.callback.update_job_metadata import update_job_metadata
11
- from quapp_common.enum.status.job_status import JobStatus
12
- from quapp_common.enum.media_type import MediaType
13
11
  from quapp_common.enum.status.status_code import StatusCode
14
12
  from quapp_common.data.response.authentication import Authentication
15
13
  from quapp_common.data.response.custom_header import CustomHeader
16
14
  from quapp_common.data.device.circuit_running_option import CircuitRunningOption
17
15
  from quapp_common.enum.invocation_step import InvocationStep
18
16
 
17
+ from quapp_common.config.log_context import phase
18
+ from quapp_common.enum.runtime_phase import RuntimePhase
19
19
  from quapp_common.util.response_utils import build_error_job_response
20
20
 
21
21
  from quapp_common.data.callback.callback_url import CallbackUrl
@@ -66,13 +66,20 @@ class QiboDevice(CustomDevice, ABC):
66
66
  update_job_metadata(job_response=job_response,
67
67
  callback_url=execution_callback.on_start)
68
68
  try:
69
- job = self._create_job(circuit=circuit, options=options)
70
- job_response.provider_job_id = self._get_provider_job_id(job)
71
- job_response.job_status = self._get_job_status(job)
69
+ # A copy of Device.run_circuit that lost every `with phase(...)`, so
70
+ # until now no qibo log line carried a phase at all. COMPILATION
71
+ # stays separate from EXECUTION: a wrong circuit type and a provider
72
+ # rejecting the job are two different failures.
73
+ with phase(RuntimePhase.COMPILATION):
74
+ job = self._create_job(circuit=circuit, options=options)
72
75
 
73
- job_result = self._get_job_result(job)
74
- job_response.job_result = job_result
75
- original_job_result = job_result
76
+ with phase(RuntimePhase.EXECUTION):
77
+ job_response.provider_job_id = self._get_provider_job_id(job)
78
+ job_response.job_status = self._get_job_status(job)
79
+
80
+ job_result = self._get_job_result(job)
81
+ job_response.job_result = job_result
82
+ original_job_result = job_result
76
83
 
77
84
  update_job_metadata(job_response=job_response,
78
85
  callback_url=execution_callback.on_done)
@@ -80,13 +87,17 @@ class QiboDevice(CustomDevice, ABC):
80
87
  return original_job_result, job_response
81
88
 
82
89
  except Exception as exception:
83
- self.logger.debug(
84
- "Execute job failed with error {}".format(str(exception)))
85
-
86
- job_response.status_code = StatusCode.ERROR
87
- job_response.content_type = MediaType.APPLICATION_JSON
88
- job_response.job_status = JobStatus.ERROR.value
89
- job_response.job_result = {"error": str(exception)}
90
+ # This used to be `{"error": str(exception)}`: no category, no phase,
91
+ # no rootCause -- exactly what left the Job Log without a readable
92
+ # reason. build_error_job_response fills the whole envelope and logs
93
+ # once itself, so the logger.debug that used to sit here is gone too,
94
+ # to avoid logging the same error twice.
95
+ job_response = build_error_job_response(
96
+ exception, job_response,
97
+ message='Error when executing job',
98
+ stage='while running the circuit on the device',
99
+ phase=RuntimePhase.EXECUTION,
100
+ context=self._error_context())
90
101
 
91
102
  update_job_metadata(job_response=job_response,
92
103
  callback_url=execution_callback.on_error)
@@ -143,19 +154,20 @@ class QiboDevice(CustomDevice, ABC):
143
154
  update_job_metadata(job_response, analysis_callback.on_start)
144
155
 
145
156
  try:
146
- self.logger.debug(
147
- "Producing histogram data from original job result")
157
+ with phase(RuntimePhase.ANALYSIS):
158
+ self.logger.debug(
159
+ "Producing histogram data from original job result")
148
160
 
149
- job_response.job_histogram = self._produce_histogram_data(
150
- original_job_result)
161
+ job_response.job_histogram = self._produce_histogram_data(
162
+ original_job_result)
151
163
 
164
+ self.logger.debug(
165
+ "Calculating execution time from parsed results")
166
+ self._calculate_execution_time(original_job_result)
167
+ job_response.execution_time = self.execution_time
152
168
 
153
- self.logger.debug("Calculating execution time from parsed results")
154
- self._calculate_execution_time(original_job_result)
155
- job_response.execution_time = self.execution_time
156
-
157
- self.logger.debug("Retrieving shot count from job result")
158
- job_response.shots = self._get_shots(original_job_result)
169
+ self.logger.debug("Retrieving shot count from job result")
170
+ job_response.shots = self._get_shots(original_job_result)
159
171
 
160
172
  update_job_metadata(job_response, analysis_callback.on_done)
161
173
 
@@ -163,12 +175,15 @@ class QiboDevice(CustomDevice, ABC):
163
175
  return job_response
164
176
 
165
177
  except Exception as exception:
166
- self.logger.exception(
167
- f"Exception when analyzing job result: {exception}",
168
- exc_info=True)
169
-
170
- job_response = build_error_job_response(exception, job_response,
171
- message='Error when analyzing job result')
178
+ # A logger.exception() here would emit a second ERROR line for the same
179
+ # failure: build_error_job_response already logs the full envelope.
180
+ # Two lines skew every metric that counts job_failed.
181
+ job_response = build_error_job_response(
182
+ exception, job_response,
183
+ message='Error when analyzing job result',
184
+ stage='while analysing the job result',
185
+ phase=RuntimePhase.ANALYSIS,
186
+ context=self._error_context())
172
187
 
173
188
  update_job_metadata(job_response, analysis_callback.on_error)
174
189
  return None
@@ -5,13 +5,13 @@
5
5
  # quapp_qibo_provider.py
6
6
  # Copyright © CITYNOW Co. Ltd. All rights reserved.
7
7
 
8
- from quapp_common.config.logging_config import job_logger
8
+ from quapp_common.config.logging_config import logger
9
9
  from qibo import set_backend
10
10
 
11
11
  from quapp_common.enum.provider_tag import ProviderTag
12
12
  from quapp_common.model.provider.provider import Provider
13
13
 
14
- logger = job_logger('QuappQiboProvider')
14
+ logger = logger.bind(context='QuappQiboProvider')
15
15
 
16
16
 
17
17
  class QuappQiboProvider(Provider):
@@ -27,6 +27,10 @@ class QuappQiboProvider(Provider):
27
27
  try:
28
28
  set_backend(device_specification)
29
29
  return device_specification
30
- except Exception:
31
- logger.exception(f'Unsupported device: {device_specification}')
32
- raise Exception('Unsupported device')
30
+ except Exception as exception:
31
+ # A logger.exception() here would emit a second ERROR line: this error
32
+ # is logged again by build_error_job_response with the full envelope.
33
+ # `from` keeps the cause chain so the classifier can still read
34
+ # Qibo's original error.
35
+ raise Exception(
36
+ f'Unsupported device: {device_specification}') from exception
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quapp-qibo
3
- Version: 0.0.1.dev2
3
+ Version: 0.0.1.dev3
4
4
  Summary: Quapp common library supporting Quapp Platform for Quantum Computing
5
5
  Author-email: "CITYNOW Co. Ltd. " <corp@citynow.vn>
6
6
  License: The MIT License (MIT)
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3
19
19
  Requires-Python: <3.11,>=3.9
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE
22
- Requires-Dist: quapp-common==0.0.11.dev10
22
+ Requires-Dist: quapp-common==0.0.14.dev10
23
23
  Requires-Dist: qibo==0.3.1
24
24
  Requires-Dist: qibojit==0.1.13
25
25
  Provides-Extra: dev
@@ -2,6 +2,7 @@ LICENSE
2
2
  README.md
3
3
  pyproject.toml
4
4
  quapp_qibo/__init__.py
5
+ quapp_qibo/error_rules.py
5
6
  quapp_qibo.egg-info/PKG-INFO
6
7
  quapp_qibo.egg-info/SOURCES.txt
7
8
  quapp_qibo.egg-info/dependency_links.txt
@@ -23,4 +24,5 @@ quapp_qibo/model/device/__init__.py
23
24
  quapp_qibo/model/device/qibo_device.py
24
25
  quapp_qibo/model/device/quapp_qibo_device.py
25
26
  quapp_qibo/model/provider/__init__.py
26
- quapp_qibo/model/provider/quapp_qibo_provider.py
27
+ quapp_qibo/model/provider/quapp_qibo_provider.py
28
+ tests/test_error_logging.py
@@ -1,4 +1,4 @@
1
- quapp-common==0.0.11.dev10
1
+ quapp-common==0.0.14.dev10
2
2
  qibo==0.3.1
3
3
  qibojit==0.1.13
4
4
 
@@ -0,0 +1,304 @@
1
+ # Quapp Platform Project
2
+ # test_error_logging.py
3
+ # Copyright © CITYNOW Co. Ltd. All rights reserved.
4
+
5
+ """Locks four classes of error-logging bug, all found in real provider-lib code.
6
+
7
+ 1. **A copied hook lost its phase.** Some devices copied ``run_circuit`` /
8
+ ``_on_execution`` / ``_on_analysis`` wholesale from ``Device`` but dropped the
9
+ ``with phase(...)`` blocks, so that lib's log had no ``phase`` field at any
10
+ stage. Same class of bug as ``CustomDevice._on_analysis`` in quapp-common.
11
+
12
+ 2. **The error was flattened into a string.** ``job_response.job_result =
13
+ {"error": str(exception)}`` leaves the Job Log with a bare string: no
14
+ category, no phase, no rootCause -- exactly what the ticket complained about.
15
+
16
+ 3. **The error was logged twice.** ``logger.exception()`` immediately before
17
+ ``build_error_job_response`` emits a second ERROR line for the same failure,
18
+ skewing every metric that counts job_failed.
19
+
20
+ 4. **A fake job_id.** ``job_logger('SomeClass')`` -- the first argument of
21
+ ``job_logger()`` is the **job_id**. Passing a class name there sets a fake
22
+ job_id and it BEATS the real one (``_inject_job_context`` only fills keys that
23
+ are still None), so filtering by jobId in Grafana loses every line that
24
+ component logged.
25
+
26
+ The tests discover files inside the package, so this file works for any lib
27
+ without hardcoding names.
28
+ """
29
+
30
+ import ast
31
+ import io
32
+ import pathlib
33
+ import re
34
+ import tokenize
35
+
36
+ import pytest
37
+
38
+ REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
39
+
40
+ # The first release carrying a phase for analysis/post-processing and one traceId
41
+ # per job.
42
+ MINIMUM_COMMON = '0.0.14.dev6'
43
+
44
+ # Hooks already instrumented in quapp-common: copying one means copying its
45
+ # phases too. _on_execution creates the job and then talks to the provider, which
46
+ # is why it needs both -- a failed transpile and a rejected job are different
47
+ # errors.
48
+ INSTRUMENTED = {'_on_execution': {'COMPILATION', 'EXECUTION'},
49
+ '_on_analysis' : {'ANALYSIS'}}
50
+
51
+
52
+ def package_root() -> pathlib.Path:
53
+ """The provider lib's package directory, inferred from the tree.
54
+
55
+ A few repos hold more than one package -- quapp-hpc also has
56
+ ``quapp_slurm_hpc``, which only contains ``runner.py`` for the Slurm node and
57
+ is not the provider lib. Pick by structural evidence (it has ``model/`` or
58
+ ``factory/``) rather than by name.
59
+ """
60
+ candidates = [d for d in REPO_ROOT.iterdir()
61
+ if d.is_dir() and re.fullmatch(r'qu?app_\w+', d.name)
62
+ and (d / '__init__.py').is_file()]
63
+ assert candidates, 'found no package at all'
64
+
65
+ if len(candidates) > 1:
66
+ providers = [d for d in candidates
67
+ if (d / 'model').is_dir() or (d / 'factory').is_dir()]
68
+ assert len(providers) == 1, (
69
+ f'could not identify the provider package: '
70
+ f'{providers or candidates}')
71
+ return providers[0]
72
+
73
+ return candidates[0]
74
+
75
+
76
+ PACKAGE = package_root()
77
+
78
+
79
+ def code_only(path) -> str:
80
+ """Source with comments stripped out.
81
+
82
+ Needed because the explanatory comments *quote* the very code being banned
83
+ (``{"error": str(exception)}``, ``logger.exception(``). A raw scan would
84
+ report a false positive on the explanation itself.
85
+ """
86
+ source = path.read_text(encoding='utf-8')
87
+ kept = [token for token in
88
+ tokenize.generate_tokens(io.StringIO(source).readline)
89
+ if token.type != tokenize.COMMENT]
90
+ return tokenize.untokenize(kept)
91
+
92
+
93
+ def phases_opened_in(source: str) -> set:
94
+ """The RuntimePhases opened with ``with phase(...)`` in a piece of code."""
95
+ tree = ast.parse(source.lstrip())
96
+ return {
97
+ argument.attr
98
+ for node in ast.walk(tree) if isinstance(node, ast.With)
99
+ for item in node.items if isinstance(item.context_expr, ast.Call)
100
+ if (getattr(item.context_expr.func, 'id', None)
101
+ or getattr(item.context_expr.func, 'attr', None)) == 'phase'
102
+ for argument in item.context_expr.args
103
+ if isinstance(argument, ast.Attribute)
104
+ }
105
+
106
+
107
+ def python_files():
108
+ return sorted(PACKAGE.rglob('*.py'))
109
+
110
+
111
+ # ------------------------------------------- (1) a copied hook keeps its phases
112
+
113
+ def test_copied_hooks_keep_their_phases():
114
+ """Overriding an instrumented hook means copying its `with phase(...)` too."""
115
+ offenders = []
116
+ for path in python_files():
117
+ source = path.read_text(encoding='utf-8')
118
+ tree = ast.parse(source, filename=str(path))
119
+ for node in ast.walk(tree):
120
+ if not isinstance(node, ast.FunctionDef) \
121
+ or node.name not in INSTRUMENTED:
122
+ continue
123
+ opened = phases_opened_in(ast.get_source_segment(source, node))
124
+ missing = INSTRUMENTED[node.name] - opened
125
+ if missing:
126
+ offenders.append(
127
+ f'{path.name}::{node.name} is missing {sorted(missing)}')
128
+
129
+ assert not offenders, (
130
+ f'overridden hooks lost their phase: {offenders}. The original in '
131
+ f'quapp-common has `with phase(...)`; copying it without that leaves the '
132
+ f'log with no phase field.')
133
+
134
+
135
+ def test_at_least_one_phase_is_opened_somewhere():
136
+ """A lib that overrides no hook needs none; one that does needs a phase."""
137
+ overrides = any(
138
+ isinstance(node, ast.FunctionDef) and node.name in INSTRUMENTED
139
+ for path in python_files()
140
+ for node in ast.walk(ast.parse(path.read_text(encoding='utf-8')))
141
+ )
142
+ if not overrides:
143
+ pytest.skip('lib overrides no hook; phases come from quapp-common')
144
+
145
+ opened = set()
146
+ for path in python_files():
147
+ opened |= phases_opened_in(path.read_text(encoding='utf-8'))
148
+ assert opened, 'hooks are overridden but no phase is opened'
149
+
150
+
151
+ # --------------------------------- (2) no hand-rolled error string
152
+
153
+ @pytest.mark.parametrize('path', python_files(), ids=lambda p: p.name)
154
+ def test_no_hand_rolled_error_dict(path):
155
+ """`{"error": str(exception)}` bypasses the entire error envelope."""
156
+ assert '{"error": str(exception)}' not in code_only(path), (
157
+ f'{path.name}: flattens the error into a string, losing '
158
+ f'category/phase/rootCause. Use '
159
+ f'build_error_job_response(..., phase=..., context=...)')
160
+
161
+
162
+ def test_error_envelope_carries_phase_and_context():
163
+ """Every build_error_job_response in the device needs phase= and context=."""
164
+ for path in python_files():
165
+ source = code_only(path)
166
+ calls = source.count('build_error_job_response(')
167
+ if not calls:
168
+ continue
169
+ assert source.count('phase=RuntimePhase.') >= calls, (
170
+ f'{path.name}: has {calls} error envelopes but is missing phase=, '
171
+ f'so the ErrorReport would come out with phase=None')
172
+ assert source.count('context=self._error_context()') >= calls, (
173
+ f'{path.name}: error envelope is missing context=, leaving support '
174
+ f'without the device/provider')
175
+
176
+
177
+ # ------------------------------------------ (3) one error is logged once
178
+
179
+ def test_no_duplicate_error_log_before_envelope():
180
+ """logger.exception()/logger.error() right before the envelope = 2 ERRORs."""
181
+ offenders = []
182
+ for path in python_files():
183
+ source = code_only(path)
184
+ for match in re.finditer(r'logger\.(exception|error)\(', source):
185
+ if 'build_error_job_response(' in source[match.end():match.end() + 400]:
186
+ line = source[:match.start()].count('\n') + 1
187
+ offenders.append(f'{path.name}:{line}')
188
+
189
+ assert not offenders, (
190
+ f'duplicate ERROR log before build_error_job_response at {offenders}; '
191
+ f'the envelope already logs once')
192
+
193
+
194
+ # ------------------------------------------------- (4) no fake job_id
195
+
196
+ @pytest.mark.parametrize('path', python_files(), ids=lambda p: p.name)
197
+ def test_no_fake_job_logger_argument(path):
198
+ """`job_logger()` takes a real job_id; a component name belongs in `context`.
199
+
200
+ Two shapes of fake id are caught: a string literal (`job_logger('Provider')`)
201
+ and `__name__`, which quietly makes the job_id the dotted module path and
202
+ blows up jobId cardinality in Grafana as well as losing the real job.
203
+ """
204
+ tree = ast.parse(path.read_text(encoding='utf-8'), filename=str(path))
205
+
206
+ offenders = []
207
+ for node in ast.walk(tree):
208
+ if not isinstance(node, ast.Call) or not node.args:
209
+ continue
210
+ if (getattr(node.func, 'id', None)
211
+ or getattr(node.func, 'attr', None)) != 'job_logger':
212
+ continue
213
+ first = node.args[0]
214
+ if isinstance(first, ast.Constant) and isinstance(first.value, str):
215
+ offenders.append(repr(first.value))
216
+ elif isinstance(first, ast.Name) and first.id == '__name__':
217
+ offenders.append('__name__')
218
+
219
+ assert not offenders, (
220
+ f'{path.name}: job_logger() got {offenders} -- those are fake job_ids. '
221
+ f"Use logger.bind(context='...') for the component name")
222
+
223
+
224
+ def test_providers_bind_their_name_to_context():
225
+ """A provider that logs must bind its own name, so lines say where they came
226
+ from."""
227
+ providers = sorted((PACKAGE / 'model' / 'provider').glob('*.py'))
228
+ if not providers:
229
+ pytest.skip('lib has no model/provider directory')
230
+
231
+ offenders = []
232
+ for path in providers:
233
+ if path.name == '__init__.py':
234
+ continue
235
+ # code_only: several providers keep their old logging commented out, and
236
+ # a raw scan would flag those as if they still logged.
237
+ source = code_only(path)
238
+ if 'logger' not in source:
239
+ continue
240
+ if not re.search(r"logger\.bind\(context='[A-Za-z]+'\)", source):
241
+ offenders.append(path.name)
242
+
243
+ assert not offenders, (
244
+ f'providers that log without binding context: {offenders}. The component '
245
+ f'name must be a field, not text inside the message, or Grafana cannot '
246
+ f'filter on it')
247
+
248
+
249
+ # ------------------------------------------ rules + the quapp-common pin
250
+
251
+ def test_error_rules_module_exists():
252
+ assert (PACKAGE / 'error_rules.py').is_file(), \
253
+ "no error_rules.py, so this SDK's errors never get classified"
254
+
255
+
256
+ def test_error_rules_are_registered_on_import():
257
+ init = (PACKAGE / '__init__.py').read_text(encoding='utf-8')
258
+ assert 'error_rules' in init, \
259
+ '__init__.py does not import error_rules, so the rules never register'
260
+
261
+
262
+ def test_error_rules_imports_no_vendor_sdk():
263
+ """The classifier has to run even with the SDK absent, hence name matching."""
264
+ tree = ast.parse((PACKAGE / 'error_rules.py').read_text(encoding='utf-8'))
265
+
266
+ roots = set()
267
+ for node in ast.walk(tree):
268
+ if isinstance(node, ast.Import):
269
+ roots.update(alias.name.split('.')[0] for alias in node.names)
270
+ elif isinstance(node, ast.ImportFrom) and node.module and not node.level:
271
+ roots.add(node.module.split('.')[0])
272
+
273
+ assert roots <= {'quapp_common'}, \
274
+ f'error_rules.py may only import quapp_common, but imports: {sorted(roots)}'
275
+
276
+
277
+ def test_common_pin_is_exact_and_current():
278
+ version = pytest.importorskip('packaging.version')
279
+
280
+ text = (REPO_ROOT / 'pyproject.toml').read_text(encoding='utf-8')
281
+ assert not re.findall(r'"quapp-common\s*(>=|>|~=)', text), (
282
+ 'quapp-common must be pinned with `==`: `>=` lets pip pick a higher '
283
+ 'stable release that is missing the feature (which already happened '
284
+ 'with quapp-qiskit 1.0.14)')
285
+
286
+ match = re.search(r'"quapp-common\s*==\s*([^"]+)"', text)
287
+ assert match, 'no quapp-common pin found'
288
+ assert version.Version(match.group(1)) >= version.Version(MINIMUM_COMMON), (
289
+ f'pin {match.group(1)} < {MINIMUM_COMMON}: that release has no phase for '
290
+ f'analysis/post-processing and still splits one job into two traceIds')
291
+
292
+
293
+ def test_registered_rules_are_reachable():
294
+ """Runs for real: import the lib and read the registry. Skips without the SDK."""
295
+ package_name = PACKAGE.name
296
+ pytest.importorskip(package_name)
297
+ __import__(package_name)
298
+ from quapp_common.util.error_classifier import registered_rules
299
+
300
+ codes = {rule.code for rule in registered_rules()}
301
+ own = {code for code in codes if not code.startswith(
302
+ ('INPUT_', 'DEPENDENCY_', 'PROVIDER_', 'HANDLER_', 'PLATFORM_',
303
+ 'COMPILATION_', 'EXECUTION_', 'ANALYSIS_', 'POST_'))}
304
+ assert own, f'no lib-specific rule registered, only generic ones: {sorted(codes)}'
@@ -1 +0,0 @@
1
- from .factory.qibo_handler_factory import QiboHandlerFactory
File without changes