jcmsuite 7.4.2__py3-none-any.whl

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.

Potentially problematic release.


This version of jcmsuite might be problematic. Click here for more details.

Files changed (119) hide show
  1. jcmsuite/__init__.py +22 -0
  2. jcmsuite/_build_info.py +4 -0
  3. jcmsuite/context/__init__.py +44 -0
  4. jcmsuite/context/interface_metadata.py +158 -0
  5. jcmsuite/context/models.py +213 -0
  6. jcmsuite/context/scope.py +210 -0
  7. jcmsuite/context/solver_context.py +569 -0
  8. jcmsuite/context/subprocess_tools.py +172 -0
  9. jcmsuite/contract.py +47 -0
  10. jcmsuite/daemon/__init__.py +19 -0
  11. jcmsuite/daemon/handler.py +925 -0
  12. jcmsuite/daemon/internal/__init__.py +4 -0
  13. jcmsuite/daemon/internal/bundle_protocol.py +64 -0
  14. jcmsuite/daemon/internal/handler_helpers.py +368 -0
  15. jcmsuite/daemon/internal/handler_resource_jobs.py +227 -0
  16. jcmsuite/daemon/internal/handler_wait.py +564 -0
  17. jcmsuite/daemon/models.py +349 -0
  18. jcmsuite/daemon/namespaces.py +250 -0
  19. jcmsuite/daemon/protocol.py +801 -0
  20. jcmsuite/daemon/resources.py +407 -0
  21. jcmsuite/daemon/scope.py +53 -0
  22. jcmsuite/daemon/transport.py +223 -0
  23. jcmsuite/editing/__init__.py +10 -0
  24. jcmsuite/editing/editor.py +274 -0
  25. jcmsuite/exceptions/__init__.py +32 -0
  26. jcmsuite/exceptions/errors.py +173 -0
  27. jcmsuite/geo/__init__.py +10 -0
  28. jcmsuite/geo/mesh.py +302 -0
  29. jcmsuite/info/__init__.py +10 -0
  30. jcmsuite/info/runtime_info.py +45 -0
  31. jcmsuite/internal/__init__.py +2 -0
  32. jcmsuite/internal/diagnostics.py +74 -0
  33. jcmsuite/internal/progress.py +80 -0
  34. jcmsuite/loaders/__init__.py +18 -0
  35. jcmsuite/loaders/blob_header.py +315 -0
  36. jcmsuite/loaders/cartesian_field_reader.py +249 -0
  37. jcmsuite/loaders/cartesian_fields.py +324 -0
  38. jcmsuite/loaders/nested_mapping.py +69 -0
  39. jcmsuite/loaders/powerflux.py +136 -0
  40. jcmsuite/loaders/table.py +637 -0
  41. jcmsuite/logging/__init__.py +22 -0
  42. jcmsuite/logging/runtime.py +210 -0
  43. jcmsuite/results/__init__.py +126 -0
  44. jcmsuite/results/artifacts.py +806 -0
  45. jcmsuite/results/discovery.py +82 -0
  46. jcmsuite/results/field_exports.py +1062 -0
  47. jcmsuite/results/field_payloads.py +453 -0
  48. jcmsuite/results/internal/__init__.py +4 -0
  49. jcmsuite/results/internal/export/__init__.py +4 -0
  50. jcmsuite/results/internal/export/common.py +56 -0
  51. jcmsuite/results/internal/export/pandas/__init__.py +4 -0
  52. jcmsuite/results/internal/export/pandas/common.py +165 -0
  53. jcmsuite/results/internal/export/pandas/payload.py +480 -0
  54. jcmsuite/results/internal/export/pandas/rows.py +484 -0
  55. jcmsuite/results/internal/export/preview.py +178 -0
  56. jcmsuite/results/internal/export/xarray/__init__.py +4 -0
  57. jcmsuite/results/internal/export/xarray/export.py +688 -0
  58. jcmsuite/results/internal/export/xarray/grouped.py +608 -0
  59. jcmsuite/results/internal/export/xarray/headers.py +290 -0
  60. jcmsuite/results/internal/export/xarray/runtime.py +99 -0
  61. jcmsuite/results/internal/export/xarray/shared.py +246 -0
  62. jcmsuite/results/internal/export/xarray/tabular.py +304 -0
  63. jcmsuite/results/internal/export/xarray/types.py +28 -0
  64. jcmsuite/results/internal/models/__init__.py +4 -0
  65. jcmsuite/results/internal/models/batch.py +1081 -0
  66. jcmsuite/results/internal/models/batch_postprocess.py +1728 -0
  67. jcmsuite/results/internal/models/bundle.py +343 -0
  68. jcmsuite/results/internal/models/core.py +556 -0
  69. jcmsuite/results/internal/models/submission.py +1157 -0
  70. jcmsuite/results/internal/resultbag/__init__.py +4 -0
  71. jcmsuite/results/internal/resultbag/access.py +489 -0
  72. jcmsuite/results/internal/resultbag/iteration.py +199 -0
  73. jcmsuite/results/internal/resultbag/query.py +226 -0
  74. jcmsuite/results/internal/resultbag/raw.py +407 -0
  75. jcmsuite/results/internal/resultbag/source_files.py +142 -0
  76. jcmsuite/results/internal/resultbag/store.py +881 -0
  77. jcmsuite/results/internal/resultbag/utils.py +258 -0
  78. jcmsuite/results/models.py +76 -0
  79. jcmsuite/results/opening.py +55 -0
  80. jcmsuite/results/pandas_export.py +325 -0
  81. jcmsuite/results/payloads.py +478 -0
  82. jcmsuite/results/postprocess.py +97 -0
  83. jcmsuite/results/postprocess_payloads.py +757 -0
  84. jcmsuite/results/postprocess_record.py +418 -0
  85. jcmsuite/results/resultbag.py +279 -0
  86. jcmsuite/results/resultbag_management.py +433 -0
  87. jcmsuite/results/structured_derivatives.py +387 -0
  88. jcmsuite/solve/__init__.py +10 -0
  89. jcmsuite/solve/daemon_bundle_staging.py +100 -0
  90. jcmsuite/solve/daemon_bundle_wait.py +89 -0
  91. jcmsuite/solve/orchestration.py +1576 -0
  92. jcmsuite/solve/orchestration_execution.py +180 -0
  93. jcmsuite/solve/orchestration_routing.py +128 -0
  94. jcmsuite/solve/orchestration_support.py +323 -0
  95. jcmsuite/solve/path_resolution.py +98 -0
  96. jcmsuite/solve/progress.py +20 -0
  97. jcmsuite/solve/solver.py +709 -0
  98. jcmsuite/solve/staging.py +264 -0
  99. jcmsuite/solve/types.py +25 -0
  100. jcmsuite/solve/working_dirs.py +227 -0
  101. jcmsuite/templates/__init__.py +11 -0
  102. jcmsuite/templates/converter.py +71 -0
  103. jcmsuite/templates/formatting.py +335 -0
  104. jcmsuite/templates/formatting_cli.py +47 -0
  105. jcmsuite/templates/rendering.py +666 -0
  106. jcmsuite/torch/__init__.py +25 -0
  107. jcmsuite/torch/autograd.py +488 -0
  108. jcmsuite/torch/coordinate_policy.py +82 -0
  109. jcmsuite/torch/core.py +312 -0
  110. jcmsuite/torch/results/__init__.py +51 -0
  111. jcmsuite/torch/types.py +84 -0
  112. jcmsuite/torch/validation.py +208 -0
  113. jcmsuite/torch/wrappers.py +4332 -0
  114. jcmsuite/view/__init__.py +10 -0
  115. jcmsuite/view/viewer.py +122 -0
  116. jcmsuite-7.4.2.dist-info/METADATA +180 -0
  117. jcmsuite-7.4.2.dist-info/RECORD +119 -0
  118. jcmsuite-7.4.2.dist-info/WHEEL +4 -0
  119. jcmsuite-7.4.2.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,564 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (c) 2001-2026 JCMwave GmbH
3
+
4
+ """Internal daemon wait/poll orchestration helpers."""
5
+
6
+ from __future__ import annotations
7
+
8
+ import asyncio
9
+ import importlib
10
+ import time
11
+ from collections.abc import Coroutine, Sequence
12
+ from pathlib import Path
13
+ from typing import TYPE_CHECKING, cast
14
+
15
+ from ...exceptions import (
16
+ BackendUnavailableError,
17
+ ConfigMappingError,
18
+ InvalidConfigurationError,
19
+ SolveExecutionError,
20
+ )
21
+ from ...internal.diagnostics import parse_source_position
22
+ from ...results import (
23
+ BatchSolveEntry,
24
+ BatchSolveResult,
25
+ BundledSolveResult,
26
+ SolveResult,
27
+ )
28
+ from ..models import PendingDaemonJob
29
+ from ..protocol import build_close_job_command, build_job_info_command
30
+ from .handler_helpers import (
31
+ WaitRequest,
32
+ build_wait_request,
33
+ coerce_int,
34
+ coerce_log_message,
35
+ convert_wait_outputs,
36
+ create_progress_bar,
37
+ extract_invalid_job_id,
38
+ map_execution_source_path,
39
+ normalize_job_record,
40
+ normalize_status_values,
41
+ resolve_result_base_dir,
42
+ )
43
+
44
+ if TYPE_CHECKING:
45
+ from ...results import BatchSolveSubmission, SolveSubmission
46
+ from ..handler import DaemonHandler, WaitBreakWhen, WaitOnError, WaitOnTimeout
47
+
48
+
49
+ def _extract_return_value(payload: object) -> object:
50
+ """Resolve return-value extraction via handler module for patch compatibility."""
51
+
52
+ handler_module = importlib.import_module("jcmsuite.daemon.handler")
53
+ return handler_module.extract_return_value(payload)
54
+
55
+
56
+ async def _sleep(seconds: float) -> None:
57
+ """Sleep via handler module namespace for test patch compatibility."""
58
+
59
+ handler_module = importlib.import_module("jcmsuite.daemon.handler")
60
+ await handler_module.asyncio.sleep(seconds)
61
+
62
+
63
+ def wait(
64
+ handler: "DaemonHandler",
65
+ submission: "SolveSubmission | BatchSolveSubmission",
66
+ *,
67
+ asynchronous: bool = False,
68
+ break_when: "WaitBreakWhen" = "all",
69
+ timeout: float | None = None,
70
+ on_timeout: "WaitOnTimeout" = "raise",
71
+ on_error: "WaitOnError" = "raise",
72
+ progress: bool = False,
73
+ progress_label: str | None = None,
74
+ ) -> (
75
+ SolveResult
76
+ | BatchSolveResult
77
+ | Coroutine[object, object, SolveResult | BatchSolveResult]
78
+ ):
79
+ """Wait for daemon jobs and collect outputs."""
80
+
81
+ cached_batch_result: BatchSolveResult | None = getattr(
82
+ submission, "_resolved_result", None
83
+ )
84
+ if cached_batch_result is not None:
85
+ batch_submission: "BatchSolveSubmission" = cast(
86
+ "BatchSolveSubmission", submission
87
+ )
88
+ live_submission: "BatchSolveSubmission" = batch_submission.__class__(
89
+ entries=batch_submission.entries
90
+ )._bind_runtime(waiter=handler)
91
+ cached_wait_request: WaitRequest = build_wait_request(live_submission)
92
+ handler._restore_pending_jobs_from_submission(cached_wait_request)
93
+
94
+ async def _wait_and_merge_cached_batch() -> BatchSolveResult:
95
+ live_result: SolveResult | BatchSolveResult = (
96
+ await handler._wait_with_request(
97
+ wait_request=cached_wait_request,
98
+ break_when=break_when,
99
+ timeout=timeout,
100
+ on_timeout=on_timeout,
101
+ on_error=on_error,
102
+ progress=progress,
103
+ progress_label=progress_label,
104
+ )
105
+ )
106
+ if not isinstance(live_result, BatchSolveResult):
107
+ raise InvalidConfigurationError(
108
+ "daemon.wait() returned invalid batch result payload."
109
+ )
110
+ merged_entries: list[BatchSolveEntry] = [
111
+ *cached_batch_result.entries,
112
+ *live_result.entries,
113
+ ]
114
+ merged_entries.sort(key=lambda entry: entry.index)
115
+ return BatchSolveResult(entries=tuple(merged_entries))
116
+
117
+ if asynchronous:
118
+ return _wait_and_merge_cached_batch()
119
+ try:
120
+ asyncio.get_running_loop()
121
+ except RuntimeError:
122
+ return asyncio.run(_wait_and_merge_cached_batch())
123
+ raise RuntimeError(
124
+ "daemon.wait() called from a running event loop; use "
125
+ "`await daemon.wait(..., asynchronous=True)`."
126
+ )
127
+
128
+ wait_request: WaitRequest = build_wait_request(submission)
129
+ handler._restore_pending_jobs_from_submission(wait_request)
130
+
131
+ if asynchronous:
132
+ return wait_with_request(
133
+ handler,
134
+ wait_request=wait_request,
135
+ break_when=break_when,
136
+ timeout=timeout,
137
+ on_timeout=on_timeout,
138
+ on_error=on_error,
139
+ progress=progress,
140
+ progress_label=progress_label,
141
+ )
142
+
143
+ try:
144
+ asyncio.get_running_loop()
145
+ except RuntimeError:
146
+ return asyncio.run(
147
+ handler._wait_with_request(
148
+ wait_request=wait_request,
149
+ break_when=break_when,
150
+ timeout=timeout,
151
+ on_timeout=on_timeout,
152
+ on_error=on_error,
153
+ progress=progress,
154
+ progress_label=progress_label,
155
+ )
156
+ )
157
+
158
+ raise RuntimeError(
159
+ "daemon.wait() called from a running event loop; use "
160
+ "`await daemon.wait(..., asynchronous=True)`."
161
+ )
162
+
163
+
164
+ def restore_pending_jobs_from_submission(
165
+ handler: "DaemonHandler",
166
+ wait_request: WaitRequest,
167
+ ) -> None:
168
+ """Rehydrate missing pending-cache entries from submission handles."""
169
+
170
+ if wait_request.mode == "single_submission":
171
+ single_submission: "SolveSubmission | None" = wait_request.single_submission
172
+ if single_submission is None:
173
+ return
174
+ restore_single_pending_job(handler, single_submission)
175
+ return
176
+
177
+ batch_submission: "BatchSolveSubmission | None" = wait_request.batch_submission
178
+ if batch_submission is None:
179
+ return
180
+ submission_entry: "SolveSubmission"
181
+ for submission_entry in batch_submission.entries:
182
+ restore_single_pending_job(handler, submission_entry)
183
+
184
+
185
+ def restore_single_pending_job(
186
+ handler: "DaemonHandler",
187
+ submission: "SolveSubmission",
188
+ ) -> None:
189
+ """Insert one pending-cache entry from a submitted solve handle."""
190
+
191
+ job_id: int = submission.job_id
192
+ if job_id in handler._pending_jobs or job_id in handler._completed_jobs:
193
+ return
194
+ handler._pending_jobs[job_id] = PendingDaemonJob(
195
+ result=submission.result,
196
+ keys=None if submission.keys is None else dict(submission.keys),
197
+ resultbag=submission._resultbag,
198
+ suffix_pattern=submission._suffix_pattern,
199
+ source_project_mappings=submission._source_project_mappings,
200
+ )
201
+
202
+
203
+ async def wait_with_request(
204
+ handler: "DaemonHandler",
205
+ *,
206
+ wait_request: WaitRequest,
207
+ break_when: "WaitBreakWhen",
208
+ timeout: float | None,
209
+ on_timeout: "WaitOnTimeout",
210
+ on_error: "WaitOnError",
211
+ progress: bool,
212
+ progress_label: str | None,
213
+ ) -> SolveResult | BatchSolveResult:
214
+ """Wait and convert daemon outputs according to request input shape."""
215
+
216
+ raw_results: list[object]
217
+ raw_logs: list[str | None]
218
+ raw_results, raw_logs = await handler._wait_impl(
219
+ job_ids=wait_request.job_ids,
220
+ break_when=break_when,
221
+ timeout=timeout,
222
+ on_timeout=on_timeout,
223
+ on_error=on_error,
224
+ progress=progress,
225
+ progress_label=progress_label,
226
+ )
227
+ return convert_wait_outputs(
228
+ wait_request=wait_request,
229
+ results=raw_results,
230
+ logs=raw_logs,
231
+ )
232
+
233
+
234
+ async def wait_impl(
235
+ handler: "DaemonHandler",
236
+ *,
237
+ job_ids: Sequence[str | int] | None,
238
+ break_when: "WaitBreakWhen",
239
+ timeout: float | None,
240
+ on_timeout: "WaitOnTimeout",
241
+ on_error: "WaitOnError",
242
+ progress: bool,
243
+ progress_label: str | None,
244
+ ) -> tuple[list[object], list[str | None]]:
245
+ """Collect daemon job outputs and release completed job handles."""
246
+
247
+ if not handler._connected:
248
+ raise BackendUnavailableError(
249
+ "Cannot wait for daemon jobs: daemon handler is not connected."
250
+ )
251
+ if break_when not in ("all", "any"):
252
+ raise InvalidConfigurationError(
253
+ f"Unsupported break_when policy: {break_when!r}"
254
+ )
255
+ if on_timeout not in ("raise", "return"):
256
+ raise InvalidConfigurationError(
257
+ f"Unsupported on_timeout policy: {on_timeout!r}"
258
+ )
259
+ if on_error not in ("raise", "collect"):
260
+ raise InvalidConfigurationError(f"Unsupported on_error policy: {on_error!r}")
261
+ if timeout is not None and timeout < 0.0:
262
+ raise InvalidConfigurationError("timeout must be non-negative.")
263
+
264
+ if handler._transport_connection is None:
265
+ return handler._wait_fallback_impl(job_ids=job_ids)
266
+
267
+ return await handler._wait_transport_impl(
268
+ job_ids=job_ids,
269
+ break_when=break_when,
270
+ timeout=timeout,
271
+ on_timeout=on_timeout,
272
+ on_error=on_error,
273
+ progress=progress,
274
+ progress_label=progress_label,
275
+ )
276
+
277
+
278
+ def wait_fallback_impl(
279
+ handler: "DaemonHandler",
280
+ *,
281
+ job_ids: Sequence[str | int] | None,
282
+ ) -> tuple[list[object], list[str | None]]:
283
+ """Collect cached fallback daemon job outputs and release them."""
284
+
285
+ resolved_job_ids: list[int] = resolve_job_ids(
286
+ handler,
287
+ raw_job_ids=job_ids,
288
+ available_job_ids=list(handler._completed_jobs.keys()),
289
+ )
290
+
291
+ results: list[object] = []
292
+ logs: list[str | None] = []
293
+ job_id: int
294
+ for job_id in resolved_job_ids:
295
+ pending_job: PendingDaemonJob
296
+ cached_log: str
297
+ pending_job, cached_log = handler._completed_jobs.pop(job_id)
298
+ handler._attach_daemon_log_to_result(
299
+ pending_job=pending_job,
300
+ log_message=cached_log,
301
+ )
302
+ handler._persist_resultbag_entry_if_needed(
303
+ job_id=job_id,
304
+ pending_job=pending_job,
305
+ log_message=cached_log,
306
+ )
307
+ results.append(pending_job.result)
308
+ logs.append(cached_log)
309
+ return results, logs
310
+
311
+
312
+ async def wait_transport_impl(
313
+ handler: "DaemonHandler",
314
+ *,
315
+ job_ids: Sequence[str | int] | None,
316
+ break_when: "WaitBreakWhen",
317
+ timeout: float | None,
318
+ on_timeout: "WaitOnTimeout",
319
+ on_error: "WaitOnError",
320
+ progress: bool,
321
+ progress_label: str | None = None,
322
+ ) -> tuple[list[object], list[str | None]]:
323
+ """Collect daemon transport job outputs through real `JobInfo` polling."""
324
+
325
+ available_job_ids: list[int] = list(handler._pending_jobs.keys()) + list(
326
+ handler._completed_jobs.keys()
327
+ )
328
+ resolved_job_ids: list[int] = resolve_job_ids(
329
+ handler,
330
+ raw_job_ids=job_ids,
331
+ available_job_ids=available_job_ids,
332
+ )
333
+ if not resolved_job_ids:
334
+ return [], []
335
+
336
+ poll_start: float = time.monotonic()
337
+ remaining_job_ids: set[int] = set(resolved_job_ids)
338
+ completed_job_ids: list[int] = []
339
+ invalid_job_messages: dict[int, str] = {}
340
+ progress_bar = create_progress_bar(
341
+ total=len(resolved_job_ids),
342
+ enabled=progress and len(resolved_job_ids) > 1,
343
+ description=progress_label or "daemon.wait",
344
+ )
345
+ result_by_job_id: dict[int, object] = {}
346
+ log_by_job_id: dict[int, str] = {}
347
+
348
+ def collect_finished_job(resolved_job_id: int) -> None:
349
+ """Fetch and persist one daemon entry when the daemon marks it finished."""
350
+
351
+ if resolved_job_id in invalid_job_messages:
352
+ pending_job_for_release: PendingDaemonJob | None = (
353
+ handler._pending_jobs.pop(resolved_job_id, None)
354
+ )
355
+ if (
356
+ pending_job_for_release is not None
357
+ and pending_job_for_release.resultbag is not None
358
+ ):
359
+ pending_job_for_release.resultbag.management.release_job(
360
+ resolved_job_id
361
+ )
362
+ result_by_job_id[resolved_job_id] = None
363
+ log_by_job_id[resolved_job_id] = invalid_job_messages[resolved_job_id]
364
+ return
365
+ job_payload: object = _extract_return_value(
366
+ handler._send_transport_command(
367
+ build_job_info_command(
368
+ job_ids=[resolved_job_id],
369
+ status_only=False,
370
+ )
371
+ )
372
+ )
373
+ job_record: dict[str, object] = normalize_job_record(job_payload)
374
+ job_exit_code: int = coerce_int(
375
+ job_record.get("ExitCode", 1),
376
+ field_name="daemon job exit code",
377
+ )
378
+ job_log_payload: object = job_record.get("Log", "")
379
+ log_message: str = coerce_log_message(job_log_payload)
380
+ pending_job: PendingDaemonJob | None = handler._pending_jobs.get(
381
+ resolved_job_id
382
+ )
383
+ if pending_job is None:
384
+ completed_entry: tuple[PendingDaemonJob, str] | None = (
385
+ handler._completed_jobs.get(resolved_job_id)
386
+ )
387
+ if completed_entry is not None:
388
+ pending_job = completed_entry[0]
389
+ if pending_job is None:
390
+ raise InvalidConfigurationError(
391
+ f"Daemon job {resolved_job_id} is not tracked locally."
392
+ )
393
+ if job_exit_code != 0:
394
+ job_error: SolveExecutionError = SolveExecutionError(
395
+ backend="daemon",
396
+ job_id=resolved_job_id,
397
+ exit_code=job_exit_code,
398
+ log=log_message,
399
+ job_info=job_record,
400
+ )
401
+ if on_error == "collect":
402
+ handler._pending_jobs.pop(resolved_job_id, None)
403
+ if pending_job.resultbag is not None:
404
+ pending_job.resultbag.management.release_job(resolved_job_id)
405
+ result_by_job_id[resolved_job_id] = None
406
+ log_by_job_id[resolved_job_id] = log_message
407
+ progress_bar.mark_failure(1)
408
+ return
409
+ _ = handler._send_transport_command(
410
+ build_close_job_command(job_ids=[resolved_job_id]),
411
+ )
412
+ handler._pending_jobs.pop(resolved_job_id, None)
413
+ if pending_job.resultbag is not None:
414
+ pending_job.resultbag.management.release_job(resolved_job_id)
415
+ base_dir: Path | None = resolve_result_base_dir(pending_job)
416
+ source_path: Path | None
417
+ line: int | None
418
+ column: int | None
419
+ source_path, line, column = parse_source_position(
420
+ log_message,
421
+ base_dir=base_dir,
422
+ )
423
+ if source_path is not None:
424
+ source_path = map_execution_source_path(
425
+ source_path,
426
+ pending_job.source_project_mappings,
427
+ )
428
+ message: str = "Daemon job failed with source-mapped diagnostics."
429
+ if isinstance(pending_job.result, BundledSolveResult):
430
+ message = str(job_error)
431
+ raise ConfigMappingError(
432
+ message,
433
+ source=source_path,
434
+ line=line,
435
+ column=column,
436
+ ) from job_error
437
+ progress_bar.mark_failure(1)
438
+ raise job_error
439
+
440
+ handler._attach_daemon_log_to_result(
441
+ pending_job=pending_job,
442
+ log_message=log_message,
443
+ )
444
+ handler._persist_resultbag_entry_if_needed(
445
+ job_id=resolved_job_id,
446
+ pending_job=pending_job,
447
+ log_message=log_message,
448
+ )
449
+ result_by_job_id[resolved_job_id] = pending_job.result
450
+ log_by_job_id[resolved_job_id] = log_message
451
+ handler._pending_jobs.pop(resolved_job_id, None)
452
+
453
+ try:
454
+ while remaining_job_ids:
455
+ if timeout is not None and (time.monotonic() - poll_start) >= timeout:
456
+ if on_timeout == "raise":
457
+ raise TimeoutError("daemon.wait() timed out before completion.")
458
+ break
459
+ try:
460
+ status_payload: object = _extract_return_value(
461
+ handler._send_transport_command(
462
+ build_job_info_command(
463
+ job_ids=sorted(remaining_job_ids),
464
+ status_only=True,
465
+ )
466
+ )
467
+ )
468
+ except BackendUnavailableError as exception:
469
+ invalid_job_id: int | None = extract_invalid_job_id(str(exception))
470
+ if (
471
+ on_error == "collect"
472
+ and invalid_job_id is not None
473
+ and invalid_job_id in remaining_job_ids
474
+ ):
475
+ remaining_job_ids.remove(invalid_job_id)
476
+ completed_job_ids.append(invalid_job_id)
477
+ invalid_job_messages[invalid_job_id] = str(exception)
478
+ collect_finished_job(invalid_job_id)
479
+ progress_bar.mark_failure(1)
480
+ progress_bar.update(1)
481
+ if break_when == "any":
482
+ break
483
+ continue
484
+ raise
485
+ job_statuses: list[str] = normalize_status_values(status_payload)
486
+ finished_job_ids: list[int] = [
487
+ job_id
488
+ for job_id, status_value in zip(sorted(remaining_job_ids), job_statuses)
489
+ if status_value in {"Finished", "Failed"}
490
+ ]
491
+ if not finished_job_ids:
492
+ await _sleep(0.1)
493
+ continue
494
+
495
+ finished_job_id: int
496
+ for finished_job_id in finished_job_ids:
497
+ remaining_job_ids.remove(finished_job_id)
498
+ completed_job_ids.append(finished_job_id)
499
+ progress_bar.update(len(finished_job_ids))
500
+ jobs_to_collect: list[int] = (
501
+ finished_job_ids if break_when == "all" else finished_job_ids[:1]
502
+ )
503
+ for finished_job_id in jobs_to_collect:
504
+ collect_finished_job(finished_job_id)
505
+ if break_when == "any":
506
+ break
507
+
508
+ # Each finished job is collected in the polling loop below. This makes
509
+ # Resultbag entries visible while a batch is still solving instead of
510
+ # waiting for every requested job to finish.
511
+ close_job_ids: list[int]
512
+ if break_when == "all" and timeout is None:
513
+ close_job_ids = list(resolved_job_ids)
514
+ elif break_when == "all":
515
+ close_job_ids = list(completed_job_ids)
516
+ else:
517
+ close_job_ids = list(completed_job_ids[:1])
518
+ if close_job_ids:
519
+ _ = handler._send_transport_command(
520
+ build_close_job_command(job_ids=close_job_ids),
521
+ )
522
+ # Completion order is nondeterministic, but batch conversion is positional.
523
+ # Preserve every requested job slot so completed values cannot shift keys.
524
+ return (
525
+ [result_by_job_id.get(job_id) for job_id in resolved_job_ids],
526
+ [log_by_job_id.get(job_id) for job_id in resolved_job_ids],
527
+ )
528
+ finally:
529
+ progress_bar.close()
530
+
531
+
532
+ def resolve_job_ids(
533
+ handler: "DaemonHandler",
534
+ *,
535
+ raw_job_ids: Sequence[str | int] | None,
536
+ available_job_ids: Sequence[int],
537
+ ) -> list[int]:
538
+ """Normalize and validate requested daemon job identifiers."""
539
+
540
+ available_ids_set: set[int] = set(available_job_ids)
541
+ resolved_job_ids: list[int]
542
+ if raw_job_ids is None:
543
+ # Preserve submission/cache order; set iteration would make batch
544
+ # entries depend on completion timing and hash layout.
545
+ resolved_job_ids = list(available_job_ids)
546
+ else:
547
+ resolved_job_ids = []
548
+ raw_job_id: str | int
549
+ for raw_job_id in raw_job_ids:
550
+ try:
551
+ parsed_job_id: int = int(raw_job_id)
552
+ except (TypeError, ValueError) as exception:
553
+ raise InvalidConfigurationError(
554
+ f"Invalid job id {raw_job_id!r}; expected int-like identifier."
555
+ ) from exception
556
+ resolved_job_ids.append(parsed_job_id)
557
+
558
+ job_id: int
559
+ for job_id in resolved_job_ids:
560
+ if job_id not in available_ids_set:
561
+ raise InvalidConfigurationError(
562
+ f"Job id {job_id} is not available in daemon cache."
563
+ )
564
+ return resolved_job_ids