File size: 5,408 Bytes
7460c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Small deterministic package used for smoke tests and first-run exploration."""

from __future__ import annotations

from pathlib import Path

from trace2env.compiler import EnvironmentCompiler
from trace2env.models import (
    ActionSchema,
    ActionSpec,
    ArgumentSpec,
    Condition,
    EnvironmentNote,
    Invariant,
    Operand,
    Outcome,
    ReconstructionArtifacts,
    ReconstructionConfig,
    RenderContract,
    StateField,
    StateMutation,
    StateSchema,
    TransitionRule,
)


def build_ledger_demo(output_dir: str | Path) -> Path:
    config = ReconstructionConfig(
        environment_id="demo.ledger",
        name="Trace2Env Ledger Demo",
        description="A tiny balance environment demonstrating contrastive success/failure rules.",
        domains=["tool", "transaction"],
        construction_kind="authored",
    )
    actions = ActionSchema(
        actions=[
            ActionSpec(
                name="deposit",
                description="Add funds to the current balance.",
                arguments={"amount": ArgumentSpec(type="number", required=True)},
            ),
            ActionSpec(
                name="withdraw",
                description="Remove funds when the balance is sufficient.",
                arguments={"amount": ArgumentSpec(type="number", required=True)},
            ),
            ActionSpec(name="balance", description="Read the current balance."),
        ]
    )
    state = StateSchema(
        fields=[
            StateField(
                path="world.balance",
                type="number",
                description="Available account balance.",
                default=100.0,
            )
        ]
    )
    rules = [
        TransitionRule(
            id="deposit_success",
            action_type="deposit",
            description="Deposits increase the available balance.",
            priority=10,
            effects=[
                StateMutation(op="increment", path="world.balance", value={"$action_arg": "amount"})
            ],
            outcome=Outcome.SUCCESS,
            renderer="ledger",
            observation_template="Deposited {action.arguments.amount}. Balance: {state_after.world.balance}",
        ),
        TransitionRule(
            id="withdraw_success",
            action_type="withdraw",
            description="A covered withdrawal decreases the balance.",
            priority=10,
            conditions=[
                Condition(
                    left=Operand(path="world.balance"),
                    op="gte",
                    right=Operand(action_arg="amount"),
                )
            ],
            effects=[
                StateMutation(op="decrement", path="world.balance", value={"$action_arg": "amount"})
            ],
            outcome=Outcome.SUCCESS,
            renderer="ledger",
            observation_template="Withdrew {action.arguments.amount}. Balance: {state_after.world.balance}",
        ),
        TransitionRule(
            id="withdraw_insufficient",
            action_type="withdraw",
            description="An uncovered withdrawal fails without changing state.",
            priority=10,
            conditions=[
                Condition(
                    left=Operand(path="world.balance"),
                    op="lt",
                    right=Operand(action_arg="amount"),
                )
            ],
            outcome=Outcome.FAILURE,
            renderer="ledger",
            observation_template="Insufficient funds. Balance: {state_after.world.balance}",
        ),
        TransitionRule(
            id="balance_read",
            action_type="balance",
            description="Reading the balance has no state effect.",
            priority=10,
            outcome=Outcome.SUCCESS,
            renderer="ledger",
            observation_template="Balance: {state_after.world.balance}",
        ),
    ]
    invariant = Invariant(
        id="nonnegative_balance",
        description="Available balance must never be negative.",
        condition=Condition(
            left=Operand(path="world.balance"), op="gte", right=Operand(literal=0)
        ),
    )
    renderers = [
        RenderContract(
            id="ledger",
            action_types=["deposit", "withdraw", "balance"],
            content_type="text/plain",
            required_fields=["state_after.world.balance"],
            instructions="Return one concise status line and preserve numeric values.",
            examples=["Balance: 100.0", "Insufficient funds. Balance: 20.0"],
        )
    ]
    notes = [
        EnvironmentNote(
            id="balance_format",
            kind="format",
            statement="Balances are rendered as decimal numbers with one fractional digit, e.g. 75.0.",
            action_types=["deposit", "withdraw", "balance"],
        ),
        EnvironmentNote(
            id="single_account",
            kind="concept",
            statement="The environment holds exactly one account; every action refers to its balance.",
        ),
    ]
    artifacts = ReconstructionArtifacts(
        episodes=[],
        transitions=[],
        evidence=[],
        action_schema=actions,
        state_schema=state,
        rules=rules,
        invariants=[invariant],
        renderers=renderers,
        notes=notes,
    )
    return EnvironmentCompiler(config).compile(artifacts, output_dir)