Repository navigation
Expand file tree
/
Copy pathnative_anchored_source.py
More file actions
104 lines (93 loc) · 4.26 KB
/
Copy pathnative_anchored_source.py
File metadata and controls
104 lines (93 loc) · 4.26 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
"""Task-anchored Native context;ordinary SDK loop and shell remain unchanged."""
from contextlib import contextmanager
import json
from pathlib import Path
from infinicoder.memory.source.anchored_source_workset import build_anchored_workset
from infinicoder.tools.native_shell_workbench import NativeShellCodeWorkbench
class NativeAnchoredSourceWorkset(NativeShellCodeWorkbench):
identity = "native_anchored_source_v1"
def __init__(
self,
*args,
source_dependency_policy=None,
workspace_aliases=(),
body_write_scopes=(),
**kwargs,
):
if source_dependency_policy not in (None, "anchored_static_v1"):
raise ValueError("unknown source dependency policy")
if any(
not isinstance(p, str)
or not p.startswith("/")
or p.endswith("/")
or ".." in p.split("/")
for p in workspace_aliases
):
raise ValueError("workspace aliases must be explicit absolute container roots")
self.source_dependency_policy = source_dependency_policy
self.workspace_aliases = tuple(workspace_aliases)
self.body_write_scopes = tuple(body_write_scopes)
super().__init__(*args, **kwargs)
def validate(self, request):
super().validate(request)
from infinicoder.memory.source.source_workset import public_target_present
for target in self.body_write_scopes:
if (
not isinstance(target, str)
or "::" not in target
or target.split("::", 1)[0] not in self.paths
or not public_target_present(request.spec.task, target)
):
raise ValueError(
"body write scope must be caller-explicit,public and in the declared implementation files"
)
def _source_policy_identity(self):
return ("public_roots_v1", self.source_dependency_policy, self.workspace_aliases)
@contextmanager
def bind(self, request, model, environment, deadline):
try:
with super().bind(request, model, environment, deadline):
from infinicoder.domains.operator.candidate_observation import HistoricalSources
forbidden = list(self.excluded_paths)
for check in request.spec.checks:
for name in check.protected_paths:
p = Path(name)
p = p if p.is_absolute() else self.workspace / p
if p.resolve().is_relative_to(self.workspace):
forbidden.append(p.resolve().relative_to(self.workspace).as_posix())
self.diagnostic_forbidden = tuple(forbidden)
self.historical_sources = HistoricalSources(
self.state / "historical-source",
self.workbench._read,
self.paths,
forbidden=forbidden,
remaining=self._remaining,
)
yield self
finally:
path = Path(request.state_dir) / "native-memory-info.json"
if path.is_file():
value = json.loads(path.read_bytes())
value.update(
source_policy="public_roots_v1",
source_dependency_policy=self.source_dependency_policy,
workspace_aliases=self.workspace_aliases,
historical_sources=str(
Path(request.state_dir) / "historical-source/manifest.json"
),
source_facts="Automatic current public roots and optional bounded static dependency candidates;no runtime binding or correctness proof",
)
path.write_text(
json.dumps(value, ensure_ascii=False, sort_keys=True), encoding="utf8"
)
def _build_source_packet(self, budget):
return build_anchored_workset(
self.index,
self.task,
self.public_targets,
budget,
lambda text: self._count([dict(role="user", content=text)]),
dependency_policy=self.source_dependency_policy,
workspace_aliases=self.workspace_aliases,
remaining=self._remaining,
)