value-stream 0.1.1__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.
Files changed (81) hide show
  1. value_stream/__init__.py +1 -0
  2. value_stream/app/SPEC.md +79 -0
  3. value_stream/app/__init__.py +1 -0
  4. value_stream/app/__main__.py +28 -0
  5. value_stream/app/contracts.py +18 -0
  6. value_stream/app/coordinator.py +359 -0
  7. value_stream/app/errors.py +17 -0
  8. value_stream/app/exports.py +142 -0
  9. value_stream/app/gateway.py +130 -0
  10. value_stream/app/generation.py +269 -0
  11. value_stream/app/insights.py +75 -0
  12. value_stream/app/metrics.py +132 -0
  13. value_stream/app/openapi.json +3699 -0
  14. value_stream/app/schemas.py +288 -0
  15. value_stream/app/server.py +298 -0
  16. value_stream/app/settings.py +46 -0
  17. value_stream/app/storage.py +651 -0
  18. value_stream/client/__init__.py +3 -0
  19. value_stream/client/simulation_runner.py +83 -0
  20. value_stream/client/views/__init__.py +4 -0
  21. value_stream/client/views/metadata_viewer.py +235 -0
  22. value_stream/client/views/result_viewer.py +76 -0
  23. value_stream/client/views/viewer.py +8 -0
  24. value_stream/client/web/__init__.py +10 -0
  25. value_stream/client/web/client.py +115 -0
  26. value_stream/client/web/errors.py +29 -0
  27. value_stream/client/web/local_service.py +92 -0
  28. value_stream/factory/__init__.py +5 -0
  29. value_stream/factory/developer_factory.py +33 -0
  30. value_stream/factory/factory.py +43 -0
  31. value_stream/factory/task_factory.py +64 -0
  32. value_stream/factory/task_generator.py +102 -0
  33. value_stream/policy/__init__.py +3 -0
  34. value_stream/policy/simulation_policy.py +20 -0
  35. value_stream/resources/__init__.py +23 -0
  36. value_stream/resources/developer.py +53 -0
  37. value_stream/resources/qa_tester.py +51 -0
  38. value_stream/resources/resource.py +162 -0
  39. value_stream/resources/resource_metadata.py +24 -0
  40. value_stream/resources/resource_policy.py +13 -0
  41. value_stream/resources/resource_pool.py +39 -0
  42. value_stream/resources/resource_tracker.py +160 -0
  43. value_stream/resources/toolchain.py +43 -0
  44. value_stream/service/SPEC.md +46 -0
  45. value_stream/service/__init__.py +1 -0
  46. value_stream/service/app.py +218 -0
  47. value_stream/service/codec.py +237 -0
  48. value_stream/service/job_store.py +322 -0
  49. value_stream/service/openapi.json +1283 -0
  50. value_stream/service/scheduler.py +234 -0
  51. value_stream/service/schemas.py +248 -0
  52. value_stream/service/settings.py +37 -0
  53. value_stream/service/worker.py +82 -0
  54. value_stream/simulation/__init__.py +16 -0
  55. value_stream/simulation/default_simulation_policy.py +68 -0
  56. value_stream/simulation/model.py +30 -0
  57. value_stream/simulation/model_factory.py +54 -0
  58. value_stream/simulation/simulation.py +144 -0
  59. value_stream/simulation/simulation_metadata.py +12 -0
  60. value_stream/simulation/simulation_result.py +20 -0
  61. value_stream/task/__init__.py +23 -0
  62. value_stream/task/epoch.py +21 -0
  63. value_stream/task/event_status.py +8 -0
  64. value_stream/task/task.py +260 -0
  65. value_stream/task/task_event.py +97 -0
  66. value_stream/task/task_history.py +208 -0
  67. value_stream/task/task_router.py +70 -0
  68. value_stream/task/task_state.py +7 -0
  69. value_stream/task/task_type.py +8 -0
  70. value_stream/workflow/__init__.py +18 -0
  71. value_stream/workflow/assignment_strategy.py +10 -0
  72. value_stream/workflow/pool_manager.py +136 -0
  73. value_stream/workflow/resource_operator.py +204 -0
  74. value_stream/workflow/sdlc_workflow.py +99 -0
  75. value_stream/workflow/support_workflow.py +23 -0
  76. value_stream/workflow/task_store.py +125 -0
  77. value_stream/workflow/workflow_policy.py +12 -0
  78. value_stream-0.1.1.dist-info/METADATA +288 -0
  79. value_stream-0.1.1.dist-info/RECORD +81 -0
  80. value_stream-0.1.1.dist-info/WHEEL +4 -0
  81. value_stream-0.1.1.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,651 @@
1
+ """Replaceable, bounded workspace storage. Locks protect atomic mutations."""
2
+
3
+ from typing import Protocol, Optional
4
+
5
+ from collections import OrderedDict
6
+ from dataclasses import dataclass, field
7
+ from threading import RLock
8
+ from uuid import UUID, uuid4
9
+ import time
10
+
11
+ from value_stream.service.schemas import ResultData
12
+ from .errors import AppError
13
+ from .generation import canonical, fingerprint, materialize_tasks, expand_scenarios
14
+ from .schemas import (
15
+ Workspace,
16
+ TaskSet,
17
+ RunStatus,
18
+ OutcomeSummary,
19
+ TERMINAL,
20
+ Preview,
21
+ ENGINE_VERSION,
22
+ GENERATOR_VERSION,
23
+ RunRequest,
24
+ ConcreteScenario,
25
+ EditorRequest,
26
+ )
27
+ from .settings import AppSettings
28
+
29
+
30
+ @dataclass
31
+ class RunRecord:
32
+ """Combines a run status with its retained results."""
33
+
34
+ status: RunStatus
35
+ task_set: TaskSet
36
+ request: RunRequest
37
+ request_hash: str
38
+ cache_keys: list[str]
39
+ results: dict[UUID, str] = field(default_factory=dict)
40
+ service_indices: list[int] = field(default_factory=list)
41
+ service_cursor: int = 0
42
+ reservation: int = 0
43
+ result_bytes: int = 0
44
+ created_at: float = field(default_factory=time.monotonic)
45
+
46
+
47
+ class WorkspaceStore(Protocol):
48
+ """Defines storage operations for workspaces and runs."""
49
+
50
+ settings: AppSettings
51
+
52
+ def create(self) -> Workspace: ...
53
+ def get(self, workspace_id: UUID) -> Workspace: ...
54
+ def run(self, workspace_id: UUID, run_id: UUID) -> RunRecord: ...
55
+ def save_editor(self, workspace_id: UUID, request: EditorRequest) -> Preview: ...
56
+ def preview(self, workspace_id: UUID) -> Preview: ...
57
+ def register(
58
+ self,
59
+ workspace_id: UUID,
60
+ request: RunRequest,
61
+ scenarios: list[ConcreteScenario],
62
+ task_set: TaskSet,
63
+ ) -> tuple[RunRecord, bool]: ...
64
+ def retain_result(self, record: RunRecord, index: int, result: ResultData) -> None: ...
65
+ def previous(self, workspace: Workspace, request: RunRequest) -> RunRecord | None: ...
66
+ def cached_result(self, key: str) -> ResultData: ...
67
+ def result(self, record: RunRecord, scenario_id: UUID) -> ResultData: ...
68
+ def finish(self, workspace_id: UUID, record: RunRecord) -> None: ...
69
+ def active_records(self) -> list[RunRecord]: ...
70
+ def comparison(self, workspace_id: UUID, run_id: UUID, action: str) -> Workspace: ...
71
+ def delete_workspace(self, workspace_id: UUID) -> None: ...
72
+ def delete_task_set(
73
+ self, workspace_id: UUID, task_set_id: UUID, expected_revision: int
74
+ ) -> None: ...
75
+
76
+
77
+ class InMemoryWorkspaceStore:
78
+ """Stores workspaces and run data in memory."""
79
+
80
+ def __init__(self, settings: Optional[AppSettings] = None) -> None:
81
+ """Initialize the in-memory workspace store.
82
+
83
+ Args:
84
+ settings (Optional[AppSettings]): Settings to validate or use.
85
+ """
86
+ self.settings = settings or AppSettings()
87
+ self.workspaces: dict[UUID, Workspace] = {}
88
+ self.runs: dict[UUID, RunRecord] = {}
89
+ self.cache: OrderedDict[str, tuple[ResultData, int]] = OrderedDict()
90
+ self.lock = RLock()
91
+
92
+ def create(self) -> Workspace:
93
+ """Create a workspace with its initial task set.
94
+
95
+ Raises:
96
+ AppError: If the workspace or task set cannot be created within configured limits.
97
+
98
+ Returns:
99
+ Workspace: The new workspace.
100
+ """
101
+ with self.lock:
102
+ if len(self.workspaces) >= self.settings.max_workspaces:
103
+ raise AppError(
104
+ "APP_CAPACITY",
105
+ "Workspace limit reached. Delete an unused workspace.",
106
+ 429,
107
+ )
108
+ workspace = Workspace()
109
+ self.workspaces[workspace.id] = workspace
110
+ return workspace
111
+
112
+ def get(self, workspace_id: UUID) -> Workspace:
113
+ """Return the workspace with the requested identifier.
114
+
115
+ Args:
116
+ workspace_id (UUID): Identifier of the workspace.
117
+
118
+ Raises:
119
+ AppError: If the workspace does not exist.
120
+
121
+ Returns:
122
+ Workspace: The stored workspace.
123
+ """
124
+ try:
125
+ return self.workspaces[workspace_id]
126
+ except KeyError as exc:
127
+ raise AppError(
128
+ "WORKSPACE_NOT_FOUND",
129
+ "This temporary workspace is unavailable. The server may have restarted.",
130
+ 404,
131
+ ) from exc
132
+
133
+ def run(self, workspace_id: UUID, run_id: UUID) -> RunRecord:
134
+ """Return a run record from the requested workspace.
135
+
136
+ Args:
137
+ workspace_id (UUID): Identifier of the workspace.
138
+ run_id (UUID): Identifier of the simulation run.
139
+
140
+ Raises:
141
+ AppError: If the workspace or run does not exist.
142
+
143
+ Returns:
144
+ RunRecord: The matching run record.
145
+ """
146
+ w = self.get(workspace_id)
147
+ if run_id not in {r.id for r in w.runs}:
148
+ raise AppError("RUN_NOT_FOUND", "Run not found", 404)
149
+ return self.runs[run_id]
150
+
151
+ def delete_workspace(self, workspace_id: UUID) -> None:
152
+ """Delete the requested workspace.
153
+
154
+ Args:
155
+ workspace_id (UUID): Identifier of the workspace.
156
+
157
+ Raises:
158
+ AppError: If the workspace does not exist or cannot be removed.
159
+ """
160
+ with self.lock:
161
+ w = self.get(workspace_id)
162
+ if any(r.state not in TERMINAL for r in w.runs):
163
+ raise AppError(
164
+ "RUN_ACTIVE",
165
+ "Cancel active runs before deleting the workspace",
166
+ 409,
167
+ )
168
+ for r in w.runs:
169
+ self.runs.pop(r.id, None)
170
+ del self.workspaces[workspace_id]
171
+ self.prune_cache()
172
+
173
+ def active_records(self) -> list[RunRecord]:
174
+ """Return the active run records.
175
+
176
+ Returns:
177
+ list[RunRecord]: Active run records in the workspace.
178
+ """
179
+ return [r for r in self.runs.values() if r.status.state not in TERMINAL]
180
+
181
+ def cached_result(self, key: str) -> ResultData:
182
+ """Return a cached simulation result.
183
+
184
+ Args:
185
+ key (str): Cache key to look up.
186
+
187
+ Returns:
188
+ ResultData: Cached result for the supplied cache key, if present.
189
+ """
190
+ self.cache.move_to_end(key)
191
+ return self.cache[key][0]
192
+
193
+ def delete_task_set(
194
+ self, workspace_id: UUID, task_set_id: UUID, expected_revision: int
195
+ ) -> None:
196
+ """Delete the requested task set.
197
+
198
+ Args:
199
+ workspace_id (UUID): Identifier of the workspace.
200
+ task_set_id (UUID): Identifier of the task set.
201
+ expected_revision (int): Revision expected by the client.
202
+
203
+ Raises:
204
+ AppError: If the task set is missing, current, or has changed since it was read.
205
+ """
206
+ with self.lock:
207
+ w = self.get(workspace_id)
208
+ if w.revision != expected_revision:
209
+ raise AppError("REVISION_CONFLICT", "Reload before deleting this task set", 409)
210
+ if task_set_id == w.current_task_set or any(
211
+ r.task_set_id == task_set_id for r in w.runs
212
+ ):
213
+ raise AppError(
214
+ "REVISION_CONFLICT",
215
+ "Task set is used by the editor or a comparison",
216
+ 409,
217
+ )
218
+ if not any(t.id == task_set_id for t in w.task_sets):
219
+ raise AppError("RESULT_NOT_FOUND", "Task set not found", 404)
220
+ w.task_sets = [t for t in w.task_sets if t.id != task_set_id]
221
+ w.revision += 1
222
+
223
+ def save_editor(self, workspace_id: UUID, request: EditorRequest) -> Preview:
224
+ """Save editor changes and validate the preview.
225
+
226
+ Args:
227
+ workspace_id (UUID): Identifier of the workspace.
228
+ request (EditorRequest): Request data to process.
229
+
230
+ Raises:
231
+ AppError: If the workspace revision conflicts or the proposed inputs are invalid or over limit.
232
+
233
+ Returns:
234
+ Preview: The resulting value.
235
+ """
236
+ with self.lock:
237
+ w = self.get(workspace_id)
238
+ if request.expected_revision != w.revision:
239
+ raise AppError(
240
+ "REVISION_CONFLICT",
241
+ "Inputs changed in another tab. Reload the workspace.",
242
+ 409,
243
+ )
244
+ if request.task_spec.count > self.settings.max_tasks:
245
+ raise AppError("LIMIT_EXCEEDED", f"Task count exceeds {self.settings.max_tasks}")
246
+ if len({d.id for d in request.definitions}) != len(request.definitions):
247
+ raise AppError("INVALID_INPUT", "Definition IDs must be unique")
248
+ scenarios = expand_scenarios(request.definitions, self.settings)
249
+ spec_hash = fingerprint(request.task_spec.model_dump())
250
+ task_set = next(
251
+ (t for t in w.task_sets if fingerprint(t.spec.model_dump()) == spec_hash),
252
+ None,
253
+ )
254
+ if task_set is None:
255
+ if len(w.task_sets) >= self.settings.max_task_sets:
256
+ raise AppError(
257
+ "APP_CAPACITY",
258
+ "Task-set limit reached. Remove unused task sets or start a new workspace.",
259
+ 429,
260
+ )
261
+ tasks = materialize_tasks(request.task_spec)
262
+ task_set = TaskSet(
263
+ spec=request.task_spec.model_copy(deep=True),
264
+ tasks=tasks,
265
+ content_hash=fingerprint([t.model_dump() for t in tasks]),
266
+ )
267
+ candidate = w.model_copy()
268
+ candidate.task_sets = list(w.task_sets)
269
+ candidate.task_spec = request.task_spec.model_copy(deep=True)
270
+ candidate.definitions = [d.model_copy(deep=True) for d in request.definitions]
271
+ if task_set.id not in {t.id for t in candidate.task_sets}:
272
+ candidate.task_sets.append(task_set)
273
+ candidate.current_task_set = task_set.id
274
+ if w.current_task_set != task_set.id:
275
+ candidate.baseline_id = candidate.latest_id = None
276
+ candidate.revision += 1
277
+ size = sum(
278
+ self.input_size(other) for other in self.workspaces.values() if other.id != w.id
279
+ ) + self.input_size(candidate)
280
+ if size > self.settings.max_input_bytes:
281
+ raise AppError("APP_CAPACITY", "Input storage limit reached", 429)
282
+ self.workspaces[w.id] = candidate
283
+ # Runs are shared immutable snapshots, not deep-copied mutable statuses.
284
+ candidate.runs = [self.runs[r.id].status for r in w.runs]
285
+ return self._preview(candidate, scenarios)
286
+
287
+ @staticmethod
288
+ def input_size(w: Workspace):
289
+ # Run summaries include their frozen model configurations, never raw results.
290
+ return len(canonical(w.model_dump(mode="json")).encode())
291
+
292
+ def _preview(self, w: Workspace, scenarios: list[ConcreteScenario]) -> Preview:
293
+ """Build a preview for concrete scenarios.
294
+
295
+ Args:
296
+ w (Workspace): W.
297
+ scenarios (list[ConcreteScenario]): Concrete scenarios to preview.
298
+
299
+ Raises:
300
+ AppError: If no task set has been saved for the workspace.
301
+
302
+ Returns:
303
+ Preview: Preview of the concrete scenarios.
304
+ """
305
+ if w.current_task_set is None:
306
+ raise AppError("INVALID_INPUT", "Save and preview inputs first")
307
+ digest = fingerprint(
308
+ [
309
+ str(w.current_task_set),
310
+ w.revision,
311
+ [s.model_dump(mode="json") for s in scenarios],
312
+ ]
313
+ )
314
+ return Preview(
315
+ digest=digest,
316
+ task_set_id=w.current_task_set,
317
+ workspace_revision=w.revision,
318
+ scenarios=scenarios,
319
+ count=len(scenarios),
320
+ )
321
+
322
+ def preview(self, workspace_id: UUID) -> Preview:
323
+ """Build a preview of the proposed changes.
324
+
325
+ Args:
326
+ workspace_id (UUID): Identifier of the workspace.
327
+
328
+ Returns:
329
+ Preview: Preview of the current workspace inputs.
330
+ """
331
+ w = self.get(workspace_id)
332
+ return self._preview(w, expand_scenarios(w.definitions, self.settings))
333
+
334
+ def previous(self, workspace: Workspace, request: RunRequest) -> RunRecord | None:
335
+ """Find the previous matching run.
336
+
337
+ Args:
338
+ workspace (Workspace): Workspace to update.
339
+ request (RunRequest): Request data to process.
340
+
341
+ Raises:
342
+ AppError: If the workspace has no matching run.
343
+
344
+ Returns:
345
+ RunRecord | None: Matching run record, or None when no prior run matches.
346
+ """
347
+ hashed = fingerprint(request.model_dump(mode="json"))
348
+ for status in workspace.runs:
349
+ record = self.runs[status.id]
350
+ if record.request.request_id == request.request_id:
351
+ if record.request_hash != hashed:
352
+ raise AppError(
353
+ "SUBMISSION_CONFLICT",
354
+ "Request ID was used with different inputs",
355
+ 409,
356
+ )
357
+ return record
358
+ return None
359
+
360
+ def register(
361
+ self,
362
+ workspace_id: UUID,
363
+ request: RunRequest,
364
+ scenarios: list[ConcreteScenario],
365
+ task_set: TaskSet,
366
+ ) -> tuple[RunRecord, bool]:
367
+ """Register a simulation run.
368
+
369
+ Args:
370
+ workspace_id (UUID): Identifier of the workspace.
371
+ request (RunRequest): Request data to process.
372
+ scenarios (list[ConcreteScenario]): Concrete scenarios to preview.
373
+ task_set (TaskSet): Task set used to create the run.
374
+
375
+ Raises:
376
+ AppError: If the workspace is missing or capacity limits prevent registration.
377
+
378
+ Returns:
379
+ tuple[RunRecord, bool]: The resulting value.
380
+ """
381
+ with self.lock:
382
+ w = self.get(workspace_id)
383
+ previous = self.previous(w, request)
384
+ if previous:
385
+ return previous, False
386
+ if any(r.state not in TERMINAL for r in w.runs):
387
+ raise AppError("RUN_ACTIVE", "Cancel or finish the current run first", 409)
388
+ if (
389
+ sum(r.status.state not in TERMINAL for r in self.runs.values())
390
+ >= self.settings.max_active_runs
391
+ ):
392
+ raise AppError("APP_CAPACITY", "All application execution slots are occupied", 429)
393
+ if (
394
+ request.intent == "interactive"
395
+ and w.runs
396
+ and time.monotonic() - self.runs[w.runs[-1].id].created_at
397
+ < self.settings.min_interactive_seconds
398
+ ):
399
+ raise AppError(
400
+ "APP_CAPACITY",
401
+ "Wait a moment before the next interactive comparison",
402
+ 429,
403
+ )
404
+ self.prune_history(w)
405
+ keys = [
406
+ fingerprint(
407
+ [
408
+ ENGINE_VERSION,
409
+ GENERATOR_VERSION,
410
+ task_set.content_hash,
411
+ s.model.model_dump(mode="json"),
412
+ s.execution_seed,
413
+ ]
414
+ )
415
+ for s in scenarios
416
+ ]
417
+ missing = [i for i, key in enumerate(keys) if key not in self.cache]
418
+ reservation = min(
419
+ self.settings.max_run_bytes,
420
+ self.settings.max_model_bytes * len(missing),
421
+ )
422
+ self.prune_cache(required=reservation, protect=set(keys))
423
+ self.prune_for_capacity(reservation, set(keys))
424
+ used = sum(v[1] for v in self.cache.values()) + sum(
425
+ r.reservation for r in self.runs.values()
426
+ )
427
+ if used + reservation > self.settings.max_result_bytes:
428
+ raise AppError(
429
+ "APP_CAPACITY",
430
+ "Result memory is full. Unpin or delete comparisons before running more models.",
431
+ 429,
432
+ )
433
+ status = RunStatus(
434
+ id=uuid4(),
435
+ name=request.name,
436
+ task_set_id=task_set.id,
437
+ state="submitting",
438
+ intent=request.intent,
439
+ outcomes=[OutcomeSummary(scenario=s.model_copy(deep=True)) for s in scenarios],
440
+ )
441
+ record = RunRecord(
442
+ status=status,
443
+ task_set=task_set,
444
+ request=request.model_copy(deep=True),
445
+ request_hash=fingerprint(request.model_dump(mode="json")),
446
+ cache_keys=keys,
447
+ service_indices=missing,
448
+ reservation=reservation,
449
+ )
450
+ input_bytes = sum(self.input_size(other) for other in self.workspaces.values())
451
+ if input_bytes + len(status.model_dump_json().encode()) > self.settings.max_input_bytes:
452
+ raise AppError(
453
+ "APP_CAPACITY",
454
+ "Input snapshot memory is full. Delete old comparisons.",
455
+ 429,
456
+ )
457
+ self.runs[status.id] = record
458
+ w.runs.append(status)
459
+ if request.intent == "interactive":
460
+ w.latest_id = status.id
461
+ return record, True
462
+
463
+ def retain_result(self, record: RunRecord, index: int, result: ResultData) -> None:
464
+ """Retain the result for a completed scenario.
465
+
466
+ Args:
467
+ record (RunRecord): Run record to update.
468
+ index (int): Index of the scenario or model.
469
+ result (ResultData): Simulation result to retain.
470
+
471
+ Raises:
472
+ AppError: If the result cannot be retained for the run.
473
+ """
474
+ with self.lock:
475
+ key = record.cache_keys[index]
476
+ size = len(result.model_dump_json().encode())
477
+ if (
478
+ size > self.settings.max_model_bytes
479
+ or record.result_bytes + size > self.settings.max_run_bytes
480
+ ):
481
+ raise AppError(
482
+ "RESULT_TOO_LARGE",
483
+ "Model results exceed the configured storage limit",
484
+ 422,
485
+ )
486
+ if key not in self.cache:
487
+ if size > record.reservation:
488
+ raise AppError("RESULT_TOO_LARGE", "Run exceeded its reserved result budget")
489
+ record.reservation -= size
490
+ self.cache[key] = (result, size)
491
+ self.cache.move_to_end(key)
492
+ record.result_bytes += size
493
+ record.results[record.status.outcomes[index].scenario.id] = key
494
+
495
+ def result(self, record: RunRecord, scenario_id: UUID) -> ResultData:
496
+ """Return the retained simulation result.
497
+
498
+ Args:
499
+ record (RunRecord): Run record to update.
500
+ scenario_id (UUID): Identifier of the scenario.
501
+
502
+ Raises:
503
+ AppError: If the requested scenario result is not available.
504
+
505
+ Returns:
506
+ ResultData: Retained result for the requested scenario.
507
+ """
508
+ key = record.results.get(scenario_id)
509
+ if key is None or key not in self.cache:
510
+ raise AppError("RESULT_NOT_FOUND", "This scenario has no completed result", 404)
511
+ self.cache.move_to_end(key)
512
+ return self.cache[key][0]
513
+
514
+ def finish(self, workspace_id: UUID, record: RunRecord) -> None:
515
+ """Mark the run as finished.
516
+
517
+ Args:
518
+ workspace_id (UUID): Identifier of the workspace.
519
+ record (RunRecord): Run record to update.
520
+ """
521
+ record.reservation = 0
522
+ w = self.get(workspace_id)
523
+ if (
524
+ any(o.status == "succeeded" for o in record.status.outcomes)
525
+ and not record.status.superseded
526
+ ):
527
+ if w.baseline_id is None and w.current_task_set == record.task_set.id:
528
+ w.baseline_id = record.status.id
529
+ if record.request.intent == "manual" and w.baseline_id != record.status.id:
530
+ w.latest_id = record.status.id
531
+
532
+ def comparison(self, workspace_id: UUID, run_id: UUID, action: str) -> Workspace:
533
+ """Apply a comparison operation to the workspace.
534
+
535
+ Args:
536
+ workspace_id (UUID): Identifier of the workspace.
537
+ run_id (UUID): Identifier of the simulation run.
538
+ action (str): Comparison action to apply.
539
+
540
+ Raises:
541
+ AppError: If the workspace or comparison action is invalid.
542
+
543
+ Returns:
544
+ Workspace: The resulting value.
545
+ """
546
+ with self.lock:
547
+ w = self.get(workspace_id)
548
+ r = self.run(workspace_id, run_id)
549
+ if r.status.state not in TERMINAL:
550
+ raise AppError("RUN_ACTIVE", "Wait until the comparison finishes", 409)
551
+ if action in {"pin", "baseline"} and not r.results:
552
+ raise AppError("INVALID_INPUT", "A comparison needs at least one successful result")
553
+ if action == "pin":
554
+ if not r.status.pinned and sum(x.pinned for x in w.runs) >= self.settings.max_pins:
555
+ raise AppError(
556
+ "COMPARISON_LIMIT",
557
+ "Unpin a comparison before pinning another",
558
+ 409,
559
+ )
560
+ r.status.pinned = True
561
+ elif action == "unpin":
562
+ r.status.pinned = False
563
+ elif action == "baseline":
564
+ w.baseline_id = run_id
565
+ w.current_task_set = r.task_set.id
566
+ w.task_spec = r.task_set.spec.model_copy(deep=True)
567
+ if w.latest_id and self.runs[w.latest_id].task_set.id != r.task_set.id:
568
+ w.latest_id = None
569
+ w.revision += 1
570
+ elif action == "delete":
571
+ w.runs = [x for x in w.runs if x.id != run_id]
572
+ self.runs.pop(run_id)
573
+ if w.baseline_id == run_id:
574
+ w.baseline_id = None
575
+ if w.latest_id == run_id:
576
+ w.latest_id = None
577
+ self.prune_cache()
578
+ return w
579
+
580
+ def prune_history(self, w: Workspace) -> None:
581
+ """Remove old run history.
582
+
583
+ Args:
584
+ w (Workspace): W.
585
+
586
+ Raises:
587
+ AppError: If run history cannot be pruned while preserving active records.
588
+ """
589
+ protected = {w.baseline_id, w.latest_id}
590
+ for r in list(w.runs):
591
+ if len(w.runs) < self.settings.max_retained_runs:
592
+ break
593
+ if r.state in TERMINAL and not r.pinned and r.id not in protected:
594
+ w.runs.remove(r)
595
+ self.runs.pop(r.id)
596
+ if len(w.runs) >= self.settings.max_retained_runs:
597
+ raise AppError("APP_CAPACITY", "Run history is full. Delete a comparison.", 429)
598
+
599
+ def prune_for_capacity(self, required: int, protect: set[str]) -> None:
600
+ """Remove retained data to free capacity.
601
+
602
+ Args:
603
+ required (int): Required capacity.
604
+ protect (set[str]): Cache keys that must be retained.
605
+ """
606
+
607
+ def used():
608
+ return sum(v[1] for v in self.cache.values()) + sum(
609
+ r.reservation for r in self.runs.values()
610
+ )
611
+
612
+ candidates = sorted(
613
+ [
614
+ (w, status)
615
+ for w in self.workspaces.values()
616
+ for status in w.runs
617
+ if status.state in TERMINAL
618
+ and not status.pinned
619
+ and status.id not in {w.baseline_id, w.latest_id}
620
+ ],
621
+ key=lambda pair: self.runs[pair[1].id].created_at,
622
+ )
623
+ for workspace, status in candidates:
624
+ if used() + required <= self.settings.max_result_bytes:
625
+ break
626
+ workspace.runs.remove(status)
627
+ self.runs.pop(status.id)
628
+ self.prune_cache(required=required, protect=protect)
629
+
630
+ def prune_cache(self, required: int = 0, protect: Optional[set[str]] = None) -> None:
631
+ """Remove cached results until capacity is available.
632
+
633
+ Args:
634
+ required (int): Required capacity.
635
+ protect (Optional[set[str]]): Cache keys that must be retained.
636
+ """
637
+ references = {key for r in self.runs.values() for key in r.results.values()}
638
+ active_keys = {
639
+ key
640
+ for r in self.runs.values()
641
+ if r.status.state not in TERMINAL
642
+ for key in r.cache_keys
643
+ }
644
+ used = sum(v[1] for v in self.cache.values()) + sum(
645
+ r.reservation for r in self.runs.values()
646
+ )
647
+ for key in list(self.cache):
648
+ if key not in references | active_keys | (protect or set()) and (
649
+ required == 0 or used + required > self.settings.max_result_bytes
650
+ ):
651
+ used -= self.cache.pop(key)[1]
@@ -0,0 +1,3 @@
1
+ from .simulation_runner import SimulationRunner
2
+
3
+ __all__ = ["SimulationRunner"]