@astrofoundry/pi-astro 0.40.52 → 0.40.53
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/agents/research.md
CHANGED
|
@@ -17,11 +17,11 @@ Each task starts with `Session: t<id>. Today: <date>.` Use that session in every
|
|
|
17
17
|
|
|
18
18
|
Work in this order:
|
|
19
19
|
|
|
20
|
-
1. **Title.** When the context says "New request",
|
|
20
|
+
1. **Title.** When the context says "New request", your reply always starts with the line `TITLE: [CATEGORY YYYY-MM-DD] <place>: <subject>`, also when it only asks questions. Example: `TITLE: [PRODUCT 2026-11-04] UAE: top 5 running shoes for men`. Categories: PRODUCT, FLIGHT, STAY, SERVICE, LOCAL, EVENT, INFO. Keep the number the user asked for. The bridge turns that line into the thread's name and removes it.
|
|
21
21
|
2. **Defaults.** Run `defaults list` and apply GLOBAL and the category's defaults.
|
|
22
22
|
3. **Questions.** If anything still open would change the answer (size, budget, dates, passengers, cabin, type of use), ask it as a numbered list of at most five short questions. Give each one your suggested answer in brackets. Then stop: do not search before the user answers. When nothing is open, state the defaults you applied in one line and research.
|
|
23
23
|
4. **Research.** Follow the skill's sources and checks. Verify every fact you report on the page itself, in this sandbox, today.
|
|
24
|
-
5. **Answer.** Use the skill's format. It has no open assumptions: anything you could not check is either checked another way or left out, never reported as "unchecked".
|
|
24
|
+
5. **Answer.** Use the skill's format. It has no open assumptions: anything you could not check is either checked another way or left out, never reported as "unchecked". Only an answer with researched results ends with the line `REFRESH: yes`; a reply that asks questions or confirms a default never does.
|
|
25
25
|
|
|
26
26
|
A later message in the thread continues the same research with its context: new constraints mean research again; "set PRODUCT shoe size EU 43", "list FLIGHT defaults", or "forget that" change the defaults through `defaults`. Save a default only when the user states it, never by inference, and confirm it in one line.
|
|
27
27
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Forced command of the research specialist's key: the request arrives as JSON on stdin
|
|
3
|
+
# and runs as `researcher`, which owns the browser and the thread folders.
|
|
4
|
+
case "${SSH_ORIGINAL_COMMAND:-}" in
|
|
5
|
+
request) exec sudo -n -u researcher /usr/local/sbin/spc-research-op ;;
|
|
6
|
+
*) echo "refused: the only command is 'request' with a JSON body on stdin" >&2; exit 2 ;;
|
|
7
|
+
esac
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
"""Sandbox operations of the research specialist on VM 110 at Mira.
|
|
3
|
+
|
|
4
|
+
Installed root-owned as /usr/local/sbin/spc-research-op and run as `researcher`
|
|
5
|
+
through spc-research-entry. It reads one JSON request on stdin and prints one
|
|
6
|
+
JSON answer. Every operation stays inside this VM: the shared Chrome (DevTools
|
|
7
|
+
on 127.0.0.1:9222), the thread folders under ~/threads, and the toolbox under
|
|
8
|
+
/opt/research-toolbox. Web content is untrusted; nothing here reads secrets.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import shutil
|
|
15
|
+
import subprocess
|
|
16
|
+
import sys
|
|
17
|
+
import urllib.request
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
HOME = Path("/home/researcher")
|
|
21
|
+
THREADS = HOME / "threads"
|
|
22
|
+
TOOLBOX = Path("/opt/research-toolbox")
|
|
23
|
+
CDP = "http://127.0.0.1:9222"
|
|
24
|
+
PLAYWRIGHT = "/usr/local/bin/playwright-cli"
|
|
25
|
+
OUTPUT_LIMIT = 60_000
|
|
26
|
+
SCRIPT_TIMEOUT = 300
|
|
27
|
+
BROWSER_TIMEOUT = 120
|
|
28
|
+
|
|
29
|
+
SESSION = re.compile(r"^[a-z0-9][a-z0-9-]{0,62}$")
|
|
30
|
+
FILE_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$")
|
|
31
|
+
RELATIVE = re.compile(r"^[A-Za-z0-9_][A-Za-z0-9_./-]{0,200}$")
|
|
32
|
+
LANGUAGES = {".py": ["/usr/bin/python3", "-I"], ".js": ["/usr/bin/node"], ".mjs": ["/usr/bin/node"], ".sh": ["/bin/bash"]}
|
|
33
|
+
|
|
34
|
+
# playwright-cli commands for reading and navigating pages. Commands that start,
|
|
35
|
+
# close, or wipe the shared browser, and cookie, storage, and run-code commands
|
|
36
|
+
# that reach the profile's sessions, are refused.
|
|
37
|
+
BROWSER_COMMANDS = {
|
|
38
|
+
"goto", "go-back", "go-forward", "reload", "snapshot", "find", "click", "dblclick", "fill", "type", "press",
|
|
39
|
+
"hover", "select", "check", "uncheck", "drag", "eval", "screenshot", "pdf", "mousewheel", "mousemove",
|
|
40
|
+
"mousedown", "mouseup", "keydown", "keyup", "dialog-accept", "dialog-dismiss", "console", "requests", "request",
|
|
41
|
+
"tab-list", "tab-new", "tab-close", "tab-select", "generate-locator",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# Page titles and frames of the challenges a person has to solve in the live view.
|
|
45
|
+
CAPTCHA_TITLES = re.compile(r"prove your humanity|just a moment|are you a robot|verify you are human|attention required|access denied", re.I)
|
|
46
|
+
CAPTCHA_FRAMES = re.compile(r"recaptcha|hcaptcha|challenges\.cloudflare\.com|captcha-delivery|arkoselabs|funcaptcha|perimeterx", re.I)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Refused(Exception):
|
|
50
|
+
"""A request this file does not accept; exit 2."""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def answer(value, code=0):
|
|
54
|
+
print(json.dumps(value, ensure_ascii=False))
|
|
55
|
+
sys.exit(code)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def capped(text):
|
|
59
|
+
return text if len(text) <= OUTPUT_LIMIT else text[:OUTPUT_LIMIT] + f"\n[cut at {OUTPUT_LIMIT} characters]"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def session_of(request):
|
|
63
|
+
session = request.get("session")
|
|
64
|
+
if not isinstance(session, str) or not SESSION.fullmatch(session):
|
|
65
|
+
raise Refused("session: lowercase letters, digits, and dashes, as the thread id")
|
|
66
|
+
return session
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def thread_dir(session):
|
|
70
|
+
path = THREADS / session
|
|
71
|
+
path.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
72
|
+
return path
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def inside(base, relative):
|
|
76
|
+
if not isinstance(relative, str) or not RELATIVE.fullmatch(relative) or any(p in ("", ".", "..") for p in relative.split("/")):
|
|
77
|
+
raise Refused("path: relative, without empty parts or parent traversal")
|
|
78
|
+
path = (base / relative).resolve()
|
|
79
|
+
if base.resolve() not in path.parents and path != base.resolve():
|
|
80
|
+
raise Refused("path leaves the thread folder")
|
|
81
|
+
return path
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def run(argv, timeout, cwd=None, stdin=None):
|
|
85
|
+
env = {"HOME": str(HOME), "PATH": "/usr/local/bin:/usr/bin:/bin", "LANG": "en_US.UTF-8", "DISPLAY": ":1"}
|
|
86
|
+
try:
|
|
87
|
+
result = subprocess.run(argv, cwd=cwd, input=stdin, capture_output=True, text=True, timeout=timeout, env=env)
|
|
88
|
+
except subprocess.TimeoutExpired:
|
|
89
|
+
return {"exit": None, "timedOut": timeout, "stdout": "", "stderr": ""}
|
|
90
|
+
return {"exit": result.returncode, "stdout": capped(result.stdout), "stderr": capped(result.stderr)}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def devtools(path):
|
|
94
|
+
with urllib.request.urlopen(CDP + path, timeout=10) as response:
|
|
95
|
+
return json.loads(response.read())
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def pages():
|
|
99
|
+
return [{"id": p.get("id"), "title": p.get("title"), "url": p.get("url")} for p in devtools("/json/list") if p.get("type") == "page"]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def attached(session):
|
|
103
|
+
listed = run([PLAYWRIGHT, "list", "--json"], 30)
|
|
104
|
+
return session in (listed.get("stdout") or "")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def op_status(request):
|
|
108
|
+
try:
|
|
109
|
+
version = devtools("/json/version")
|
|
110
|
+
except OSError as error:
|
|
111
|
+
return {"browser": "down", "error": str(error)}
|
|
112
|
+
return {"browser": version.get("Browser"), "pages": pages()}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def op_browser(request):
|
|
116
|
+
"""One playwright-cli command in the thread's own session, attached to the shared Chrome."""
|
|
117
|
+
session = session_of(request)
|
|
118
|
+
args = request.get("args")
|
|
119
|
+
if not isinstance(args, list) or not args or not all(isinstance(a, str) for a in args):
|
|
120
|
+
raise Refused("args: a non-empty list of strings, the playwright-cli command and its arguments")
|
|
121
|
+
command = args[1] if args[0] == "--raw" and len(args) > 1 else args[0]
|
|
122
|
+
if command not in BROWSER_COMMANDS:
|
|
123
|
+
raise Refused("browser command, after an optional --raw: " + ", ".join(sorted(BROWSER_COMMANDS)))
|
|
124
|
+
thread_dir(session)
|
|
125
|
+
if not attached(session):
|
|
126
|
+
first = run([PLAYWRIGHT, f"-s={session}", "attach", f"--cdp={CDP}"], BROWSER_TIMEOUT, cwd=THREADS / session)
|
|
127
|
+
if first["exit"] != 0:
|
|
128
|
+
return {"attached": False, **first}
|
|
129
|
+
run([PLAYWRIGHT, f"-s={session}", "tab-new"], BROWSER_TIMEOUT, cwd=THREADS / session)
|
|
130
|
+
return run([PLAYWRIGHT, f"-s={session}", *args], BROWSER_TIMEOUT, cwd=THREADS / session)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def op_captcha(request):
|
|
134
|
+
"""Whether any open page shows a challenge that needs a person in the live view."""
|
|
135
|
+
found = []
|
|
136
|
+
for page in pages():
|
|
137
|
+
if CAPTCHA_TITLES.search(page.get("title") or "") or CAPTCHA_FRAMES.search(page.get("url") or ""):
|
|
138
|
+
found.append(page)
|
|
139
|
+
session = request.get("session")
|
|
140
|
+
if isinstance(session, str) and SESSION.fullmatch(session) and attached(session):
|
|
141
|
+
frames = run([PLAYWRIGHT, f"-s={session}", "--raw", "eval",
|
|
142
|
+
"[document.title, ...[...document.querySelectorAll('iframe')].map(f => f.src)].join(' ')"], 60)
|
|
143
|
+
if CAPTCHA_TITLES.search(frames["stdout"]) or CAPTCHA_FRAMES.search(frames["stdout"]):
|
|
144
|
+
found.append({"session": session, "evidence": frames["stdout"][:300]})
|
|
145
|
+
return {"captcha": bool(found), "pages": found}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def op_script(request):
|
|
149
|
+
"""Writes the agent's script into the thread folder and runs it there, as researcher, with a time limit."""
|
|
150
|
+
session = session_of(request)
|
|
151
|
+
name, code = request.get("name"), request.get("code")
|
|
152
|
+
if not isinstance(name, str) or not FILE_NAME.fullmatch(name) or Path(name).suffix not in LANGUAGES:
|
|
153
|
+
raise Refused("name: a file name ending in " + ", ".join(LANGUAGES))
|
|
154
|
+
if not isinstance(code, str) or not code.strip() or len(code) > 200_000:
|
|
155
|
+
raise Refused("code: the script text, at most 200,000 characters")
|
|
156
|
+
args = request.get("args", [])
|
|
157
|
+
if not isinstance(args, list) or not all(isinstance(a, str) for a in args):
|
|
158
|
+
raise Refused("args: a list of strings")
|
|
159
|
+
scripts = thread_dir(session) / "scripts"
|
|
160
|
+
scripts.mkdir(mode=0o700, exist_ok=True)
|
|
161
|
+
path = scripts / name
|
|
162
|
+
path.write_text(code, encoding="utf-8")
|
|
163
|
+
return {"script": str(path.relative_to(THREADS / session)), **run([*LANGUAGES[path.suffix], str(path), *args], SCRIPT_TIMEOUT, cwd=THREADS / session)}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def op_read(request):
|
|
167
|
+
session = session_of(request)
|
|
168
|
+
path = inside(thread_dir(session), request.get("path"))
|
|
169
|
+
if not path.is_file():
|
|
170
|
+
return {"path": request.get("path"), "exists": False}
|
|
171
|
+
return {"path": request.get("path"), "bytes": path.stat().st_size, "text": capped(path.read_text(encoding="utf-8", errors="replace"))}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def op_files(request):
|
|
175
|
+
base = thread_dir(session_of(request))
|
|
176
|
+
return {"files": sorted(str(p.relative_to(base)) for p in base.rglob("*") if p.is_file())[:500]}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def op_toolbox(request):
|
|
180
|
+
"""Runs a reviewed tool from /opt/research-toolbox in the thread folder."""
|
|
181
|
+
session = session_of(request)
|
|
182
|
+
name = request.get("tool")
|
|
183
|
+
if name is None:
|
|
184
|
+
return {"tools": sorted(p.name for p in TOOLBOX.iterdir() if p.is_file() and os.access(p, os.X_OK)) if TOOLBOX.is_dir() else []}
|
|
185
|
+
if not isinstance(name, str) or not FILE_NAME.fullmatch(name) or not (TOOLBOX / name).is_file():
|
|
186
|
+
raise Refused("tool: one of the names `toolbox` lists without a tool")
|
|
187
|
+
args = request.get("args", [])
|
|
188
|
+
if not isinstance(args, list) or not all(isinstance(a, str) for a in args):
|
|
189
|
+
raise Refused("args: a list of strings")
|
|
190
|
+
return run([str(TOOLBOX / name), *args], SCRIPT_TIMEOUT, cwd=thread_dir(session))
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def op_disk(request):
|
|
194
|
+
usage = shutil.disk_usage(HOME)
|
|
195
|
+
return {"freeBytes": usage.free, "threads": sorted(p.name for p in THREADS.iterdir() if p.is_dir()) if THREADS.is_dir() else []}
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
OPERATIONS = {"status": op_status, "browser": op_browser, "captcha": op_captcha, "script": op_script, "read": op_read,
|
|
199
|
+
"files": op_files, "toolbox": op_toolbox, "disk": op_disk}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def main():
|
|
203
|
+
os.umask(0o077)
|
|
204
|
+
try:
|
|
205
|
+
request = json.loads(sys.stdin.read(400_000) or "{}")
|
|
206
|
+
if not isinstance(request, dict) or request.get("op") not in OPERATIONS:
|
|
207
|
+
raise Refused("op: " + ", ".join(OPERATIONS))
|
|
208
|
+
answer(OPERATIONS[request["op"]](request))
|
|
209
|
+
except Refused as error:
|
|
210
|
+
answer({"refused": str(error)}, 2)
|
|
211
|
+
except (ValueError, OSError) as error:
|
|
212
|
+
answer({"failed": f"{type(error).__name__}: {error}"}, 1)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
if __name__ == "__main__":
|
|
216
|
+
main()
|