Repository navigation
Expand file tree
/
Copy pathcontinuous_conversation.py
More file actions
477 lines (439 loc) · 19.3 KB
/
Copy pathcontinuous_conversation.py
File metadata and controls
477 lines (439 loc) · 19.3 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""Opt-in projection of the canonical Runtime action history, not another agent.
No generated summaries, hidden requests, execution authority, or acceptance
certificates. Historical observations keep their actual revision identities.
"""
from infinicoder._resources import package_root
from dataclasses import asdict
import hashlib
import json
from infinicoder.models.budget import BudgetExceeded
from infinicoder.runtime.feedback import acceptance_packet
from infinicoder.memory.revision_feedback import revision
from infinicoder.execution.workspace_tools import Workspace
def encoded(value):
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
def digest(value):
return hashlib.sha256(encoded(value).encode()).hexdigest()
def identity():
root = package_root()
return dict(
version="continuous_v1",
sha256=hashlib.sha256(
b"".join(
(root / name).read_bytes()
for name in (
"memory/continuous_conversation.py",
"runtime/shell_loop.py",
"runtime/text_frontend.py",
"runtime/engine.py",
)
)
).hexdigest(),
)
def validate(runtime):
"""Reject competing automatic routes before any request, never downgrade."""
if not runtime.shell_loop or runtime.decision_driver != "text_task_first":
raise ValueError("continuous_v1 requires shell_loop and text_task_first")
# Runtime internally enables command_worker for every shell_loop. This is
# the ordinary original executor, not a competing source-worker route.
forbidden = (
"automatic_workers",
"worker_path",
"worker_symbol",
"source_focus",
"inspection_recovery",
"native_plan",
"problem_workflow",
"stalled_feedback_policy",
"flow_update_policy",
"design_contracts",
"design_probes",
"design_investigation",
"grounded_planning",
"diagnostic_planning",
"development_workflows",
"repair_memory",
"source_sync",
"source_archive",
"command_evidence",
"operator_protocol",
"tensor_dataflow",
"profiling",
"optimization",
)
active = [name for name in forbidden if getattr(runtime, name, False)]
if active:
raise ValueError(
"continuous_v1 does not support automatic context/routes: " + ",".join(active)
)
def preflight(runtime, state):
if state.get("planning_required"):
raise ValueError(
"continuous_v1 requires an explicit writable contract before the first request; use the original route for mandatory planning"
)
if any(unit.kind != "implement" or unit.worker_path for unit in runtime._graph.units):
raise ValueError(
"continuous_v1 supports ordinary implementation phases only; use the original route for mandatory stages/workers"
)
def implementation_targets(workspace, paths, protected):
"""Only explicit exact caller paths identify implementation targets.
A glob/directory permission is not an inferred candidate declaration. Do
not guess from task IDs, extensions, test filenames, or arbitrary writes.
"""
paths = sorted(set(paths))
if not paths or any(any(char in path for char in "*?[") for path in paths):
return dict(status="unknown", reason="no_explicit_exact_target_files")
hashes = {}
try:
for name in paths:
path = workspace.path(name, write=True)
if path.is_dir() or str(path.resolve()) in protected:
return dict(status="unknown", reason="directory_or_protected_target")
hashes[name] = hashlib.sha256(path.read_bytes()).hexdigest() if path.is_file() else None
except (OSError, ValueError):
return dict(status="unknown", reason="target_identity_unavailable")
return dict(status="known", scope_identity=digest(paths), hashes=hashes)
def snapshot(runtime, spec, state):
ordinal = min(state["milestone"], len(runtime._graph.order) - 1)
unit = runtime._graph.unit(runtime._graph.order[ordinal])
# Full caller data and work-unit interfaces stay mandatory; teaching cards,
# automatically retrieved source and duplicate state logs are not a contract.
work_unit = asdict(unit)
if unit.goal == spec.task or unit.goal.startswith(spec.task + "\n"):
work_unit.pop("goal")
work_unit.update(task_ref="task", goal_suffix=unit.goal[len(spec.task) :])
contract = dict(
task=spec.task,
allowed_paths=spec.allowed_paths,
required_artifacts=spec.required_artifacts,
target_facts=spec.target_facts,
tensor_layouts=spec.tensor_layouts,
acceptance=acceptance_packet(spec.checks),
capabilities=spec.capabilities,
amendments=spec.amendments,
clarifications=state.get("clarifications", []),
current_work_unit=work_unit,
)
if getattr(runtime, "reference_contract", None) is not None:
from infinicoder.verification.reference_compare import view as reference_view
contract["public_reference_experiment"] = reference_view(
runtime.reference_contract, state["milestone"]
)
contract = json.loads(encoded(contract))
workspace = Workspace(runtime.workspace, spec.allowed_paths)
return dict(
phase=state["milestone"],
requirements=digest(contract),
source_revision=revision(spec, workspace),
contract=contract,
implementation_targets=implementation_targets(
workspace, unit.write_set or spec.allowed_paths, state.get("checker_files", {})
),
)
def stamp(runtime, spec, state):
"""Attach provenance to the original record after original guarded dispatch."""
records = state.get("shell_loop_history", [])
if not records:
return
session = state.get("continuous_conversation", {})
before = session.get("current_snapshot")
after = snapshot(runtime, spec, state)
old_targets = (before or {}).get("implementation_targets", {})
new_targets = after["implementation_targets"]
comparable = (
old_targets.get("status") == new_targets.get("status") == "known"
and old_targets["scope_identity"] == new_targets["scope_identity"]
and before["requirements"] == after["requirements"]
and before["phase"] == after["phase"]
)
changed = (
[
name
for name in new_targets["hashes"]
if old_targets["hashes"][name] != new_targets["hashes"][name]
]
if comparable
else None
)
records[-1]["conversation_provenance"] = dict(
before={k: v for k, v in (before or {}).items() if k != "contract"},
after={k: v for k, v in after.items() if k != "contract"},
source_changed=bool(before and before["source_revision"] != after["source_revision"]),
implementation_changed=bool(changed) if comparable else None,
implementation_changed_paths=changed,
acceptance_proof=False,
)
def _ranges(calls):
ranges = []
for call in calls:
if ranges and ranges[-1][1] + 1 == call:
ranges[-1][1] = call
else:
ranges.append([call, call])
return ranges
def context(runtime, spec, state, index):
from infinicoder.runtime.shell_loop import _cost, _bounded_pair, tools
current = snapshot(runtime, spec, state)
session = state.setdefault("continuous_conversation", {})
if not session:
contract = dict(
current["contract"],
remaining_model_calls=runtime.max_model_calls - state.get("model_calls", 0),
)
text = (
encoded(contract)
+ "\n"
+ runtime.registry.context(spec.capabilities, spec.target_facts)
)
guidance = runtime._inspection_guidance()
if runtime.auto_verify_commands:
from infinicoder.memory.revision_feedback import GUIDANCE
guidance += "\n" + GUIDANCE
initial = [
dict(
role="system",
content=runtime._decision_frontend.system(
tools(runtime), extended=bool(runtime.task_memory or runtime.symbol_tools)
)
+ guidance,
),
dict(role="user", content=runtime._decision_frontend.instance(text)),
]
session.update(
policy="continuous_v1",
initial_messages=initial,
initial_contract_hash=current["requirements"],
initial_messages_hash=digest(initial),
initial_snapshot=current,
current_snapshot=current,
boundaries=[],
)
if (
session["policy"] != "continuous_v1"
or digest(session["initial_messages"]) != session["initial_messages_hash"]
):
raise ValueError("Continuous conversation initial contract/policy integrity mismatch")
prior = session["current_snapshot"]
if any(prior[key] != current[key] for key in ("requirements", "phase", "source_revision")):
boundary = dict(
after_call=state.get("model_calls", 0),
origin="runtime_revision_or_contract_boundary",
previous={k: v for k, v in prior.items() if k != "contract"},
current={k: v for k, v in current.items() if k != "contract"},
historical_observations_remain_historical=True,
acceptance_proof=False,
)
if prior["requirements"] != current["requirements"]:
boundary["current_contract"] = current["contract"]
if not session["boundaries"] or session["boundaries"][-1] != boundary:
session["boundaries"].append(boundary)
session["current_snapshot"] = current
initial = [dict(message) for message in session["initial_messages"]]
limit = runtime.assembler.profile.input_budget()
records = state.get("shell_loop_history", [])
calls = [record["call"] for record in records]
# Source edits and their most recent acceptance observation cannot be
# displaced by a suffix of read-only commands. They remain historical if
# the current revision/requirements differ, never reusable acceptance.
def current_scope(record):
after = record.get("conversation_provenance", {}).get("after", {})
targets = after.get("implementation_targets", {})
return (
after.get("phase") == current["phase"]
and after.get("requirements") == current["requirements"]
and targets.get("status") == current["implementation_targets"].get("status") == "known"
and targets.get("scope_identity")
== current["implementation_targets"].get("scope_identity")
)
candidate = next(
(
record
for record in reversed(records)
if record.get("conversation_provenance", {}).get("implementation_changed") is True
and current_scope(record)
),
None,
)
required = {candidate["call"]} if candidate else set()
from infinicoder.runtime.feedback import command_has_check
check = next(
(
record
for record in reversed(records)
if (not candidate or record["call"] >= candidate["call"]) and command_has_check(record)
),
None,
)
if check:
required.add(check["call"])
if getattr(runtime, "reference_contract", None) is not None:
reference = next(
(
r
for r in reversed(records)
if (r.get("action") or {}).get("tool") == "reference_compare" and current_scope(r)
),
None,
)
if reference:
required.add(reference["call"])
if records:
required.add(records[-1]["call"])
selected = {}
bounded = []
versions = {}
def version_ref(value):
version = {key: value[key] for key in ("phase", "requirements", "source_revision")}
key = digest(version)
if key not in versions:
versions[key] = (str(len(versions)), version)
return versions[key][0]
version_ref(session["initial_snapshot"])
current_ref = version_ref(current)
for record in records:
provenance = record.get("conversation_provenance")
if provenance:
version_ref(provenance["before"])
version_ref(provenance["after"])
def compose(chosen):
omitted = [call for call in calls if call not in chosen]
needed_refs = {current_ref}
for call in chosen:
provenance = by_call[call].get("conversation_provenance")
if provenance:
needed_refs.update(
(version_ref(provenance["before"]), version_ref(provenance["after"]))
)
tail = dict(
origin="runtime_conversation_projection",
policy="continuous_v1",
current_version_ref=current_ref,
recorded_versions={
label: value for label, value in versions.values() if label in needed_refs
},
remaining_model_calls=max(0, runtime.max_model_calls - state.get("model_calls", 0)),
omitted_call_ranges=_ranges(omitted),
bounded_calls=bounded,
historical_observations_are_not_current_facts=True,
acceptance_proof=False,
)
if getattr(runtime, "implementation_provider", None) is not None and state.get(
"source_unit_last_check"
):
from infinicoder.memory.source.source_units import check_view
tail["latest_original_check"] = check_view(
spec, state, Workspace(runtime.workspace, spec.allowed_paths)
)
tail["implementation_target_status"] = current["implementation_targets"]["status"]
tail["target_candidate_call"] = candidate["call"] if candidate else None
tail["latest_check_call"] = check["call"] if check else None
# Current permissions/requirements must survive even if their boundary
# predates the retained suffix. No model inference supplies this data.
if current["requirements"] != session["initial_contract_hash"]:
tail["current_contract"] = current["contract"]
messages = list(initial)
# Each turn carries its exact original versions. Only the latest
# boundary needs a separate body; replaying every transition would
# recreate the repeated state injection this policy is testing.
boundaries = session["boundaries"][-1:]
done = set()
for call in sorted(chosen):
for ordinal, event in enumerate(boundaries):
if event["after_call"] < call and ordinal not in done:
messages.append(
dict(role="user", content="HISTORICAL RUNTIME BOUNDARY\n" + encoded(event))
)
done.add(ordinal)
messages.extend(chosen[call])
# Older boundary bodies can themselves grow. Retain the latest actual
# boundary; omitted bodies remain durable with no invented summary.
if boundaries and len(boundaries) - 1 not in done:
messages.append(
dict(role="user", content="RUNTIME BOUNDARY\n" + encoded(boundaries[-1]))
)
messages.append(dict(role="user", content="CURRENT RUNTIME ENVELOPE\n" + encoded(tail)))
return messages
# Store boundary identities on each record; all old boundary bodies need
# not be repeated alongside old observations at every request.
def pair(record):
from infinicoder.runtime.shell_loop import render_pair
result = render_pair(runtime, record)
provenance = record.get("conversation_provenance")
if provenance:
result = [dict(message) for message in result]
references = dict(
before_version=version_ref(provenance["before"]),
after_version=version_ref(provenance["after"]),
source_changed=provenance["source_changed"],
implementation_changed=provenance.get("implementation_changed"),
implementation_changed_paths=provenance.get("implementation_changed_paths"),
acceptance_proof=False,
)
result[-1]["content"] = (
"HISTORICAL TURN PROVENANCE\n" + encoded(references) + "\n" + result[-1]["content"]
)
return result
by_call = {record["call"]: record for record in records}
if _cost(runtime, compose({})) > limit:
raise BudgetExceeded(
"Continuous conversation caller contract/current boundary exceeds input budget;zero request permitted"
)
for call in sorted(required):
selected[call] = pair(by_call[call])
if _cost(runtime, compose(selected)) > limit:
selected.pop(call)
# Existing bounded observation is explicitly partial user evidence;
# it is never a truncated assistant action or a fake executed turn.
budget = max(0, limit - _cost(runtime, compose(selected)) - 64)
partial = _bounded_pair(runtime, by_call[call], budget)
if partial is None:
raise BudgetExceeded(
"Latest necessary continuous conversation evidence cannot fit;zero request permitted"
)
bounded.append(call)
selected[call] = partial
if _cost(runtime, compose(selected)) > limit:
raise BudgetExceeded(
"Bounded continuous conversation evidence cannot fit;zero request permitted"
)
for record in reversed(records):
call = record["call"]
if call in selected:
continue
selected[call] = pair(record)
if _cost(runtime, compose(selected)) > limit:
selected.pop(call)
break # Retain a consecutive suffix plus explicitly pinned turns.
messages = compose(selected)
report = dict(
shell_loop=True,
conversation_policy="continuous_v1",
initial_contract_hash=session["initial_contract_hash"],
initial_messages_hash=session["initial_messages_hash"],
selected_calls=sorted(selected),
omitted_call_ranges=_ranges([call for call in calls if call not in selected]),
target_candidate_call=candidate["call"] if candidate else None,
latest_check_call=check["call"] if check else None,
implementation_target_status=current["implementation_targets"]["status"],
bounded_calls=bounded,
total_history=len(records),
input_tokens=_cost(runtime, messages),
input_budget=limit,
system_tokens=_cost(runtime, initial[:1]),
initial_task_tokens=_cost(runtime, initial[1:]),
history_tokens=sum(_cost(runtime, item) for item in selected.values()),
optional_injection_tokens=0,
memory_reserved_tokens=0,
memory_included=False,
estimator="caller tokenizer" if runtime.assembler.estimator.tokenizer else "heuristic",
)
report["boundary_and_envelope_tokens"] = (
report["input_tokens"]
- report["system_tokens"]
- report["initial_task_tokens"]
- report["history_tokens"]
)
state["budget_report"] = report
session["projection"] = report
runtime.store.event("continuous_conversation_projection", report)
return messages