mlai-code 0.0.0-stage → 0.3.0
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/README.md +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"""Interactive terminal tabs for cloud chats.
|
|
2
|
+
|
|
3
|
+
Each tab is a login shell on a pseudo-terminal, kept by a small detached daemon inside the sandbox. The web app talks
|
|
4
|
+
to it through short commands (POST /api/workspace pty_*): keystrokes go into a fifo, output is appended to a file that
|
|
5
|
+
the app reads from a byte offset, and the window size is a file the daemon watches. A tab ends when its shell exits,
|
|
6
|
+
when it is closed, or after IDLE seconds with nobody reading it.
|
|
7
|
+
|
|
8
|
+
cloud_pty.py open <id> <cols> <rows> <cwd> [title]
|
|
9
|
+
cloud_pty.py io <id> <offset> <wait_ms> [input_base64] [cols rows]
|
|
10
|
+
cloud_pty.py rename <id> <title>
|
|
11
|
+
cloud_pty.py close <id>
|
|
12
|
+
cloud_pty.py list
|
|
13
|
+
Every command prints one JSON object.
|
|
14
|
+
"""
|
|
15
|
+
import base64, errno, fcntl, json, os, pty, re, select, shutil, signal, struct, sys, termios, time
|
|
16
|
+
|
|
17
|
+
BASE = os.environ.get('MLAI_PTY_DIR') or os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pty')
|
|
18
|
+
ID = re.compile(r'^t[a-z0-9]{6,24}$')
|
|
19
|
+
OUT_MAX, OUT_KEEP = 4 * 1024 * 1024, 1024 * 1024
|
|
20
|
+
READ_MAX = 256 * 1024
|
|
21
|
+
IDLE = int(os.environ.get('MLAI_PTY_IDLE') or 1800)
|
|
22
|
+
MAX_TABS = 8
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def out(v):
|
|
26
|
+
sys.stdout.write(json.dumps(v))
|
|
27
|
+
sys.stdout.flush()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def path(i, name=''):
|
|
31
|
+
if not ID.match(i or ''):
|
|
32
|
+
raise ValueError('Unknown terminal tab')
|
|
33
|
+
return os.path.join(BASE, i, name) if name else os.path.join(BASE, i)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def read(p, default=''):
|
|
37
|
+
try:
|
|
38
|
+
with open(p) as f:
|
|
39
|
+
return f.read()
|
|
40
|
+
except OSError:
|
|
41
|
+
return default
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def write(p, text):
|
|
45
|
+
tmp = p + '.tmp'
|
|
46
|
+
with open(tmp, 'w') as f:
|
|
47
|
+
f.write(text)
|
|
48
|
+
os.replace(tmp, p)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def clamp(v, lo, hi, d):
|
|
52
|
+
try:
|
|
53
|
+
return max(lo, min(hi, int(v)))
|
|
54
|
+
except (TypeError, ValueError):
|
|
55
|
+
return d
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def alive(pid):
|
|
59
|
+
try:
|
|
60
|
+
os.kill(pid, 0)
|
|
61
|
+
return True
|
|
62
|
+
except (OSError, ValueError, TypeError):
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def pids(i):
|
|
67
|
+
try:
|
|
68
|
+
d, c = read(path(i, 'pid')).split()
|
|
69
|
+
return int(d), int(c)
|
|
70
|
+
except ValueError:
|
|
71
|
+
return 0, 0
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def winsize(fd, cols, rows):
|
|
75
|
+
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def sanitize_title(t):
|
|
79
|
+
return re.sub(r'[\x00-\x1f\x7f]', '', str(t or '')).strip()[:40]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def daemon(i, cwd, cols, rows):
|
|
83
|
+
"""The long-lived side: one shell on a pty, copying between it and the tab's files."""
|
|
84
|
+
d = path(i)
|
|
85
|
+
null = os.open(os.devnull, os.O_RDWR)
|
|
86
|
+
for fd in (0, 1, 2):
|
|
87
|
+
os.dup2(null, fd)
|
|
88
|
+
child, master = pty.fork()
|
|
89
|
+
if child == 0:
|
|
90
|
+
try:
|
|
91
|
+
os.chdir(cwd)
|
|
92
|
+
except OSError:
|
|
93
|
+
pass
|
|
94
|
+
env = dict(os.environ, TERM='xterm-256color', COLORTERM='truecolor', TERM_PROGRAM='MLAICode')
|
|
95
|
+
for k in ('MLAI_PTY_DIR', 'MLAI_PTY_IDLE'):
|
|
96
|
+
env.pop(k, None)
|
|
97
|
+
sh = shutil.which('bash') or '/bin/sh'
|
|
98
|
+
os.execvpe(sh, [sh, '-l'] if sh.endswith('bash') else [sh], env)
|
|
99
|
+
winsize(master, cols, rows)
|
|
100
|
+
write(os.path.join(d, 'pid'), '%d %d' % (os.getpid(), child))
|
|
101
|
+
fin = os.path.join(d, 'in')
|
|
102
|
+
rin = os.open(fin, os.O_RDONLY | os.O_NONBLOCK)
|
|
103
|
+
keep = os.open(fin, os.O_WRONLY | os.O_NONBLOCK) # holds the fifo open, so a reader never sees end of file
|
|
104
|
+
fout = open(os.path.join(d, 'out'), 'ab')
|
|
105
|
+
base = 0
|
|
106
|
+
size_file, seen_file = os.path.join(d, 'size'), os.path.join(d, 'seen')
|
|
107
|
+
size_at = 0.0
|
|
108
|
+
last = time.time()
|
|
109
|
+
code = None
|
|
110
|
+
try:
|
|
111
|
+
while True:
|
|
112
|
+
r, _, _ = select.select([master, rin], [], [], 0.5)
|
|
113
|
+
if master in r:
|
|
114
|
+
try:
|
|
115
|
+
data = os.read(master, 65536)
|
|
116
|
+
except OSError as e:
|
|
117
|
+
if e.errno != errno.EIO:
|
|
118
|
+
raise
|
|
119
|
+
data = b''
|
|
120
|
+
if not data:
|
|
121
|
+
break
|
|
122
|
+
fout.write(data)
|
|
123
|
+
fout.flush()
|
|
124
|
+
if fout.tell() > OUT_MAX:
|
|
125
|
+
# Keep the recent output only; `base` is the absolute offset of the file's first byte.
|
|
126
|
+
total = base + fout.tell()
|
|
127
|
+
fout.close()
|
|
128
|
+
with open(os.path.join(d, 'out'), 'rb') as f:
|
|
129
|
+
f.seek(-OUT_KEEP, 2)
|
|
130
|
+
tail = f.read()
|
|
131
|
+
base = total - len(tail)
|
|
132
|
+
write(os.path.join(d, 'base'), str(base))
|
|
133
|
+
with open(os.path.join(d, 'out'), 'wb') as f:
|
|
134
|
+
f.write(tail)
|
|
135
|
+
fout = open(os.path.join(d, 'out'), 'ab')
|
|
136
|
+
if rin in r:
|
|
137
|
+
try:
|
|
138
|
+
data = os.read(rin, 65536)
|
|
139
|
+
except BlockingIOError:
|
|
140
|
+
data = b''
|
|
141
|
+
if data:
|
|
142
|
+
os.write(master, data)
|
|
143
|
+
last = time.time()
|
|
144
|
+
try:
|
|
145
|
+
m = os.path.getmtime(size_file)
|
|
146
|
+
if m != size_at:
|
|
147
|
+
size_at = m
|
|
148
|
+
c, rw = read(size_file, '').split()
|
|
149
|
+
winsize(master, clamp(c, 10, 500, 100), clamp(rw, 4, 200, 24))
|
|
150
|
+
except (OSError, ValueError):
|
|
151
|
+
pass
|
|
152
|
+
try:
|
|
153
|
+
last = max(last, os.path.getmtime(seen_file))
|
|
154
|
+
except OSError:
|
|
155
|
+
pass
|
|
156
|
+
done, status = os.waitpid(child, os.WNOHANG)
|
|
157
|
+
if done:
|
|
158
|
+
code = os.waitstatus_to_exitcode(status) if hasattr(os, 'waitstatus_to_exitcode') else status >> 8
|
|
159
|
+
# Drain what the shell printed last.
|
|
160
|
+
try:
|
|
161
|
+
while select.select([master], [], [], 0.05)[0]:
|
|
162
|
+
data = os.read(master, 65536)
|
|
163
|
+
if not data:
|
|
164
|
+
break
|
|
165
|
+
fout.write(data)
|
|
166
|
+
except OSError:
|
|
167
|
+
pass
|
|
168
|
+
break
|
|
169
|
+
if time.time() - last > IDLE:
|
|
170
|
+
fout.write(b'\r\n[Closed after %d minutes without use]\r\n' % (IDLE // 60))
|
|
171
|
+
break
|
|
172
|
+
finally:
|
|
173
|
+
fout.flush()
|
|
174
|
+
if code is None:
|
|
175
|
+
try:
|
|
176
|
+
os.killpg(child, signal.SIGHUP)
|
|
177
|
+
except OSError:
|
|
178
|
+
pass
|
|
179
|
+
try:
|
|
180
|
+
_, status = os.waitpid(child, 0)
|
|
181
|
+
code = status >> 8
|
|
182
|
+
except OSError:
|
|
183
|
+
code = -1
|
|
184
|
+
write(os.path.join(d, 'exit'), str(code))
|
|
185
|
+
for fd in (rin, keep, master):
|
|
186
|
+
try:
|
|
187
|
+
os.close(fd)
|
|
188
|
+
except OSError:
|
|
189
|
+
pass
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def live(i):
|
|
193
|
+
d, c = pids(i)
|
|
194
|
+
return bool(d) and alive(d) and not os.path.exists(path(i, 'exit'))
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def tab_info(i):
|
|
198
|
+
return {'id': i, 'title': read(path(i, 'title')).strip() or 'bash', 'live': live(i)}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def cmd_open(i, cols, rows, cwd, title=''):
|
|
202
|
+
d = path(i)
|
|
203
|
+
os.makedirs(BASE, exist_ok=True)
|
|
204
|
+
tidy()
|
|
205
|
+
if os.path.isdir(d) and live(i):
|
|
206
|
+
return tab_info(i)
|
|
207
|
+
if sum(1 for x in os.listdir(BASE) if ID.match(x) and live(x)) >= MAX_TABS:
|
|
208
|
+
raise ValueError('Close a terminal tab first. Up to %d are open at once.' % MAX_TABS)
|
|
209
|
+
shutil.rmtree(d, ignore_errors=True)
|
|
210
|
+
os.makedirs(d)
|
|
211
|
+
os.mkfifo(os.path.join(d, 'in'), 0o600)
|
|
212
|
+
open(os.path.join(d, 'out'), 'wb').close()
|
|
213
|
+
write(os.path.join(d, 'base'), '0')
|
|
214
|
+
write(os.path.join(d, 'title'), sanitize_title(title) or 'bash')
|
|
215
|
+
cols, rows = clamp(cols, 10, 500, 100), clamp(rows, 4, 200, 24)
|
|
216
|
+
pid = os.fork()
|
|
217
|
+
if pid == 0:
|
|
218
|
+
os.setsid()
|
|
219
|
+
if os.fork() == 0:
|
|
220
|
+
try:
|
|
221
|
+
daemon(i, cwd, cols, rows)
|
|
222
|
+
finally:
|
|
223
|
+
os._exit(0)
|
|
224
|
+
os._exit(0)
|
|
225
|
+
os.waitpid(pid, 0)
|
|
226
|
+
for _ in range(100):
|
|
227
|
+
if pids(i)[0]:
|
|
228
|
+
break
|
|
229
|
+
time.sleep(0.02)
|
|
230
|
+
if not live(i):
|
|
231
|
+
raise ValueError('The terminal could not start.')
|
|
232
|
+
return tab_info(i)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def snapshot(i, offset):
|
|
236
|
+
"""Output from an absolute offset (bounded), with where it starts and ends."""
|
|
237
|
+
for _ in range(3):
|
|
238
|
+
base = clamp(read(path(i, 'base'), '0'), 0, 1 << 62, 0)
|
|
239
|
+
try:
|
|
240
|
+
with open(path(i, 'out'), 'rb') as f:
|
|
241
|
+
f.seek(0, 2)
|
|
242
|
+
end = base + f.tell()
|
|
243
|
+
start = min(max(offset, base), end)
|
|
244
|
+
f.seek(start - base)
|
|
245
|
+
data = f.read(READ_MAX)
|
|
246
|
+
except OSError:
|
|
247
|
+
return offset, offset, b'', False
|
|
248
|
+
if clamp(read(path(i, 'base'), '0'), 0, 1 << 62, 0) == base:
|
|
249
|
+
return start, start + len(data), data, offset < base
|
|
250
|
+
return offset, offset, b'', False
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def cmd_io(i, offset, wait_ms, data_b64='', cols=None, rows=None):
|
|
254
|
+
d = path(i)
|
|
255
|
+
if not os.path.isdir(d) or not (live(i) or os.path.exists(path(i, 'exit'))):
|
|
256
|
+
return {'id': i, 'gone': True, 'from': offset, 'offset': offset, 'data': '', 'exited': True, 'code': None}
|
|
257
|
+
with open(path(i, 'seen'), 'a'):
|
|
258
|
+
os.utime(path(i, 'seen'), None)
|
|
259
|
+
if cols and rows:
|
|
260
|
+
write(path(i, 'size'), '%d %d' % (clamp(cols, 10, 500, 100), clamp(rows, 4, 200, 24)))
|
|
261
|
+
if data_b64:
|
|
262
|
+
raw = base64.b64decode(data_b64)
|
|
263
|
+
if raw and live(i):
|
|
264
|
+
fd = os.open(path(i, 'in'), os.O_WRONLY | os.O_NONBLOCK)
|
|
265
|
+
try:
|
|
266
|
+
view = memoryview(raw)
|
|
267
|
+
while view:
|
|
268
|
+
try:
|
|
269
|
+
n = os.write(fd, view)
|
|
270
|
+
view = view[n:]
|
|
271
|
+
except BlockingIOError:
|
|
272
|
+
time.sleep(0.01)
|
|
273
|
+
finally:
|
|
274
|
+
os.close(fd)
|
|
275
|
+
offset = max(0, int(offset))
|
|
276
|
+
deadline = time.time() + max(0, min(int(wait_ms), 4000)) / 1000.0
|
|
277
|
+
while True:
|
|
278
|
+
start, end, data, skipped = snapshot(i, offset)
|
|
279
|
+
exited = os.path.exists(path(i, 'exit'))
|
|
280
|
+
if data or exited or time.time() >= deadline:
|
|
281
|
+
break
|
|
282
|
+
time.sleep(0.03)
|
|
283
|
+
if exited:
|
|
284
|
+
start, end, data, skipped = snapshot(i, offset)
|
|
285
|
+
code = read(path(i, 'exit')).strip() if exited else None
|
|
286
|
+
# Exited is reported once the reader has everything the shell printed.
|
|
287
|
+
caught_up = exited and not snapshot(i, end)[2]
|
|
288
|
+
return {'id': i, 'from': start, 'offset': end, 'data': base64.b64encode(data).decode(), 'skipped': skipped,
|
|
289
|
+
'exited': caught_up, 'code': int(code) if caught_up and code and code.lstrip('-').isdigit() else None}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def cmd_close(i):
|
|
293
|
+
d, c = pids(i)
|
|
294
|
+
for p, sig in ((c, signal.SIGHUP), (d, signal.SIGTERM)):
|
|
295
|
+
if p:
|
|
296
|
+
try:
|
|
297
|
+
os.killpg(p, sig) if p == c else os.kill(p, sig)
|
|
298
|
+
except OSError:
|
|
299
|
+
pass
|
|
300
|
+
shutil.rmtree(path(i), ignore_errors=True)
|
|
301
|
+
return {'ok': True}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def tidy():
|
|
305
|
+
"""Forget tabs whose shell ended over an hour ago."""
|
|
306
|
+
try:
|
|
307
|
+
names = os.listdir(BASE)
|
|
308
|
+
except OSError:
|
|
309
|
+
return
|
|
310
|
+
for x in names:
|
|
311
|
+
if ID.match(x) and os.path.exists(path(x, 'exit')) and time.time() - os.path.getmtime(path(x, 'exit')) > 3600:
|
|
312
|
+
shutil.rmtree(path(x), ignore_errors=True)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def cmd_list():
|
|
316
|
+
tidy()
|
|
317
|
+
try:
|
|
318
|
+
names = sorted(os.listdir(BASE), key=lambda x: os.path.getmtime(os.path.join(BASE, x)))
|
|
319
|
+
except OSError:
|
|
320
|
+
names = []
|
|
321
|
+
return {'tabs': [tab_info(x) for x in names if ID.match(x) and live(x)]}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def main(argv):
|
|
325
|
+
try:
|
|
326
|
+
op = argv[0] if argv else ''
|
|
327
|
+
if op == 'open':
|
|
328
|
+
r = cmd_open(argv[1], argv[2], argv[3], argv[4], argv[5] if len(argv) > 5 else '')
|
|
329
|
+
elif op == 'io':
|
|
330
|
+
r = cmd_io(argv[1], int(argv[2]), int(argv[3]), argv[4] if len(argv) > 4 else '',
|
|
331
|
+
argv[5] if len(argv) > 6 else None, argv[6] if len(argv) > 6 else None)
|
|
332
|
+
elif op == 'rename':
|
|
333
|
+
path(argv[1])
|
|
334
|
+
t = sanitize_title(argv[2] if len(argv) > 2 else '')
|
|
335
|
+
if not t:
|
|
336
|
+
raise ValueError('Enter a name for the tab.')
|
|
337
|
+
write(path(argv[1], 'title'), t)
|
|
338
|
+
r = tab_info(argv[1])
|
|
339
|
+
elif op == 'close':
|
|
340
|
+
r = cmd_close(argv[1])
|
|
341
|
+
elif op == 'list':
|
|
342
|
+
r = cmd_list()
|
|
343
|
+
else:
|
|
344
|
+
raise ValueError('Unknown terminal operation')
|
|
345
|
+
except (ValueError, IndexError, OSError) as e:
|
|
346
|
+
r = {'error': str(e) or 'Terminal operation failed'}
|
|
347
|
+
out(r)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
if __name__ == '__main__':
|
|
351
|
+
main(sys.argv[1:])
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Deterministic command policies that do not require the worker runtime."""
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
_OFFLINE_TASK = re.compile(
|
|
6
|
+
r"\b(?:work|working)\s+offline\b|\boffline\s+(?:task|environment|sandbox|setup)\b",
|
|
7
|
+
re.IGNORECASE,
|
|
8
|
+
)
|
|
9
|
+
_NETWORK_PROBE = re.compile(
|
|
10
|
+
r"\b(?:curl|wget|nslookup|dig)\b|\bgetent\s+hosts\b|socket\.gethostbyname|https?://",
|
|
11
|
+
re.IGNORECASE,
|
|
12
|
+
)
|
|
13
|
+
_RETRY_LOOP = re.compile(
|
|
14
|
+
r"\bwhile\s+(?:true|:)\b|\bfor\s+\w+\s+in\s+(?:\$\()?seq\b",
|
|
15
|
+
re.IGNORECASE,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def offline_network_wait_reason(command, task):
|
|
20
|
+
"""Reject only long/repeated public-network waits for explicitly offline tasks.
|
|
21
|
+
|
|
22
|
+
One bounded probe remains available for useful diagnosis. Local long-running tests,
|
|
23
|
+
builds, and background jobs are unaffected.
|
|
24
|
+
"""
|
|
25
|
+
command = str(command or "")
|
|
26
|
+
task = str(task or "")
|
|
27
|
+
if not _OFFLINE_TASK.search(task) or not _NETWORK_PROBE.search(command):
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
sleeps = [float(value) for value in re.findall(r"\bsleep\s+([0-9]+(?:\.[0-9]+)?)\b", command, re.I)]
|
|
31
|
+
timeouts = [float(value) for value in re.findall(r"\btimeout\s+([0-9]+(?:\.[0-9]+)?)(?:s)?\b", command, re.I)]
|
|
32
|
+
if not any(value >= 10 for value in sleeps) and not any(value >= 30 for value in timeouts) and not _RETRY_LOOP.search(command):
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
"This task is explicitly offline, so a long or repeated public-network wait was not executed. "
|
|
37
|
+
"Use at most one bounded probe, inspect available dependency metadata/cache once, then implement "
|
|
38
|
+
"against the pinned contract and report any unresolved verification honestly."
|
|
39
|
+
)
|