Coverage for src/copytrading_engine/host/self_test/store.py: 77%

419 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Durable self-test workflows owned by one dedicated worker thread.""" 

2 

3import asyncio 

4import concurrent.futures 

5import contextlib 

6import json 

7import sqlite3 

8import threading 

9import uuid 

10from collections.abc import Callable, Generator 

11from datetime import UTC, datetime 

12from typing import Any 

13 

14from copytrading_engine.host.errors import ( 

15 ForeignInstallation, 

16 IdentityConflict, 

17 InstallationAlreadyRunning, 

18 InvalidCommand, 

19 InvalidTransition, 

20 StoreClosed, 

21 StoreUnavailable, 

22 UnknownSchemaVersion, 

23 WorkflowNotFound, 

24) 

25from copytrading_engine.host.installation import Installation 

26from copytrading_engine.host.self_test.model import ( 

27 DestinationOutcome, 

28 ParsedSelfTest, 

29 ParseRejected, 

30 SelfTestAccepted, 

31 SelfTestCompleted, 

32 SelfTestParsed, 

33 Stage, 

34 SubmitSelfTest, 

35 WorkflowAcceptance, 

36 WorkflowView, 

37) 

38from copytrading_engine.shared.sqlite import ( 

39 SchemaComponent, 

40 SchemaMismatch, 

41 ensure_schema, 

42 verify_schema, 

43) 

44 

45_PENDING_LIMIT = 100 

46SELF_TEST_SCHEMA = SchemaComponent( 

47 "self_test", 

48 1, 

49 """ 

50 CREATE TABLE IF NOT EXISTS workflows ( 

51 command_id TEXT PRIMARY KEY, canonical_command TEXT NOT NULL, 

52 stage TEXT NOT NULL CHECK (stage IN ('captured', 'parsed', 'completed', 'failed')), 

53 parsed_json TEXT, outcomes_json TEXT NOT NULL, trace_id TEXT NOT NULL UNIQUE, 

54 trace_anchor_exported INTEGER NOT NULL DEFAULT 0 

55 CHECK (trace_anchor_exported IN (0, 1)), 

56 created_at TEXT NOT NULL); 

57 CREATE TABLE IF NOT EXISTS jobs ( 

58 command_id TEXT PRIMARY KEY REFERENCES workflows(command_id), 

59 status TEXT NOT NULL CHECK (status IN ('pending', 'completed')), 

60 created_at TEXT NOT NULL); 

61 CREATE TABLE IF NOT EXISTS audit_events ( 

62 event_id INTEGER PRIMARY KEY AUTOINCREMENT, 

63 command_id TEXT NOT NULL REFERENCES workflows(command_id), 

64 event_type TEXT NOT NULL, payload_json TEXT NOT NULL, created_at TEXT NOT NULL); 

65 CREATE INDEX IF NOT EXISTS jobs_status_created ON jobs(status, created_at, command_id) 

66 """, 

67) 

68 

69 

70class SQLiteSelfTestStore: 

71 """Self-test workflow tables on one worker thread, inside an opened installation.""" 

72 

73 def __init__(self, installation: Installation) -> None: 

74 self.installation = installation 

75 self.path = installation.path 

76 self._busy_timeout_ms = installation.busy_timeout_ms 

77 self._read_only = installation.read_only 

78 self._executor = concurrent.futures.ThreadPoolExecutor( 

79 max_workers=1, thread_name_prefix="desktop-sqlite-owner" 

80 ) 

81 self._state_lock = threading.Lock() 

82 self._connection: sqlite3.Connection | None = None 

83 self._owner_thread_id: int | None = None 

84 self._entered = False 

85 self._closing = False 

86 self._closed = False 

87 self._usable = True 

88 self._close_future: concurrent.futures.Future[None] | None = None 

89 

90 def __enter__(self) -> SQLiteSelfTestStore: 

91 _ = self.installation.instance_id # the installation must already be open 

92 with self._state_lock: 

93 if self._entered or self._closed: 93 ↛ 94line 93 didn't jump to line 94 because the condition on line 93 was never true

94 raise StoreClosed("store cannot be opened more than once") 

95 self._entered = True 

96 future = self._executor.submit(self._run, self._open) 

97 try: 

98 future.result() 

99 except BaseException: 

100 cleanup = self._executor.submit(self._run, self._release_resources) 

101 try: 

102 cleanup.result() 

103 except BaseException: # noqa: BLE001 - cleanup must not mask the primary outcome 

104 pass 

105 self._executor.shutdown(wait=True) 

106 with self._state_lock: 

107 self._closed = True 

108 raise 

109 return self 

110 

111 def __exit__(self, exc_type: object, exc: object, traceback: object) -> None: 

112 self.close() 

113 

114 def accept(self, command: SubmitSelfTest) -> WorkflowAcceptance: 

115 return self._call(self._accept, command) 

116 

117 def get(self, command_id: str) -> WorkflowView: 

118 return self._call(self._get, command_id) 

119 

120 def pending(self, limit: int | None = None) -> tuple[str, ...]: 

121 return self._call(self._pending, limit) 

122 

123 def advance( 

124 self, 

125 command_id: str, 

126 expected: Stage, 

127 next_stage: Stage, 

128 outcomes: tuple[DestinationOutcome, ...], 

129 *, 

130 parsed: ParsedSelfTest | None = None, 

131 parse_rejection: ParseRejected | None = None, 

132 ) -> WorkflowView: 

133 return self._call( 

134 self._advance, 

135 command_id, 

136 expected, 

137 next_stage, 

138 outcomes, 

139 parsed, 

140 parse_rejection, 

141 ) 

142 

143 def load_command(self, command_id: str) -> SubmitSelfTest: 

144 return self._call(self._load_command, command_id) 

145 

146 def load_parsed(self, command_id: str) -> ParsedSelfTest: 

147 return self._call(self._load_parsed, command_id) 

148 

149 def counts(self) -> tuple[int, int, int]: 

150 return self._call(self._counts) 

151 

152 def mark_trace_anchor_exported(self, command_id: str) -> None: 

153 self._call(self._mark_trace_anchor_exported, command_id) 

154 

155 async def accept_async(self, command: SubmitSelfTest) -> WorkflowAcceptance: 

156 return await self._call_async(self._accept, command) 

157 

158 async def get_async(self, command_id: str) -> WorkflowView: 

159 return await self._call_async(self._get, command_id) 

160 

161 async def pending_async(self, limit: int | None = None) -> tuple[str, ...]: 

162 return await self._call_async(self._pending, limit) 

163 

164 async def advance_async( 

165 self, 

166 command_id: str, 

167 expected: Stage, 

168 next_stage: Stage, 

169 outcomes: tuple[DestinationOutcome, ...], 

170 *, 

171 parsed: ParsedSelfTest | None = None, 

172 parse_rejection: ParseRejected | None = None, 

173 ) -> WorkflowView: 

174 return await self._call_async( 

175 self._advance, 

176 command_id, 

177 expected, 

178 next_stage, 

179 outcomes, 

180 parsed, 

181 parse_rejection, 

182 ) 

183 

184 async def load_command_async(self, command_id: str) -> SubmitSelfTest: 

185 return await self._call_async(self._load_command, command_id) 

186 

187 async def load_parsed_async(self, command_id: str) -> ParsedSelfTest: 

188 return await self._call_async(self._load_parsed, command_id) 

189 

190 async def counts_async(self) -> tuple[int, int, int]: 

191 return await self._call_async(self._counts) 

192 

193 async def trace_anchor_exported_async(self, command_id: str) -> bool: 

194 return await self._call_async(self._trace_anchor_exported, command_id) 

195 

196 async def aclose(self) -> None: 

197 future = self._begin_close() 

198 if future is None: 198 ↛ 199line 198 didn't jump to line 199 because the condition on line 198 was never true

199 return 

200 try: 

201 await _await_without_abandoning(future) 

202 finally: 

203 self._finish_close() 

204 

205 def close(self) -> None: 

206 future = self._begin_close() 

207 if future is None: 207 ↛ 208line 207 didn't jump to line 208 because the condition on line 207 was never true

208 return 

209 try: 

210 future.result() 

211 finally: 

212 self._finish_close() 

213 

214 def _call[T](self, operation: Callable[..., T], *args: object) -> T: 

215 return self._schedule(operation, *args).result() 

216 

217 async def _call_async[T](self, operation: Callable[..., T], *args: object) -> T: 

218 return await _await_without_abandoning(self._schedule(operation, *args)) 

219 

220 def _schedule[T]( 

221 self, operation: Callable[..., T], *args: object 

222 ) -> concurrent.futures.Future[T]: 

223 with self._state_lock: 

224 if not self._entered or self._closing or self._closed: 

225 raise StoreClosed("store is not accepting operations") 

226 

227 def run() -> T: 

228 return self._run(lambda: operation(*args)) 

229 

230 return self._executor.submit(run) 

231 

232 def _run[T](self, operation: Callable[[], T]) -> T: 

233 self._assert_owner_thread() 

234 if not self._usable: 234 ↛ 235line 234 didn't jump to line 235 because the condition on line 234 was never true

235 raise StoreUnavailable("database outcome is uncertain; reopen the store") 

236 try: 

237 return operation() 

238 except ( 

239 IdentityConflict, 

240 InstallationAlreadyRunning, 

241 ForeignInstallation, 

242 UnknownSchemaVersion, 

243 InvalidCommand, 

244 InvalidTransition, 

245 WorkflowNotFound, 

246 StoreClosed, 

247 StoreUnavailable, 

248 ): 

249 raise 

250 except (sqlite3.Error, OSError) as exc: 

251 raise StoreUnavailable("durable storage is unavailable") from exc 

252 

253 def _assert_owner_thread(self) -> None: 

254 current_thread = threading.get_ident() 

255 if self._owner_thread_id is None: 

256 self._owner_thread_id = current_thread 

257 elif self._owner_thread_id != current_thread: 257 ↛ 258line 257 didn't jump to line 258 because the condition on line 257 was never true

258 raise RuntimeError("SQLite worker ownership was violated") 

259 

260 def _open(self) -> None: 

261 try: 

262 self._connection = connection = self.installation.connect() 

263 connection.execute("PRAGMA foreign_keys = ON") 

264 if self._read_only: 

265 try: 

266 verify_schema(connection, SELF_TEST_SCHEMA) 

267 except SchemaMismatch as exc: 

268 raise UnknownSchemaVersion("restore candidate self-test schema") from exc 

269 return 

270 connection.execute("PRAGMA journal_mode = WAL") 

271 connection.execute("PRAGMA synchronous = FULL") 

272 with self._transaction(connection): 

273 try: 

274 ensure_schema(connection, SELF_TEST_SCHEMA) 

275 except SchemaMismatch as exc: 

276 raise UnknownSchemaVersion("database self-test schema is unsupported") from exc 

277 except BaseException: 

278 self._release_resources() 

279 raise 

280 

281 @contextlib.contextmanager 

282 def _transaction( 

283 self, connection: sqlite3.Connection, *, write: bool = True 

284 ) -> Generator[None]: 

285 connection.execute("BEGIN IMMEDIATE" if write else "BEGIN") 

286 try: 

287 yield 

288 except BaseException: 

289 try: 

290 connection.execute("ROLLBACK") 

291 except sqlite3.Error: 

292 self._usable = False 

293 raise 

294 else: 

295 try: 

296 connection.execute("COMMIT") 

297 except BaseException as exc: 

298 self._usable = False 

299 try: 

300 connection.close() 

301 except sqlite3.Error: 

302 pass 

303 self._connection = None 

304 raise StoreUnavailable("commit outcome is uncertain; reopen the store") from exc 

305 

306 def _accept(self, command: SubmitSelfTest) -> WorkflowAcceptance: 

307 connection = self._require_connection() 

308 canonical = _canonical_command(command) 

309 trace_id = str(uuid.uuid4()) 

310 now = _now() 

311 newly_accepted = False 

312 with self._transaction(connection): 

313 existing = connection.execute( 

314 "SELECT canonical_command FROM workflows WHERE command_id = ?", 

315 (command.command_id,), 

316 ).fetchone() 

317 if existing is not None: 

318 if existing[0] != canonical: 

319 raise IdentityConflict("command ID belongs to different command content") 

320 else: 

321 newly_accepted = True 

322 connection.execute( 

323 "INSERT INTO workflows(command_id, canonical_command, stage, parsed_json, " 

324 "outcomes_json, trace_id, created_at) " 

325 "VALUES (?, ?, 'captured', NULL, '[]', ?, ?)", 

326 (command.command_id, canonical, trace_id, now), 

327 ) 

328 connection.execute( 

329 "INSERT INTO jobs(command_id, status, created_at) VALUES (?, 'pending', ?)", 

330 (command.command_id, now), 

331 ) 

332 accepted = SelfTestAccepted(command.command_id, trace_id) 

333 self._append_audit(connection, "SelfTestAccepted", accepted, now) 

334 view = self._read_view(connection, command.command_id) 

335 return WorkflowAcceptance(view, newly_accepted) 

336 

337 def _get(self, command_id: str) -> WorkflowView: 

338 connection = self._require_connection() 

339 with self._transaction(connection, write=False): 

340 view = self._read_view(connection, command_id) 

341 return view 

342 

343 def _pending(self, limit: int | None) -> tuple[str, ...]: 

344 if limit is not None and limit < 0: 344 ↛ 345line 344 didn't jump to line 345 because the condition on line 344 was never true

345 raise InvalidCommand("pending limit must not be negative") 

346 connection = self._require_connection() 

347 sql = "SELECT command_id FROM jobs WHERE status = 'pending' ORDER BY created_at, command_id" 

348 with self._transaction(connection, write=False): 

349 if limit is not None: 

350 sql += " LIMIT ?" 

351 rows = connection.execute(sql, (limit,)).fetchall() 

352 else: 

353 rows = connection.execute(sql).fetchall() 

354 return tuple(row[0] for row in rows) 

355 

356 def _advance( 

357 self, 

358 command_id: str, 

359 expected: Stage, 

360 next_stage: Stage, 

361 outcomes: tuple[DestinationOutcome, ...], 

362 parsed: ParsedSelfTest | None, 

363 parse_rejection: ParseRejected | None, 

364 ) -> WorkflowView: 

365 allowed = { 

366 (Stage.CAPTURED, Stage.PARSED), 

367 (Stage.CAPTURED, Stage.FAILED), 

368 (Stage.PARSED, Stage.COMPLETED), 

369 } 

370 if (expected, next_stage) not in allowed: 370 ↛ 371line 370 didn't jump to line 371 because the condition on line 370 was never true

371 raise InvalidTransition("workflow transition is not allowed") 

372 

373 connection = self._require_connection() 

374 with self._transaction(connection): 

375 row = connection.execute( 

376 "SELECT canonical_command, stage, parsed_json FROM workflows WHERE command_id = ?", 

377 (command_id,), 

378 ).fetchone() 

379 if row is None: 379 ↛ 380line 379 didn't jump to line 380 because the condition on line 379 was never true

380 raise WorkflowNotFound("workflow was not found") 

381 if row[1] != expected.value: 381 ↛ 382line 381 didn't jump to line 382 because the condition on line 381 was never true

382 raise InvalidTransition("workflow stage changed before transition") 

383 command = _decode_command(command_id, row[0]) 

384 now = _now() 

385 if next_stage is Stage.PARSED: 

386 if parsed is None or parse_rejection is not None or outcomes: 386 ↛ 387line 386 didn't jump to line 387 because the condition on line 386 was never true

387 raise InvalidTransition("parsed transition requires parsed evidence only") 

388 parsed_json = _json(_parsed_payload(parsed)) 

389 event: object = SelfTestParsed(command_id, parsed) 

390 changed = connection.execute( 

391 "UPDATE workflows SET stage = ?, parsed_json = ? " 

392 "WHERE command_id = ? AND stage = ?", 

393 (next_stage.value, parsed_json, command_id, expected.value), 

394 ).rowcount 

395 elif next_stage is Stage.FAILED: 

396 if parse_rejection != ParseRejected(command_id) or parsed is not None or outcomes: 396 ↛ 397line 396 didn't jump to line 397 because the condition on line 396 was never true

397 raise InvalidTransition("failed transition requires a parse rejection") 

398 event = parse_rejection 

399 changed = connection.execute( 

400 "UPDATE workflows SET stage = ? WHERE command_id = ? AND stage = ?", 

401 (next_stage.value, command_id, expected.value), 

402 ).rowcount 

403 updated = connection.execute( 

404 "UPDATE jobs SET status = 'completed' " 

405 "WHERE command_id = ? AND status = 'pending'", 

406 (command_id,), 

407 ).rowcount 

408 if updated != 1: 408 ↛ 409line 408 didn't jump to line 409 because the condition on line 408 was never true

409 raise InvalidTransition("pending job was not available to complete") 

410 else: 

411 if parsed is not None or parse_rejection is not None: 411 ↛ 412line 411 didn't jump to line 412 because the condition on line 411 was never true

412 raise InvalidTransition("completed transition cannot replace parsed evidence") 

413 persisted = _decode_parsed(row[2]) if row[2] else None 

414 if persisted is None: 414 ↛ 415line 414 didn't jump to line 415 because the condition on line 414 was never true

415 raise InvalidTransition("parsed evidence is required before completion") 

416 expected_accounts = command.destination_ids 

417 actual_accounts = tuple(outcome.account_id for outcome in outcomes) 

418 if actual_accounts != expected_accounts or any( 418 ↛ 421line 418 didn't jump to line 421 because the condition on line 418 was never true

419 outcome.result != "simulated" for outcome in outcomes 

420 ): 

421 raise InvalidTransition( 

422 "completion requires one simulated outcome per destination" 

423 ) 

424 event = SelfTestCompleted(command_id, outcomes) 

425 outcomes_json = _json([_outcome_payload(outcome) for outcome in outcomes]) 

426 changed = connection.execute( 

427 "UPDATE workflows SET stage = ?, outcomes_json = ? " 

428 "WHERE command_id = ? AND stage = ?", 

429 (next_stage.value, outcomes_json, command_id, expected.value), 

430 ).rowcount 

431 updated = connection.execute( 

432 "UPDATE jobs SET status = 'completed' " 

433 "WHERE command_id = ? AND status = 'pending'", 

434 (command_id,), 

435 ).rowcount 

436 if updated != 1: 436 ↛ 437line 436 didn't jump to line 437 because the condition on line 436 was never true

437 raise InvalidTransition("pending job was not available to complete") 

438 

439 if changed != 1: 439 ↛ 440line 439 didn't jump to line 440 because the condition on line 439 was never true

440 raise InvalidTransition("workflow compare-and-set failed") 

441 event_type = type(event).__name__ 

442 self._append_audit(connection, event_type, event, now) 

443 view = self._read_view(connection, command_id) 

444 return view 

445 

446 def _load_command(self, command_id: str) -> SubmitSelfTest: 

447 connection = self._require_connection() 

448 with self._transaction(connection, write=False): 

449 row = connection.execute( 

450 "SELECT canonical_command FROM workflows WHERE command_id = ?", (command_id,) 

451 ).fetchone() 

452 if row is None: 452 ↛ 453line 452 didn't jump to line 453 because the condition on line 452 was never true

453 raise WorkflowNotFound("workflow was not found") 

454 return _decode_command(command_id, row[0]) 

455 

456 def _load_parsed(self, command_id: str) -> ParsedSelfTest: 

457 connection = self._require_connection() 

458 with self._transaction(connection, write=False): 

459 row = connection.execute( 

460 "SELECT parsed_json FROM workflows WHERE command_id = ?", (command_id,) 

461 ).fetchone() 

462 if row is None: 462 ↛ 463line 462 didn't jump to line 463 because the condition on line 462 was never true

463 raise WorkflowNotFound("workflow was not found") 

464 if row[0] is None: 464 ↛ 465line 464 didn't jump to line 465 because the condition on line 464 was never true

465 raise StoreUnavailable("workflow has no persisted parse evidence") 

466 return _decode_parsed(row[0]) 

467 

468 def _counts(self) -> tuple[int, int, int]: 

469 connection = self._require_connection() 

470 with self._transaction(connection, write=False): 

471 row = connection.execute( 

472 "SELECT (SELECT COUNT(*) FROM workflows), " 

473 "(SELECT COUNT(*) FROM jobs WHERE status = 'pending'), " 

474 "(SELECT COUNT(*) FROM workflows WHERE stage = 'completed')" 

475 ).fetchone() 

476 return int(row[0]), int(row[1]), int(row[2]) 

477 

478 def _trace_anchor_exported(self, command_id: str) -> bool: 

479 connection = self._require_connection() 

480 with self._transaction(connection, write=False): 

481 row = connection.execute( 

482 "SELECT trace_anchor_exported FROM workflows WHERE command_id = ?", 

483 (command_id,), 

484 ).fetchone() 

485 if row is None: 485 ↛ 486line 485 didn't jump to line 486 because the condition on line 485 was never true

486 raise WorkflowNotFound("workflow was not found") 

487 return bool(row[0]) 

488 

489 def _mark_trace_anchor_exported(self, command_id: str) -> None: 

490 connection = self._open_receipt_connection() 

491 try: 

492 connection.execute("BEGIN IMMEDIATE") 

493 cursor = connection.execute( 

494 "UPDATE workflows SET trace_anchor_exported = 1 WHERE command_id = ?", 

495 (command_id,), 

496 ) 

497 if cursor.rowcount == 0: 497 ↛ 498line 497 didn't jump to line 498 because the condition on line 497 was never true

498 raise WorkflowNotFound("workflow was not found") 

499 connection.execute("COMMIT") 

500 except BaseException as exc: 

501 if connection.in_transaction: 

502 try: 

503 connection.execute("ROLLBACK") 

504 except sqlite3.Error: 

505 pass 

506 if isinstance(exc, (WorkflowNotFound, StoreUnavailable)): 

507 raise 

508 if isinstance(exc, sqlite3.Error): 

509 raise StoreUnavailable("diagnostics anchor receipt could not be committed") from exc 

510 raise 

511 finally: 

512 connection.close() 

513 

514 def _open_receipt_connection(self) -> sqlite3.Connection: 

515 """Open a bounded connection isolated from operational transaction state.""" 

516 connection = sqlite3.connect( 

517 self.path, 

518 timeout=self._busy_timeout_ms / 1000, 

519 isolation_level=None, 

520 ) 

521 try: 

522 connection.execute(f"PRAGMA busy_timeout = {self._busy_timeout_ms}") 

523 except BaseException: 

524 connection.close() 

525 raise 

526 return connection 

527 

528 def _read_view(self, connection: sqlite3.Connection, command_id: str) -> WorkflowView: 

529 row = connection.execute( 

530 "SELECT stage, outcomes_json, trace_id FROM workflows WHERE command_id = ?", 

531 (command_id,), 

532 ).fetchone() 

533 if row is None: 

534 raise WorkflowNotFound("workflow was not found") 

535 try: 

536 stage = Stage(row[0]) 

537 outcomes = tuple( 

538 DestinationOutcome(account_id=item["account_id"], result=item["result"]) 

539 for item in json.loads(row[1]) 

540 ) 

541 return WorkflowView(command_id, stage, outcomes, row[2]) 

542 except (InvalidCommand, KeyError, TypeError, ValueError, json.JSONDecodeError) as exc: 

543 raise StoreUnavailable("persisted workflow data is invalid") from exc 

544 

545 def _append_audit( 

546 self, connection: sqlite3.Connection, event_type: str, event: object, timestamp: str 

547 ) -> None: 

548 payload = _audit_payload(event) 

549 connection.execute( 

550 "INSERT INTO audit_events(command_id, event_type, payload_json, created_at) " 

551 "VALUES (?, ?, ?, ?)", 

552 (payload["command_id"], event_type, _json(payload), timestamp), 

553 ) 

554 

555 def _require_connection(self) -> sqlite3.Connection: 

556 if not self._usable or self._connection is None: 556 ↛ 557line 556 didn't jump to line 557 because the condition on line 556 was never true

557 raise StoreUnavailable("durable store must be reopened") 

558 return self._connection 

559 

560 def _begin_close(self) -> concurrent.futures.Future[None] | None: 

561 with self._state_lock: 

562 if self._closed: 562 ↛ 563line 562 didn't jump to line 563 because the condition on line 562 was never true

563 return None 

564 if self._close_future is not None: 564 ↛ 565line 564 didn't jump to line 565 because the condition on line 564 was never true

565 return self._close_future 

566 if not self._entered: 566 ↛ 567line 566 didn't jump to line 567 because the condition on line 566 was never true

567 self._closing = True 

568 self._closed = True 

569 self._executor.shutdown(wait=True) 

570 return None 

571 self._closing = True 

572 self._close_future = self._executor.submit(self._close_task) 

573 return self._close_future 

574 

575 def _close_task(self) -> None: 

576 self._assert_owner_thread() 

577 self._close_on_worker() 

578 

579 def _finish_close(self) -> None: 

580 self._executor.shutdown(wait=True) 

581 with self._state_lock: 

582 self._closed = True 

583 

584 def _close_on_worker(self) -> None: 

585 connection = self._connection 

586 try: 

587 if connection is not None: 

588 if not self._read_only: 

589 try: 

590 connection.execute("PRAGMA wal_checkpoint(TRUNCATE)") 

591 except sqlite3.Error: 

592 pass 

593 try: 

594 connection.close() 

595 finally: 

596 self._connection = None 

597 finally: 

598 self._usable = False 

599 

600 def _release_resources(self) -> None: 

601 connection, self._connection = self._connection, None 

602 if connection is not None: 

603 try: 

604 connection.close() 

605 except sqlite3.Error: 

606 pass 

607 

608 

609async def _await_without_abandoning[T](future: concurrent.futures.Future[T]) -> T: 

610 wrapped = asyncio.wrap_future(future) 

611 cancelled = False 

612 while True: 

613 try: 

614 result = await asyncio.shield(wrapped) 

615 break 

616 except asyncio.CancelledError: 

617 cancelled = True 

618 if wrapped.done(): 618 ↛ 619line 618 didn't jump to line 619 because the condition on line 618 was never true

619 break 

620 except BaseException: # noqa: BLE001 - keep draining until the worker settles 

621 break 

622 try: 

623 result = wrapped.result() 

624 except BaseException: 

625 if cancelled: 625 ↛ 626line 625 didn't jump to line 626 because the condition on line 625 was never true

626 raise asyncio.CancelledError from None 

627 raise 

628 if cancelled: 

629 raise asyncio.CancelledError from None 

630 return result 

631 

632 

633def _canonical_command(command: SubmitSelfTest) -> str: 

634 return _json({"text": command.text, "destination_ids": list(command.destination_ids)}) 

635 

636 

637def _decode_command(command_id: str, canonical: str) -> SubmitSelfTest: 

638 try: 

639 payload = json.loads(canonical) 

640 return SubmitSelfTest( 

641 command_id=command_id, 

642 text=payload["text"], 

643 destination_ids=tuple(payload["destination_ids"]), 

644 ) 

645 except (InvalidCommand, KeyError, TypeError, ValueError, json.JSONDecodeError) as exc: 

646 raise StoreUnavailable("persisted command is invalid") from exc 

647 

648 

649def _parsed_payload(parsed: ParsedSelfTest) -> dict[str, object]: 

650 return { 

651 "symbol": parsed.symbol, 

652 "action": parsed.action, 

653 "quantity_numerator": parsed.quantity.numerator, 

654 "quantity_denominator": parsed.quantity.denominator, 

655 "unit_price": str(parsed.unit_price), 

656 } 

657 

658 

659def _decode_parsed(value: str) -> ParsedSelfTest: 

660 from decimal import Decimal 

661 from fractions import Fraction 

662 

663 try: 

664 payload = json.loads(value) 

665 return ParsedSelfTest( 

666 symbol=payload["symbol"], 

667 action=payload["action"], 

668 quantity=Fraction(payload["quantity_numerator"], payload["quantity_denominator"]), 

669 unit_price=Decimal(payload["unit_price"]), 

670 ) 

671 except (KeyError, TypeError, ValueError, ZeroDivisionError, json.JSONDecodeError) as exc: 

672 raise StoreUnavailable("persisted parse evidence is invalid") from exc 

673 

674 

675def _outcome_payload(outcome: DestinationOutcome) -> dict[str, str]: 

676 return {"account_id": outcome.account_id, "result": outcome.result} 

677 

678 

679def _audit_payload(event: object) -> dict[str, Any]: 

680 if isinstance(event, SelfTestAccepted): 

681 return {"command_id": event.command_id, "trace_id": event.trace_id} 

682 if isinstance(event, SelfTestParsed): 

683 return {"command_id": event.command_id, "parsed": _parsed_payload(event.parsed)} 

684 if isinstance(event, SelfTestCompleted): 

685 return { 

686 "command_id": event.command_id, 

687 "outcomes": [_outcome_payload(outcome) for outcome in event.outcomes], 

688 } 

689 if isinstance(event, ParseRejected): 689 ↛ 691line 689 didn't jump to line 691 because the condition on line 689 was always true

690 return {"command_id": event.command_id, "reason": event.reason} 

691 raise InvalidTransition("unknown audit event type") 

692 

693 

694def _json(value: object) -> str: 

695 return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False) 

696 

697 

698def _now() -> str: 

699 return datetime.now(UTC).isoformat(timespec="microseconds")