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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
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
import asyncio
import json
import sqlite3
import threading
from contextlib import closing, suppress
from unittest.mock import AsyncMock, Mock, patch

import pytest
import pytest_asyncio

from free_claude_code.messaging.models import IncomingMessage, MessageScope
from free_claude_code.messaging.trees import MessagingStorageError, TreeQueueManager
from free_claude_code.messaging.trees.snapshot import TreeSnapshot
from free_claude_code.messaging.workflow import MessagingWorkflow
from free_claude_code.runtime.code_sessions_sqlite import SQLiteCodeStore
from free_claude_code.runtime.messaging_import import import_legacy
from free_claude_code.runtime.messaging_sqlite import SQLiteMessagingStore
from free_claude_code.runtime.sqlite_database import SQLiteDatabase

pytestmark = pytest.mark.asyncio


def tree(platform="telegram", chat="chat", root="root"):
    return TreeSnapshot(
        scope=MessageScope(platform=platform, chat_id=chat),
        root_id=root,
        nodes={
            root: {
                "node_id": root,
                "status_message_id": f"status-{root}",
                "state": "completed",
                "parent_id": None,
                "parent_reference_id": None,
                "session_id": "native-session",
            }
        },
    )


@pytest_asyncio.fixture
async def storage(tmp_path):
    database = SQLiteDatabase(tmp_path / "fcc.db", tmp_path / "code.lock")
    await database.start()
    try:
        yield SQLiteMessagingStore(database, managed_message_cap=2)
    finally:
        await database.close()


async def test_tree_and_managed_messages_are_durable_and_scope_isolated(storage):
    first, second = tree(), tree("discord")
    await storage.commit_trees((first, second))
    for message in ("one", "two", "two", "three"):
        await storage.record_message_id("telegram", "chat", message, "in", "prompt")
    assert await storage.get_tracked_message_ids_for_chat("telegram", "chat") == [
        "two",
        "three",
    ]
    await storage.commit_trees((), clear_scope=first.scope)
    loaded = await storage.load_conversation_snapshot()
    assert list(loaded.trees) == [second.identity]
    assert loaded.trees[second.identity].nodes["root"]["session_id"] == "native-session"
    assert not await storage.get_tracked_message_ids_for_chat("telegram", "chat")


async def test_reference_collision_rolls_back_whole_write(storage):
    original = tree()
    await storage.commit_trees((original,))
    collision = tree(root="status-root")
    with pytest.raises(MessagingStorageError) as failure:
        await storage.commit_trees((collision,))
    assert isinstance(failure.value.__cause__, sqlite3.IntegrityError)
    assert (await storage.load_conversation_snapshot()).trees == {
        original.identity: original
    }


@pytest.mark.parametrize("damaged", [False, True])
async def test_import_keeps_valid_data_and_never_replays_after_clear(
    storage, tmp_path, damaged
):
    source = tmp_path / "sessions.json"
    valid = tree()
    source.write_text(
        json.dumps(
            {
                "conversation": {
                    "trees": [valid.to_json(), *([{"broken": True}] if damaged else [])]
                }
            }
        )
    )
    warning = await import_legacy(storage.database, source)
    assert bool(warning) is damaged
    assert source.exists() is damaged
    assert (await storage.load_conversation_snapshot()).trees == {valid.identity: valid}
    await storage.commit_trees((), clear_scope=valid.scope)
    assert await import_legacy(storage.database, source) is None
    assert (await storage.load_conversation_snapshot()).is_empty


async def test_unreadable_json_does_not_block_new_conversations(storage, tmp_path):
    source = tmp_path / "sessions.json"
    source.write_text('{"conversation":')
    assert await import_legacy(storage.database, source)
    assert source.read_text() == '{"conversation":'
    await storage.commit_trees((tree(),))
    assert not (await storage.load_conversation_snapshot()).is_empty
    assert await import_legacy(storage.database, source) is None


async def test_pending_import_keeps_restart_repair_information(storage, tmp_path):
    pending = tree()
    pending.nodes["root"]["state"] = "in_progress"
    source = tmp_path / "sessions.json"
    source.write_text(json.dumps(pending_to_legacy(pending)))
    await import_legacy(storage.database, source)
    assert (await storage.load_conversation_snapshot()).trees[pending.identity].nodes[
        "root"
    ]["state"] == "in_progress"


def pending_to_legacy(snapshot):
    return {"trees": {snapshot.root_id: snapshot.to_json()}}


async def test_new_schema_preserves_foreign_keys(storage):
    await storage.commit_trees((tree(),))
    with closing(sqlite3.connect(storage.database.path)) as connection:
        assert connection.execute("PRAGMA foreign_key_check").fetchall() == []


async def test_sqlite_prevents_deleting_a_root_without_its_tree(storage):
    original = tree()
    await storage.commit_trees((original,))
    with pytest.raises(sqlite3.IntegrityError):
        await storage.database.run(
            lambda connection: connection.execute("DELETE FROM messaging_nodes")
        )
    assert (await storage.load_conversation_snapshot()).trees == {
        original.identity: original
    }


@pytest.mark.parametrize("wrapped", [False, True])
@pytest.mark.parametrize("dictionary", [False, True])
async def test_historical_formats_and_message_log_preserve_resume_and_order(
    storage, tmp_path, wrapped, dictionary
):
    original = tree()
    legacy = original.to_json()
    del legacy["scope"]
    legacy["nodes"]["root"]["incoming"] = {"platform": "telegram", "chat_id": "chat"}
    trees = {"old": legacy} if dictionary else [legacy]
    conversation = {"trees": trees}
    payload = {"conversation": conversation} if wrapped else conversation
    payload["message_log"] = {
        "telegram:chat": [
            {"message_id": value, "direction": "out", "kind": "notice"}
            for value in (1, 2, 2, 3)
        ]
    }
    source = tmp_path / "sessions.json"
    source.write_text(json.dumps(payload))
    assert await import_legacy(storage.database, source) is None
    assert not source.exists()
    loaded = (await storage.load_conversation_snapshot()).trees[original.identity]
    assert loaded.nodes["root"]["session_id"] == "native-session"
    await storage.trim()
    assert await storage.get_tracked_message_ids_for_chat("telegram", "chat") == [
        "2",
        "3",
    ]


async def test_cleanup_failure_cannot_resurrect_cleared_history(storage, tmp_path):
    source = tmp_path / "sessions.json"
    original = tree()
    source.write_text(json.dumps(pending_to_legacy(original)))
    with patch.object(type(source), "unlink", side_effect=PermissionError("busy")):
        assert await import_legacy(storage.database, source)
    assert source.exists()
    await storage.commit_trees((), clear_scope=original.scope)
    assert await import_legacy(storage.database, source) is None
    assert not source.exists()
    assert (await storage.load_conversation_snapshot()).is_empty


async def test_invalid_graph_and_invalid_log_entry_do_not_discard_good_records(
    storage, tmp_path
):
    original, invalid = tree(), tree(root="bad")
    invalid.nodes["bad"]["parent_id"] = "missing"
    source = tmp_path / "sessions.json"
    source.write_text(
        json.dumps(
            {
                "trees": [original.to_json(), invalid.to_json()],
                "managed_messages": {
                    "discord:other": [
                        {"message_id": "good", "direction": "in", "kind": "prompt"},
                        {"message_id": "bad"},
                    ]
                },
            }
        )
    )
    assert await import_legacy(storage.database, source)
    assert source.exists()
    assert list((await storage.load_conversation_snapshot()).trees) == [
        original.identity
    ]
    assert await storage.get_tracked_message_ids_for_chat("discord", "other") == [
        "good"
    ]


async def test_failed_import_transaction_keeps_source_and_allows_retry(
    storage, tmp_path
):
    from free_claude_code.runtime.messaging_sqlite import write_tree

    source = tmp_path / "sessions.json"
    source.write_text(
        json.dumps({"trees": [tree().to_json(), tree(root="second").to_json()]})
    )

    def fail_second_tree(connection, snapshot):
        write_tree(connection, snapshot)
        if snapshot.root_id == "second":
            raise sqlite3.OperationalError("disk failure")

    with (
        patch(
            "free_claude_code.runtime.messaging_import.write_tree",
            side_effect=fail_second_tree,
        ),
        pytest.raises(sqlite3.OperationalError),
    ):
        await import_legacy(storage.database, source)
    assert source.exists()
    assert (await storage.load_conversation_snapshot()).is_empty
    assert (
        await storage.database.run(
            lambda connection: connection.execute(
                "SELECT count(*) FROM messaging_legacy_import"
            ).fetchone()[0],
            write=False,
        )
        == 0
    )
    assert await import_legacy(storage.database, source) is None
    assert not source.exists()


def incoming(node, *, text="prompt"):
    return IncomingMessage(
        platform="telegram", chat_id="chat", message_id=node, user_id="user", text=text
    )


async def import_receipt(database):
    return await database.run(
        lambda connection: dict(
            connection.execute("SELECT * FROM messaging_legacy_import").fetchone()
        ),
        write=False,
    )


@pytest.mark.parametrize(
    "field,value",
    [
        ("platform", "\ud800"),
        ("chat_id", "\ud800"),
        ("root_id", "\ud800"),
        ("root_id", "\0root"),
        ("node_id", "\ud800"),
        ("node_id", "\0child"),
        ("status_message_id", "\ud800"),
        ("status_message_id", "\0status"),
        ("session_id", "\ud800"),
    ],
)
async def test_import_rejects_storage_invalid_tree_without_reserving_references(
    storage, tmp_path, field, value
):
    before, after = tree(root="before"), tree(root="after")
    invalid = tree(root=value if field == "root_id" else "bad").to_json()
    root = invalid["root_id"]
    child = value if field == "node_id" else "child"
    invalid["nodes"][child] = {
        "node_id": child,
        "parent_id": root,
        "parent_reference_id": invalid["nodes"][root]["status_message_id"],
        "status_message_id": "shared-reference",
        "state": "completed",
        "session_id": "native-child",
    }
    if field in {"platform", "chat_id"}:
        invalid["scope"][field] = value
    elif field not in {"root_id", "node_id"}:
        invalid["nodes"][child][field] = value
    # A rejected tree must not claim a reference needed by a later valid tree.
    after.nodes["after"]["status_message_id"] = "shared-reference"
    source = tmp_path / "sessions.json"
    original = json.dumps({"trees": [before.to_json(), invalid, after.to_json()]})
    source.write_text(original)

    assert await import_legacy(storage.database, source)
    assert (await storage.load_conversation_snapshot()).trees == {
        before.identity: before,
        after.identity: after,
    }
    assert source.read_text() == original
    assert await import_receipt(storage.database) == {
        "source": "sessions.json",
        "outcome": "partial",
        "trees": 2,
        "messages": 0,
        "skipped": 1,
        "cleanup_pending": 0,
    }
    with closing(sqlite3.connect(storage.database.path)) as connection:
        assert connection.execute("PRAGMA foreign_key_check").fetchall() == []
        assert (
            connection.execute("SELECT count(*) FROM messaging_references").fetchone()[
                0
            ]
            == 4
        )
        assert (
            connection.execute("SELECT count(*) FROM messaging_trees").fetchone()[0]
            == 2
        )


@pytest.mark.parametrize("field", ["platform", "chat_id", "message_id", "ts", "kind"])
@pytest.mark.parametrize("value", ["\ud800", "\0kind"])
async def test_import_message_storage_validation_and_successful_duplicate_ownership(
    storage, tmp_path, field, value
):
    bad = {"message_id": "same", "direction": "in", "kind": "prompt", "ts": "old"}
    key = "telegram:chat"
    if field == "platform":
        key = f"{value}:chat"
    elif field == "chat_id":
        key = f"telegram:{value}"
    else:
        bad[field] = value
    good = {"message_id": "same", "direction": "out", "kind": "notice", "ts": "new"}
    messages = {key: [bad]}
    messages.setdefault("telegram:chat", []).extend([good, {**good, "ts": "duplicate"}])
    source = tmp_path / "sessions.json"
    source.write_text(json.dumps({"managed_messages": messages}))

    rejected = value == "\ud800" or field == "kind"
    assert bool(await import_legacy(storage.database, source)) is rejected
    assert source.exists() is rejected
    rows = await storage.database.run(
        lambda connection: [
            tuple(row)
            for row in connection.execute(
                "SELECT platform,chat_id,message_id,ts,direction,kind FROM messaging_managed_messages ORDER BY sequence"
            )
        ],
        write=False,
    )
    if rejected:
        assert rows == [("telegram", "chat", "same", "new", "out", "notice")]
    else:
        # NUL is legal in fields without a length CHECK. Preserve accepted text.
        assert rows[0] == (
            *key.split(":", 1),
            bad["message_id"],
            bad["ts"],
            "in",
            bad["kind"],
        )
        assert len(rows) == (1 if field == "ts" else 2)
    receipt = await import_receipt(storage.database)
    assert receipt["messages"] == len(rows)
    assert receipt["skipped"] == int(rejected)


@pytest.mark.parametrize("last_invalid", [False, True])
async def test_import_last_tree_identity_wins_even_when_unrecoverable(
    storage, tmp_path, last_invalid
):
    first, last = tree(), tree()
    last.nodes["root"]["session_id"] = "\ud800" if last_invalid else "last-session"
    source = tmp_path / "sessions.json"
    source.write_text(json.dumps({"trees": [first.to_json(), last.to_json()]}))
    assert await import_legacy(storage.database, source)
    assert (await storage.load_conversation_snapshot()).trees == (
        {} if last_invalid else {last.identity: last}
    )
    assert (await import_receipt(storage.database))["skipped"] == 1 + int(last_invalid)
    await storage.commit_trees((tree(root="fresh"),))


async def test_partial_import_survives_reopen_and_clear_without_replay(
    storage, tmp_path
):
    source = tmp_path / "sessions.json"
    source.write_text(
        json.dumps(
            {
                "trees": [tree().to_json()],
                "managed_messages": {
                    "telegram:chat": [
                        {"message_id": "bad", "direction": "in", "kind": "\0"}
                    ]
                },
            }
        )
    )
    assert await import_legacy(storage.database, source)
    receipt = await import_receipt(storage.database)
    await storage.database.close()
    for restart in range(2):
        database = SQLiteDatabase(storage.database.path, tmp_path / "code.lock")
        await database.start()
        try:
            reopened = SQLiteMessagingStore(database)
            assert await import_legacy(database, source) is None
            assert await import_receipt(database) == receipt
            assert (await reopened.load_conversation_snapshot()).is_empty is bool(
                restart
            )
            await reopened.commit_trees((), clear_scope=tree().scope)
        finally:
            await database.close()
    assert source.exists()


@pytest.mark.parametrize("failure_stage", ["receipt", "commit"])
async def test_import_outer_failure_rolls_back_released_units(
    storage, tmp_path, failure_stage
):
    source = tmp_path / "sessions.json"
    source.write_text(
        json.dumps(
            {
                "trees": [tree().to_json()],
                "managed_messages": {
                    "telegram:chat": [
                        {"message_id": "one", "direction": "in", "kind": "prompt"}
                    ]
                },
            }
        )
    )
    transaction = storage.database._transaction
    inserted = []

    def faulting_transaction(pool, operation, *, write):
        def authorize(action, arg1, arg2, database_name, trigger):
            if action == sqlite3.SQLITE_INSERT:
                inserted.append(arg1)
                if failure_stage == "receipt" and arg1 == "messaging_legacy_import":
                    return sqlite3.SQLITE_DENY
            if (
                failure_stage == "commit"
                and action == sqlite3.SQLITE_TRANSACTION
                and arg1 == "COMMIT"
                and "messaging_managed_messages" in inserted
            ):
                return sqlite3.SQLITE_DENY
            return sqlite3.SQLITE_OK

        observed = []

        def faulting(connection):
            observed.append(connection)
            connection.set_authorizer(authorize)
            return operation(connection)

        result = transaction(pool, faulting, write=write)
        # Failed transactions discard their connection; only successful leases
        # need the injected connection state restored before another checkout.
        for connection in observed:
            connection.set_authorizer(None)
        return result

    await storage.database.run(
        lambda connection: connection.execute("SELECT 1").close()
    )
    with (
        patch.object(storage.database, "_transaction", faulting_transaction),
        pytest.raises(sqlite3.DatabaseError, match="not authorized"),
    ):
        await import_legacy(storage.database, source)
    assert "messaging_nodes" in inserted and "messaging_managed_messages" in inserted
    assert source.exists()
    assert (await storage.load_conversation_snapshot()).is_empty
    assert not await storage.get_tracked_message_ids_for_chat("telegram", "chat")
    assert (
        await storage.database.run(
            lambda connection: connection.execute(
                "SELECT count(*) FROM messaging_legacy_import"
            ).fetchone()[0],
            write=False,
        )
        == 0
    )
    assert await import_legacy(storage.database, source) is None
    assert (await import_receipt(storage.database))["trees"] == 1
    assert (await import_receipt(storage.database))["messages"] == 1


@pytest.mark.parametrize("platform", ["none", "telegram"])
async def test_startup_partial_import_preserves_links_scopes_and_code(
    storage, tmp_path, platform
):
    from free_claude_code.application.code_sessions.models import CodeSession
    from free_claude_code.config.settings import Settings
    from free_claude_code.runtime.application import ApplicationRuntime
    from free_claude_code.runtime.configuration import ConfigurationService
    from free_claude_code.runtime.provider_manager import ProviderRuntimeManager

    code = SQLiteCodeStore(storage.database)
    await code.start()
    session = await code.create(
        CodeSession(id="saved-code", cwd=str(tmp_path), model="provider/model")
    )
    valid = tree(root="root-\U0001f600\0suffix")
    parent = valid.root_id
    for node_id in ("child", "grandchild"):
        valid.nodes[node_id] = {
            "node_id": node_id,
            "status_message_id": f"status-{node_id}",
            "parent_id": parent,
            "parent_reference_id": valid.nodes[parent]["status_message_id"],
            "state": "in_progress",
            "session_id": "native-\U0001f600",
        }
        parent = node_id
    inactive = tree("discord", root=valid.root_id)
    conflict = tree(root="status-child")
    source = tmp_path / "sessions.json"
    source.write_text(
        json.dumps(
            {
                "trees": [valid.to_json(), conflict.to_json(), inactive.to_json()],
                "managed_messages": {
                    "telegram:chat": [
                        {
                            "message_id": str(index),
                            "direction": "in",
                            "kind": "\0" if index == 0 else "prompt",
                        }
                        for index in range(4)
                    ]
                },
            }
        )
    )
    manager = ProviderRuntimeManager(
        Settings().model_copy(
            update={
                "messaging_platform": platform,
                "max_message_log_entries_per_chat": 2,
            }
        )
    )
    runtime = ApplicationRuntime(
        manager,
        configuration=AsyncMock(spec=ConfigurationService),
        transcriber=None,
        database=storage.database,
    )
    try:
        with patch(
            "free_claude_code.runtime.messaging_service.messaging_state_dir_path",
            return_value=str(tmp_path),
        ):
            await runtime._messaging._initialize_messaging_storage()
        assert runtime._messaging._messaging_storage_error is None
        assert runtime._messaging._messaging_warning
        assert (await storage.load_conversation_snapshot()).trees == {
            valid.identity: valid,
            inactive.identity: inactive,
        }
        assert await storage.get_tracked_message_ids_for_chat("telegram", "chat") == [
            "2",
            "3",
        ]
        assert (await import_receipt(storage.database))["skipped"] == 2
        assert (await import_receipt(storage.database))["messages"] == 3
        assert await code.get_session(session.id) == session
        assert (
            await storage.database.run(
                lambda connection: connection.execute(
                    "PRAGMA foreign_key_check"
                ).fetchall(),
                write=False,
            )
            == []
        )
    finally:
        await manager.close()
        await code.close()


async def test_failed_admission_never_launches_harness_or_publishes_memory(storage):
    processor = AsyncMock()
    manager = TreeQueueManager(processor, store=storage)
    with (
        patch.object(
            storage, "commit_trees", side_effect=sqlite3.OperationalError("disk full")
        ),
        pytest.raises(sqlite3.OperationalError),
    ):
        await manager.admit(incoming("new"), "status-new")
    assert manager.get_tree_count() == 0
    assert manager.task_count() == 0
    processor.assert_not_awaited()
    assert (await storage.load_conversation_snapshot()).is_empty


async def test_failed_clear_restores_runtime_prompts_queue_and_claim(storage):
    started, release = asyncio.Event(), asyncio.Event()
    prompts = []

    async def process(claim):
        prompts.append(claim.prompt)
        started.set()
        await release.wait()
        await manager.complete_claim(claim, "resumable")

    manager = TreeQueueManager(process, store=storage)
    root = await manager.admit(incoming("root", text="first"), "status-root")
    assert root.claim is not None
    await started.wait()
    await manager.admit(
        incoming("child", text="second"),
        "status-child",
        parent_reference_id="status-root",
    )
    try:
        with (
            patch.object(
                storage,
                "commit_trees",
                side_effect=sqlite3.OperationalError("disk full"),
            ),
            pytest.raises(sqlite3.OperationalError),
        ):
            await manager.clear_scope(root.claim.identity.scope)
        assert manager.task_count() == 1
        assert (
            len(
                (await storage.load_conversation_snapshot())
                .trees[root.claim.identity]
                .nodes
            )
            == 2
        )
    finally:
        release.set()
        await asyncio.wait_for(manager.wait_idle(), 5)
    assert prompts == ["first", "second"]
    child = await manager.get_node(root.claim.identity.scope, "child")
    assert child is not None and child.session_id == "resumable"


async def test_status_subtree_delete_preserves_prompt_sibling_branch(storage):
    original = tree()
    for node, reference in (("status-reply", "status-root"), ("prompt-reply", "root")):
        original.nodes[node] = {
            "node_id": node,
            "status_message_id": f"status-{node}",
            "state": "completed",
            "parent_id": "root",
            "parent_reference_id": reference,
            "session_id": node,
        }
    await storage.commit_trees((original,))
    manager = TreeQueueManager.from_snapshot(
        await storage.load_conversation_snapshot(), AsyncMock(), store=storage
    )
    await manager.remove_message_subtree(original.scope, "status-root")
    persisted = (await storage.load_conversation_snapshot()).trees[original.identity]
    assert set(persisted.nodes) == {"root", "prompt-reply"}
    assert persisted.nodes["root"]["status_message_id"] is None
    assert persisted.nodes["root"]["session_id"] is None
    assert persisted.nodes["prompt-reply"]["parent_reference_id"] == "root"


async def test_failure_after_scope_delete_rolls_back_both_trees_and_message_log(
    storage,
):
    original = tree()
    await storage.commit_trees((original,))
    await storage.record_message_id("telegram", "chat", "tracked", "out", "notice")
    invalid = tree(root="invalid")
    invalid.nodes["invalid"]["state"] = "not-a-state"
    with pytest.raises(ValueError):
        await storage.commit_trees((invalid,), clear_scope=original.scope)
    assert (await storage.load_conversation_snapshot()).trees == {
        original.identity: original
    }
    assert await storage.get_tracked_message_ids_for_chat("telegram", "chat") == [
        "tracked"
    ]


async def test_code_close_does_not_release_shared_database(storage, tmp_path):
    code = SQLiteCodeStore(storage.database)
    await code.start()
    await code.close()
    other = SQLiteDatabase(tmp_path / "fcc.db", tmp_path / "code.lock")
    try:
        with pytest.raises(sqlite3.OperationalError, match="another FCC"):
            await other.start()
        await storage.commit_trees((tree(),))
        assert not (await storage.load_conversation_snapshot()).is_empty
    finally:
        await other.close()


async def test_cancellation_during_commit_preserves_memory_and_database_agreement(
    storage,
):
    entered, release = threading.Event(), threading.Event()
    execute = storage.database.execute

    def blocked(operation, *, write=True):
        entered.set()
        if not release.wait(5):
            raise TimeoutError("test commit barrier")
        return execute(operation, write=write)

    manager = TreeQueueManager(AsyncMock(), store=storage)
    with patch.object(storage.database, "execute", blocked):
        admission = asyncio.create_task(manager.admit(incoming("new"), "status-new"))
        try:
            assert await asyncio.to_thread(entered.wait, 3)
            admission.cancel()
        finally:
            release.set()
        decision = await admission
    assert decision.accepted
    await asyncio.wait_for(manager.wait_idle(), 5)
    assert (await manager.snapshot()).trees == (
        await storage.load_conversation_snapshot()
    ).trees


async def test_restore_does_not_consume_inactive_platform_status_repairs(storage):
    telegram, discord = tree(), tree("discord")
    telegram.nodes["root"]["state"] = "in_progress"
    discord.nodes["root"]["state"] = "pending"
    await storage.commit_trees((telegram, discord))
    workflow = MessagingWorkflow(
        AsyncMock(), AsyncMock(), storage, platform_name="telegram"
    )
    await workflow.restore()
    await workflow.close()
    loaded = await storage.load_conversation_snapshot()
    assert loaded.trees[telegram.identity].nodes["root"]["state"] == "error"
    assert loaded.trees[discord.identity].nodes["root"]["state"] == "pending"
    next_workflow = MessagingWorkflow(
        AsyncMock(), AsyncMock(), storage, platform_name="discord"
    )
    await next_workflow.restore()
    assert any(
        target.scope.platform == "discord"
        for target in next_workflow.tree_queue.restored_stale_targets
    )


@pytest.mark.parametrize("boundary", ["session", "terminal", "failure", "successor"])
async def test_worker_write_failure_interrupts_queue_and_new_reply_recovers(
    storage, boundary
):
    started, release = asyncio.Event(), asyncio.Event()
    processed, interrupted = [], []

    async def process(claim):
        processed.append(claim.node.node_id)
        if claim.node.node_id == "root":
            started.set()
            await release.wait()
            if boundary == "session":
                await manager.record_session(claim, "native")
            elif boundary == "failure":
                await manager.fail_claim(claim)
        await manager.complete_claim(claim, "native")

    manager = TreeQueueManager(
        process, store=storage, unexpected_failure_callback=interrupted.append
    )
    root = await manager.admit(incoming("root"), "status-root")
    assert root.claim is not None
    await started.wait()
    await manager.admit(incoming("child"), "status-child", parent_reference_id="root")
    condition = {
        "session": "NEW.node_id='root' AND NEW.session_id='native'",
        "terminal": "NEW.node_id='root' AND NEW.state='completed'",
        "failure": "NEW.node_id='root' AND NEW.state='error'",
        "successor": "NEW.node_id='child' AND NEW.state='in_progress'",
    }[boundary]
    await storage.database.run(
        lambda connection: connection.execute(
            f"CREATE TRIGGER reject_transition BEFORE UPDATE ON messaging_nodes WHEN {condition} "
            "BEGIN SELECT RAISE(ABORT, 'test storage failure'); END"
        )
    )
    release.set()
    await asyncio.wait_for(manager.wait_idle(), 5)
    assert processed == ["root"]
    assert manager.task_count() == 0
    assert interrupted
    assert "child" in {
        node.node_id for result in interrupted for node in result.affected
    }
    saved = await storage.load_conversation_snapshot()
    assert (await manager.snapshot()).trees == saved.trees
    await storage.database.run(
        lambda connection: connection.execute("DROP TRIGGER reject_transition")
    )
    await manager.admit(incoming("later"), "status-later", parent_reference_id="root")
    await asyncio.wait_for(manager.wait_idle(), 5)
    assert processed == ["root", "later"]
    nodes = (
        (await storage.load_conversation_snapshot()).trees[root.claim.identity].nodes
    )
    assert nodes["root"]["state"] == (
        "completed" if boundary == "successor" else "error"
    )
    assert nodes["child"]["state"] == "error"
    assert nodes["later"]["state"] == "completed"


async def test_shutdown_drains_workers_even_when_interruption_cannot_be_saved(
    storage, mock_platform
):
    started, stopped = asyncio.Event(), asyncio.Event()
    cli = AsyncMock()
    workflow = MessagingWorkflow(mock_platform, cli, storage, platform_name="telegram")

    async def process(claim):
        started.set()
        try:
            await asyncio.Event().wait()
        finally:
            await manager.fail_claim(claim)
            stopped.set()

    manager = TreeQueueManager(process, store=storage)
    workflow._tree_queue = manager
    await manager.admit(incoming("root"), "status-root")
    await started.wait()
    await manager.admit(incoming("child"), "status-child", parent_reference_id="root")
    execute = storage.database.execute
    writes = 0

    def broken(operation, *, write=True):
        nonlocal writes
        if write:
            writes += 1
            raise sqlite3.OperationalError("disk full")
        return execute(operation, write=write)

    try:
        with patch.object(storage.database, "execute", broken):
            await asyncio.wait_for(workflow.close(), 5)
        assert writes == 1
        assert stopped.is_set()
        assert manager.task_count() == 0
        cli.stop_all.assert_awaited()
        assert any(
            call.args[1] == "status-child"
            for call in mock_platform.queue_edit_message.call_args_list
        )
    finally:
        await manager.shutdown()
        await manager.wait_idle()


async def test_persistent_failure_has_no_retries_and_failed_recovery_keeps_history(
    storage,
):
    started, release = asyncio.Event(), asyncio.Event()
    processed = []
    # An outbound failure must not prevent task retirement.
    notify = Mock(side_effect=RuntimeError("platform unavailable"))

    async def process(claim):
        processed.append(claim.node.node_id)
        started.set()
        await release.wait()
        await manager.complete_claim(claim, "native")

    manager = TreeQueueManager(
        process, store=storage, unexpected_failure_callback=notify
    )
    root = await manager.admit(incoming("root"), "status-root")
    await started.wait()
    await manager.admit(incoming("child"), "status-child", parent_reference_id="root")
    original = await storage.load_conversation_snapshot()
    writes = 0
    execute = storage.database.execute

    def broken(operation, *, write=True):
        nonlocal writes
        if write:
            writes += 1
            raise sqlite3.OperationalError("disk full")
        return execute(operation, write=False)

    with patch.object(storage.database, "execute", broken):
        release.set()
        await asyncio.wait_for(manager.wait_idle(), 5)
        assert writes == 1
        assert processed == ["root"]
        assert manager.task_count() == 0
        with pytest.raises(MessagingStorageError):
            await manager.admit(
                incoming("new"), "status-new", parent_reference_id="root"
            )
        assert writes == 2
        assert (await manager.snapshot()) == original
        assert (await storage.load_conversation_snapshot()) == original
    notify.assert_called_once()
    await manager.admit(incoming("new"), "status-new", parent_reference_id="root")
    await asyncio.wait_for(manager.wait_idle(), 5)
    assert processed == ["root", "new"]
    nodes = (
        (await storage.load_conversation_snapshot()).trees[root.claim.identity].nodes
    )
    assert nodes["root"]["state"] == nodes["child"]["state"] == "error"
    assert nodes["new"]["state"] == "completed"


async def test_gated_tree_can_be_cleared_and_replaced_before_old_cleanup_finishes(
    storage, monkeypatch
):
    monkeypatch.setattr(
        "free_claude_code.messaging.trees.manager.CANCEL_TASK_DRAIN_TIMEOUT_S", 0.01
    )
    faulted, cleanup_release = asyncio.Event(), asyncio.Event()
    new_started, new_release = asyncio.Event(), asyncio.Event()
    claims = []
    notifications = []

    async def process(claim):
        claims.append(claim)
        if len(claims) == 1:
            try:
                with patch.object(
                    storage.database,
                    "execute",
                    side_effect=sqlite3.OperationalError("disk full"),
                ):
                    await manager.record_session(claim, "old-session")
            except MessagingStorageError:
                faulted.set()
                while not cleanup_release.is_set():
                    with suppress(asyncio.CancelledError):
                        await cleanup_release.wait()
                # This late update must not affect the same-ID replacement.
                await manager.complete_claim(claim, "old-session")
                raise
        new_started.set()
        await new_release.wait()
        await manager.complete_claim(claim, "new-session")

    manager = TreeQueueManager(
        process, store=storage, unexpected_failure_callback=notifications.append
    )
    old = await manager.admit(incoming("root"), "status-root")
    assert old.claim is not None
    await faulted.wait()
    try:
        with pytest.raises(MessagingStorageError, match="cleaning up"):
            await manager.admit(
                incoming("child"), "status-child", parent_reference_id="root"
            )
        await manager.clear_scope(old.claim.identity.scope)
        replacement = await manager.admit(incoming("root"), "status-root")
        assert replacement.claim is not None
        await new_started.wait()
        assert replacement.claim.claim_id != old.claim.claim_id
        assert manager.task_count() == 2
    finally:
        cleanup_release.set()
        new_release.set()
        await asyncio.wait_for(manager.wait_idle(), 5)
    nodes = (await storage.load_conversation_snapshot()).trees[old.claim.identity].nodes
    assert list(nodes) == ["root"]
    assert nodes["root"]["session_id"] == "new-session"
    assert not notifications