tortoise-graph 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tortoise/__init__.py +45 -0
- tortoise/__main__.py +1865 -0
- tortoise/analyze.py +473 -0
- tortoise/api.py +303 -0
- tortoise/archived/search_legacy.py +31 -0
- tortoise/audit.py +236 -0
- tortoise/audit_events.py +284 -0
- tortoise/auth.py +107 -0
- tortoise/backup.py +155 -0
- tortoise/config.py +99 -0
- tortoise/connector_loader.py +74 -0
- tortoise/connectors/__init__.py +5 -0
- tortoise/connectors/github.py +432 -0
- tortoise/connectors/github_docs.py +146 -0
- tortoise/connectors/linear.py +199 -0
- tortoise/connectors/slack.py +222 -0
- tortoise/consistency.py +132 -0
- tortoise/crypto.py +52 -0
- tortoise/deployment.py +62 -0
- tortoise/domain_loader.py +142 -0
- tortoise/dream.py +153 -0
- tortoise/embedded_reaper.py +744 -0
- tortoise/embeddings.py +227 -0
- tortoise/ep.py +534 -0
- tortoise/exceptions.py +16 -0
- tortoise/extractor.py +945 -0
- tortoise/hosted_api.py +1982 -0
- tortoise/hosted_middleware.py +22 -0
- tortoise/idempotency.py +39 -0
- tortoise/ids.py +23 -0
- tortoise/indexer/github_indexer.py +129 -0
- tortoise/ingest.py +614 -0
- tortoise/log.py +29 -0
- tortoise/m0.py +50 -0
- tortoise/mcp_auth.py +337 -0
- tortoise/mcp_client.py +108 -0
- tortoise/mcp_server.py +1356 -0
- tortoise/memory_orchestrator.py +492 -0
- tortoise/memory_scope.py +15 -0
- tortoise/migrate_db.py +244 -0
- tortoise/migrate_kinds.py +141 -0
- tortoise/mining.py +338 -0
- tortoise/models.py +109 -0
- tortoise/monitoring.py +133 -0
- tortoise/navigation.py +216 -0
- tortoise/pack_registry.py +767 -0
- tortoise/pipeline_cli.py +197 -0
- tortoise/projection/__init__.py +913 -0
- tortoise/projection/edges.py +311 -0
- tortoise/projection/entities.py +521 -0
- tortoise/projection/grounding.py +95 -0
- tortoise/projection/propagation.py +114 -0
- tortoise/projection.py +37 -0
- tortoise/quadrature.py +90 -0
- tortoise/query_suggestions.py +194 -0
- tortoise/quota.py +163 -0
- tortoise/ranking.py +280 -0
- tortoise/render.py +108 -0
- tortoise/scripts/manifest.py +67 -0
- tortoise/sdk.py +4713 -0
- tortoise/search_engine.py +887 -0
- tortoise/security.py +268 -0
- tortoise/selfhost.py +157 -0
- tortoise/selfhost_api.py +201 -0
- tortoise/session_continuity.py +82 -0
- tortoise/session_indexer.py +569 -0
- tortoise/shared_state/__init__.py +20 -0
- tortoise/shared_state/concurrency.py +113 -0
- tortoise/shared_state/event_log.py +192 -0
- tortoise/shared_state/events.py +150 -0
- tortoise/shared_state/monitoring.py +87 -0
- tortoise/shared_state/recovery.py +126 -0
- tortoise/shared_state/tests/__init__.py +15 -0
- tortoise/shared_state/tests/test_concurrency.py +123 -0
- tortoise/shared_state/tests/test_events.py +145 -0
- tortoise/shared_state/tests/test_monitoring.py +54 -0
- tortoise/shared_state/tests/test_recovery.py +181 -0
- tortoise/source_credibility.py +306 -0
- tortoise/svbp.py +446 -0
- tortoise/taxonomy.py +71 -0
- tortoise/test_cross_ontology.py +180 -0
- tortoise/test_tortoise_client.py +206 -0
- tortoise/tool_registry.py +805 -0
- tortoise/tortoise_client.py +262 -0
- tortoise/verify_ontology.py +154 -0
- tortoise/version_vector.py +97 -0
- tortoise/weights.py +61 -0
- tortoise_graph-0.2.0.dist-info/METADATA +152 -0
- tortoise_graph-0.2.0.dist-info/RECORD +93 -0
- tortoise_graph-0.2.0.dist-info/WHEEL +5 -0
- tortoise_graph-0.2.0.dist-info/entry_points.txt +4 -0
- tortoise_graph-0.2.0.dist-info/licenses/LICENSE +95 -0
- tortoise_graph-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""Tests for concurrency — locked_append (flock) and atomic_claim (os.rename).
|
|
2
|
+
|
|
3
|
+
Covers: successful append, timeout path, atomic claim, race-condition guard.
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
import time
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
16
|
+
|
|
17
|
+
from shared_state.concurrency import atomic_claim, locked_append
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class TestLockedAppend:
|
|
21
|
+
def test_successful_append(self):
|
|
22
|
+
d = Path(tempfile.mkdtemp())
|
|
23
|
+
log = d / "test.jsonl"
|
|
24
|
+
ok = locked_append(log, {"event": "start", "n": 1})
|
|
25
|
+
assert ok
|
|
26
|
+
assert log.exists()
|
|
27
|
+
lines = log.read_text().strip().split("\n")
|
|
28
|
+
assert len(lines) == 1
|
|
29
|
+
ev = json.loads(lines[0])
|
|
30
|
+
assert ev["event"] == "start"
|
|
31
|
+
assert ev["n"] == 1
|
|
32
|
+
|
|
33
|
+
def test_multiple_appends(self):
|
|
34
|
+
d = Path(tempfile.mkdtemp())
|
|
35
|
+
log = d / "multi.jsonl"
|
|
36
|
+
locked_append(log, {"n": 1})
|
|
37
|
+
locked_append(log, {"n": 2})
|
|
38
|
+
locked_append(log, {"n": 3})
|
|
39
|
+
lines = log.read_text().strip().split("\n")
|
|
40
|
+
assert len(lines) == 3
|
|
41
|
+
for i, line in enumerate(lines, 1):
|
|
42
|
+
assert json.loads(line)["n"] == i
|
|
43
|
+
|
|
44
|
+
def test_creates_parent_dirs(self):
|
|
45
|
+
d = Path(tempfile.mkdtemp())
|
|
46
|
+
nested = d / "deep" / "nested" / "log.jsonl"
|
|
47
|
+
ok = locked_append(nested, {"x": 1})
|
|
48
|
+
assert ok
|
|
49
|
+
assert nested.exists()
|
|
50
|
+
|
|
51
|
+
def test_timeout_returns_false(self):
|
|
52
|
+
d = Path(tempfile.mkdtemp())
|
|
53
|
+
log = d / "timeout.jsonl"
|
|
54
|
+
# Hold the lock, then try to acquire with minimal timeout
|
|
55
|
+
import fcntl
|
|
56
|
+
import os
|
|
57
|
+
|
|
58
|
+
# Open and lock the file
|
|
59
|
+
fd = os.open(log, os.O_WRONLY | os.O_CREAT, 0o600)
|
|
60
|
+
try:
|
|
61
|
+
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
62
|
+
# Now try locked_append with 1ms timeout — should fail
|
|
63
|
+
ok = locked_append(log, {"should": "fail"}, timeout_ms=1.0)
|
|
64
|
+
assert not ok, "expected timeout (False)"
|
|
65
|
+
finally:
|
|
66
|
+
fcntl.flock(fd, fcntl.LOCK_UN)
|
|
67
|
+
os.close(fd)
|
|
68
|
+
|
|
69
|
+
def test_append_unicode(self):
|
|
70
|
+
d = Path(tempfile.mkdtemp())
|
|
71
|
+
log = d / "unicode.jsonl"
|
|
72
|
+
locked_append(log, {"msg": "café résumé — ñ"})
|
|
73
|
+
lines = log.read_text().strip().split("\n")
|
|
74
|
+
ev = json.loads(lines[0])
|
|
75
|
+
assert ev["msg"] == "café résumé — ñ"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TestAtomicClaim:
|
|
79
|
+
def test_successful_claim(self):
|
|
80
|
+
d = Path(tempfile.mkdtemp())
|
|
81
|
+
cards = d / "cards"
|
|
82
|
+
result = atomic_claim(cards, "card-001", {"owner": "agent-1", "priority": 5})
|
|
83
|
+
assert result is not None
|
|
84
|
+
assert result.exists()
|
|
85
|
+
data = json.loads(result.read_text())
|
|
86
|
+
assert data["owner"] == "agent-1"
|
|
87
|
+
assert data["priority"] == 5
|
|
88
|
+
|
|
89
|
+
def test_second_claim_returns_none(self):
|
|
90
|
+
d = Path(tempfile.mkdtemp())
|
|
91
|
+
cards = d / "cards"
|
|
92
|
+
first = atomic_claim(cards, "card-001", {"owner": "agent-1"})
|
|
93
|
+
assert first is not None
|
|
94
|
+
second = atomic_claim(cards, "card-001", {"owner": "agent-2"})
|
|
95
|
+
assert second is None
|
|
96
|
+
|
|
97
|
+
def test_creates_parent_dirs(self):
|
|
98
|
+
d = Path(tempfile.mkdtemp())
|
|
99
|
+
nested = d / "deep" / "board" / "cards"
|
|
100
|
+
result = atomic_claim(nested, "card-001", {"x": 1})
|
|
101
|
+
assert result is not None
|
|
102
|
+
assert result.exists()
|
|
103
|
+
|
|
104
|
+
def test_custom_suffix(self):
|
|
105
|
+
d = Path(tempfile.mkdtemp())
|
|
106
|
+
cards = d / "cards"
|
|
107
|
+
result = atomic_claim(cards, "task-5", {"id": "task-5"}, suffix=".task")
|
|
108
|
+
assert result is not None
|
|
109
|
+
assert result.suffix == ".task"
|
|
110
|
+
|
|
111
|
+
def test_tempfile_cleanup_on_error(self):
|
|
112
|
+
d = Path(tempfile.mkdtemp())
|
|
113
|
+
cards = d / "cards"
|
|
114
|
+
# Make the target directory read-only so os.rename fails after tempfile write
|
|
115
|
+
cards.mkdir(parents=True)
|
|
116
|
+
|
|
117
|
+
# Use a path that will cause write failure: write to readonly dir
|
|
118
|
+
# Instead, test that the function handles the card_dir properly
|
|
119
|
+
# by making it unwriteable after creating the temp file
|
|
120
|
+
# Actually, the simplest test: claim twice — second returns None
|
|
121
|
+
atomic_claim(cards, "card-002", {"x": 1})
|
|
122
|
+
result2 = atomic_claim(cards, "card-002", {"x": 2})
|
|
123
|
+
assert result2 is None
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Tests for events — EventCodec, register_event_type, event_types."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
import tempfile
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
12
|
+
|
|
13
|
+
from shared_state.events import EventCodec, event_types, register_event_type
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture(autouse=True)
|
|
17
|
+
def _clear_registry():
|
|
18
|
+
"""Isolate tests from global state."""
|
|
19
|
+
import shared_state.events as _ev
|
|
20
|
+
_ev._event_types.clear()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class TestEventCodecEncode:
|
|
24
|
+
def test_encode_basic(self):
|
|
25
|
+
register_event_type("test_type")
|
|
26
|
+
ev = EventCodec.encode("test_type", {"key": "value"}, event_id="ev-1")
|
|
27
|
+
assert ev["type"] == "test_type"
|
|
28
|
+
assert ev["version"] == 1
|
|
29
|
+
assert ev["event_id"] == "ev-1"
|
|
30
|
+
assert ev["key"] == "value"
|
|
31
|
+
assert "timestamp" in ev
|
|
32
|
+
|
|
33
|
+
def test_encode_custom_timestamp(self):
|
|
34
|
+
register_event_type("test_type")
|
|
35
|
+
ev = EventCodec.encode("test_type", {}, timestamp=42.0)
|
|
36
|
+
assert ev["timestamp"] == 42.0
|
|
37
|
+
|
|
38
|
+
def test_encode_with_extra_fields(self):
|
|
39
|
+
register_event_type("test_type")
|
|
40
|
+
ev = EventCodec.encode("test_type", {"a": 1}, extra="bonus")
|
|
41
|
+
assert ev["extra"] == "bonus"
|
|
42
|
+
assert ev["a"] == 1
|
|
43
|
+
|
|
44
|
+
def test_encode_unregistered_type_raises(self):
|
|
45
|
+
with pytest.raises(KeyError, match="Unknown event type"):
|
|
46
|
+
EventCodec.encode("nonexistent", {})
|
|
47
|
+
|
|
48
|
+
def test_encode_version_matches_upcaster_chain(self):
|
|
49
|
+
register_event_type("v2_type", upcasters=[lambda e: e])
|
|
50
|
+
ev = EventCodec.encode("v2_type", {})
|
|
51
|
+
assert ev["version"] == 2
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TestEventCodecDecode:
|
|
55
|
+
def test_decode_no_type_passthrough(self):
|
|
56
|
+
assert EventCodec.decode({"x": 1}) == {"x": 1}
|
|
57
|
+
|
|
58
|
+
def test_decode_unknown_type_passthrough(self):
|
|
59
|
+
assert EventCodec.decode({"type": "unknown", "x": 1}) == {"type": "unknown", "x": 1}
|
|
60
|
+
|
|
61
|
+
def test_decode_current_version_no_change(self):
|
|
62
|
+
register_event_type("test_type")
|
|
63
|
+
raw = {"type": "test_type", "version": 1, "data": "hi"}
|
|
64
|
+
assert EventCodec.decode(raw) == raw
|
|
65
|
+
|
|
66
|
+
def test_decode_upcaster_chain(self):
|
|
67
|
+
register_event_type("v3_type", upcasters=[
|
|
68
|
+
lambda e: {**e, "v2_added": True, "version": 2},
|
|
69
|
+
lambda e: {**e, "v3_added": True, "version": 3},
|
|
70
|
+
])
|
|
71
|
+
raw = {"type": "v3_type", "version": 1, "orig": "data"}
|
|
72
|
+
result = EventCodec.decode(raw)
|
|
73
|
+
assert result["version"] == 3
|
|
74
|
+
assert result["v2_added"] is True
|
|
75
|
+
assert result["v3_added"] is True
|
|
76
|
+
assert result["orig"] == "data"
|
|
77
|
+
|
|
78
|
+
def test_decode_already_current_skips_upcasters(self):
|
|
79
|
+
register_event_type("v2_type", upcasters=[lambda e: {**e, "extra": True}])
|
|
80
|
+
raw = {"type": "v2_type", "version": 2, "x": 1}
|
|
81
|
+
result = EventCodec.decode(raw)
|
|
82
|
+
assert result == raw
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class TestReadAll:
|
|
86
|
+
def test_read_all_from_jsonl(self):
|
|
87
|
+
register_event_type("test_type")
|
|
88
|
+
d = Path(tempfile.mkdtemp())
|
|
89
|
+
log = d / "events.jsonl"
|
|
90
|
+
log.write_text(
|
|
91
|
+
json.dumps(EventCodec.encode("test_type", {"n": 1})) + "\n" +
|
|
92
|
+
json.dumps(EventCodec.encode("test_type", {"n": 2})) + "\n"
|
|
93
|
+
)
|
|
94
|
+
events = EventCodec.read_all(str(log))
|
|
95
|
+
assert len(events) == 2
|
|
96
|
+
assert events[0]["n"] == 1
|
|
97
|
+
assert events[1]["n"] == 2
|
|
98
|
+
|
|
99
|
+
def test_read_all_empty_file(self):
|
|
100
|
+
d = Path(tempfile.mkdtemp())
|
|
101
|
+
log = d / "empty.jsonl"
|
|
102
|
+
log.write_text("")
|
|
103
|
+
assert EventCodec.read_all(str(log)) == []
|
|
104
|
+
|
|
105
|
+
def test_read_all_skips_blank_lines(self):
|
|
106
|
+
register_event_type("test_type")
|
|
107
|
+
d = Path(tempfile.mkdtemp())
|
|
108
|
+
log = d / "events.jsonl"
|
|
109
|
+
log.write_text(
|
|
110
|
+
"\n" +
|
|
111
|
+
json.dumps(EventCodec.encode("test_type", {"n": 1})) + "\n\n"
|
|
112
|
+
)
|
|
113
|
+
events = EventCodec.read_all(str(log))
|
|
114
|
+
assert len(events) == 1
|
|
115
|
+
|
|
116
|
+
def test_read_all_upcasts_on_read(self):
|
|
117
|
+
register_event_type("v2_type", upcasters=[lambda e: {**e, "upgraded": True}])
|
|
118
|
+
d = Path(tempfile.mkdtemp())
|
|
119
|
+
log = d / "events.jsonl"
|
|
120
|
+
log.write_text(json.dumps({"type": "v2_type", "version": 1, "x": 1}) + "\n")
|
|
121
|
+
events = EventCodec.read_all(str(log))
|
|
122
|
+
assert events[0]["upgraded"] is True
|
|
123
|
+
|
|
124
|
+
def test_read_all_nonexistent_file(self):
|
|
125
|
+
with pytest.raises(FileNotFoundError):
|
|
126
|
+
EventCodec.read_all("/nonexistent/path.jsonl")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class TestEventTypes:
|
|
130
|
+
def test_event_types_returns_version_map(self):
|
|
131
|
+
register_event_type("a")
|
|
132
|
+
register_event_type("b", upcasters=[lambda e: e, lambda e: e])
|
|
133
|
+
types = event_types()
|
|
134
|
+
assert types == {"a": 1, "b": 3}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class TestRegistration:
|
|
138
|
+
def test_duplicate_registration_raises(self):
|
|
139
|
+
register_event_type("dup")
|
|
140
|
+
with pytest.raises(ValueError, match="already registered"):
|
|
141
|
+
register_event_type("dup")
|
|
142
|
+
|
|
143
|
+
def test_register_with_upcasters(self):
|
|
144
|
+
register_event_type("with_upcasters", upcasters=[lambda e: e, lambda e: e])
|
|
145
|
+
assert event_types()["with_upcasters"] == 3
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Tests for monitoring — GoldenSignals, collect_signals."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
10
|
+
|
|
11
|
+
from shared_state.monitoring import GoldenSignals, collect_signals
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TestGoldenSignals:
|
|
15
|
+
def test_initial_state(self):
|
|
16
|
+
gs = GoldenSignals()
|
|
17
|
+
assert gs.latency_p99 == 0.0
|
|
18
|
+
assert gs.error_rate == 0.0
|
|
19
|
+
assert gs.throughput == 0.0
|
|
20
|
+
|
|
21
|
+
def test_record_and_latency_p99(self):
|
|
22
|
+
gs = GoldenSignals(max_samples=100)
|
|
23
|
+
for i in range(100):
|
|
24
|
+
gs.record(float(i))
|
|
25
|
+
snap = gs.snapshot()
|
|
26
|
+
assert snap["sample_count"] == 100
|
|
27
|
+
assert snap["latency_p99"] >= 98.0
|
|
28
|
+
|
|
29
|
+
def test_error_rate(self):
|
|
30
|
+
gs = GoldenSignals(max_samples=10)
|
|
31
|
+
for i in range(10):
|
|
32
|
+
gs.record(1.0, is_error=(i < 3))
|
|
33
|
+
assert gs.error_rate == 0.3
|
|
34
|
+
|
|
35
|
+
def test_throughput(self):
|
|
36
|
+
gs = GoldenSignals()
|
|
37
|
+
gs.record(1.0)
|
|
38
|
+
assert gs.throughput > 0
|
|
39
|
+
|
|
40
|
+
def test_saturation(self):
|
|
41
|
+
gs = GoldenSignals(max_samples=5)
|
|
42
|
+
for i in range(5):
|
|
43
|
+
gs.record(1.0)
|
|
44
|
+
assert gs.saturation == 1.0
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TestCollectSignals:
|
|
48
|
+
def test_collect_signals_returns_stub(self):
|
|
49
|
+
result = collect_signals("test-component")
|
|
50
|
+
assert result["component"] == "test-component"
|
|
51
|
+
assert result["latency_p99"] == 0.0
|
|
52
|
+
assert result["throughput"] == 0.0
|
|
53
|
+
assert result["error_rate"] == 0.0
|
|
54
|
+
assert result["saturation"] == 0.0
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Tests for recovery — scan_incomplete_cards, dedup_events, find_last_checkpoint.
|
|
2
|
+
|
|
3
|
+
Covers: incomplete card detection, event ID deduplication, checkpoint recovery.
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
15
|
+
|
|
16
|
+
from shared_state.recovery import (
|
|
17
|
+
dedup_events,
|
|
18
|
+
find_last_checkpoint,
|
|
19
|
+
scan_incomplete_cards,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# ── scan_incomplete_cards ───────────────────────────────────
|
|
24
|
+
|
|
25
|
+
class TestScanIncomplete:
|
|
26
|
+
def test_empty_dir(self):
|
|
27
|
+
d = Path(tempfile.mkdtemp())
|
|
28
|
+
assert scan_incomplete_cards(d) == []
|
|
29
|
+
|
|
30
|
+
def test_nonexistent_dir(self):
|
|
31
|
+
assert scan_incomplete_cards(Path("/nonexistent/xyzzy")) == []
|
|
32
|
+
|
|
33
|
+
def test_all_complete(self):
|
|
34
|
+
d = Path(tempfile.mkdtemp())
|
|
35
|
+
(d / "c1.card").write_text(json.dumps({"card_id": "c1", "completed_at": 123}))
|
|
36
|
+
assert scan_incomplete_cards(d) == []
|
|
37
|
+
|
|
38
|
+
def test_some_incomplete(self):
|
|
39
|
+
d = Path(tempfile.mkdtemp())
|
|
40
|
+
(d / "c1.card").write_text(json.dumps({"card_id": "c1", "completed_at": 123}))
|
|
41
|
+
(d / "c2.card").write_text(json.dumps({"card_id": "c2"}))
|
|
42
|
+
incomplete = scan_incomplete_cards(d)
|
|
43
|
+
assert len(incomplete) == 1
|
|
44
|
+
assert incomplete[0]["card_id"] == "c2"
|
|
45
|
+
|
|
46
|
+
def test_all_incomplete(self):
|
|
47
|
+
d = Path(tempfile.mkdtemp())
|
|
48
|
+
(d / "a.card").write_text(json.dumps({"card_id": "a"}))
|
|
49
|
+
(d / "b.card").write_text(json.dumps({"card_id": "b"}))
|
|
50
|
+
incomplete = scan_incomplete_cards(d)
|
|
51
|
+
assert len(incomplete) == 2
|
|
52
|
+
|
|
53
|
+
def test_skips_dotfiles(self):
|
|
54
|
+
d = Path(tempfile.mkdtemp())
|
|
55
|
+
(d / ".tmp.card").write_text(json.dumps({"card_id": "tmp"}))
|
|
56
|
+
(d / "real.card").write_text(json.dumps({"card_id": "real"}))
|
|
57
|
+
incomplete = scan_incomplete_cards(d)
|
|
58
|
+
assert len(incomplete) == 1
|
|
59
|
+
assert incomplete[0]["card_id"] == "real"
|
|
60
|
+
|
|
61
|
+
def test_skips_invalid_json(self):
|
|
62
|
+
d = Path(tempfile.mkdtemp())
|
|
63
|
+
(d / "bad.card").write_text("not json")
|
|
64
|
+
(d / "good.card").write_text(json.dumps({"card_id": "good"}))
|
|
65
|
+
incomplete = scan_incomplete_cards(d)
|
|
66
|
+
assert len(incomplete) == 1
|
|
67
|
+
assert incomplete[0]["card_id"] == "good"
|
|
68
|
+
|
|
69
|
+
def test_custom_suffix(self):
|
|
70
|
+
d = Path(tempfile.mkdtemp())
|
|
71
|
+
(d / "t1.task").write_text(json.dumps({"task_id": "t1"}))
|
|
72
|
+
(d / "t2.task").write_text(json.dumps({"task_id": "t2", "completed_at": 1}))
|
|
73
|
+
incomplete = scan_incomplete_cards(d, suffix=".task")
|
|
74
|
+
assert len(incomplete) == 1
|
|
75
|
+
assert incomplete[0]["task_id"] == "t1"
|
|
76
|
+
|
|
77
|
+
def test_sorted_output(self):
|
|
78
|
+
d = Path(tempfile.mkdtemp())
|
|
79
|
+
(d / "z.card").write_text(json.dumps({"card_id": "z"}))
|
|
80
|
+
(d / "a.card").write_text(json.dumps({"card_id": "a"}))
|
|
81
|
+
incomplete = scan_incomplete_cards(d)
|
|
82
|
+
assert [c["card_id"] for c in incomplete] == ["a", "z"]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# ── dedup_events ────────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
class TestDedupEvents:
|
|
88
|
+
def test_empty(self):
|
|
89
|
+
assert dedup_events([]) == []
|
|
90
|
+
|
|
91
|
+
def test_no_duplicates(self):
|
|
92
|
+
events = [
|
|
93
|
+
{"event_id": "ev-1", "x": 1},
|
|
94
|
+
{"event_id": "ev-2", "x": 2},
|
|
95
|
+
]
|
|
96
|
+
assert dedup_events(events) == events
|
|
97
|
+
|
|
98
|
+
def test_dedup_keeps_first(self):
|
|
99
|
+
events = [
|
|
100
|
+
{"event_id": "ev-1", "x": 1},
|
|
101
|
+
{"event_id": "ev-1", "x": 2},
|
|
102
|
+
{"event_id": "ev-2", "x": 3},
|
|
103
|
+
]
|
|
104
|
+
result = dedup_events(events)
|
|
105
|
+
assert len(result) == 2
|
|
106
|
+
assert result[0]["x"] == 1
|
|
107
|
+
|
|
108
|
+
def test_no_event_id_always_included(self):
|
|
109
|
+
events = [
|
|
110
|
+
{"x": 1},
|
|
111
|
+
{"x": 2},
|
|
112
|
+
]
|
|
113
|
+
assert dedup_events(events) == events
|
|
114
|
+
|
|
115
|
+
def test_mixed_ids_and_no_ids(self):
|
|
116
|
+
events = [
|
|
117
|
+
{"event_id": "ev-1", "x": 1},
|
|
118
|
+
{"x": 2},
|
|
119
|
+
{"event_id": "ev-1", "x": 3}, # duplicate event_id
|
|
120
|
+
]
|
|
121
|
+
result = dedup_events(events)
|
|
122
|
+
assert len(result) == 2 # ev-1 (x=1) + no-id (x=2)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
# ── find_last_checkpoint ────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
class TestFindLastCheckpoint:
|
|
128
|
+
def test_empty_log(self):
|
|
129
|
+
d = Path(tempfile.mkdtemp())
|
|
130
|
+
log = d / "empty.jsonl"
|
|
131
|
+
log.write_text("")
|
|
132
|
+
assert find_last_checkpoint(log) is None
|
|
133
|
+
|
|
134
|
+
def test_nonexistent_log(self):
|
|
135
|
+
assert find_last_checkpoint(Path("/nonexistent/log.jsonl")) is None
|
|
136
|
+
|
|
137
|
+
def test_no_gate_passed(self):
|
|
138
|
+
d = Path(tempfile.mkdtemp())
|
|
139
|
+
log = d / "log.jsonl"
|
|
140
|
+
log.write_text(json.dumps({"type": "card_started"}))
|
|
141
|
+
assert find_last_checkpoint(log) is None
|
|
142
|
+
|
|
143
|
+
def test_single_gate_passed(self):
|
|
144
|
+
d = Path(tempfile.mkdtemp())
|
|
145
|
+
log = d / "log.jsonl"
|
|
146
|
+
log.write_text(json.dumps({"type": "gate_passed", "gate": "review"}))
|
|
147
|
+
cp = find_last_checkpoint(log)
|
|
148
|
+
assert cp is not None
|
|
149
|
+
assert cp["gate"] == "review"
|
|
150
|
+
|
|
151
|
+
def test_returns_last_gate_passed(self):
|
|
152
|
+
d = Path(tempfile.mkdtemp())
|
|
153
|
+
log = d / "log.jsonl"
|
|
154
|
+
log.write_text('\n'.join([
|
|
155
|
+
json.dumps({"type": "gate_passed", "gate": "first"}),
|
|
156
|
+
json.dumps({"type": "card_started"}),
|
|
157
|
+
json.dumps({"type": "gate_passed", "gate": "last"}),
|
|
158
|
+
]))
|
|
159
|
+
cp = find_last_checkpoint(log)
|
|
160
|
+
assert cp["gate"] == "last"
|
|
161
|
+
|
|
162
|
+
def test_skips_invalid_json(self):
|
|
163
|
+
d = Path(tempfile.mkdtemp())
|
|
164
|
+
log = d / "log.jsonl"
|
|
165
|
+
log.write_text('\n'.join([
|
|
166
|
+
"not json",
|
|
167
|
+
json.dumps({"type": "gate_passed", "gate": "valid"}),
|
|
168
|
+
]))
|
|
169
|
+
cp = find_last_checkpoint(log)
|
|
170
|
+
assert cp["gate"] == "valid"
|
|
171
|
+
|
|
172
|
+
def test_skips_blank_lines(self):
|
|
173
|
+
d = Path(tempfile.mkdtemp())
|
|
174
|
+
log = d / "log.jsonl"
|
|
175
|
+
log.write_text('\n'.join([
|
|
176
|
+
"",
|
|
177
|
+
json.dumps({"type": "gate_passed", "gate": "valid"}),
|
|
178
|
+
"",
|
|
179
|
+
]))
|
|
180
|
+
cp = find_last_checkpoint(log)
|
|
181
|
+
assert cp["gate"] == "valid"
|