File size: 9,479 Bytes
f726fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The released API recovery schedule, identities, and public interface."""

from __future__ import annotations

from copy import deepcopy
import hashlib
import json
from pathlib import Path
from typing import Any

from .recovery_policy import RECOVERY_PROFILES, RECOVERY_RULE_CELLS


CASE_VERSION = "api-recovery-lifecycle-evolution-v1"
# Schema and identity namespaces are stable so saved results remain regradable.
SNAPSHOT_SCHEMA = "rescopebench-api-reliability-stream-snapshot/v10"
BUSINESS_BUDGET = 57
INITIAL_BUDGET = 56
TASK_IDS = tuple(f"T{position:02d}" for position in range(1, 17))
DRIFT_EVENT_ID = "evt-recovery-profile-registry-2026-09-v2"
EXACT_IDENTITY_READ_COST = 3
RATE_READ_COST = 3
EXACT_RULE_READ_COST = 17
RETRY_COST = 10
DATA = Path(__file__).resolve().parent / "data"

PROMPT = {
    "system_prompt": "You are operating in an unfamiliar persistent environment. Use only the available tools to complete the task within its stated limits. Do not claim that an action succeeded unless the tool confirms it.\n\nUse retained evidence to avoid repeating discovery of stable facts and rules. Distinguish reusable bindings from changing task data, availability, and other live state. Recheck when the operation requires fresh evidence, the relevant state may have changed, or current observations contradict what you retained. Current tool results take precedence. Estimate remaining work from the operations still needed, taking already known information into account.\n\nRecords from previous tasks may be available through the history tools. Consult them only when they help resolve a concrete uncertainty. The history tools share a limit of 30 calls per episode, so use them selectively. Previous records may be incomplete or outdated; verify relevant information against the current environment when necessary.\n\nCompletion evidence: a successful synchronous tool receipt is authoritative for the committed facts it explicitly reports. No additional readback is required merely to reconfirm those facts. A successful substep does not prove the whole artifact is complete. Pending, rejected, or failed operations are not completed operations. Perform any substantive validation, authorization, release, or other business step required by the task. If an operation requires a separate confirmation, use its declared status interface. Only explicitly designated narrow confirmation of your own current-task operations and quota readings are free; discovery and substantive business work retain their declared costs. Follow the task's completion and partial-delivery semantics and report only the outcomes established by current evidence.",
    "task_prefix": "Below is the task for this episode:\n\n",
    "after_task_prompt": "After completing or stopping this task, consider what the observed evidence could help resolve in future tasks in this environment.\n\nUse memory for concrete environment facts and executable bindings: retain the observed names or conditions, the exact public identifiers needed to act, their relationships, and relevant evidence or scope. Use skills for conditional rules and procedures: retain the observed conditions, valid or invalid actions and parameters, expected outcomes, and relevant costs. A generic instruction to look everything up again is not a substitute for the specific evidence learned.\n\nKeep notes compact and evidence-based. Preserve uncertainty when a relation has not been verified. Distinguish a cost that recurs for every new request from a discovery cost that retained knowledge can avoid. Update or remove contradicted entries rather than keeping incompatible claims. You decide which observations are worth retaining; if nothing useful was learned, do not update either store.",
    "carrier_contract": "Use memory for concrete environment facts and executable bindings: retain the observed names or conditions, the exact public identifiers needed to act, their relationships, and relevant evidence or scope. Use skills for conditional rules and procedures: retain the observed conditions, valid or invalid actions and parameters, expected outcomes, and relevant costs. A generic instruction to look everything up again is not a substitute for the specific evidence learned.",
    "completion_evidence_profile": "authoritative-receipt-v1"
}

OPERATION_BY_CLASS = {
    "observation": "track_parcel",
    "decision": "review_case",
    "mutation": "reserve_stock",
    "pricing": "quote_shipment",
}

RULE_SCHEDULE = {
    "T01": ("R01", "R05", "R07", "R08"),
    "T02": ("R01", "R03", "R09", "R10"),
    "T03": ("R01", "R02", "R12", "R13"),
    "T04": ("R01", "R03", "R06", "R15"),
    "T05": ("R01", "R02", "R11", "R16"),
    "T06": ("R03", "R06", "R14", "R04"),
    "T07": ("R01", "R02", "R11", "R03"),
    "T08": ("R01", "R02", "R06", "R11"),
    "T09": ("R03", "R06", "R11", "R14"),
    "T10": ("R03", "R06", "R11", "R14"),
    "T11": ("R03", "R06", "R11", "R14"),
    "T12": ("R14", "R03", "R06", "R11"),
    "T13": ("R03", "R06", "R11", "R14"),
    "T14": ("R01", "R02", "R06", "R11"),
    "T15": ("R01", "R03", "R11", "R14"),
    "T16": ("R02", "R03", "R06", "R14"),
}

STRUCTURE_SCHEDULE = {
    "T01": ("S01", "S05", "S06", "S08"),
    "T02": ("S01", "S02", "S05", "S08"),
    "T03": ("S01", "S03", "S06", "S08"),
    "T04": ("S01", "S02", "S04", "S08"),
    "T05": ("S01", "S03", "S05", "S08"),
    "T06": ("S02", "S04", "S06", "S08"),
    "T07": ("S01", "S03", "S05", "S07"),
    "T08": ("S01", "S03", "S04", "S05"),
    "T09": ("S02", "S04", "S05", "S06"),
    "T10": ("S02", "S04", "S05", "S06"),
    "T11": ("S02", "S04", "S05", "S06"),
    "T12": ("S06", "S02", "S04", "S05"),
    "T13": ("S02", "S04", "S05", "S06"),
    "T14": ("S01", "S03", "S04", "S05"),
    "T15": ("S01", "S02", "S05", "S06"),
    "T16": ("S03", "S02", "S04", "S06"),
}

PRE_RULES = {row["rule_atom"]: deepcopy(row) for row in RECOVERY_RULE_CELLS}
CHANGED_RULE_IDS = frozenset({"R03", "R06", "R11", "R14"})
PRESERVED_RULE_IDS = frozenset(PRE_RULES) - CHANGED_RULE_IDS
DRIFTED_RULES = deepcopy(PRE_RULES)
for _rule_id in CHANGED_RULE_IDS:
    _row = DRIFTED_RULES[_rule_id]
    _index = RECOVERY_PROFILES.index(_row["recovery_profile_id"])
    _row["recovery_profile_id"] = RECOVERY_PROFILES[(_index + 1) % len(RECOVERY_PROFILES)]

def _private_handle(category: str, *parts: Any) -> str:
    payload = "|".join(
        ["api-reliability-v5", "private-object", category]
        + [str(part) for part in parts]
    )
    suffix = hashlib.sha256(payload.encode("utf-8")).hexdigest()[:12]
    return f"{category}-{suffix}"

STRUCTURE_CLUSTERS = tuple(
    {
        "atom_id": f"S{index:02d}",
        "integration_id": _private_handle("integration", index),
        "integration_name": f"Provider Connection {chr(64 + index)}",
        "credential_id": _private_handle("credential", index),
        "retry_budget_id": _private_handle("retry-budget", index),
    }
    for index in range(1, 9)
)

STRUCTURE_BY_ID = {row["atom_id"]: row for row in STRUCTURE_CLUSTERS}

def _bucket_id(structure_id: str, operation: str) -> str:
    return _private_handle("rate-bucket", structure_id, operation)

def stream_targets(task_id: str) -> tuple[dict[str, Any], ...]:
    if task_id not in TASK_IDS:
        raise ValueError(f"unknown stream task: {task_id}")
    rows = []
    for slot, (structure_id, rule_id) in enumerate(
        zip(STRUCTURE_SCHEDULE[task_id], RULE_SCHEDULE[task_id]), 1
    ):
        structure = STRUCTURE_BY_ID[structure_id]
        rule = PRE_RULES[rule_id]
        operation = OPERATION_BY_CLASS[rule["operation_class"]]
        rows.append(
            {
                "target_id": _private_handle("recovery", task_id, slot),
                "task_id": task_id,
                "slot": slot,
                "structure_atom": structure_id,
                "rule_atom": rule_id,
                "integration_id": structure["integration_id"],
                "integration_name": structure["integration_name"],
                "operation": operation,
                "failure_signal": rule["failure_signal"],
                "operation_class": rule["operation_class"],
                "call_id": _private_handle("call", task_id, slot),
                "credential_id": structure["credential_id"],
                "retry_budget_id": structure["retry_budget_id"],
                "rate_limit_bucket_id": _bucket_id(structure_id, operation),
                "fault_policy_id": _private_handle("fault-policy", task_id, slot),
            }
        )
    return tuple(rows)

TARGETS = {task_id: stream_targets(task_id) for task_id in TASK_IDS}

def event_id(task_id: str) -> str:
    return _private_handle(
        "event", task_id, [target["call_id"] for target in TARGETS[task_id]]
    ).upper()

def public_task(task_id: str, data: Path = DATA) -> dict[str, Any]:
    if task_id not in TASK_IDS:
        raise ValueError(f"unknown stream task: {task_id}")
    return json.loads((data / "public/tasks" / f"{task_id}.json").read_text(encoding="utf-8"))


def provider_tools() -> list[dict[str, Any]]:
    return json.loads((DATA / "public/tools.json").read_text(encoding="utf-8"))


PUBLIC_TOOLS = [
    {"name": row["function"]["name"], "description": row["function"]["description"],
     "input_schema": deepcopy(row["function"]["parameters"])}
    for row in provider_tools()
]
PUBLIC_TOOL_NAMES = tuple(row["name"] for row in PUBLIC_TOOLS)
TOOL_BY_NAME = {row["name"]: row for row in PUBLIC_TOOLS}