Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions sage/gateway/reference_f1a.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,34 @@ def _same_name_elsewhere(self, p: Path) -> list:
continue
return out

def _same_name_note(self, p: Path, *, created: bool) -> str:
"""One sentence naming files that share `p`'s name one directory away, or "".

THE ABSENCE TEXT WAS THE ROUTE TO A DUPLICATE. Measured 2026-11-16 06:23Z on cbp-being
(beat heartbeat-5d75f0a150eb): the seat asked for two memory_edit range deletions on
scratch/xor_not_linearly_separable.py (401 lines). The being sent the bare name, got
"does not exist ... memory_write creates a file", did exactly that, and the receipt
said "created xor_not_linearly_separable.py with 2059 chars". It then edited, read and
ran the NEW 66-line top-level file as if it were the one it was asked to trim; the
scratch/ file never changed, and the script that imports it would have run the old
401 lines. memory_read already names a same-name file on a miss (#140); edit and the
create receipt did not. Matches are listed neutrally, as memory_read does."""
near = self._same_name_elsewhere(p)
if not near:
return ""
shown = near[:_SAME_NAME_SHOWN]
where = ", ".join(f"'{n}'" for n in shown)
more = f" and {len(near) - len(shown)} more" if len(near) > len(shown) else ""
if created:
return (f" A file with this name already exists elsewhere: {where}{more}. It was "
f"NOT changed; this write made a separate new file. Anything that imports or "
f"runs {where if len(near) == 1 else 'one of those'} still sees the old text.")
if len(near) == 1:
return (f" But a file with that name DOES exist at '{near[0]}': to edit it, send the "
f"same memory_edit with path \"{near[0]}\".")
return (f" But {len(near)} files with that name exist: {where}{more}. Nothing tells "
f"which you mean; send memory_edit with the full path of the one you mean.")

@staticmethod
def _existence(p: Path) -> str:
"""'absent' | 'present' | 'unknown'. Never guesses.
Expand Down Expand Up @@ -1942,7 +1970,9 @@ def _do_memory_edit(self, intent: BeingIntent) -> ResultEnvelope:
if not p.exists():
return ResultEnvelope(ok=False, error=(
f"nothing to edit: '{path}' does not exist. This is an absence, not a "
f"refusal. memory_write creates a file; memory_edit changes one that is there."))
f"refusal." + (self._same_name_note(p, created=False) or
" memory_write creates a file; memory_edit changes one that is "
"there.")))
if p.is_dir():
return ResultEnvelope(ok=False, error=f"'{path}' is a directory.")
text = p.read_text(errors="replace")
Expand Down Expand Up @@ -2427,7 +2457,8 @@ def _do_memory_write(self, intent: BeingIntent) -> ResultEnvelope:
with open(p, "a") as f: # under the home lock (see __call__)
f.write(content + ("\n" if not content.endswith("\n") else ""))
if not existed:
result = f"created {p.name} with {len(content)} chars"
result = (f"created {p.name} with {len(content)} chars"
+ self._same_name_note(p, created=True))
else:
result = (f"appended {len(content)} chars to the END of {p.name}, below the {before} "
f"lines already there. memory_write only adds; it never replaces or edits a line. "
Expand Down
70 changes: 70 additions & 0 deletions sage/gateway/tests/test_edit_absence_names_same_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""An edit miss and a fresh create both name a same-name file one directory away.

Measured 2026-11-16 06:23Z on cbp-being: memory_edit on the bare name of
scratch/xor_not_linearly_separable.py said only "does not exist ... memory_write creates a
file"; the being then memory_wrote the bare name, the receipt said "created ...", and every
later act in the beat worked on the new top-level copy while the scratch/ file (the one an
importing script reads) stayed as it was.
"""
import os
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from sage.gateway.being_gate_client import BeingIntent, GatewayVerdict # noqa: E402
from sage.gateway.reference_f1a import ReferenceF1aDispatcher # noqa: E402

_ALLOW = GatewayVerdict("allow")


def _home():
root = tempfile.mkdtemp(prefix="edit-same-name-")
os.makedirs(os.path.join(root, "scratch"))
with open(os.path.join(root, "scratch", "xor.py"), "w") as f:
f.write("a = 1\nb = 2\nc = 3\n")
return root, ReferenceF1aDispatcher(memory_root=root)


def test_edit_miss_names_the_scratch_file_and_does_not_route_to_write():
root, d = _home()
r = d(BeingIntent("memory_edit", {"path": "xor.py", "start_line": 1, "end_line": 1,
"new": ""}), _ALLOW)
assert not r.ok and "does not exist" in r.error, r.error
assert "'scratch/xor.py'" in r.error and 'path "scratch/xor.py"' in r.error, r.error
assert "memory_write creates a file" not in r.error, r.error
# the suggested call works
r = d(BeingIntent("memory_edit", {"path": "scratch/xor.py", "start_line": 1, "end_line": 1,
"new": ""}), _ALLOW)
assert r.ok, r.error
assert open(os.path.join(root, "scratch", "xor.py")).read() == "b = 2\nc = 3\n"


def test_true_absence_keeps_the_old_text():
_, d = _home()
r = d(BeingIntent("memory_edit", {"path": "nope.py", "old": "a", "new": "b"}), _ALLOW)
assert not r.ok and "memory_write creates a file" in r.error, r.error
assert "DOES exist" not in r.error, r.error


def test_create_receipt_says_the_same_name_file_was_not_changed():
root, d = _home()
r = d(BeingIntent("memory_write", {"path": "xor.py", "content": "print(1)\n"}), _ALLOW)
assert r.ok and r.result.startswith("created xor.py"), r.result
assert "'scratch/xor.py'" in r.result and "NOT changed" in r.result, r.result
assert open(os.path.join(root, "scratch", "xor.py")).read() == "a = 1\nb = 2\nc = 3\n"


def test_create_with_no_namesake_is_unchanged():
_, d = _home()
r = d(BeingIntent("memory_write", {"path": "scratch/fresh.py", "content": "x = 1\n"}),
_ALLOW)
assert r.ok and "NOT changed" not in r.result, r.result


if __name__ == "__main__":
for name, fn in list(globals().items()):
if name.startswith("test_") and callable(fn):
fn()
print("ok", name)
Loading