comicbox.online_session ¶
OnlineSession — Codex-facing API for batch online metadata tagging.
Wraps the existing Comicbox.run_online_lookup() per-file flow with a Codex-friendly surface:
- Per-source credential validation up front (fail-fast).
- Direct :class:
MatchModeAPI (no string alias layer;ASKneeds a PromptHandler ordefer_prompts=True, since the session has no built-in prompt resolver of its own). - One programmatic PromptHandler per session that the matcher calls whenever it would otherwise hit the CLI questionary prompt.
- Event stream via on_event= for UI feedback.
- Cancellation token: callers stop the batch between files.
The internal matcher / selector / rate-limit machinery is untouched; this module is a façade.
BatchedPromptHandler ¶
Bases: Protocol
A PromptHandler that also supports batched prompt delivery.
Codex’s defer-mode UI naturally yields a window of prompts the user will resolve together. When the engine gains the per-session prompt queue (step 8), it will prefer request_many over request for handlers structurally matching this Protocol.
Source code in comicbox/online_session.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
request(prompt) ¶
Resolve one prompt; return the chosen action/payload.
Source code in comicbox/online_session.py
211 212 213 | |
request_many(prompts) ¶
Resolve a window of prompts in one call.
Return one response per prompt, in the same order. The engine will not interleave request and request_many calls within a single drain window.
Source code in comicbox/online_session.py
215 216 217 218 219 220 221 222 223 | |
DeferredPrompt dataclass ¶
A prompt the session skipped under defer_prompts mode.
Captures everything Codex needs to render the prompt later in a review-tagging UI: the file it came from, source, candidates, match context, and the fingerprint used to key the dedup cache. Codex feeds the user’s resolution back via :meth:OnlineSession.preload_resolution.
Source code in comicbox/online_session.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
Effort ¶
Bases: StrEnum
API-call effort per comic, for fan-out sources.
Orthogonal to MatchMode (which controls how the matcher’s verdict is applied). Effort controls how aggressively pre-call algorithms trade accuracy for API throughput — it only bites on sources that fan out per candidate (ComicVine). Single-call sources (Metron since PR #143) have no fan-out to throttle and ignore it.
MINIMAL: aggressive pre-filtering; trade accuracy for throughput.BALANCED: today’s behavior; the default.THOROUGH: spend API budget freely; max accuracy.
Source code in comicbox/config/online/settings.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
MatchMode ¶
Bases: StrEnum
Match-resolution aggressiveness.
Strictly increasing aggressiveness: each level auto-writes a superset of what the previous one does (ask ⊂ careful ⊂ auto ⊂ eager). See match-resolution-user-doc.md for the full decision algorithm.
A StrEnum so dataclass equality, dict keys, JSON serialization, and str() all “just work”.
Source code in comicbox/config/online/settings.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
OnlineConfigurationError ¶
Bases: ComicboxError
Raised when OnlineSession inputs are inconsistent or incomplete.
Source code in comicbox/exceptions.py
132 133 | |
OnlineCredentials dataclass ¶
Credentials for the online sources Codex may enable.
Source code in comicbox/online_session.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
key_for(source) ¶
Return the key, or token, for source.
Source code in comicbox/online_session.py
133 134 135 | |
url_for(source) ¶
Return the API url override for source.
Source code in comicbox/online_session.py
137 138 139 | |
OnlinePrompt dataclass ¶
Codex-facing handoff for an ambiguous match.
The PromptHandler receives one of these and returns a PromptResponse.
Source code in comicbox/online_session.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
OnlineResult dataclass ¶
Per-file outcome of an online tagging call.
tags is the file’s full merged metadata, present even when the lookup applied nothing new (skip, no-match, deferred prompt) — it is NOT evidence of a match. matched is: it’s True only when an online source actually contributed metadata, so writers should gate on it to avoid re-writing a file with its own existing tags.
Source code in comicbox/online_session.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
OnlineSession ¶
Codex-friendly façade over Comicbox’s online lookup.
Construction validates per-source credentials and pre-computes the ComicboxSettings layer that each per-file Comicbox instance will see. Mutable state — the lookup policy (match / prompts) and the cancel token — lives on the instance and may be updated from any thread. The policy lives in an OnlineSessionState shared with every box the session spawns, so a change made at a prompt needs no mirroring back: the box and the session read the same owner.
sources is ordered, and its order is both run priority and merge precedence: the first source runs first and, under first_wins, its match ends the lookup; with first_wins off every source runs and the first one wins every tag they disagree on, except that each source keeps its own id: Comic Vine’s id beats Metron’s cv_id cross-reference to it.
ids pins an issue id per source for a single-comic session: a pinned source fetches that id directly while the unpinned sources search, so one run can mix id retrieval and search and merge both.
config supplies the settings the session layers its tagging preferences over. An embedder that already holds configured ComicboxSettings — naming its own cache directory or effort, neither of which an OnlineSession keyword covers — passes them here instead of exporting environment variables for the settings loader to find. Omitted, the session reads config files and the environment itself.
Source code in comicbox/online_session.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 | |
abort_reason property ¶
Why the session stopped itself, or None if nobody’s lookup aborted.
None after a plain :meth:cancel — that is the caller’s own doing and needs no explanation. Set when a lookup raised OnlineLookupAbortedError, most usefully when the day’s API quota ran out, which stops a run the caller never asked to stop.
cancelled property ¶
Whether cancel() has been called.
defer_prompts property ¶
Whether the session is currently in defer-prompts mode.
match property ¶
Current session match mode (read-only; mutate via set_match()).
set_match accepts MatchMode.ASK only while a prompt handler or deferred prompts can resolve the prompts it raises. A selector answering set_policy: "ask" sets it without that check, so this may report ASK where set_match would refuse.
prompts property ¶
Current session prompt policy.
__enter__() ¶
Enter the context manager, returning this session.
Source code in comicbox/online_session.py
501 502 503 | |
__exit__(*_exc_info) ¶
Exit the context manager, releasing pooled connections.
Source code in comicbox/online_session.py
505 506 507 | |
__init__(*, sources=('metron', 'comicvine'), ids=None, credentials=None, match=MatchMode.AUTO, prompts=Prompts.ASK, prompt_handler=None, on_event=None, rematch=False, first_wins=True, defer_prompts=False, series_batching=True, config=None, client_name=None) ¶
Validate inputs, build per-session state. See class docstring.
Source code in comicbox/online_session.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
cancel() ¶
Stop accepting new files. In-flight lookup runs to completion.
Source code in comicbox/online_session.py
466 467 468 | |
clear_deferred_prompts() ¶
Drop the deferred-prompt queue without resolving them.
Source code in comicbox/online_session.py
551 552 553 554 | |
clear_series_cache() ¶
Drop every series resolution from the cache.
Source code in comicbox/online_session.py
606 607 608 609 | |
close() ¶
Release the pooled connections and cache handles every source holds.
Optional, and not a cancel: the session stays usable afterwards, at the cost of reopening what it needs on its next request. The upstream clients are process-wide per credential set, so closing while another OnlineSession in the same process is mid-run costs that run the same reopen. Acceptable, and the alternative – leaving the sockets and sqlite handles to be finalized – is what emits ResourceWarning.
Comic Vine is the one sharp edge: its rate-limit buckets are closed with the session, so a lookup already holding one can fail outright rather than merely reconnect. Close between files, not under one.
Source code in comicbox/online_session.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
deferred_prompts() ¶
Snapshot the queued deferred prompts. Codex’s review-UI input.
Source code in comicbox/online_session.py
546 547 548 549 | |
preload_resolution(fingerprint, *, action, payload=None, chosen_volume_id=None) ¶
Seed the dedup cache so a re-run auto-resolves the fingerprint.
Codex’s defer-mode flow: drain deferred_prompts() after the batch, present them in a review UI, call preload_resolution() for each one the user resolves, then re-tag the affected files (with defer_prompts toggled off if desired). The cache hit fires the same way it does for in-batch dedup. A non-empty cache is enough to install the bridged selector — but with no handler and defer_prompts off, a cache MISS (the re-search produced a different candidate set than the fingerprint was minted from) raises rather than falling back to the interactive CLI prompt.
Source code in comicbox/online_session.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | |
preload_series_resolution(*, source, series_fingerprint, volume_id) ¶
Seed the series cache from outside the session.
Codex’s use case: persist resolved series across runs in its own DB and replay them at session-start to skip the cold-path search entirely on subsequent imports. Same first-writer-wins semantic as in-batch population — if the key is already cached, this is a no-op.
Source code in comicbox/online_session.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
rate_limit_status() ¶
Snapshot of each enabled source’s current rate-limit budget.
Every entry maps a window name to a JSON-safe limit / remaining / reset_epoch triple, so one renderer handles every source; only the window names differ, because the services meter differently.
Metron carries the live per-account state mokkari>=4 tracks from X-RateLimit-* response headers::
{"burst": {"limit": 20, "remaining": 19, "reset_epoch": ...},
"sustained": {"limit": 5000, "remaining": 4987, "reset_epoch": ...}}
The sustained (daily) limit varies by Metron OpenCollective donor tier, so it is only discoverable from these headers.
Comic Vine meters per resource, so it reports one window per endpoint pool it has actually used::
{"issues": {"limit": 200, "remaining": 197, "reset_epoch": ...},
"search": {"limit": 200, "remaining": 199, "reset_epoch": ...}}
Its numbers are read from the rate-limit bucket file rather than from a live client, so they survive across runs and are available before this session issues a request. reset_epoch is when the window’s oldest request ages out and frees the next slot.
VerseDB meters per account per hour, and reports the window its shared client last saw in X-RateLimit-*. It sends no reset time, so reset_epoch is None; the 4,000-a-day cap of its free tier has no header at all::
{"hour": {"limit": 300, "remaining": 287, "reset_epoch": None}}
A source reports {} until it has data: for Metron that means no request has gone out yet in this process (no shared session, or no header seen); for Comic Vine, that no bucket file exists yet or it could not be read; for VerseDB, that no shared client has had a response yet.
Source code in comicbox/online_session.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 | |
series_cache_snapshot() ¶
Snapshot the current series cache. Useful for persistence.
Source code in comicbox/online_session.py
601 602 603 604 | |
set_defer_prompts(*, defer) ¶
Toggle defer-prompts mode for subsequent file lookups.
Turning deferral off leaves the match mode alone, even when it is MatchMode.ASK and no prompt handler is set. The ASK guard in the constructor and set_match is an entry check, not an invariant this toggle re-enforces: a session left in ASK without a resolver prompts for every match, and each lookup reports the missing resolver at prompt time, as for any session without one.
Source code in comicbox/online_session.py
533 534 535 536 537 538 539 540 541 542 543 544 | |
set_match(match) ¶
Change the session match mode for subsequent file lookups.
MatchMode.ASK prompts for every match, so it is accepted only while the session has a prompt handler or defers its prompts.
Source code in comicbox/online_session.py
453 454 455 456 457 458 459 460 | |
set_prompts(prompts) ¶
Change the session prompt policy for subsequent file lookups.
Source code in comicbox/online_session.py
462 463 464 | |
tag(path) ¶
Tag one file synchronously.
Source code in comicbox/online_session.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | |
tag_many(paths) ¶
Tag many files sequentially. Stops accepting new ones on cancel.
When series_batching is on (the default), paths are reordered upfront by a lightweight filename-derived fingerprint so issues of the same series cluster together. The first issue of each cluster triggers the cold-path search; the remaining issues hit the per-session series cache and skip the search entirely. Order within a cluster is the original input order; the cluster order itself is deterministic (sorted by fingerprint) so re-runs produce identical cache-key sequences.
Counting each cluster up front also lets the source that resolves a series decide to list the whole thing once rather than once per comic (OnlineSource.prefetch_volume) — worth the most here, where the run is sequential and every saved request is saved wall clock too.
Source code in comicbox/online_session.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | |
PromptHandler ¶
Bases: Protocol
Codex’s prompt-resolution callback.
All handlers must implement :meth:request — the single-prompt form used by every code path that ships today. Handlers MAY additionally implement :meth:request_many to opt into the future batched-prompt delivery path (plan §3.4 / step 8): when the engine grows the per-session prompt queue, handlers exposing request_many get the full pending window in one call; handlers without it continue to be invoked one prompt at a time. v1 delivers prompts via request only — see tasks/prepare-for-codex-writing/03-batched-prompt-handler.md for the deferred implementation plan.
Source code in comicbox/online_session.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
request(prompt) ¶
Resolve one prompt; return the chosen action/payload.
Source code in comicbox/online_session.py
190 191 192 | |
PromptResponse dataclass ¶
Codex-side decision for one prompt.
action mirrors :data:SelectorAction; payload is the index for choose, the "<source>:<id>" string for manual, the new :class:MatchMode value (e.g. "auto") for set_policy, the new :class:Prompts value (e.g. "never") for set_prompts, or None otherwise.
Source code in comicbox/online_session.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Prompts ¶
Bases: StrEnum
Whether comicbox is allowed to prompt the user mid-run.
Source code in comicbox/config/online/settings.py
45 46 47 48 49 | |
RunEstimate dataclass ¶
The projected cost of a batch online-tag run.
Source code in comicbox/online_estimate.py
145 146 147 148 149 150 151 152 | |
SourceAttribution dataclass ¶
A credit and the link it must carry.
Source code in comicbox/formats/base/online/attribution.py
15 16 17 18 19 20 | |
estimate_run(comics, sources, *, effort=Effort.BALANCED.value, merge_all_sources=False) ¶
Project the request count and wall-clock seconds for a batch online-tag run.
comics is how many comics remain to look up and sources the enabled source names in priority order. effort is an :class:~comicbox.config.online.settings.Effort value ("balanced" etc.), resolved for the source it prices. merge_all_sources mirrors first_wins=False: every source is queried per comic and their per-comic costs summed, instead of stopping at the first match.
The projection assumes the effort it is handed. A CLI run can auto-engage a lower Comic Vine effort for a large unattended batch, which this cannot see.
Source code in comicbox/online_estimate.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
source_attribution(name) ¶
Return the credit name’s terms require, or None when they ask for none.
Source code in comicbox/formats/base/online/attribution.py
23 24 25 26 27 28 29 30 31 | |