@raytrace-cli/cli 0.15.0 → 0.15.1
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.
- package/package.json +1 -1
- package/worker/sandbox/engine.py +27 -10
- package/worker/sandbox/test_capture.py +43 -3
package/package.json
CHANGED
package/worker/sandbox/engine.py
CHANGED
|
@@ -101,7 +101,11 @@ MAX_PACKAGES = 400
|
|
|
101
101
|
REJECTED = [ipaddress.ip_network(block) for block in network.REJECTED]
|
|
102
102
|
OWN_KINDS = frozenset(['open', 'connect', 'listen', 'setid'])
|
|
103
103
|
PROCESS_KINDS = frozenset(['exec_succeeded', 'clone', 'process_exit', 'collection_gap', 'collector_disconnected'])
|
|
104
|
-
|
|
104
|
+
# Claude Code runs a command as `bash -c "... eval '<command>'"`: the export follows a quote.
|
|
105
|
+
CALL_MARKER = re.compile(r'(?:^|[\s;\'"])export RAYTRACE_CALL_ID=([A-Za-z0-9_-]{1,128})(?=[\s;]|$)')
|
|
106
|
+
# RayTrace's own hook in the sandbox: after each tool call it runs git over the
|
|
107
|
+
# whole project to keep a snapshot. What it and its children do is not the agent's doing.
|
|
108
|
+
OWN_HOOK = '/opt/raytace-claude/hook.mjs'
|
|
105
109
|
SANDBOX_ID = re.compile(r'rt[pf]-[a-f0-9]{32}')
|
|
106
110
|
|
|
107
111
|
|
|
@@ -604,7 +608,9 @@ class Engine(threading.Thread):
|
|
|
604
608
|
box.processes[pid] = {'start_ns': event.get('process_start_ns'), 'ppid': event.get('ppid'),
|
|
605
609
|
'name': posixpath.basename(event.get('executable') or (argv[0] if argv else '')) or event.get('process_name'),
|
|
606
610
|
'argv': [part[:400] for part in argv[:24]],
|
|
607
|
-
'call_id': marker or known.get('call_id') or parent.get('call_id')
|
|
611
|
+
'call_id': marker or known.get('call_id') or parent.get('call_id'),
|
|
612
|
+
'own': bool(known.get('own') or parent.get('own')
|
|
613
|
+
or (len(argv) > 1 and argv[1] == OWN_HOOK and posixpath.basename(argv[0]) == 'node'))}
|
|
608
614
|
if len(box.processes) > 20_000: # exits that never arrived: forget the oldest half
|
|
609
615
|
for stale in list(box.processes)[:10_000]:
|
|
610
616
|
del box.processes[stale]
|
|
@@ -635,7 +641,7 @@ class Engine(threading.Thread):
|
|
|
635
641
|
parent = box.processes.get(event.get('ppid')) or {}
|
|
636
642
|
return {'pid': pid, 'start_ns': known.get('start_ns') or event.get('process_start_ns'),
|
|
637
643
|
'name': known.get('name') or event.get('process_name') or None, 'argv': known.get('argv') or [],
|
|
638
|
-
'call_id': known.get('call_id') or parent.get('call_id')}
|
|
644
|
+
'call_id': known.get('call_id') or parent.get('call_id'), 'own': bool(known.get('own') or parent.get('own'))}
|
|
639
645
|
|
|
640
646
|
# ------------------------------------------------------------ facts
|
|
641
647
|
|
|
@@ -748,7 +754,9 @@ class Engine(threading.Thread):
|
|
|
748
754
|
'basis': {**basis, 'errno': REFUSED[errno]}}, at_ns)
|
|
749
755
|
return
|
|
750
756
|
if not writing:
|
|
751
|
-
|
|
757
|
+
# Reads of ordinary files are counted, as before: a test run reads hundreds.
|
|
758
|
+
# So is the snapshot hook reading every file after each tool call.
|
|
759
|
+
if guard == 'project' or who.get('own'):
|
|
752
760
|
return self.counted(box, who, top, at_ns)
|
|
753
761
|
self.fact(box, 'file', (path, 'read'), who, {'path': path, 'guard': guard, 'effect': 'read', 'certainty': 'confirmed',
|
|
754
762
|
'basis': {**basis, 'note': 'opened for reading; the bytes read are not captured'}}, at_ns)
|
|
@@ -809,7 +817,9 @@ class Engine(threading.Thread):
|
|
|
809
817
|
about when the watcher saw `path` change, likeliest first."""
|
|
810
818
|
found = {}
|
|
811
819
|
for call in box.raw:
|
|
812
|
-
|
|
820
|
+
# (Not the snapshot hook's git, which renames its own index beside every tool call.)
|
|
821
|
+
if (call['rule'] == rule and not call['errno'] and not call['who'].get('own')
|
|
822
|
+
and at_ns - RAW_WINDOW[0] <= call['at_ns'] <= at_ns + RAW_WINDOW[1]):
|
|
813
823
|
found.setdefault((call['who']['pid'], call['who']['start_ns']), call)
|
|
814
824
|
names = lambda call: any(posixpath.basename(path) == posixpath.basename(part) for part in call['who']['argv'])
|
|
815
825
|
ranked = sorted(found.values(), key=lambda call: (not names(call), abs(call['at_ns'] - at_ns)))
|
|
@@ -823,7 +833,8 @@ class Engine(threading.Thread):
|
|
|
823
833
|
moved = {cookie: posixpath.join(self.watched[watch][1], name) for watch, mask, cookie, name in events
|
|
824
834
|
if mask & Inotify.MOVED_TO and watch in self.watched}
|
|
825
835
|
# Kept files that were renamed: the new name is the same file, not a new one.
|
|
826
|
-
renamed = {cookie for watch, mask, cookie, name in events
|
|
836
|
+
renamed = {cookie: posixpath.join(self.watched[watch][1], name) for watch, mask, cookie, name in events
|
|
837
|
+
if mask & Inotify.MOVED_FROM and watch in self.watched
|
|
827
838
|
and posixpath.join(self.watched[watch][1], name) in self.watched[watch][0].guarded}
|
|
828
839
|
for watch, mask, cookie, name in events:
|
|
829
840
|
if mask & Inotify.OVERFLOW: # the kernel dropped notices: look at everything again
|
|
@@ -847,19 +858,25 @@ class Engine(threading.Thread):
|
|
|
847
858
|
continue
|
|
848
859
|
path = posixpath.join(folder, name)
|
|
849
860
|
if path not in box.guarded and box.project and mask & (Inotify.CREATE | Inotify.MOVED_TO) and name not in SKIPPED:
|
|
850
|
-
self.appeared(box, path, bool(mask & Inotify.ISDIR), cookie
|
|
861
|
+
self.appeared(box, path, bool(mask & Inotify.ISDIR), renamed.get(cookie))
|
|
851
862
|
if path in box.guarded:
|
|
852
863
|
self.noticed(box, path, [word for bit, word in Inotify.WORDS if mask & bit],
|
|
853
864
|
moved.get(cookie) if mask & Inotify.MOVED_FROM else None)
|
|
854
865
|
return len(events)
|
|
855
866
|
|
|
856
|
-
def appeared(self, box, path, folder,
|
|
867
|
+
def appeared(self, box, path, folder, source=None):
|
|
857
868
|
"""A name the watcher saw arrive in a project that is kept whole, with
|
|
858
|
-
no open that created it: a file moved in, or a folder with its files.
|
|
869
|
+
no open that created it: a file moved in, or a folder with its files.
|
|
870
|
+
`source` is the kept file it was renamed from, if it was one."""
|
|
871
|
+
renamed = source is not None
|
|
859
872
|
if not folder:
|
|
860
873
|
if len(box.guarded) < PROJECT_LIMIT:
|
|
861
874
|
# A kept file under its new name has its `renamed` fact already.
|
|
862
|
-
|
|
875
|
+
# Not one that was never looked at under its old name: a file
|
|
876
|
+
# written under a temporary name and moved here, as an agent's
|
|
877
|
+
# Write tool saves a new file. That is this file being created.
|
|
878
|
+
seen = renamed and box.photos.get(source) is not None
|
|
879
|
+
self.cover(box, path, photo(box.workspace, path, box.hashing) if seen else None)
|
|
863
880
|
return
|
|
864
881
|
folders = [path]
|
|
865
882
|
while folders and len(box.guarded) < PROJECT_LIMIT:
|
|
@@ -297,17 +297,57 @@ class EveryProjectFile(unittest.TestCase):
|
|
|
297
297
|
def test_a_file_or_a_folder_moved_in_is_kept(self):
|
|
298
298
|
self.engine.refresh(self.box)
|
|
299
299
|
self.write('src/moved.js', 'm')
|
|
300
|
-
self.engine.appeared(self.box, 'src/moved.js', False
|
|
300
|
+
self.engine.appeared(self.box, 'src/moved.js', False)
|
|
301
301
|
self.assertEqual(self.looked('src/moved.js', 'moved_to'), [('src/moved.js', 'created', 'project', None)])
|
|
302
302
|
self.write('lib/deep/a.js', 'a')
|
|
303
303
|
self.write('lib/node_modules/b.js', 'b')
|
|
304
|
-
self.engine.appeared(self.box, 'lib', True
|
|
304
|
+
self.engine.appeared(self.box, 'lib', True)
|
|
305
305
|
self.assertEqual({path for path in self.box.guarded if path.startswith('lib/')}, {'lib/deep/a.js'})
|
|
306
306
|
# A kept file under a new name is the same file: no second line for it.
|
|
307
307
|
os.rename(Path(self.folder.name) / 'src/app.js', Path(self.folder.name) / 'src/main.js')
|
|
308
|
-
self.engine.appeared(self.box, 'src/main.js', False,
|
|
308
|
+
self.engine.appeared(self.box, 'src/main.js', False, 'src/app.js')
|
|
309
309
|
self.assertEqual([fact for fact in self.looked('src/main.js', 'moved_to') if fact[0] == 'src/main.js'], [])
|
|
310
310
|
|
|
311
|
+
RENAME = {'kind': 'syscall_return', 'sysno': 38, 'errno': 0, 'ppid': 1, 'cwd': '/workspace'}
|
|
312
|
+
|
|
313
|
+
def test_a_new_file_saved_under_a_temporary_name_is_created_by_the_mover(self):
|
|
314
|
+
"""How an agent's Write tool saves: it writes a temporary file and
|
|
315
|
+
renames it, while the snapshot hook's git renames its own index."""
|
|
316
|
+
self.engine.refresh(self.box)
|
|
317
|
+
self.engine.executed(self.box, {'pid': 30, 'ppid': 1, 'process_start_ns': '30', 'executable': '/usr/local/bin/node',
|
|
318
|
+
'argv': ['node', engine.OWN_HOOK]})
|
|
319
|
+
self.engine.executed(self.box, {'pid': 31, 'ppid': 30, 'process_start_ns': '31', 'executable': '/usr/bin/git', 'argv': ['git', 'add', '-A']})
|
|
320
|
+
self.opened('src/new.js.tmp.1', flags=engine.O_CREAT | 1)
|
|
321
|
+
self.write('src/new.js.tmp.1', 'new')
|
|
322
|
+
os.rename(Path(self.folder.name) / 'src/new.js.tmp.1', Path(self.folder.name) / 'src/new.js')
|
|
323
|
+
for pid in (31, 10):
|
|
324
|
+
self.engine.numbered_call(self.box, {**self.RENAME, 'pid': pid, 'process_start_ns': str(pid), 'timestamp_ns': str(time.time_ns())})
|
|
325
|
+
self.engine.appeared(self.box, 'src/new.js', False, 'src/new.js.tmp.1')
|
|
326
|
+
self.looked('src/new.js', 'moved_to')
|
|
327
|
+
(fact,) = self.engine.facts.values()
|
|
328
|
+
self.assertEqual((fact['path'], fact['effect'], fact['certainty'], fact['pid']), ('src/new.js', 'created', 'confirmed', 10))
|
|
329
|
+
|
|
330
|
+
def test_the_snapshot_hook_reading_every_file_is_counted_not_listed(self):
|
|
331
|
+
self.engine.refresh(self.box)
|
|
332
|
+
self.engine.executed(self.box, {'pid': 30, 'ppid': 1, 'process_start_ns': '30', 'executable': '/usr/local/bin/node',
|
|
333
|
+
'argv': ['node', engine.OWN_HOOK]})
|
|
334
|
+
self.engine.executed(self.box, {'pid': 31, 'ppid': 30, 'process_start_ns': '31', 'executable': '/usr/bin/git', 'argv': ['git', 'add', '-A']})
|
|
335
|
+
read = {'kind': 'open', 'pathname': '/workspace/config/payment-keys.js', 'flags': 0, 'errno': 0, 'ppid': 30,
|
|
336
|
+
'timestamp_ns': str(time.time_ns()), 'sysno': 56}
|
|
337
|
+
self.engine.opened(self.box, {**read, 'pid': 31, 'process_start_ns': '31'})
|
|
338
|
+
self.assertEqual([fact for fact in self.engine.facts.values() if fact['kind'] == 'file'], [])
|
|
339
|
+
# Anything else named like the hook is not it, and its read is a fact.
|
|
340
|
+
self.engine.executed(self.box, {'pid': 40, 'ppid': 1, 'process_start_ns': '40', 'executable': '/usr/local/bin/node',
|
|
341
|
+
'argv': ['node', '/tmp/hook.mjs']})
|
|
342
|
+
self.engine.opened(self.box, {**read, 'pid': 40, 'ppid': 1, 'process_start_ns': '40'})
|
|
343
|
+
self.assertEqual([fact['pid'] for fact in self.engine.facts.values() if fact['kind'] == 'file'], [40])
|
|
344
|
+
|
|
345
|
+
def test_the_shell_of_a_tool_call_carries_the_call_in_its_own_text(self):
|
|
346
|
+
"""Claude Code runs `bash -c "... eval '<command>'"`: a redirect is the shell's own write."""
|
|
347
|
+
self.engine.refresh(self.box)
|
|
348
|
+
self.engine.executed(self.box, {'pid': 50, 'ppid': 1, 'process_start_ns': '50', 'executable': '/bin/bash', 'argv': [
|
|
349
|
+
'/bin/bash', '-c', "source /home/node/snap.sh || true && eval 'export RAYTRACE_CALL_ID=toolu_01ABC\necho hi > src/app.js' < /dev/null"]})
|
|
350
|
+
self.assertEqual(self.box.processes[50]['call_id'], 'toolu_01ABC')
|
|
311
351
|
|
|
312
352
|
def test_a_write_through_a_link_is_still_the_writers(self):
|
|
313
353
|
os.symlink('app.js', Path(self.folder.name) / 'src/alias.js')
|