remit-lang 0.1.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.
- remit/__init__.py +2 -0
- remit/__main__.py +5 -0
- remit/ask.py +83 -0
- remit/ast.py +264 -0
- remit/broker.py +411 -0
- remit/checker.py +1108 -0
- remit/cli.py +531 -0
- remit/data/AI_GUIDE.md +89 -0
- remit/data/LANGUAGE_SPEC.md +271 -0
- remit/data/examples/invoices/host.rmti +24 -0
- remit/data/examples/invoices/invoices.rmt +44 -0
- remit/data/examples/invoices/policy.toml +24 -0
- remit/data/examples/research/host.rmti +21 -0
- remit/data/examples/research/policy.toml +14 -0
- remit/data/examples/research/research.rmt +42 -0
- remit/data/examples/support/host.rmti +30 -0
- remit/data/examples/support/policy.toml +21 -0
- remit/data/examples/support/support.rmt +42 -0
- remit/errors.py +65 -0
- remit/formatter.py +259 -0
- remit/interpreter.py +484 -0
- remit/ir.py +82 -0
- remit/lexer.py +179 -0
- remit/mcp_server.py +305 -0
- remit/models.py +167 -0
- remit/parser.py +803 -0
- remit/patch.py +71 -0
- remit/project.py +154 -0
- remit/rterrors.py +72 -0
- remit/stdadapters.py +94 -0
- remit/types.py +178 -0
- remit/values.py +71 -0
- remit_lang-0.1.0.dist-info/METADATA +190 -0
- remit_lang-0.1.0.dist-info/RECORD +38 -0
- remit_lang-0.1.0.dist-info/WHEEL +5 -0
- remit_lang-0.1.0.dist-info/entry_points.txt +2 -0
- remit_lang-0.1.0.dist-info/licenses/LICENSE +202 -0
- remit_lang-0.1.0.dist-info/top_level.txt +1 -0
remit/__init__.py
ADDED
remit/__main__.py
ADDED
remit/ask.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""`remit ask`: model-proposed program changes, validated before they can be applied.
|
|
2
|
+
|
|
3
|
+
The model returns a complete revised program. The proposal is parsed, type/flow/bound-checked and compared
|
|
4
|
+
with the deployment policy. Checker errors are fed back for at most two repair rounds (bounded). The CLI shows
|
|
5
|
+
a unified diff and an authority diff (new resources, higher worst-case counts or cost, new tagged flows, new
|
|
6
|
+
approval sites). Nothing is written without --apply, and authority widening additionally needs
|
|
7
|
+
--allow-widening. Host interface and policy are never edited by this command.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import difflib
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import subprocess
|
|
15
|
+
|
|
16
|
+
from .broker import Policy
|
|
17
|
+
from .checker import check
|
|
18
|
+
from .errors import RemitError
|
|
19
|
+
from .parser import parse_program
|
|
20
|
+
from .project import policy_diagnostics
|
|
21
|
+
|
|
22
|
+
SCHEMA = {"type": "object", "properties": {"program": {"type": "string"}, "explanation": {"type": "string"},
|
|
23
|
+
"refused": {"type": "boolean"}},
|
|
24
|
+
"required": ["program", "explanation", "refused"], "additionalProperties": False}
|
|
25
|
+
|
|
26
|
+
SYSTEM = ("You modify programs written in Remit, a small checked language. Return the COMPLETE revised program. "
|
|
27
|
+
"You cannot change the host interface or the policy. If the request needs authority the host does not "
|
|
28
|
+
"grant, or would weaken a safety property, set refused=true, return the original program unchanged and "
|
|
29
|
+
"explain why.")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _model(prompt: str, model: str, timeout: float = 300) -> dict:
|
|
33
|
+
cmd = ["claude", "-p", prompt, "--model", model, "--tools", "", "--system-prompt", SYSTEM,
|
|
34
|
+
"--no-session-persistence", "--json-schema", json.dumps(SCHEMA), "--output-format", "json"]
|
|
35
|
+
env = dict(os.environ)
|
|
36
|
+
env.pop("CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD", None)
|
|
37
|
+
p = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, cwd="/tmp", env=env)
|
|
38
|
+
d = json.loads(p.stdout)
|
|
39
|
+
if d.get("is_error"):
|
|
40
|
+
raise RuntimeError(str(d.get("result"))[:300])
|
|
41
|
+
out = d.get("structured_output") or json.loads(d["result"])
|
|
42
|
+
out["_cost"] = d.get("total_cost_usd")
|
|
43
|
+
return out
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def validate(src: str, path: str, host, policy: Policy | None):
|
|
47
|
+
try:
|
|
48
|
+
prog = parse_program(src, os.path.basename(path))
|
|
49
|
+
except RemitError as e:
|
|
50
|
+
return None, [e.diag]
|
|
51
|
+
c = check(prog, host)
|
|
52
|
+
diags = [d for d in c.diagnostics if d.severity == "error"]
|
|
53
|
+
if not diags and policy is not None:
|
|
54
|
+
diags = policy_diagnostics(c, policy)
|
|
55
|
+
return c, diags
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def ask(request: str, path: str, host, host_src: str, policy: Policy | None, spec: str, model: str,
|
|
59
|
+
max_repairs: int = 2) -> dict:
|
|
60
|
+
with open(path, encoding="utf-8") as f:
|
|
61
|
+
original = f.read()
|
|
62
|
+
prompt = (f"Language reference:\n<spec>\n{spec}\n</spec>\n\nHost interface (read-only):\n<host>\n{host_src}\n"
|
|
63
|
+
f"</host>\n\nCurrent program ({os.path.basename(path)}):\n<program>\n{original}\n</program>\n\n"
|
|
64
|
+
f"Change request: {request}")
|
|
65
|
+
rounds, cost = [], 0.0
|
|
66
|
+
src = original
|
|
67
|
+
for i in range(max_repairs + 1):
|
|
68
|
+
out = _model(prompt, model)
|
|
69
|
+
cost += out.get("_cost") or 0
|
|
70
|
+
src = out["program"]
|
|
71
|
+
c, diags = validate(src, path, host, policy)
|
|
72
|
+
rounds.append({"round": i, "errors": [d.render({os.path.basename(path): src}) for d in diags],
|
|
73
|
+
"explanation": out["explanation"], "refused": out["refused"]})
|
|
74
|
+
if out["refused"] or not diags:
|
|
75
|
+
break
|
|
76
|
+
prompt += (f"\n\nYour previous proposal:\n<program>\n{src}\n</program>\nThe checker rejected it:\n"
|
|
77
|
+
+ "\n".join(rounds[-1]["errors"]) + "\nFix the errors and return the complete program again.")
|
|
78
|
+
return {"original": original, "proposed": src, "checked": c, "errors": diags, "rounds": rounds,
|
|
79
|
+
"refused": rounds[-1]["refused"], "cost_usd": round(cost, 5)}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def unified(a: str, b: str, path: str) -> str:
|
|
83
|
+
return "".join(difflib.unified_diff(a.splitlines(True), b.splitlines(True), f"a/{path}", f"b/{path}"))
|
remit/ast.py
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""AST node definitions. Every node carries a Span and a stable id (assigned by the parser)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Optional, Union
|
|
6
|
+
|
|
7
|
+
from .errors import Span
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class Node:
|
|
12
|
+
span: Span = field(repr=False, compare=False, kw_only=True)
|
|
13
|
+
nid: int = field(default=0, repr=False, compare=False, kw_only=True)
|
|
14
|
+
comments: list[str] = field(default_factory=list, repr=False, compare=False, kw_only=True)
|
|
15
|
+
trailing: Optional[str] = field(default=None, repr=False, compare=False, kw_only=True)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# ---------- type expressions ----------
|
|
19
|
+
@dataclass
|
|
20
|
+
class TName(Node):
|
|
21
|
+
name: str
|
|
22
|
+
args: list["TypeExpr"] = field(default_factory=list)
|
|
23
|
+
max: Optional[int] = None # only for List
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class TRecord(Node):
|
|
28
|
+
fields: list[tuple[str, "TypeExpr"]]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class TUnion(Node): # union of string literals: "a" | "b"
|
|
33
|
+
options: list[str]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
TypeExpr = Union[TName, TRecord, TUnion]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# ---------- expressions ----------
|
|
40
|
+
@dataclass
|
|
41
|
+
class Lit(Node):
|
|
42
|
+
value: object # int | float | bool | str
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class Interp(Node): # interpolated string
|
|
47
|
+
parts: list[Union[str, "Expr"]]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class Name(Node):
|
|
52
|
+
name: str
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class Field(Node):
|
|
57
|
+
obj: "Expr"
|
|
58
|
+
name: str
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass
|
|
62
|
+
class Index(Node):
|
|
63
|
+
obj: "Expr"
|
|
64
|
+
index: "Expr"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass
|
|
68
|
+
class ListLit(Node):
|
|
69
|
+
items: list["Expr"]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class RecordLit(Node):
|
|
74
|
+
fields: list[tuple[str, "Expr"]]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class Unary(Node):
|
|
79
|
+
op: str
|
|
80
|
+
operand: "Expr"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass
|
|
84
|
+
class Binary(Node):
|
|
85
|
+
op: str
|
|
86
|
+
left: "Expr"
|
|
87
|
+
right: "Expr"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass
|
|
91
|
+
class Arg(Node):
|
|
92
|
+
name: Optional[str]
|
|
93
|
+
value: "Expr"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@dataclass
|
|
97
|
+
class Call(Node):
|
|
98
|
+
"""f(args) or ns.cap(args) or Agent.ask(T, ...). `callee` is a dotted path."""
|
|
99
|
+
callee: list[str]
|
|
100
|
+
args: list[Arg]
|
|
101
|
+
retry: Optional[int] = None
|
|
102
|
+
timeout: Optional[float] = None
|
|
103
|
+
fallback: Optional["Expr"] = None
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class IfExpr(Node):
|
|
108
|
+
cond: "Expr"
|
|
109
|
+
then: "Block"
|
|
110
|
+
els: Optional[Union["Block", "IfExpr"]] = None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@dataclass
|
|
114
|
+
class ForExpr(Node):
|
|
115
|
+
var: str
|
|
116
|
+
iter: "Expr"
|
|
117
|
+
body: "Block"
|
|
118
|
+
limit: Optional[int] = None
|
|
119
|
+
where: Optional["Expr"] = None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@dataclass
|
|
123
|
+
class MatchArm(Node):
|
|
124
|
+
pattern: Union[str, None] # string literal or None for `_`
|
|
125
|
+
body: "Block"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@dataclass
|
|
129
|
+
class MatchExpr(Node):
|
|
130
|
+
subject: "Expr"
|
|
131
|
+
arms: list[MatchArm]
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
Expr = Union[Lit, Interp, Name, Field, Index, ListLit, RecordLit, Unary, Binary, Call, IfExpr, ForExpr, MatchExpr]
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# ---------- statements ----------
|
|
138
|
+
@dataclass
|
|
139
|
+
class Let(Node):
|
|
140
|
+
name: str
|
|
141
|
+
mutable: bool
|
|
142
|
+
type: Optional[TypeExpr]
|
|
143
|
+
value: Expr
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass
|
|
147
|
+
class Assign(Node):
|
|
148
|
+
name: str
|
|
149
|
+
value: Expr
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@dataclass
|
|
153
|
+
class Return(Node):
|
|
154
|
+
value: Optional[Expr]
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
@dataclass
|
|
158
|
+
class ExprStmt(Node):
|
|
159
|
+
expr: Expr
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@dataclass
|
|
163
|
+
class Expect(Node):
|
|
164
|
+
cond: Expr
|
|
165
|
+
message: Optional[str] = None
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@dataclass
|
|
169
|
+
class Break(Node):
|
|
170
|
+
pass
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
Stmt = Union[Let, Assign, Return, ExprStmt, Expect, Break]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@dataclass
|
|
177
|
+
class Block(Node):
|
|
178
|
+
stmts: list[Stmt]
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
# ---------- declarations ----------
|
|
182
|
+
@dataclass
|
|
183
|
+
class Param(Node):
|
|
184
|
+
name: str
|
|
185
|
+
type: TypeExpr
|
|
186
|
+
deny: list[str] = field(default_factory=list) # capability params only
|
|
187
|
+
approve: list[str] = field(default_factory=list) # capability params only
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@dataclass
|
|
191
|
+
class TypeDecl(Node):
|
|
192
|
+
name: str
|
|
193
|
+
type: TypeExpr
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@dataclass
|
|
197
|
+
class FnDecl(Node):
|
|
198
|
+
name: str
|
|
199
|
+
params: list[Param]
|
|
200
|
+
ret: Optional[TypeExpr]
|
|
201
|
+
body: Block
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@dataclass
|
|
205
|
+
class AgentDecl(Node):
|
|
206
|
+
name: str
|
|
207
|
+
model: str
|
|
208
|
+
system: str
|
|
209
|
+
max_calls: Optional[int]
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
@dataclass
|
|
213
|
+
class TestDecl(Node):
|
|
214
|
+
name: str
|
|
215
|
+
fixtures: Optional[str]
|
|
216
|
+
body: Block
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@dataclass
|
|
220
|
+
class Program(Node):
|
|
221
|
+
name: str
|
|
222
|
+
uses: list[str]
|
|
223
|
+
budget: Optional[float]
|
|
224
|
+
types: list[TypeDecl]
|
|
225
|
+
fns: list[FnDecl]
|
|
226
|
+
agents: list[AgentDecl]
|
|
227
|
+
tests: list[TestDecl]
|
|
228
|
+
file: str = ""
|
|
229
|
+
header_comments: list[str] = field(default_factory=list)
|
|
230
|
+
order: list[Node] = field(default_factory=list) # declaration order for the formatter
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
# ---------- host interface (.rmti) ----------
|
|
234
|
+
@dataclass
|
|
235
|
+
class CapabilityDecl(Node):
|
|
236
|
+
name: str # dotted, e.g. "email.send"
|
|
237
|
+
params: list[Param]
|
|
238
|
+
ret: Optional[TypeExpr]
|
|
239
|
+
tags: list[str] = field(default_factory=list)
|
|
240
|
+
clears: list[str] = field(default_factory=list)
|
|
241
|
+
cost: float = 0.0
|
|
242
|
+
idempotent: bool = False
|
|
243
|
+
approval_always: bool = False
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@dataclass
|
|
247
|
+
class ModelDecl(Node):
|
|
248
|
+
name: str
|
|
249
|
+
cost: float = 0.0
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
@dataclass
|
|
253
|
+
class InputDecl(Node):
|
|
254
|
+
name: str # parameter name of main
|
|
255
|
+
tags: list[str]
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@dataclass
|
|
259
|
+
class HostInterface(Node):
|
|
260
|
+
types: list[TypeDecl]
|
|
261
|
+
capabilities: list[CapabilityDecl]
|
|
262
|
+
models: list[ModelDecl]
|
|
263
|
+
inputs: list[InputDecl]
|
|
264
|
+
file: str = ""
|