File size: 7,202 Bytes
5e5d6c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import copy
import hashlib
import pickle
import sqlite3
from concurrent.futures import ThreadPoolExecutor
from dataclasses import replace
from pathlib import Path

import pytest

from ctx.engine.content import PreparedCapabilityContent
from ctx.engine.engine import _PromptContextReceiptPermit
from ctx.engine.protocol import Transition
from ctx.engine.replay import ReplayInput
from ctx.runtime.prepared_query_delivery import (
    PreparedQueryDelivery,
    PreparedQueryDeliveryError,
    _create_prepared_query_delivery,
    accept_prepared_query_delivery,
)
from ctx.runtime.query_decision import QueryHostDescriptor
from ctx.runtime.query_session import prepare_query_delivery


def _delivery(tmp_path: Path) -> PreparedQueryDelivery:
    result = prepare_query_delivery(
        host=QueryHostDescriptor.codex("activate"),
        task="Fix the Python tests",
        language="python",
        session_id="prepared-delivery-test",
        workspace=tmp_path,
        journal_path=tmp_path / "engine.sqlite3",
        benefit_audit_path=tmp_path / "benefit.sqlite3",
        host_invocation_digest=hashlib.sha256(b"host-invocation").hexdigest(),
    )
    assert isinstance(result, PreparedQueryDelivery)
    return result


def test_prepared_query_delivery_accepts_a_defensive_exact_copy(tmp_path: Path) -> None:
    original = _delivery(tmp_path)

    accepted = accept_prepared_query_delivery(
        original,
        host=QueryHostDescriptor.codex("activate"),
    )

    assert accepted is not original
    assert accepted.delivery_digest == original.delivery_digest
    assert accepted.context == original.context
    assert repr(accepted) == (
        f"PreparedQueryDelivery(delivery_digest={accepted.delivery_digest!r})"
    )
    assert "# ctx Python Testing" not in repr(accepted)
    with pytest.raises(PreparedQueryDeliveryError, match="already accepted"):
        accept_prepared_query_delivery(
            original,
            host=QueryHostDescriptor.codex("activate"),
        )
    with pytest.raises(PreparedQueryDeliveryError, match="already accepted"):
        accept_prepared_query_delivery(
            accepted,
            host=QueryHostDescriptor.codex("activate"),
        )


@pytest.mark.parametrize(
    "host",
    [
        QueryHostDescriptor.claude_code("activate"),
        QueryHostDescriptor.codex("experiment"),
        QueryHostDescriptor.codex(),
    ],
)
def test_prepared_query_delivery_rejects_wrong_host_or_intent(
    tmp_path: Path,
    host: QueryHostDescriptor,
) -> None:
    delivery = _delivery(tmp_path)

    with pytest.raises(PreparedQueryDeliveryError):
        accept_prepared_query_delivery(delivery, host=host)


@pytest.mark.parametrize(
    ("field_name", "replacement"),
    [
        ("context", "substituted material"),
        ("context_sha256", hashlib.sha256(b"substituted").hexdigest()),
        ("final_journal_record_digest", hashlib.sha256(b"other journal").hexdigest()),
        ("action_content_digest", hashlib.sha256(b"other action").hexdigest()),
    ],
)
def test_prepared_query_delivery_rejects_substitution(
    tmp_path: Path,
    field_name: str,
    replacement: str,
) -> None:
    original = _delivery(tmp_path)

    with pytest.raises(PreparedQueryDeliveryError):
        replace(original, **{field_name: replacement})  # type: ignore[arg-type]


def test_prepared_query_delivery_rejects_copy_and_serialization(tmp_path: Path) -> None:
    delivery = _delivery(tmp_path)

    with pytest.raises(TypeError):
        copy.copy(delivery)
    with pytest.raises(TypeError):
        copy.deepcopy(delivery)
    with pytest.raises(TypeError):
        pickle.dumps(delivery)


def test_prepared_query_delivery_rejects_a_forged_engine_receipt_permit(
    tmp_path: Path,
) -> None:
    delivery = _delivery(tmp_path)
    with sqlite3.connect(tmp_path / "engine.sqlite3") as connection:
        rev2 = Transition.from_json(
            connection.execute(
                "SELECT transition_json FROM engine_journal WHERE revision = 2"
            ).fetchone()[0]
        )
        replay3 = ReplayInput.from_json(
            connection.execute(
                "SELECT replay_json FROM engine_journal WHERE revision = 3"
            ).fetchone()[0]
        )
    action = next(item for item in rev2.actions if item.kind == "PreparePromptContext")
    metadata = delivery.capabilities[0]
    raw_content = delivery.context.split(">\n", 1)[1].rsplit(
        "\n</ctx-capability>",
        1,
    )[0]
    content = PreparedCapabilityContent(
        capability_id=metadata.capability_id,
        source_digest=metadata.source_digest,
        catalog_snapshot_digest=action.catalog_snapshot_id or "",
        action_id=action.action_id,
        lease_id=action.lease_id or "",
        content=raw_content,
        content_sha256=metadata.content_sha256,
        content_bytes=metadata.content_bytes,
        estimated_tokens=metadata.estimated_tokens,
    )
    forged_permit = object.__new__(_PromptContextReceiptPermit)

    with pytest.raises(
        PreparedQueryDeliveryError,
        match="authoritative revision-three receipt",
    ):
        _create_prepared_query_delivery(
            decision=delivery.decision,
            execution_intent="activate",
            action=action,
            receipt_event=replay3.reducer_event,
            contents=(content,),
            receipt_authority=forged_permit,
        )

    malicious_body = "arbitrary caller-supplied prompt instructions"
    malicious = replace(
        content,
        content=malicious_body,
        content_sha256=hashlib.sha256(malicious_body.encode()).hexdigest(),
        content_bytes=len(malicious_body.encode()),
    )
    with pytest.raises(PreparedQueryDeliveryError, match="changed authorized material"):
        _create_prepared_query_delivery(
            decision=delivery.decision,
            execution_intent="activate",
            action=action,
            receipt_event=replay3.reducer_event,
            contents=(malicious,),
            receipt_authority=forged_permit,
        )

    with pytest.raises(TypeError, match="final_journal_record_digest"):
        _create_prepared_query_delivery(
            decision=delivery.decision,
            execution_intent="activate",
            action=action,
            receipt_event=replay3.reducer_event,
            contents=(content,),
            receipt_authority=forged_permit,
            final_journal_record_digest=hashlib.sha256(b"fake final record").hexdigest(),  # type: ignore[call-arg]
        )


def test_prepared_query_delivery_acceptance_is_concurrently_one_shot(
    tmp_path: Path,
) -> None:
    delivery = _delivery(tmp_path)
    host = QueryHostDescriptor.codex("activate")

    def accept_once() -> str:
        try:
            accept_prepared_query_delivery(delivery, host=host)
        except PreparedQueryDeliveryError:
            return "rejected"
        return "accepted"

    with ThreadPoolExecutor(max_workers=16) as executor:
        outcomes = tuple(executor.map(lambda _index: accept_once(), range(64)))

    assert outcomes.count("accepted") == 1
    assert outcomes.count("rejected") == 63