Repository navigation
Expand file tree
/
Copy pathmodel_plan_escape.py
More file actions
223 lines (199 loc) · 7.27 KB
/
Copy pathmodel_plan_escape.py
File metadata and controls
223 lines (199 loc) · 7.27 KB
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
213
214
215
216
217
218
219
220
221
222
223
"""A model's implementation plan is revocable; the caller contract is not.
One transition per caller version, after a real ordinary action has settled.
No rejected edit, model request, check, budget or permission is replayed here.
"""
from dataclasses import asdict
import hashlib
import json
from pathlib import Path
from infinicoder.analysis.task_compiler import compile_task
from infinicoder.planning.task_contract import TaskSpec
from infinicoder.execution.workspace_tools import Workspace
POLICY = "model_plan_escape_v1"
def digest(value):
return hashlib.sha256(json.dumps(value, sort_keys=True).encode()).hexdigest()
def identity():
return dict(policy=POLICY, sha256=hashlib.sha256(Path(__file__).read_bytes()).hexdigest())
def bind(runtime, state, caller):
value = asdict(caller)
contract = digest(value)
old = state.get("model_plan_escape")
if old and old["contract_sha256"] == contract:
return
if old:
state.setdefault("model_plan_escape_history", []).append(old)
state["model_plan_escape"] = dict(
contract_sha256=contract,
caller=value,
applicable=not caller.milestones
and not runtime._explicit_worker_path
and not runtime._configured_worker_symbol,
consumed=False,
origin=None,
)
def active(state):
return bool(state.get("model_plan_escape", {}).get("consumed"))
def eligible(state):
lease = state.get("model_plan_escape", {})
return (
lease.get("applicable")
and not lease.get("consumed")
and lease.get("origin") == "model"
and state.get("plan_milestones")
and state.get("status") == "running"
)
def scope_refusal(runtime, state, workspace, unit, args, protected):
"""Called only inside the real phase Workspace.path rejection handler."""
if not eligible(state):
return
try:
caller = TaskSpec.from_dict(state["model_plan_escape"]["caller"])
path = Workspace(workspace.root, caller.allowed_paths).path(args["path"], write=True)
if str(path.resolve()) in protected or not path.is_file():
return
current = hashlib.sha256(path.read_bytes()).hexdigest()
if args.get("expected_hash") != current:
return
# Recheck the specific phase restriction; other path errors do not qualify.
if not unit.write_set:
return
try:
Workspace(workspace.root, unit.write_set).path(args["path"], write=True)
except ValueError:
pass
else:
return
state["model_plan_escape_pending"] = dict(
kind="phase_scope_refusal",
path=args["path"],
source_sha256=current,
revision=workspace.revision(),
phase=state["milestone"],
edit_applied=False,
)
except (ValueError, OSError, KeyError, TypeError):
return
def public_rejection(
runtime, state, workspace, check_ids, require_artifacts, evidence, live_before
):
if not eligible(state) or not require_artifacts:
return
if state["milestone"] != len(runtime._graph.units) - 1:
return
caller = TaskSpec.from_dict(state["model_plan_escape"]["caller"])
ids = {c.id for c in caller.checks}
if (
not ids
or set(check_ids) != ids
or live_before is None
or observed_revision(workspace) != live_before
):
return
executed = [e for e in evidence if e.get("executed_argv") and type(e.get("exit_code")) is int]
if {e.get("check_id") for e in executed} != ids:
return
outcomes = getattr(runtime._acceptance, "last_process_outcomes", {})
if set(outcomes) != ids or not all(
o.get("completed_normally") is True for o in outcomes.values()
):
return
if any(
e.get("error") or e.get("failure_origin") or e.get("check_id") not in ids for e in evidence
):
return
if not all(
e.get("revision")
and e["revision"] == e.get("snapshot_after")
and e.get("checker_files") == state.get("checker_files")
for e in executed
):
return
state["model_plan_escape_pending"] = dict(
kind="last_stage_public_rejection",
revision=live_before,
phase=state["milestone"],
check_ids=sorted(ids),
evidence_sha256=digest(evidence),
)
def pending(state):
return eligible(state) and bool(state.get("model_plan_escape_pending"))
def observed_revision(workspace):
try:
return workspace.revision()
except (OSError, ValueError):
return None
def settle(runtime, state, workspace, remaining, reserve):
proof = state.pop("model_plan_escape_pending", None)
if not proof or not eligible(state):
return None
lease = state["model_plan_escape"]
try:
caller = TaskSpec.from_dict(lease["caller"])
original = compile_task(caller)
historical_graph = asdict(runtime._graph)
historical_hash = digest(historical_graph)
original_hash = digest(asdict(original))
current = observed_revision(workspace)
controlled = (runtime.store.path / "control.json").exists()
except (OSError, ValueError):
return None
# Observations and graph construction can be expensive. Recheck control and
# budget after them, immediately before retiring the current implementation.
if (
not eligible(state)
or controlled
or current is None
or current != proof["revision"]
or remaining() <= reserve + 1
or state["model_calls"] >= runtime.max_model_calls
or state["steps"] >= runtime.max_steps
):
return None
lease.update(
consumed=True,
trigger=proof,
call=state["model_calls"],
step=state["steps"],
prior_graph=historical_graph,
prior_plan=state["plan_milestones"],
prior_phase=state["milestone"],
)
for key in (
"plan_milestones",
"source_plan_certificate",
"phase_execution",
"pending_replan",
"phase_context",
"edit_focus",
"fallback_path",
"source_scope",
"source_inspections",
"planning_feedback",
"planning_inspection_required",
"planning_inspection_gate",
):
state.pop(key, None)
state.update(milestone=0, planning_required=False, evidence=[])
runtime._graph = original
state["progress_hint"] = (
"The model-generated phase plan has been retired after an observed scope refusal "
"or final public rejection. Use ordinary source inspection, edits and verification "
"under the complete original caller contract. The rejected edit was not applied. "
"All previous failures, costs and original final checks remain binding. "
"Do not submit another mandatory plan; repair the implementation directly."
)
runtime.store.event(
"model_plan_escaped",
dict(
contract_sha256=lease["contract_sha256"],
trigger=proof,
call=state["model_calls"],
step=state["steps"],
historical_graph_sha256=historical_hash,
original_graph_sha256=original_hash,
budgets_reset=False,
rejected_action_replayed=False,
acceptance_proof=False,
),
)
return caller