File size: 39,079 Bytes
5dec903
6ce52dc
bd7a550
6ce52dc
 
 
 
0d76282
5dec903
bd7a550
f42a9ce
0d76282
 
5dec903
6ce52dc
0d76282
ead0f88
5dec903
ead0f88
6ce52dc
5dec903
ead0f88
8a6d815
d6c09dd
5e443d5
6ce52dc
5dec903
 
ead0f88
7228c1c
6ce52dc
7228c1c
0d76282
 
 
5dec903
 
 
 
 
 
 
 
 
 
3a87472
d6c09dd
 
 
6ce52dc
 
 
 
 
 
 
 
 
 
ce15291
6ce52dc
5dec903
 
 
 
 
7228c1c
5dec903
 
 
fcc6c8d
9504d33
0d76282
 
 
 
3a87472
0d76282
3a87472
0d76282
9504d33
5e443d5
3a87472
ddbac90
9504d33
0d76282
6ce52dc
3a87472
44cb30a
0d76282
742c042
0d76282
 
 
 
 
 
 
 
 
 
093358b
0d76282
093358b
0d76282
 
 
 
d6c09dd
6ce52dc
d6c09dd
6ce52dc
 
 
 
 
 
 
d6c09dd
 
0d76282
 
 
 
 
 
 
6ce52dc
0d76282
6ce52dc
0d76282
4dbad06
 
0d76282
 
47c3025
16b0aa2
6ce52dc
 
 
 
38409c4
bbcc8ae
472eaf0
0d76282
 
4dbad06
500ba6f
0d76282
 
6ce52dc
d6c09dd
c07aa14
472eaf0
4dbad06
0d76282
093358b
4dbad06
0d76282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c09dd
0d76282
 
5dec903
 
6ce52dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af60fe1
 
 
6ce52dc
 
 
 
 
 
af60fe1
6ce52dc
af60fe1
6ce52dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c07aa14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38409c4
 
 
 
 
c07aa14
 
 
 
 
 
 
 
38409c4
 
 
 
 
c07aa14
 
 
 
 
 
38409c4
 
 
 
c07aa14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ce52dc
472eaf0
 
 
 
 
6ce52dc
472eaf0
 
 
 
6ce52dc
 
 
 
 
472eaf0
6ce52dc
 
 
 
 
 
 
c07aa14
 
 
 
16b0aa2
 
 
6ce52dc
af60fe1
 
c07aa14
 
 
 
 
 
 
 
6ce52dc
 
 
c07aa14
 
b8f1204
 
c07aa14
 
b8f1204
c07aa14
b8f1204
 
 
 
 
 
 
 
 
 
6ce52dc
 
 
b8f1204
6ce52dc
af60fe1
6ce52dc
 
 
 
 
47c3025
 
 
bbcc8ae
 
6ce52dc
8a6d815
 
ead0f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6d815
16b0aa2
 
 
 
 
 
6ce52dc
 
 
 
 
 
16b0aa2
 
 
bbcc8ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16b0aa2
 
 
 
6ce52dc
8a6d815
 
 
6ce52dc
 
 
 
 
 
 
 
 
 
51096c9
 
 
47c3025
 
6ce52dc
8a6d815
 
 
6ce52dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a6d815
 
0d76282
8a6d815
 
 
 
 
 
 
 
0d76282
8a6d815
5dec903
6ce52dc
38409c4
 
 
bbcc8ae
38409c4
6ce52dc
38409c4
 
 
 
bbcc8ae
 
 
 
 
6ce52dc
 
 
38409c4
6ce52dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38409c4
 
 
 
bbcc8ae
38409c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7519fad
38409c4
 
6ce52dc
7519fad
 
 
 
6ce52dc
 
 
38409c4
7519fad
6ce52dc
 
 
 
 
bbcc8ae
 
 
 
6ce52dc
 
 
38409c4
 
6ce52dc
38409c4
 
 
6ce52dc
 
472eaf0
 
 
 
 
 
6ce52dc
 
bbcc8ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7519fad
bbcc8ae
 
bf9de41
 
 
 
bbcc8ae
bf9de41
 
 
 
 
 
 
 
 
 
9633033
 
 
6ce52dc
 
 
38409c4
 
 
6ce52dc
 
 
 
 
 
 
 
 
 
38409c4
bbcc8ae
38409c4
 
 
 
 
 
 
 
 
 
 
 
857d7e9
 
 
 
 
 
6ce52dc
 
 
38409c4
bbcc8ae
6ce52dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
742c042
 
 
8a6d815
742c042
6ce52dc
 
 
 
 
 
7228c1c
c07aa14
 
 
 
 
 
 
bbcc8ae
 
500ba6f
4dbad06
 
 
093358b
 
 
 
 
 
 
 
 
 
 
 
3dcdedb
 
 
 
 
 
 
9633033
 
3dcdedb
 
 
093358b
3dcdedb
093358b
 
 
3dcdedb
 
 
 
 
093358b
3dcdedb
 
093358b
 
8e11cb8
 
 
 
 
 
 
 
 
 
 
 
 
3dcdedb
 
 
 
8e11cb8
 
9633033
 
8e11cb8
3dcdedb
 
 
 
 
8e11cb8
 
 
 
3dcdedb
 
8e11cb8
9633033
 
 
8e11cb8
 
 
9633033
8e11cb8
3dcdedb
 
 
093358b
3dcdedb
 
 
 
 
 
8e11cb8
3dcdedb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
093358b
 
4dbad06
 
 
 
500ba6f
3dcdedb
4dbad06
bbcc8ae
 
 
 
 
4dbad06
 
093358b
6ce52dc
4dbad06
c07aa14
093358b
4dbad06
 
 
 
8e11cb8
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
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
# -*- coding: utf-8 -*-
"""SmartPaper edge-first authentication and browser task/payment bridge.

Cloudflare owns the durable browser session, quota, tasks, and payments. The
Hugging Face process verifies the signed edge assertion but does not call the
Worker over the server-to-server network. Authenticated business operations are
executed by the user's browser through the same-origin invisible component.
"""
from __future__ import annotations

import base64
import hashlib
import hmac
import html
import io
import json
import logging
import os
import re
import secrets
import time
from pathlib import Path
from typing import Any, Callable
from urllib.parse import unquote, urlsplit

import qrcode
import streamlit as st

from config import APP_CONFIG
from i18n import tr
from smartpaper_restore_bridge import edge_bridge

EDGE_ASSERTION_COOKIE_NAME = "smartpaper_edge_assertion"
EDGE_ASSERTION_AUDIENCE = "smartpaper-streamlit"
ASSERTION_CLOCK_SKEW_SECONDS = 30
def _runtime_value(name: str) -> str:
    value = os.environ.get(name, "")
    if not value:
        try:
            value = st.secrets.get(name, "")
        except Exception:
            value = ""
    return str(value or "").strip()


def _api_base() -> str:
    value = str(st.session_state.get("sp_auth_api_origin") or "").strip().rstrip("/")
    if not value:
        value = _runtime_value("SMARTPAPER_AUTH_API").rstrip("/")
    parsed = urlsplit(value)
    if (
        parsed.scheme != "https"
        or not parsed.netloc
        or parsed.username is not None
        or parsed.password is not None
        or parsed.path not in ("", "/")
        or parsed.query
        or parsed.fragment
    ):
        raise RuntimeError("SMARTPAPER_AUTH_API must be an HTTPS origin.")
    return f"https://{parsed.netloc}"


def _service_key() -> str:
    value = _runtime_value("SMARTPAPER_SERVICE_KEY")
    if not value:
        raise RuntimeError("SMARTPAPER_SERVICE_KEY is not configured.")
    return value


def _context_cookie(name: str) -> str:
    try:
        cookies = st.context.cookies
        value = cookies.get(name, "")
        if value:
            return unquote(str(value).strip())
        target = name.lower()
        for key, item in cookies.items():
            if str(key).lower() == target:
                return unquote(str(item or "").strip())
    except Exception:
        return ""
    return ""


def _decode_b64url(value: str) -> bytes:
    return base64.urlsafe_b64decode(value + "=" * (-len(value) % 4))


def _verify_edge_assertion(token: str) -> dict[str, Any]:
    try:
        encoded, supplied_signature = token.split(".", 1)
    except ValueError as exc:
        raise RuntimeError("invalid_edge_assertion") from exc
    expected_signature = hmac.new(
        _service_key().encode("utf-8"),
        encoded.encode("ascii"),
        hashlib.sha256,
    ).hexdigest()
    if not hmac.compare_digest(expected_signature, supplied_signature):
        raise RuntimeError("invalid_edge_assertion_signature")
    try:
        payload = json.loads(_decode_b64url(encoded).decode("utf-8"))
    except Exception as exc:
        raise RuntimeError("invalid_edge_assertion_payload") from exc
    now = int(time.time())
    if payload.get("ver") != 1 or payload.get("aud") != EDGE_ASSERTION_AUDIENCE:
        raise RuntimeError("invalid_edge_assertion_claims")
    api_origin = str(payload.get("api_origin") or "").strip().rstrip("/")
    parsed = urlsplit(api_origin)
    if (
        parsed.scheme != "https"
        or not parsed.netloc
        or parsed.username is not None
        or parsed.password is not None
        or parsed.path not in ("", "/")
        or parsed.query
        or parsed.fragment
    ):
        raise RuntimeError("invalid_edge_assertion_origin")
    issued_at = int(payload.get("iat") or 0)
    expires_at = int(payload.get("exp") or 0)
    if issued_at > now + ASSERTION_CLOCK_SKEW_SECONDS or expires_at <= now - ASSERTION_CLOCK_SKEW_SECONDS:
        raise RuntimeError("expired_edge_assertion")
    if expires_at - issued_at > 9 * 60 * 60:
        raise RuntimeError("invalid_edge_assertion_lifetime")
    user = payload.get("user") or {}
    if not isinstance(user, dict) or not user.get("id") or user.get("status") != "active":
        raise RuntimeError("invalid_edge_assertion_identity")
    payload["api_origin"] = f"https://{parsed.netloc}"
    return payload


def _clear_generation_session_state() -> None:
    for key in (
        "sp_generation_task", "sp_tracked_task_id", "sp_current_node",
        "sp_task_recovery_initialized", "sp_last_ui_task_status",
        "sp_generation_idempotency_key", "zip_buffer", "zip_filename",
        "is_running", "start_pipeline", "resume_pipeline", "run_mode",
        "rag_engine", "up_amj_ref", "up_amj_data", "up_asq_ref",
        "up_asq_data", "sp_payment_required", "sp_payment_order",
        "sp_payment_request_id", "sp_payment_package_sku",
        "sp_payment_channel", "sp_payment_order_channel",
        "sp_payment_dialog_pending",
    ):
        st.session_state.pop(key, None)


def _clear_session() -> None:
    for key in (
        "sp_session_expires_at", "sp_user",
        "sp_last_verified_at", "sp_edge_assertion", "sp_auth_api_origin",
        "sp_product_ui_projection", "sp_product_tier",
        "sp_account_pending_command_id",
    ):
        st.session_state.pop(key, None)


def require_alipay_login(language: str = "zh") -> dict[str, Any]:
    """Return the user authenticated by the Cloudflare edge gateway."""
    token = _context_cookie(EDGE_ASSERTION_COOKIE_NAME)
    if token and token == st.session_state.get("sp_edge_assertion") and st.session_state.get("sp_user"):
        return dict(st.session_state["sp_user"])
    if not token:
        _clear_session()
        st.stop()
    try:
        payload = _verify_edge_assertion(token)
    except Exception:
        _clear_session()
        st.stop()
    user = dict(payload["user"])
    st.session_state["sp_edge_assertion"] = token
    st.session_state["sp_session_expires_at"] = payload["exp"]
    st.session_state["sp_user"] = user
    st.session_state["sp_auth_api_origin"] = str(payload["api_origin"]).rstrip("/")
    st.session_state["sp_last_verified_at"] = time.time()
    return user


def _bridge_key(prefix: str, value: str) -> str:
    digest = hashlib.sha256(str(value).encode("utf-8")).hexdigest()[:20]
    return f"{prefix}_{digest}"


def _edge_call(
    action: str,
    command_id: str,
    payload: dict[str, Any] | None = None,
    *,
    key_prefix: str = "sp_edge",
) -> dict[str, Any] | None:
    return edge_bridge(
        action=action,
        command_id=command_id,
        api_origin=_api_base(),
        payload=payload or {},
        key=_bridge_key(key_prefix, command_id),
    )


def _raise_edge_error(result: dict[str, Any], fallback: str) -> None:
    message = (
        result.get("message")
        or result.get("reason")
        or result.get("error")
        or fallback
    )
    raise RuntimeError(str(message))


def _merge_quota_into_session(result: dict[str, Any]) -> None:
    """Refresh the approved account card immediately after a quota mutation."""
    quota = result.get("quota")
    if not isinstance(quota, dict):
        return
    user = dict(st.session_state.get("sp_user") or {})
    for key in (
        "quota_date",
        "daily_free_limit",
        "daily_free_reserved",
        "daily_free_used",
        "daily_free_remaining",
        "bonus_papers",
    ):
        if key in quota:
            user[key] = quota[key]
    if user:
        st.session_state["sp_user"] = user
        st.session_state["sp_account_revision"] = int(
            st.session_state.get("sp_account_revision") or 0
        ) + 1


def _legacy_product_ui_projection() -> dict[str, Any]:
    return {
        "schema_version": "a5-product-ui-v1",
        "mode": "legacy",
        "product_tier_selection_required": False,
        "products": {},
    }


def _normalize_product_ui_projection(value: Any) -> dict[str, Any]:
    """Accept only the Worker-owned A5 UI projection; never reinterpret legacy quota fields."""
    if not isinstance(value, dict):
        return _legacy_product_ui_projection()
    mode = str(value.get("mode") or "legacy").strip().lower()
    if mode == "legacy":
        return _legacy_product_ui_projection()
    if mode != "a5_product":
        raise RuntimeError("product_ui_projection_invalid_mode")

    raw_products = value.get("products")
    raw_entitlement = value.get("entitlement")
    if not isinstance(raw_products, dict) or not isinstance(raw_entitlement, dict):
        raise RuntimeError("product_ui_projection_incomplete")

    products: dict[str, dict[str, Any]] = {}
    for tier in ("STANDARD", "FLAGSHIP"):
        raw = raw_products.get(tier)
        if not isinstance(raw, dict):
            raise RuntimeError("product_ui_projection_missing_tier")
        try:
            minimum = int(raw.get("min_body_words"))
            maximum = int(raw.get("max_body_words"))
        except (TypeError, ValueError) as exc:
            raise RuntimeError("product_ui_projection_invalid_word_policy") from exc
        if minimum <= 0 or maximum < minimum:
            raise RuntimeError("product_ui_projection_invalid_word_policy")
        try:
            amount_fen = int(raw.get("amount_fen"))
            credits_granted = int(raw.get("credits_granted"))
        except (TypeError, ValueError) as exc:
            raise RuntimeError("product_ui_projection_invalid_billing_policy") from exc
        products[tier] = {
            "product_tier": tier,
            "product_policy_version": str(raw.get("product_policy_version") or ""),
            "word_count_policy_version": str(raw.get("word_count_policy_version") or ""),
            "body_metric": str(raw.get("body_metric") or ""),
            "min_body_words": minimum,
            "max_body_words": maximum,
            "counting_contract_version": str(raw.get("counting_contract_version") or ""),
            "package_sku": str(raw.get("package_sku") or ""),
            "billing_policy_version": str(raw.get("billing_policy_version") or ""),
            "amount_fen": amount_fen,
            "currency": str(raw.get("currency") or ""),
            "credits_granted": credits_granted,
        }
        if not all((
            products[tier]["product_policy_version"],
            products[tier]["word_count_policy_version"],
            products[tier]["body_metric"],
            products[tier]["counting_contract_version"],
            products[tier]["package_sku"],
            products[tier]["billing_policy_version"],
            products[tier]["currency"],
        )) or amount_fen <= 0 or credits_granted <= 0:
            raise RuntimeError("product_ui_projection_incomplete")

    try:
        standard_remaining = int(raw_entitlement.get("standard_remaining"))
        flagship_remaining = int(raw_entitlement.get("flagship_remaining"))
    except (TypeError, ValueError) as exc:
        raise RuntimeError("product_ui_projection_invalid_entitlement") from exc
    if standard_remaining < 0 or flagship_remaining < 0:
        raise RuntimeError("product_ui_projection_invalid_entitlement")

    return {
        "schema_version": str(value.get("schema_version") or "a5-product-ui-v1"),
        "mode": "a5_product",
        "product_tier_selection_required": True,
        "products": products,
        "entitlement": {
            "standard_remaining": standard_remaining,
            "flagship_remaining": flagship_remaining,
        },
    }


def get_product_ui_projection() -> dict[str, Any]:
    value = st.session_state.get("sp_product_ui_projection")
    return _normalize_product_ui_projection(value)


def get_account_snapshot(*, block: bool = False) -> dict[str, Any] | None:
    # External entitlement mutations (administrator adjustment/payment credit/
    # another device) cannot increment this browser's local revision. Keep one
    # command id stable only for the browser-bridge handshake, then retire it
    # after a completed response so the next Streamlit rerun performs a fresh
    # Worker/D1 account read instead of reusing a time-bucketed cached result.
    revision = int(st.session_state.get("sp_account_revision") or 0)
    command_id = str(st.session_state.get("sp_account_pending_command_id") or "").strip()
    if not command_id:
        command_id = f"account:{revision}:{secrets.token_urlsafe(10)}"
        st.session_state["sp_account_pending_command_id"] = command_id
    result = _edge_call("account", command_id, {}, key_prefix="sp_account")
    if result is None:
        if block:
            st.stop()
        return None
    st.session_state.pop("sp_account_pending_command_id", None)
    if not result.get("ok"):
        if block:
            _raise_edge_error(result, "account_query_failed")
        return None
    user = result.get("user")
    if isinstance(user, dict):
        st.session_state["sp_user"] = dict(user)
    projection = _normalize_product_ui_projection(result.get("product_ui"))
    st.session_state["sp_product_ui_projection"] = projection
    if projection["mode"] != "a5_product":
        st.session_state.pop("sp_product_tier", None)
    # R3.1 single-writer rule: account refresh owns identity/quota only.
    # Task UI state is initialized by recover_generation_task() and thereafter
    # updated only by generation_task_status()/explicit task actions.
    return result


def start_generation_task(
    *,
    idempotency_key: str,
    mode: str,
    interface_language: str,
    target_words: int,
    product_tier: str | None = None,
) -> dict[str, Any]:
    normalized = str(idempotency_key or "").strip()
    if not normalized:
        raise RuntimeError("idempotency_key_required")
    projection = get_product_ui_projection()
    normalized_tier = str(product_tier or "").strip().upper()
    product_mode = projection.get("mode") == "a5_product"
    if product_mode:
        if normalized_tier not in projection.get("products", {}):
            raise RuntimeError("product_tier_required")
    elif normalized_tier:
        raise RuntimeError("product_tier_not_available_in_legacy_mode")
    payload = {
        "idempotency_key": normalized,
        "mode": str(mode or "").strip().lower(),
        "interface_language": str(interface_language or "zh").strip(),
        "target_words": int(target_words),
    }
    if product_mode:
        # Product tier is user intent only. Worker/D1 resolves entitlement and
        # freezes the trusted TaskPolicySnapshot; browser state is never authority.
        payload["product_tier"] = normalized_tier
    result = _edge_call(
        "task_start",
        normalized,
        payload,
        key_prefix="sp_task_start",
    )
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "task_start_failed")
    _merge_quota_into_session(result)
    started_task = result.get("task")
    if isinstance(started_task, dict) and started_task.get("task_id"):
        st.session_state["sp_tracked_task_id"] = str(started_task["task_id"])
        # A user-started task must never inherit the prior-login completed-task UI mode.
        st.session_state["sp_recovered_completed_task_id"] = ""
    return dict(result)


def _completed_output_path(task: dict[str, Any]) -> Path | None:
    user = dict(st.session_state.get("sp_user") or {})
    user_key = re.sub(r"[^A-Za-z0-9_.-]", "_", str(user.get("id") or "").strip())[:100]
    task_key = re.sub(r"[^A-Za-z0-9_.-]", "_", str((task or {}).get("task_id") or "").strip())[:100]
    filename = os.path.basename(str((task or {}).get("output_filename") or "").strip())
    if not user_key or not task_key or not filename or not filename.lower().endswith(".zip"):
        return None
    workspace = Path(str(APP_CONFIG.get("workspace_dir", ".workspace")))
    return workspace / "users" / user_key / "tasks" / task_key / "outputs" / filename


def _restore_completed_artifact_if_needed(task: dict[str, Any]) -> None:
    """Best-effort restore for a completed task when this runtime lost its local ZIP."""
    if str((task or {}).get("status") or "").strip().lower() != "completed":
        return
    output_path = _completed_output_path(task)
    if output_path is None or output_path.is_file():
        return
    user = dict(st.session_state.get("sp_user") or {})
    numeric_user_id = user.get("numeric_id")
    if numeric_user_id in (None, ""):
        logging.warning("[SP_AUTH] bucket_restore_skipped task_id=%s reason=numeric_user_missing", str(task.get("task_id") or ""))
        return
    try:
        from hf_bucket_store import restore_completed_zip

        restored = restore_completed_zip(
            user_id=numeric_user_id,
            task_id=str(task.get("task_id") or ""),
            output_filename=str(task.get("output_filename") or ""),
            output_path=output_path,
        )
        if restored:
            logging.info("[SP_AUTH] bucket_restore_ok task_id=%s", str(task.get("task_id") or ""))
    except Exception as exc:
        # Recovery storage is never authoritative for task state or quota.
        logging.warning(
            "[SP_AUTH] bucket_restore_nonfatal task_id=%s error=%s",
            str((task or {}).get("task_id") or ""), type(exc).__name__,
        )


def recover_generation_task() -> dict[str, Any]:
    """Initialize current task state once from the authoritative Worker.

    Account polling is intentionally forbidden from writing task state. This
    recovery call is the only fresh-session initializer; later changes come
    from generation_task_status() or explicit start/cancel operations.
    """
    command_id = f"recover:{int(time.time() // 10)}"
    result = _edge_call("task_recover", command_id, {}, key_prefix="sp_task_recover")
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "task_recover_failed")
    _merge_quota_into_session(result)
    task = result.get("task")
    if isinstance(task, dict) and task.get("task_id"):
        recovered_status = str(task.get("status") or "").strip().lower()
        if recovered_status == "completed":
            # Historical completed tasks remain authoritative in D1/Bucket, but a
            # fresh login must open a clean workspace. Do not restore the artifact,
            # attach the completed task to Streamlit session state, or render any
            # historical generation/download UI.
            st.session_state["sp_generation_task"] = None
            st.session_state["sp_last_ui_task_status"] = ""
            st.session_state.pop("sp_tracked_task_id", None)
            logging.info(
                "[SP_AUTH] completed_task_hidden_on_fresh_login task_id=%s",
                str(task.get("task_id") or ""),
            )
        else:
            st.session_state["sp_generation_task"] = dict(task)
            st.session_state["sp_tracked_task_id"] = str(task["task_id"])
            st.session_state["sp_last_ui_task_status"] = recovered_status
    else:
        st.session_state["sp_generation_task"] = None
        st.session_state["sp_last_ui_task_status"] = ""
        st.session_state.pop("sp_tracked_task_id", None)
    return dict(result)


def generation_task_status(task_id: str) -> dict[str, Any]:
    normalized = str(task_id or "").strip()
    command_id = f"status:{normalized}:{int(time.time() // 5)}"
    result = _edge_call(
        "task_status", command_id, {"task_id": normalized}, key_prefix="sp_task_status"
    )
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "task_status_failed")
    _merge_quota_into_session(result)
    task = result.get("task")
    if isinstance(task, dict):
        st.session_state["sp_generation_task"] = dict(task)
        if task.get("task_id"):
            st.session_state["sp_tracked_task_id"] = str(task["task_id"])
    return dict(result)


def cancel_generation_task(task_id: str | None = None) -> dict[str, Any]:
    """Cancel synchronously so quota release is confirmed before local cleanup."""
    normalized = str(task_id or "").strip()
    command_id = f"cancel:{normalized or 'latest'}"
    result = _edge_call(
        "task_cancel",
        command_id,
        {"task_id": normalized} if normalized else {},
        key_prefix="sp_task_cancel",
    )
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "task_cancel_failed")
    reservation = result.get("reservation")
    if result.get("cancelled") and reservation and str(reservation.get("status") or "").lower() != "released":
        raise RuntimeError("task_cancel_quota_not_released")
    _merge_quota_into_session(result)
    return dict(result)


def logout_alipay_session(on_logout: Callable[[str, str], None] | None = None) -> None:
    user_id = str((st.session_state.get("sp_user") or {}).get("id") or "")
    task = st.session_state.get("sp_generation_task") or {}
    task_id = str(task.get("task_id") or "")
    if on_logout is not None:
        try:
            on_logout(user_id, task_id)
        except Exception:
            pass
    _clear_session()
    _clear_generation_session_state()


def create_payment_order(
    idempotency_key: str,
    package_sku: str | None = None,
    payment_channel: str | None = None,
) -> dict[str, Any]:
    normalized = str(idempotency_key or "").strip()
    payload: dict[str, Any] = {"idempotency_key": normalized}
    normalized_sku = str(package_sku or "").strip()
    if normalized_sku:
        payload["package_sku"] = normalized_sku
    normalized_channel = str(payment_channel or "").strip().upper()
    if normalized_channel:
        if normalized_channel not in {"ALIPAY", "WECHAT"}:
            raise RuntimeError("payment_channel_invalid")
        payload["payment_channel"] = normalized_channel
    result = _edge_call(
        "pay_create",
        normalized,
        payload,
        key_prefix="sp_pay_create",
    )
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "payment_create_failed")
    return dict(result)


def payment_order_status(merchant_order_no: str) -> dict[str, Any]:
    order_no = str(merchant_order_no or "").strip()
    command_id = f"pay-status:{order_no}:{int(time.time() // 3)}"
    result = _edge_call(
        "pay_status",
        command_id,
        {"merchant_order_no": order_no},
        key_prefix="sp_pay_status",
    )
    if result is None:
        st.stop()
    if not result.get("ok"):
        _raise_edge_error(result, "payment_status_failed")
    return dict(result)


def _qr_png_bytes(value: str) -> bytes:
    image = qrcode.make(str(value))
    buffer = io.BytesIO()
    image.save(buffer, format="PNG")
    return buffer.getvalue()


def _render_payment_purchase_body(language: str) -> None:
    projection = get_product_ui_projection()
    product_mode = projection.get("mode") == "a5_product"
    selected_product: dict[str, Any] | None = None
    package_sku = ""
    existing_order = st.session_state.get("sp_payment_order")
    if product_mode:
        selected_tier = str(st.session_state.get("sp_product_tier") or "STANDARD").strip().upper()
        raw_product = (projection.get("products") or {}).get(selected_tier)
        if not isinstance(raw_product, dict):
            raise RuntimeError("product_payment_projection_missing_tier")
        selected_product = dict(raw_product)
        package_sku = str(selected_product.get("package_sku") or "").strip()
        amount_fen = int(selected_product.get("amount_fen") or 0)
        credits = int(selected_product.get("credits_granted") or 0)
        if not package_sku or amount_fen <= 0 or credits <= 0:
            raise RuntimeError("product_payment_projection_invalid")
        amount_text = f"{amount_fen / 100:.2f}".rstrip("0").rstrip(".")
        currency = str(selected_product.get("currency") or "").strip()
        if not currency:
            raise RuntimeError("product_payment_projection_invalid_currency")
        tier_name = tr(language, f"product_tier_{selected_tier.lower()}")
        st.warning(
            tr(
                language,
                "payment_notice_product",
                tier=tier_name,
                amount=amount_text,
                currency=currency,
                credits=credits,
            )
        )
        existing_sku = str(st.session_state.get("sp_payment_package_sku") or "").strip()
        order_sku = str((existing_order or {}).get("package_sku") or "").strip() if isinstance(existing_order, dict) else ""
        if (existing_sku and existing_sku != package_sku) or (existing_order and order_sku != package_sku):
            st.session_state["sp_payment_order"] = None
            st.session_state["sp_payment_request_id"] = ""
            existing_order = None
    else:
        st.warning(tr(language, "payment_notice"))

    # R4-1D: opening the purchase dialog is the explicit purchase action.
    # Create one trusted order for the selected server-authoritative SKU;
    # users no longer preselect a payment channel or click a second create button.
    if not existing_order and not str(st.session_state.get("sp_payment_request_id") or "").strip():
        st.session_state["sp_payment_request_id"] = (
            f"pay_{int(time.time() * 1000)}_{secrets.token_urlsafe(12)}"
        )
        st.session_state["sp_payment_package_sku"] = package_sku
        st.session_state["sp_payment_order_channel"] = ""

    request_id = str(st.session_state.get("sp_payment_request_id") or "").strip()
    order = st.session_state.get("sp_payment_order")
    if request_id and not order:
        st.info(tr(language, "payment_creating"))
        order_result = create_payment_order(
            request_id,
            package_sku or None,
        )
        order = dict(order_result.get("order") or {})
        if not order.get("merchant_order_no") or not order.get("qr_code"):
            raise RuntimeError("payment_order_missing_qr_code")
        if product_mode and str(order.get("package_sku") or "") != package_sku:
            raise RuntimeError("payment_order_package_mismatch")
        st.session_state["sp_payment_order"] = order
        projected = order_result.get("product_ui")
        if isinstance(projected, dict):
            st.session_state["sp_product_ui_projection"] = _normalize_product_ui_projection(projected)

    order = st.session_state.get("sp_payment_order") or {}
    if str(order.get("merchant_order_no") or "").strip():
        # The app-level pending flag exists only to survive the initial
        # browser-bridge handshake. Once the trusted order exists, clear it
        # inside the dialog fragment so manually closing the dialog does not
        # cause an unrelated later rerun to reopen it.
        st.session_state["sp_payment_dialog_pending"] = False
    qr_code = str(order.get("qr_code") or "").strip()
    if qr_code:
        payment_provider = str(order.get("payment_provider") or "").strip().upper()
        if payment_provider == "MANUAL_ALIPAY":
            alipay_bytes = _qr_png_bytes(qr_code)
            wechat_path = Path(__file__).resolve().parent / "static" / "payment_wechat_qr.png"
            if not wechat_path.is_file():
                raise RuntimeError("payment_wechat_qr_missing")
            wechat_bytes = wechat_path.read_bytes()
            alipay_uri = "data:image/png;base64," + base64.b64encode(alipay_bytes).decode("ascii")
            wechat_uri = "data:image/png;base64," + base64.b64encode(wechat_bytes).decode("ascii")
            st.markdown(
                f"""
                <style media="all">
                .sp-payment-grid {{
                    display:grid;
                    grid-template-columns:repeat(2,minmax(0,1fr));
                    gap:1rem;
                    align-items:start;
                    margin:.35rem 0 .6rem;
                }}
                .sp-payment-card {{text-align:center; min-width:0;}}
                .sp-payment-card strong {{display:block; margin-bottom:.4rem;}}
                .sp-payment-qr-frame {{
                    width:min(100%,300px);
                    aspect-ratio:1/1;
                    margin:0 auto;
                    display:flex;
                    align-items:center;
                    justify-content:center;
                    overflow:hidden;
                }}
                .sp-payment-qr-frame img {{
                    width:100%;
                    height:100%;
                    object-fit:contain;
                    display:block;
                }}
                @media (max-width:640px) {{
                    .sp-payment-grid {{grid-template-columns:1fr;}}
                    .sp-payment-qr-frame {{width:min(86vw,300px);}}
                }}
                </style>
                <div class="sp-payment-grid">
                  <div class="sp-payment-card">
                    <strong>{html.escape(tr(language, 'payment_channel_alipay'))}</strong>
                    <div class="sp-payment-qr-frame"><img src="{alipay_uri}" alt="Alipay QR"></div>
                  </div>
                  <div class="sp-payment-card">
                    <strong>{html.escape(tr(language, 'payment_channel_wechat'))}</strong>
                    <div class="sp-payment-qr-frame"><img src="{wechat_uri}" alt="WeChat QR"></div>
                  </div>
                </div>
                """,
                unsafe_allow_html=True,
            )
            st.caption(tr(language, "payment_scan_dual"))
        else:
            st.image(_qr_png_bytes(qr_code), width=280)
        order_no = str(order.get("merchant_order_no") or "").strip()
        if order_no:
            st.caption(tr(language, "payment_order_no", order_no=order_no))
            st.code(order_no)
        if payment_provider != "MANUAL_ALIPAY":
            st.caption(tr(language, "payment_scan"))
        support = order.get("payment_support") if isinstance(order.get("payment_support"), dict) else {}
        support_wechat = str((support or {}).get("wechat") or "").strip()
        support_whatsapp = str((support or {}).get("whatsapp") or "").strip()
        if support_wechat or support_whatsapp:
            st.info(tr(language, "payment_support_prompt"))
            if support_wechat:
                st.caption(tr(language, "payment_support_wechat", value=support_wechat))
            if support_whatsapp:
                st.caption(tr(language, "payment_support_whatsapp", value=support_whatsapp))
        if payment_provider != "MANUAL_ALIPAY" and st.button(
            tr(language, "payment_check"), key="sp_check_payment_status"
        ):
            status_result = payment_order_status(order.get("merchant_order_no", ""))
            updated = dict(status_result.get("order") or {})
            st.session_state["sp_payment_order"] = updated
            projected = status_result.get("product_ui")
            if isinstance(projected, dict):
                st.session_state["sp_product_ui_projection"] = _normalize_product_ui_projection(projected)
            if updated.get("status") == "paid" and updated.get("credit_applied"):
                user = status_result.get("user")
                if isinstance(user, dict):
                    st.session_state["sp_user"] = dict(user)
                    st.session_state["sp_account_revision"] = int(
                        st.session_state.get("sp_account_revision") or 0
                    ) + 1
                st.session_state["sp_payment_required"] = False
                st.session_state["sp_payment_order"] = None
                st.session_state["sp_payment_request_id"] = ""
                st.session_state["sp_payment_package_sku"] = ""
                st.session_state["sp_payment_order_channel"] = ""
                if product_mode and selected_product is not None:
                    tier_name = tr(language, f"product_tier_{str(selected_product.get('product_tier') or '').lower()}")
                    st.success(
                        tr(
                            language,
                            "payment_paid_product",
                            tier=tier_name,
                            credits=int(selected_product.get("credits_granted") or 0),
                        )
                    )
                else:
                    st.success(tr(language, "payment_paid"))
            elif str(updated.get("manual_review_status") or "").upper() == "REJECTED":
                st.session_state["sp_payment_order"] = None
                st.session_state["sp_payment_request_id"] = ""
                st.session_state["sp_payment_package_sku"] = ""
                st.session_state["sp_payment_order_channel"] = ""
                st.warning(tr(language, "payment_rejected"))
            elif updated.get("status") in {"closed", "failed"}:
                st.session_state["sp_payment_order"] = None
                st.session_state["sp_payment_request_id"] = ""
                st.session_state["sp_payment_package_sku"] = ""
                st.session_state["sp_payment_order_channel"] = ""
                st.warning(tr(language, "payment_expired"))


def render_payment_purchase(language: str) -> None:
    """Show payment in a modal so the approved normal-page layout is unchanged."""
    dialog_factory = getattr(st, "dialog", None)
    if callable(dialog_factory):
        @dialog_factory(tr(language, "payment_dialog_title"))
        def _payment_dialog():
            _render_payment_purchase_body(language)
        _payment_dialog()
        return
    # Compatibility fallback for older Streamlit builds.
    _render_payment_purchase_body(language)


def render_logged_in_account(
    language: str,
    user: dict[str, Any] | None = None,
    on_logout: Callable[[str, str], None] | None = None,
) -> None:
    """Render the approved responsive account summary with live quota values."""
    live = get_account_snapshot(block=False)
    if live and isinstance(live.get("user"), dict):
        user = dict(live["user"])
    else:
        user = user or st.session_state.get("sp_user", {})
    user_id = user.get("id", "—")
    projection = get_product_ui_projection()
    product_mode = projection.get("mode") == "a5_product"
    if product_mode:
        entitlement = projection["entitlement"]
        standard_remaining = entitlement["standard_remaining"]
        flagship_remaining = entitlement["flagship_remaining"]
    else:
        standard_remaining = "—"
        flagship_remaining = "—"

    st.markdown(
        """
        <style>
        section[data-testid="stSidebar"] {
            display: none !important;
        }

        [data-testid="collapsedControl"] {
            display: none !important;
        }

        .sp-account-card {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: clamp(0.3rem, 0.55vw, 0.58rem);
            width: 100%;
            min-width: 0;
            min-height: 2.85rem;
            padding: 0.48rem clamp(0.55rem, 0.85vw, 0.9rem);
            border: 1px solid rgba(148, 163, 184, 0.30);
            border-radius: 13px;
            background: #dbeafe;
            color: #1e3a5f;
            font-size: clamp(0.76rem, 0.84vw, 0.92rem);
            font-weight: 500;
            line-height: 1.25;
            white-space: nowrap;
            overflow: visible;
            box-sizing: border-box;
        }

        .sp-account-card span {
            display: inline-block;
            white-space: nowrap;
        }

        .sp-account-separator {
            flex: 0 0 auto;
            opacity: 0.48;
        }

        div[data-testid="stMarkdownContainer"]:has(.sp-logout-link) {
            width: 100%;
        }

        div[data-testid="stMarkdownContainer"]:has(.sp-logout-link) p {
            width: 100%;
            margin: 0;
        }

        .sp-logout-link {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            min-width: 6.8rem;
            min-height: 2.85rem;
            padding: 0.45rem 0.72rem;
            border: 1px solid rgba(148, 163, 184, 0.30);
            border-radius: 0.5rem;
            background: #dbeafe;
            color: #1e3a5f !important;
            text-decoration: none !important;
            white-space: nowrap !important;
            overflow-wrap: normal !important;
            word-break: keep-all !important;
            line-height: 1.15;
            font-size: clamp(0.78rem, 0.82vw, 0.92rem);
            font-weight: 400;
            box-sizing: border-box;
            cursor: pointer;
            transition: border-color 120ms ease, background-color 120ms ease;
        }

        .sp-logout-link:hover {
            border-color: #93c5fd;
            background: #bfdbfe;
            color: #1e3a5f !important;
        }

        .sp-logout-link:focus-visible {
            outline: 2px solid #60a5fa;
            outline-offset: 2px;
        }

        @media (max-width: 1150px) {
            .sp-account-card {
                font-size: 0.75rem;
                gap: 0.3rem;
                padding-left: 0.48rem;
                padding-right: 0.48rem;
            }

            .sp-logout-link {
                min-width: 5.8rem;
                font-size: 0.78rem;
                padding-left: 0.46rem;
                padding-right: 0.46rem;
            }
        }

        @media (max-width: 760px) {
            .sp-account-card {
                justify-content: flex-start;
                overflow-x: auto;
                scrollbar-width: none;
            }

            .sp-account-card::-webkit-scrollbar {
                display: none;
            }
        }
        </style>
        """,
        unsafe_allow_html=True,
    )

    info_col, logout_col = st.columns([5.9, 1.65])
    with info_col:
        balance_markup = (
            f"<span>{html.escape(tr(language, 'account_chip_standard', value=standard_remaining))}</span>"
            f'<span class="sp-account-separator">•</span>'
            f"<span>{html.escape(tr(language, 'account_chip_flagship', value=flagship_remaining))}</span>"
        )
        st.markdown(
            f"""
            <div class="sp-account-card">
              <span>{html.escape(tr(language, 'account_chip_user', value=user_id))}</span>
              <span class="sp-account-separator">•</span>
              {balance_markup}
            </div>
            """,
            unsafe_allow_html=True,
        )
    with logout_col:
        logout_label = html.escape(tr(language, "logout_short"))
        logout_help = html.escape(tr(language, "logout"), quote=True)
        st.markdown(
            f'<a class="sp-logout-link" href="/edge/logout" target="_self" '
            f'rel="nofollow" role="button" aria-label="{logout_help}" '
            f'title="{logout_help}">{logout_label}</a>',
            unsafe_allow_html=True,
        )