optio-core 0.2.0__tar.gz → 0.2.2__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 (63) hide show
  1. {optio_core-0.2.0/src/optio_core.egg-info → optio_core-0.2.2}/PKG-INFO +1 -1
  2. {optio_core-0.2.0 → optio_core-0.2.2}/pyproject.toml +1 -1
  3. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/__init__.py +5 -2
  4. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/_engine_service.py +2 -0
  5. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/_generated/optio_engine.py +1 -0
  6. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/lifecycle.py +24 -2
  7. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/models.py +13 -0
  8. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/store.py +21 -0
  9. {optio_core-0.2.0 → optio_core-0.2.2/src/optio_core.egg-info}/PKG-INFO +1 -1
  10. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core.egg-info/SOURCES.txt +1 -0
  11. optio_core-0.2.2/tests/test_group_cancel_purge.py +106 -0
  12. {optio_core-0.2.0 → optio_core-0.2.2}/LICENSE +0 -0
  13. {optio_core-0.2.0 → optio_core-0.2.2}/README.md +0 -0
  14. {optio_core-0.2.0 → optio_core-0.2.2}/setup.cfg +0 -0
  15. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/_force_cancel.py +0 -0
  16. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/_launch_block_store.py +0 -0
  17. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/context.py +0 -0
  18. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/exceptions.py +0 -0
  19. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/executor.py +0 -0
  20. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/migrations/__init__.py +0 -0
  21. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/migrations/m001_status_subdocument.py +0 -0
  22. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/migrations/m002_backfill_child_metadata.py +0 -0
  23. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/migrations/m003_backfill_has_saved_state.py +0 -0
  24. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/migrations/m004_create_expire_at_ttl_index.py +0 -0
  25. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/progress_helpers.py +0 -0
  26. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/scheduler.py +0 -0
  27. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core/state_machine.py +0 -0
  28. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core.egg-info/dependency_links.txt +0 -0
  29. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core.egg-info/requires.txt +0 -0
  30. {optio_core-0.2.0 → optio_core-0.2.2}/src/optio_core.egg-info/top_level.txt +0 -0
  31. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_cancel_propagation.py +0 -0
  32. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_cancel_race_parent_overwrite.py +0 -0
  33. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_child_failure_cancel_distinction.py +0 -0
  34. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_child_failure_structured.py +0 -0
  35. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_child_progress.py +0 -0
  36. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_client_directed_events.py +0 -0
  37. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_context_blob.py +0 -0
  38. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_context_resume.py +0 -0
  39. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_deadline_cancel.py +0 -0
  40. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_deadline_cancel_launchguard.py +0 -0
  41. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_engine_service.py +0 -0
  42. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_engine_service_resolve.py +0 -0
  43. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_executor.py +0 -0
  44. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_group_cancel.py +0 -0
  45. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_heartbeat.py +0 -0
  46. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_launch_guard.py +0 -0
  47. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_lifecycle_reconciliation.py +0 -0
  48. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_migration_m003.py +0 -0
  49. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_migration_m004.py +0 -0
  50. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_models.py +0 -0
  51. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_no_redis.py +0 -0
  52. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_outcomes.py +0 -0
  53. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_parallel.py +0 -0
  54. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_persistent_launch_blocks.py +0 -0
  55. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_progress_helpers.py +0 -0
  56. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_progress_throttle.py +0 -0
  57. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_resync.py +0 -0
  58. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_resync_cancel_stale.py +0 -0
  59. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_scheduler.py +0 -0
  60. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_state_machine.py +0 -0
  61. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_store.py +0 -0
  62. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_task_ttl.py +0 -0
  63. {optio_core-0.2.0 → optio_core-0.2.2}/tests/test_widget_primitives.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: optio-core
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Reusable async process management library
5
5
  Author-email: Kristof Csillag <kristof.csillag@deai-labs.com>
6
6
  License-Expression: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "optio-core"
7
- version = "0.2.0"
7
+ version = "0.2.2"
8
8
  description = "Reusable async process management library"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -2,7 +2,7 @@
2
2
 
3
3
  from optio_core.models import (
4
4
  TaskInstance, TaskInstanceCore, ChildResult, LaunchBlocked,
5
- LaunchOutcome, CancelOutcome, DismissOutcome,
5
+ LaunchOutcome, CancelOutcome, DismissOutcome, MongoStore,
6
6
  )
7
7
  from optio_core.lifecycle import Optio
8
8
 
@@ -34,7 +34,7 @@ __all__ = [
34
34
  "get_process", "list_processes",
35
35
  "block_launches", "unblock_launches",
36
36
  "group_cancel", "group_cancel_and_wait",
37
- "rpc_server",
37
+ "rpc_server", "store", "MongoStore",
38
38
  ]
39
39
 
40
40
 
@@ -47,4 +47,7 @@ def __getattr__(name: str):
47
47
  """
48
48
  if name == "rpc_server":
49
49
  return _instance.rpc_server
50
+ if name == "store":
51
+ # Runtime-populated: the (db, prefix) binding only exists after init().
52
+ return _instance.store
50
53
  raise AttributeError(f"module 'optio_core' has no attribute {name!r}")
@@ -126,6 +126,7 @@ class OptioEngineService(OptioEngineServiceBase):
126
126
  block_new_launches=bool(params.block_new_launches),
127
127
  persist=bool(params.persist),
128
128
  reason=params.reason,
129
+ purge_records=bool(params.purge_records),
129
130
  )
130
131
  return GroupCancelResult.model_validate({"ok": True, "cancelledCount": count})
131
132
 
@@ -141,6 +142,7 @@ class OptioEngineService(OptioEngineServiceBase):
141
142
  block_new_launches=bool(params.block_new_launches),
142
143
  persist=bool(params.persist),
143
144
  reason=params.reason,
145
+ purge_records=bool(params.purge_records),
144
146
  )
145
147
  return GroupCancelAndWaitResult.model_validate({"ok": True, "cancelledCount": count})
146
148
 
@@ -246,6 +246,7 @@ class GroupCancelParams(BaseModel):
246
246
  block_new_launches: bool | None = Field(None, alias="blockNewLaunches")
247
247
  persist: bool | None = None
248
248
  reason: str | None = None
249
+ purge_records: bool | None = Field(None, alias="purgeRecords")
249
250
 
250
251
 
251
252
  class GroupCancelResult1(BaseModel):
@@ -26,14 +26,14 @@ except ImportError:
26
26
  Redis = None # type: ignore[assignment,misc]
27
27
 
28
28
  from optio_core.models import (
29
- TaskInstance, OptioConfig, ProcessStatus, ProcessMetadataFilter,
29
+ TaskInstance, OptioConfig, MongoStore, ProcessStatus, ProcessMetadataFilter,
30
30
  matches_filter, LaunchBlocked,
31
31
  LaunchOutcome, CancelOutcome, DismissOutcome,
32
32
  )
33
33
  from optio_core.store import (
34
34
  upsert_process, remove_stale_processes, find_stale_process_ids,
35
35
  get_process_by_process_id, update_status, clear_result_fields,
36
- append_log, compute_expire_at,
36
+ append_log, compute_expire_at, purge_processes,
37
37
  )
38
38
  from optio_core.state_machine import (
39
39
  ACTIVE_STATES, CANCELLABLE_STATES, DISMISSABLE_STATES, END_STATES,
@@ -86,6 +86,18 @@ class Optio:
86
86
  self._supervisor_task: asyncio.Task | None = None
87
87
  self._launch_blocks: dict[uuid.UUID, _BlockEntry] = {}
88
88
 
89
+ @property
90
+ def store(self) -> MongoStore:
91
+ """The (db, prefix) binding this instance was initialized with.
92
+
93
+ Hand it as-is to helpers that need db+prefix (e.g.
94
+ ``optio_claudecode.delete_seed``) instead of re-deriving the prefix
95
+ from your own config. Raises if accessed before ``init()``.
96
+ """
97
+ if self._config is None:
98
+ raise RuntimeError("optio.store accessed before init()")
99
+ return MongoStore(db=self._config.mongo_db, prefix=self._config.prefix)
100
+
89
101
  async def init(
90
102
  self,
91
103
  mongo_db: AsyncIOMotorDatabase,
@@ -668,6 +680,7 @@ class Optio:
668
680
  *,
669
681
  persist: bool = False,
670
682
  reason: str | None = None,
683
+ purge_records: bool = False,
671
684
  ) -> int:
672
685
  """Cancel every active process matching `metadata_filter`. Does NOT
673
686
  wait for terminal state.
@@ -694,6 +707,10 @@ class Optio:
694
707
  )
695
708
  )
696
709
  pending_ids = await self._group_cancel_issue(metadata_filter, block_new_launches)
710
+ if purge_records:
711
+ await purge_processes(
712
+ self._config.mongo_db, self._config.prefix, metadata_filter,
713
+ )
697
714
  return len(pending_ids)
698
715
 
699
716
  async def group_cancel_and_wait(
@@ -703,6 +720,7 @@ class Optio:
703
720
  *,
704
721
  persist: bool = False,
705
722
  reason: str | None = None,
723
+ purge_records: bool = False,
706
724
  ) -> int:
707
725
  """Cancel every active process matching `metadata_filter` and wait
708
726
  for all of them to reach a terminal state. See
@@ -752,6 +770,10 @@ class Optio:
752
770
  f"(filter={metadata_filter})"
753
771
  )
754
772
  await asyncio.sleep(0.1)
773
+ if purge_records:
774
+ await purge_processes(
775
+ self._config.mongo_db, self._config.prefix, metadata_filter,
776
+ )
755
777
  return len(pending)
756
778
 
757
779
  async def dismiss(self, process_id: str) -> DismissOutcome:
@@ -151,6 +151,19 @@ class OptioConfig:
151
151
  cancel_grace_seconds: float = 5.0
152
152
 
153
153
 
154
+ @dataclass(frozen=True)
155
+ class MongoStore:
156
+ """The (db, prefix) pair identifying where optio's Mongo data lives.
157
+
158
+ Exposed by an initialized Optio instance (``optio.store``) so consumers can
159
+ hand the whole binding to helpers that need db+prefix (e.g. seed APIs)
160
+ instead of threading the two through their own config. ``db`` is the motor
161
+ AsyncIOMotorDatabase; ``prefix`` is the collection/stream namespace.
162
+ """
163
+ db: Any
164
+ prefix: str
165
+
166
+
154
167
  @dataclass
155
168
  class BasicAuth:
156
169
  username: str
@@ -291,6 +291,27 @@ async def delete_process(
291
291
  await _collection(db, prefix).delete_one({"_id": proc["_id"]})
292
292
 
293
293
 
294
+ async def purge_processes(
295
+ db: AsyncIOMotorDatabase,
296
+ prefix: str,
297
+ metadata_filter: dict,
298
+ ) -> int:
299
+ """Delete every process record whose metadata matches `metadata_filter`
300
+ (flat AND-equality), plus all descendants of the matched records. Used by
301
+ group-cancel teardown when the caller owns the matched processes wholesale
302
+ (dataspace / customer deletion). Returns the number of docs deleted."""
303
+ coll = _collection(db, prefix)
304
+ query = {f"metadata.{k}": v for k, v in metadata_filter.items()}
305
+ matched = await coll.find(query, {"_id": 1}).to_list(None)
306
+ deleted = 0
307
+ for m in matched:
308
+ deleted += await delete_descendants(db, prefix, m["_id"])
309
+ if matched:
310
+ result = await coll.delete_many({"_id": {"$in": [m["_id"] for m in matched]}})
311
+ deleted += result.deleted_count
312
+ return deleted
313
+
314
+
294
315
  async def clear_result_fields(
295
316
  db: AsyncIOMotorDatabase, prefix: str, process_oid: ObjectId,
296
317
  ) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: optio-core
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Reusable async process management library
5
5
  Author-email: Kristof Csillag <kristof.csillag@deai-labs.com>
6
6
  License-Expression: Apache-2.0
@@ -39,6 +39,7 @@ tests/test_engine_service.py
39
39
  tests/test_engine_service_resolve.py
40
40
  tests/test_executor.py
41
41
  tests/test_group_cancel.py
42
+ tests/test_group_cancel_purge.py
42
43
  tests/test_heartbeat.py
43
44
  tests/test_launch_guard.py
44
45
  tests/test_lifecycle_reconciliation.py
@@ -0,0 +1,106 @@
1
+ """Tests for the `purge_records` flag on group_cancel / group_cancel_and_wait.
2
+
3
+ Spec: docs/superpowers/specs/2026-05-31-delete-customer-design.md (section 1).
4
+
5
+ When `purge_records=True`, the group-cancel family deletes every process
6
+ record matching the metadata filter (roots + descendants) after the in-scope
7
+ pids reach a terminal state. With the default `purge_records=False`, records
8
+ are left in place (back-compat).
9
+ """
10
+ import asyncio
11
+
12
+ import pytest
13
+
14
+ from optio_core.lifecycle import Optio
15
+ from optio_core.models import TaskInstance
16
+
17
+ pytestmark = pytest.mark.asyncio
18
+
19
+
20
+ async def _start_optio(mongo_db, prefix, tasks, cancel_grace_seconds=2.0):
21
+ """Helper: init an Optio with the given tasks, return (optio, run_task)."""
22
+ async def gen(_s, _f):
23
+ return list(tasks)
24
+ optio = Optio()
25
+ await optio.init(
26
+ mongo_db=mongo_db, prefix=prefix,
27
+ get_task_definitions=gen,
28
+ cancel_grace_seconds=cancel_grace_seconds,
29
+ )
30
+ run_task = asyncio.create_task(optio.run())
31
+ return optio, run_task
32
+
33
+
34
+ async def _stop_optio(optio, run_task):
35
+ await optio.shutdown()
36
+ run_task.cancel()
37
+ try:
38
+ await run_task
39
+ except (asyncio.CancelledError, Exception):
40
+ pass
41
+
42
+
43
+ def _make_cooperative(started: asyncio.Event):
44
+ async def fn(ctx):
45
+ started.set()
46
+ for _ in range(200):
47
+ if ctx.cancellation_flag.is_set():
48
+ return
49
+ await asyncio.sleep(0.02)
50
+ return fn
51
+
52
+
53
+ async def test_group_cancel_and_wait_purges_records_when_flag_set(mongo_db):
54
+ """purge_records=True deletes every process record matching the filter
55
+ after the in-scope pids reach a terminal state."""
56
+ started = [asyncio.Event() for _ in range(2)]
57
+ tasks = [
58
+ TaskInstance(
59
+ process_id=f"p.d1.{i}", name=f"D1-{i}", params={},
60
+ execute=_make_cooperative(started[i]), metadata={"dataspace": "d1"},
61
+ )
62
+ for i in range(2)
63
+ ]
64
+
65
+ optio, run_task = await _start_optio(mongo_db, "gcp_purge", tasks)
66
+ try:
67
+ for i in range(2):
68
+ await optio.launch(f"p.d1.{i}", session_id=None)
69
+ for ev in started:
70
+ await ev.wait()
71
+
72
+ await optio.group_cancel_and_wait(
73
+ {"dataspace": "d1"}, block_new_launches=True, purge_records=True,
74
+ )
75
+
76
+ count = await mongo_db["gcp_purge_processes"].count_documents(
77
+ {"metadata.dataspace": "d1"}
78
+ )
79
+ assert count == 0
80
+ finally:
81
+ await _stop_optio(optio, run_task)
82
+
83
+
84
+ async def test_group_cancel_and_wait_keeps_records_by_default(mongo_db):
85
+ """Default purge_records=False leaves the cancelled records in place."""
86
+ started = asyncio.Event()
87
+ task = TaskInstance(
88
+ process_id="p.d2", name="D2", params={},
89
+ execute=_make_cooperative(started), metadata={"dataspace": "d2"},
90
+ )
91
+
92
+ optio, run_task = await _start_optio(mongo_db, "gcp_keep", [task])
93
+ try:
94
+ await optio.launch("p.d2", session_id=None)
95
+ await started.wait()
96
+
97
+ await optio.group_cancel_and_wait(
98
+ {"dataspace": "d2"}, block_new_launches=True,
99
+ )
100
+
101
+ count = await mongo_db["gcp_keep_processes"].count_documents(
102
+ {"metadata.dataspace": "d2"}
103
+ )
104
+ assert count >= 1
105
+ finally:
106
+ await _stop_optio(optio, run_task)
File without changes
File without changes
File without changes