scope-agent-workbench 0.2.0 → 0.2.3
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/CHANGELOG.md +41 -0
- package/bridge/agent-email-relay.mjs +160 -160
- package/bridge/app-server-websocket-transport.mjs +219 -219
- package/bridge/archive/service.mjs +130 -130
- package/bridge/auth-profile-failover.mjs +58 -58
- package/bridge/auth-profile-manager.mjs +215 -215
- package/bridge/capacity-recovery.mjs +90 -90
- package/bridge/gpu-usage-history.mjs +322 -322
- package/bridge/local/history.mjs +20 -5
- package/bridge/plugins/aliyun-dlc/monitor.mjs +236 -236
- package/bridge/plugins/aliyun-dlc/probe.mjs +701 -701
- package/bridge/private-gateway.mjs +371 -371
- package/bridge/private-start.mjs +22 -22
- package/bridge/probes/app-server-handoff-probe.mjs +87 -87
- package/bridge/probes/app-server-writer-probe.mjs +69 -69
- package/bridge/probes/assistant-search-probe.mjs +321 -321
- package/bridge/probes/dialogue-probe.mjs +462 -462
- package/bridge/probes/history-probe.mjs +430 -345
- package/bridge/probes/new-session-preflight.mjs +14 -14
- package/bridge/probes/profile-cancel-probe.mjs +17 -17
- package/bridge/probes/profile-delete-probe.mjs +26 -26
- package/bridge/probes/profile-list-probe.mjs +114 -114
- package/bridge/probes/profile-write-probe.mjs +109 -109
- package/bridge/probes/raw-context-probe.mjs +51 -51
- package/bridge/probes/remote-probe.mjs +627 -626
- package/bridge/probes/submission-probe.mjs +2 -2
- package/bridge/probes/takeover-probe.mjs +326 -326
- package/bridge/probes/vscode-open-preflight.mjs +21 -21
- package/bridge/providers/claude-interactive.py +463 -341
- package/bridge/providers/cli-command.mjs +103 -103
- package/bridge/providers/conversations.mjs +231 -231
- package/bridge/providers/native-commands.py +38 -38
- package/bridge/providers/native-session-state.mjs +14 -14
- package/bridge/providers/native-session-state.py +96 -96
- package/bridge/providers/remote-job.py +130 -130
- package/bridge/remote-app-server-command.mjs +121 -121
- package/bridge/remote-directory-listing.mjs +148 -148
- package/bridge/rollout-summary.mjs +20 -2
- package/bridge/routes/dialogue.mjs +48 -3
- package/bridge/routes/models.mjs +312 -312
- package/bridge/server.mjs +3357 -3332
- package/bridge/session-attachments.mjs +150 -150
- package/bridge/session-catalog/agy-history.py +141 -2
- package/bridge/session-catalog/catalog.mjs +17 -0
- package/bridge/session-catalog/probe.py +1207 -331
- package/bridge/session-catalog/routes.mjs +6 -0
- package/bridge/start.mjs +205 -205
- package/bridge/state-persistence.mjs +9 -1
- package/bridge/tmux-session-control.mjs +29 -29
- package/bridge/vscode-opener.mjs +157 -157
- package/bridge/web-terminal.mjs +52 -52
- package/bridge/workbench/manager-context.mjs +51 -51
- package/bridge/workbench/service.mjs +301 -301
- package/dist/client/.vite/manifest.json +4 -4
- package/dist/client/assets/{index-Enu1x0PS.js → index-BMior55c.js} +2 -2
- package/dist/client/assets/{layout-segment-context-CmJzExWq.js → layout-segment-context-DjGudgXn.js} +1 -1
- package/dist/client/assets/page-CJqXlhDs.js +551 -0
- package/dist/client/assets/page-LYzRz6_S.css +1 -0
- package/dist/client/favicon.svg +6 -6
- package/dist/server/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/index.js +7 -7
- package/dist/server/ssr/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/ssr/assets/{page-CYYyCnKD.js → page-BB9Dri2u.js} +2416 -271
- package/dist/server/ssr/assets/page-LYzRz6_S.css +1 -0
- package/dist/server/ssr/index.js +1 -1
- package/dist/server/ssr/vinext-server.json +1 -1
- package/dist/server/vinext-server.json +1 -1
- package/dist/server/wrangler.json +1 -1
- package/lib/browser-snapshot-cache.js +53 -53
- package/lib/formula-preview.js +1 -1
- package/lib/incremental-snapshot.js +300 -300
- package/lib/markdown-math.js +151 -151
- package/lib/remote-links.js +95 -95
- package/lib/runtime-config.js +70 -70
- package/lib/session-filters.js +38 -38
- package/lib/ssh-config.js +26 -26
- package/lib/stable-order.js +39 -39
- package/lib/subagent-display-prefs.ts +130 -0
- package/package.json +1 -1
- package/scripts/agent-email-relay.mjs +104 -104
- package/scripts/email-notification-cli.mjs +28 -28
- package/scripts/enable-private-access.ps1 +109 -109
- package/scripts/fixtures/native-conversations.mjs +164 -6
- package/scripts/fixtures/workbench-conversations.mjs +475 -352
- package/scripts/preview-workbench.mjs +669 -669
- package/dist/client/assets/page-COBv6VWj.css +0 -1
- package/dist/client/assets/page-DVjU6VG_.js +0 -550
- package/dist/server/ssr/assets/page-COBv6VWj.css +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// Compatibility export; implementation belongs to the Aliyun plugin.
|
|
2
|
-
export { SUBMISSION_PROBE } from "../plugins/aliyun-dlc/probe.mjs";
|
|
1
|
+
// Compatibility export; implementation belongs to the Aliyun plugin.
|
|
2
|
+
export { SUBMISSION_PROBE } from "../plugins/aliyun-dlc/probe.mjs";
|
|
@@ -1,328 +1,328 @@
|
|
|
1
1
|
/** Remote probe, kept independent of HTTP and local mutable state. */
|
|
2
|
-
export const TAKEOVER_PROBE = String.raw`
|
|
3
|
-
import glob
|
|
4
|
-
import json
|
|
5
|
-
import os
|
|
6
|
-
import shlex
|
|
7
|
-
import shutil
|
|
8
|
-
import subprocess
|
|
9
|
-
import sys
|
|
10
|
-
import time
|
|
11
|
-
|
|
12
|
-
pid = int(sys.argv[1])
|
|
13
|
-
expected_rollout = os.path.realpath(sys.argv[2])
|
|
14
|
-
expected_cwd = os.path.realpath(sys.argv[3])
|
|
15
|
-
executable = sys.argv[4] or "codex"
|
|
16
|
-
session_id = sys.argv[5]
|
|
17
|
-
tmux_name = sys.argv[6]
|
|
18
|
-
pre_launch_command = sys.argv[7] if len(sys.argv) > 7 else ""
|
|
19
|
-
model = sys.argv[8] if len(sys.argv) > 8 else ""
|
|
20
|
-
yolo = len(sys.argv) <= 9 or sys.argv[9] != "0"
|
|
21
|
-
|
|
22
|
-
def fail(message, code=1):
|
|
23
|
-
print(message, file=sys.stderr)
|
|
24
|
-
raise SystemExit(code)
|
|
25
|
-
|
|
26
|
-
def process_alive(target_pid):
|
|
27
|
-
stat_path = f"/proc/{target_pid}/stat"
|
|
28
|
-
try:
|
|
29
|
-
with open(stat_path, "r", encoding="utf-8", errors="replace") as handle:
|
|
30
|
-
fields = handle.read().split()
|
|
31
|
-
return len(fields) > 2 and fields[2] != "Z"
|
|
32
|
-
except Exception:
|
|
33
|
-
return False
|
|
34
|
-
|
|
35
|
-
def rollout_owners():
|
|
36
|
-
owners = []
|
|
37
|
-
for fd_path in glob.glob("/proc/[0-9]*/fd/*"):
|
|
38
|
-
try:
|
|
39
|
-
target = os.path.realpath(os.readlink(fd_path))
|
|
40
|
-
except Exception:
|
|
41
|
-
continue
|
|
42
|
-
if target == expected_rollout:
|
|
43
|
-
try:
|
|
44
|
-
owners.append(int(fd_path.split("/")[2]))
|
|
45
|
-
except Exception:
|
|
46
|
-
pass
|
|
47
|
-
return sorted(set(owners))
|
|
48
|
-
|
|
49
|
-
if pid > 0:
|
|
50
|
-
fail("安全策略禁止终止活动 CLI/VS Code Session", 23)
|
|
51
|
-
owners = rollout_owners()
|
|
52
|
-
if owners:
|
|
53
|
-
fail(
|
|
54
|
-
"该 Session 已由 PID " + ",".join(map(str, owners)) + " 打开;请刷新页面",
|
|
55
|
-
17,
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
if not os.path.isdir(expected_cwd):
|
|
59
|
-
fail(f"工作目录不存在:{expected_cwd}", 18)
|
|
60
|
-
|
|
61
|
-
def pane_processes(root_pid):
|
|
62
|
-
records = {}
|
|
63
|
-
for stat_path in glob.glob("/proc/[0-9]*/stat"):
|
|
64
|
-
try:
|
|
65
|
-
process_pid = int(stat_path.split("/")[2])
|
|
66
|
-
fields = open(
|
|
67
|
-
stat_path, "r", encoding="utf-8", errors="replace"
|
|
68
|
-
).read().split()
|
|
69
|
-
parent_pid = int(fields[3])
|
|
70
|
-
cmdline = open(
|
|
71
|
-
f"/proc/{process_pid}/cmdline", "rb"
|
|
72
|
-
).read().replace(b"\0", b" ").decode("utf-8", errors="replace")
|
|
73
|
-
records[process_pid] = (parent_pid, cmdline)
|
|
74
|
-
except Exception:
|
|
75
|
-
continue
|
|
76
|
-
descendants = []
|
|
77
|
-
pending = [root_pid]
|
|
78
|
-
seen = set()
|
|
79
|
-
while pending:
|
|
80
|
-
parent = pending.pop()
|
|
81
|
-
if parent in seen:
|
|
82
|
-
continue
|
|
83
|
-
seen.add(parent)
|
|
84
|
-
if parent in records:
|
|
85
|
-
descendants.append((parent, records[parent][1]))
|
|
86
|
-
pending.extend(
|
|
87
|
-
process_pid
|
|
88
|
-
for process_pid, (parent_pid, _) in records.items()
|
|
89
|
-
if parent_pid == parent and process_pid not in seen
|
|
90
|
-
)
|
|
91
|
-
return descendants
|
|
92
|
-
|
|
93
|
-
def existing_tmux():
|
|
94
|
-
checked = subprocess.run(
|
|
95
|
-
[
|
|
96
|
-
"tmux",
|
|
97
|
-
"display-message",
|
|
98
|
-
"-p",
|
|
99
|
-
"-t",
|
|
100
|
-
f"{tmux_name}:0.0",
|
|
101
|
-
"#S:#I.#P\t#{pane_current_command}\t#{pane_pid}",
|
|
102
|
-
],
|
|
103
|
-
stdout=subprocess.PIPE,
|
|
104
|
-
stderr=subprocess.PIPE,
|
|
105
|
-
text=True,
|
|
106
|
-
)
|
|
107
|
-
if checked.returncode != 0:
|
|
108
|
-
return
|
|
109
|
-
fields = checked.stdout.strip().split("\t")
|
|
110
|
-
target = fields[0] if fields else f"{tmux_name}:0.0"
|
|
111
|
-
command = fields[1] if len(fields) > 1 else ""
|
|
112
|
-
try:
|
|
113
|
-
pane_pid = int(fields[2]) if len(fields) > 2 else 0
|
|
114
|
-
except Exception:
|
|
115
|
-
pane_pid = 0
|
|
116
|
-
processes = pane_processes(pane_pid)
|
|
117
|
-
exact_session = any(
|
|
118
|
-
session_id in cmdline
|
|
119
|
-
and ("codex" in cmdline or os.path.basename(executable) in cmdline)
|
|
120
|
-
for _, cmdline in processes
|
|
121
|
-
)
|
|
122
|
-
if not exact_session:
|
|
123
|
-
fail(
|
|
124
|
-
f"tmux {tmux_name} 已存在但不属于 Session {session_id};为保护现有进程,拒绝覆盖或终止",
|
|
125
|
-
28,
|
|
126
|
-
)
|
|
127
|
-
preview = subprocess.run(
|
|
128
|
-
["tmux", "capture-pane", "-p", "-S", "-20", "-t", target],
|
|
129
|
-
stdout=subprocess.PIPE,
|
|
130
|
-
stderr=subprocess.DEVNULL,
|
|
131
|
-
text=True,
|
|
132
|
-
).stdout.strip()[-1600:]
|
|
133
|
-
print(json.dumps({
|
|
134
|
-
"ok": True,
|
|
135
|
-
"killedPid": None,
|
|
136
|
-
"forced": False,
|
|
137
|
-
"reused": True,
|
|
138
|
-
"tmux": tmux_name,
|
|
139
|
-
"target": target,
|
|
140
|
-
"paneCommand": command,
|
|
141
|
-
"panePid": pane_pid,
|
|
142
|
-
"readyInMs": 0,
|
|
143
|
-
"cwdChoice": "existing",
|
|
144
|
-
"preview": preview,
|
|
145
|
-
"cwd": expected_cwd,
|
|
146
|
-
"sessionId": session_id,
|
|
147
|
-
}, ensure_ascii=False, separators=(",", ":")))
|
|
148
|
-
raise SystemExit(0)
|
|
149
|
-
|
|
150
|
-
# Recovery is idempotent. Never kill an existing tmux to make room: that pane
|
|
151
|
-
# may own an active Codex turn or a child experiment.
|
|
152
|
-
existing_tmux()
|
|
153
|
-
resume_command = (
|
|
154
|
-
"exec "
|
|
155
|
-
+ shlex.quote(executable)
|
|
156
|
-
+ (" --yolo" if yolo else " --ask-for-approval on-request --sandbox workspace-write")
|
|
157
|
-
+ ((" --model " + shlex.quote(model)) if model else "")
|
|
158
|
-
+ " resume "
|
|
159
|
-
+ shlex.quote(session_id)
|
|
160
|
-
)
|
|
161
|
-
create_args = [
|
|
162
|
-
"tmux",
|
|
163
|
-
"new-session",
|
|
164
|
-
"-d",
|
|
165
|
-
"-s",
|
|
166
|
-
tmux_name,
|
|
167
|
-
"-c",
|
|
168
|
-
expected_cwd,
|
|
169
|
-
]
|
|
170
|
-
if pre_launch_command:
|
|
171
|
-
zsh_handler = "; ".join([
|
|
172
|
-
pre_launch_command,
|
|
173
|
-
resume_command,
|
|
174
|
-
])
|
|
175
|
-
bash_handler = "; ".join([
|
|
176
|
-
"source ~/.bashrc >/dev/null 2>&1 || true",
|
|
177
|
-
pre_launch_command,
|
|
178
|
-
resume_command,
|
|
179
|
-
])
|
|
180
|
-
if shutil.which("zsh"):
|
|
181
|
-
create_args.append("exec zsh -ic " + shlex.quote(zsh_handler))
|
|
182
|
-
else:
|
|
183
|
-
create_args.append("exec bash -ic " + shlex.quote(bash_handler))
|
|
184
|
-
else:
|
|
185
|
-
create_args.append(resume_command)
|
|
186
|
-
created = subprocess.run(
|
|
187
|
-
create_args,
|
|
188
|
-
stdout=subprocess.PIPE,
|
|
189
|
-
stderr=subprocess.PIPE,
|
|
190
|
-
text=True,
|
|
191
|
-
)
|
|
192
|
-
if created.returncode != 0:
|
|
193
|
-
fail("tmux 创建失败:" + (created.stderr.strip() or created.stdout.strip()), 19)
|
|
194
|
-
|
|
195
|
-
# zsh startup can legitimately spend a few seconds in conda/plugin handlers
|
|
196
|
-
# before its final exec. Treat that shell as transitional instead of failing a
|
|
197
|
-
# healthy takeover at the old six-second boundary.
|
|
198
|
-
deadline = time.time() + 15.0
|
|
199
|
-
checked = None
|
|
200
|
-
target = ""
|
|
201
|
-
pane_command = ""
|
|
202
|
-
pane_pid = 0
|
|
203
|
-
pane_target = f"{tmux_name}:0.0"
|
|
204
|
-
ready_started_at = time.time()
|
|
205
|
-
while time.time() < deadline:
|
|
206
|
-
checked = subprocess.run(
|
|
207
|
-
[
|
|
208
|
-
"tmux",
|
|
209
|
-
"display-message",
|
|
210
|
-
"-p",
|
|
211
|
-
"-t",
|
|
212
|
-
pane_target,
|
|
213
|
-
"#S:#I.#P\t#{pane_current_command}\t#{pane_pid}",
|
|
214
|
-
],
|
|
215
|
-
stdout=subprocess.PIPE,
|
|
216
|
-
stderr=subprocess.PIPE,
|
|
217
|
-
text=True,
|
|
218
|
-
)
|
|
219
|
-
if checked.returncode != 0:
|
|
220
|
-
fail(
|
|
221
|
-
"tmux 已创建但 Codex 立即退出:"
|
|
222
|
-
+ (checked.stderr.strip() or "请检查远端 Codex CLI")
|
|
223
|
-
+ f";已保留 tmux {tmux_name} 供检查",
|
|
224
|
-
20,
|
|
225
|
-
)
|
|
226
|
-
checked_fields = checked.stdout.strip().split("\t")
|
|
227
|
-
target = checked_fields[0] if checked_fields else ""
|
|
228
|
-
pane_command = checked_fields[1] if len(checked_fields) > 1 else ""
|
|
229
|
-
try:
|
|
230
|
-
pane_pid = int(checked_fields[2]) if len(checked_fields) > 2 else 0
|
|
231
|
-
except Exception:
|
|
232
|
-
pane_pid = 0
|
|
233
|
-
# npm/global Codex installations commonly exec a Node entrypoint, so tmux
|
|
234
|
-
# reports node even though the pane is the intended Codex process.
|
|
235
|
-
if pane_command in ("codex", "node"):
|
|
236
|
-
break
|
|
237
|
-
time.sleep(0.2)
|
|
238
|
-
if pane_command not in ("codex", "node"):
|
|
239
|
-
fail(
|
|
240
|
-
"tmux 已创建但 Codex 未在 15 秒内保持运行"
|
|
241
|
-
+ (f"(pane command: {pane_command})" if pane_command else "")
|
|
242
|
-
+ f";已保留 tmux {tmux_name} 供检查",
|
|
243
|
-
25,
|
|
244
|
-
)
|
|
245
|
-
|
|
246
|
-
# The Node process can exist before the TUI has finished restoring the thread.
|
|
247
|
-
# Inputs pasted during that gap are rendered briefly and then discarded by the
|
|
248
|
-
# final screen initialization, so do not report a usable tmux target yet.
|
|
249
|
-
ui_deadline = time.time() + 8.0
|
|
250
|
-
ui_ready = False
|
|
251
|
-
cwd_choice = "unchanged"
|
|
252
|
-
cwd_prompt_answered = False
|
|
253
|
-
preview = ""
|
|
254
|
-
while time.time() < ui_deadline:
|
|
255
|
-
captured = subprocess.run(
|
|
256
|
-
["tmux", "capture-pane", "-p", "-t", pane_target, "-S", "-80"],
|
|
257
|
-
stdout=subprocess.PIPE,
|
|
258
|
-
stderr=subprocess.PIPE,
|
|
259
|
-
text=True,
|
|
260
|
-
)
|
|
261
|
-
preview = captured.stdout
|
|
262
|
-
cwd_prompt = "Choose working directory to resume this session"
|
|
263
|
-
if (
|
|
264
|
-
captured.returncode == 0
|
|
265
|
-
and cwd_prompt in preview
|
|
266
|
-
and not cwd_prompt_answered
|
|
267
|
-
):
|
|
268
|
-
# Preserve the directory of the process being taken over. Rollouts are
|
|
269
|
-
# often created under /tmp and later continued from the real project or
|
|
270
|
-
# worktree, so choose option 2 (Current) instead of the original Session
|
|
271
|
-
# directory.
|
|
272
|
-
answered = subprocess.run(
|
|
273
|
-
["tmux", "send-keys", "-t", pane_target, "Down", "Enter"],
|
|
274
|
-
stdout=subprocess.PIPE,
|
|
275
|
-
stderr=subprocess.PIPE,
|
|
276
|
-
text=True,
|
|
277
|
-
)
|
|
278
|
-
if answered.returncode != 0:
|
|
279
|
-
fail(
|
|
280
|
-
"Codex 请求选择恢复目录,但无法确认当前工作目录:"
|
|
281
|
-
+ (answered.stderr.strip() or "tmux send-keys 失败")
|
|
282
|
-
+ f";已保留 tmux {tmux_name} 供检查",
|
|
283
|
-
27,
|
|
284
|
-
)
|
|
285
|
-
cwd_prompt_answered = True
|
|
286
|
-
cwd_choice = "current"
|
|
287
|
-
time.sleep(0.5)
|
|
288
|
-
continue
|
|
289
|
-
if (
|
|
290
|
-
captured.returncode == 0
|
|
291
|
-
and "OpenAI Codex" in preview
|
|
292
|
-
and cwd_prompt not in preview
|
|
293
|
-
):
|
|
294
|
-
# Let the input editor finish replacing the startup screen.
|
|
295
|
-
time.sleep(0.5)
|
|
296
|
-
ui_ready = True
|
|
297
|
-
break
|
|
298
|
-
time.sleep(0.2)
|
|
299
|
-
if not ui_ready:
|
|
300
|
-
fail(
|
|
301
|
-
"tmux 已创建但 Codex TUI 未在 8 秒内就绪"
|
|
302
|
-
+ f";已保留 tmux {tmux_name} 供检查",
|
|
303
|
-
26,
|
|
304
|
-
)
|
|
305
|
-
|
|
306
|
-
captured = subprocess.run(
|
|
307
|
-
["tmux", "capture-pane", "-p", "-S", "-8", "-t", target],
|
|
308
|
-
stdout=subprocess.PIPE,
|
|
309
|
-
stderr=subprocess.DEVNULL,
|
|
310
|
-
text=True,
|
|
311
|
-
)
|
|
312
|
-
preview = captured.stdout.strip()[-1600:] if captured.returncode == 0 else ""
|
|
313
|
-
|
|
314
|
-
print(json.dumps({
|
|
315
|
-
"ok": True,
|
|
316
|
-
"killedPid": None,
|
|
317
|
-
"forced": False,
|
|
318
|
-
"tmux": tmux_name,
|
|
319
|
-
"target": target,
|
|
320
|
-
"paneCommand": pane_command,
|
|
321
|
-
"panePid": pane_pid,
|
|
322
|
-
"readyInMs": int((time.time() - ready_started_at) * 1000),
|
|
323
|
-
"cwdChoice": cwd_choice,
|
|
324
|
-
"preview": preview,
|
|
325
|
-
"cwd": expected_cwd,
|
|
326
|
-
"sessionId": session_id,
|
|
327
|
-
}, ensure_ascii=False, separators=(",", ":")))
|
|
2
|
+
export const TAKEOVER_PROBE = String.raw`
|
|
3
|
+
import glob
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import shlex
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
pid = int(sys.argv[1])
|
|
13
|
+
expected_rollout = os.path.realpath(sys.argv[2])
|
|
14
|
+
expected_cwd = os.path.realpath(sys.argv[3])
|
|
15
|
+
executable = sys.argv[4] or "codex"
|
|
16
|
+
session_id = sys.argv[5]
|
|
17
|
+
tmux_name = sys.argv[6]
|
|
18
|
+
pre_launch_command = sys.argv[7] if len(sys.argv) > 7 else ""
|
|
19
|
+
model = sys.argv[8] if len(sys.argv) > 8 else ""
|
|
20
|
+
yolo = len(sys.argv) <= 9 or sys.argv[9] != "0"
|
|
21
|
+
|
|
22
|
+
def fail(message, code=1):
|
|
23
|
+
print(message, file=sys.stderr)
|
|
24
|
+
raise SystemExit(code)
|
|
25
|
+
|
|
26
|
+
def process_alive(target_pid):
|
|
27
|
+
stat_path = f"/proc/{target_pid}/stat"
|
|
28
|
+
try:
|
|
29
|
+
with open(stat_path, "r", encoding="utf-8", errors="replace") as handle:
|
|
30
|
+
fields = handle.read().split()
|
|
31
|
+
return len(fields) > 2 and fields[2] != "Z"
|
|
32
|
+
except Exception:
|
|
33
|
+
return False
|
|
34
|
+
|
|
35
|
+
def rollout_owners():
|
|
36
|
+
owners = []
|
|
37
|
+
for fd_path in glob.glob("/proc/[0-9]*/fd/*"):
|
|
38
|
+
try:
|
|
39
|
+
target = os.path.realpath(os.readlink(fd_path))
|
|
40
|
+
except Exception:
|
|
41
|
+
continue
|
|
42
|
+
if target == expected_rollout:
|
|
43
|
+
try:
|
|
44
|
+
owners.append(int(fd_path.split("/")[2]))
|
|
45
|
+
except Exception:
|
|
46
|
+
pass
|
|
47
|
+
return sorted(set(owners))
|
|
48
|
+
|
|
49
|
+
if pid > 0:
|
|
50
|
+
fail("安全策略禁止终止活动 CLI/VS Code Session", 23)
|
|
51
|
+
owners = rollout_owners()
|
|
52
|
+
if owners:
|
|
53
|
+
fail(
|
|
54
|
+
"该 Session 已由 PID " + ",".join(map(str, owners)) + " 打开;请刷新页面",
|
|
55
|
+
17,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
if not os.path.isdir(expected_cwd):
|
|
59
|
+
fail(f"工作目录不存在:{expected_cwd}", 18)
|
|
60
|
+
|
|
61
|
+
def pane_processes(root_pid):
|
|
62
|
+
records = {}
|
|
63
|
+
for stat_path in glob.glob("/proc/[0-9]*/stat"):
|
|
64
|
+
try:
|
|
65
|
+
process_pid = int(stat_path.split("/")[2])
|
|
66
|
+
fields = open(
|
|
67
|
+
stat_path, "r", encoding="utf-8", errors="replace"
|
|
68
|
+
).read().split()
|
|
69
|
+
parent_pid = int(fields[3])
|
|
70
|
+
cmdline = open(
|
|
71
|
+
f"/proc/{process_pid}/cmdline", "rb"
|
|
72
|
+
).read().replace(b"\0", b" ").decode("utf-8", errors="replace")
|
|
73
|
+
records[process_pid] = (parent_pid, cmdline)
|
|
74
|
+
except Exception:
|
|
75
|
+
continue
|
|
76
|
+
descendants = []
|
|
77
|
+
pending = [root_pid]
|
|
78
|
+
seen = set()
|
|
79
|
+
while pending:
|
|
80
|
+
parent = pending.pop()
|
|
81
|
+
if parent in seen:
|
|
82
|
+
continue
|
|
83
|
+
seen.add(parent)
|
|
84
|
+
if parent in records:
|
|
85
|
+
descendants.append((parent, records[parent][1]))
|
|
86
|
+
pending.extend(
|
|
87
|
+
process_pid
|
|
88
|
+
for process_pid, (parent_pid, _) in records.items()
|
|
89
|
+
if parent_pid == parent and process_pid not in seen
|
|
90
|
+
)
|
|
91
|
+
return descendants
|
|
92
|
+
|
|
93
|
+
def existing_tmux():
|
|
94
|
+
checked = subprocess.run(
|
|
95
|
+
[
|
|
96
|
+
"tmux",
|
|
97
|
+
"display-message",
|
|
98
|
+
"-p",
|
|
99
|
+
"-t",
|
|
100
|
+
f"{tmux_name}:0.0",
|
|
101
|
+
"#S:#I.#P\t#{pane_current_command}\t#{pane_pid}",
|
|
102
|
+
],
|
|
103
|
+
stdout=subprocess.PIPE,
|
|
104
|
+
stderr=subprocess.PIPE,
|
|
105
|
+
text=True,
|
|
106
|
+
)
|
|
107
|
+
if checked.returncode != 0:
|
|
108
|
+
return
|
|
109
|
+
fields = checked.stdout.strip().split("\t")
|
|
110
|
+
target = fields[0] if fields else f"{tmux_name}:0.0"
|
|
111
|
+
command = fields[1] if len(fields) > 1 else ""
|
|
112
|
+
try:
|
|
113
|
+
pane_pid = int(fields[2]) if len(fields) > 2 else 0
|
|
114
|
+
except Exception:
|
|
115
|
+
pane_pid = 0
|
|
116
|
+
processes = pane_processes(pane_pid)
|
|
117
|
+
exact_session = any(
|
|
118
|
+
session_id in cmdline
|
|
119
|
+
and ("codex" in cmdline or os.path.basename(executable) in cmdline)
|
|
120
|
+
for _, cmdline in processes
|
|
121
|
+
)
|
|
122
|
+
if not exact_session:
|
|
123
|
+
fail(
|
|
124
|
+
f"tmux {tmux_name} 已存在但不属于 Session {session_id};为保护现有进程,拒绝覆盖或终止",
|
|
125
|
+
28,
|
|
126
|
+
)
|
|
127
|
+
preview = subprocess.run(
|
|
128
|
+
["tmux", "capture-pane", "-p", "-S", "-20", "-t", target],
|
|
129
|
+
stdout=subprocess.PIPE,
|
|
130
|
+
stderr=subprocess.DEVNULL,
|
|
131
|
+
text=True,
|
|
132
|
+
).stdout.strip()[-1600:]
|
|
133
|
+
print(json.dumps({
|
|
134
|
+
"ok": True,
|
|
135
|
+
"killedPid": None,
|
|
136
|
+
"forced": False,
|
|
137
|
+
"reused": True,
|
|
138
|
+
"tmux": tmux_name,
|
|
139
|
+
"target": target,
|
|
140
|
+
"paneCommand": command,
|
|
141
|
+
"panePid": pane_pid,
|
|
142
|
+
"readyInMs": 0,
|
|
143
|
+
"cwdChoice": "existing",
|
|
144
|
+
"preview": preview,
|
|
145
|
+
"cwd": expected_cwd,
|
|
146
|
+
"sessionId": session_id,
|
|
147
|
+
}, ensure_ascii=False, separators=(",", ":")))
|
|
148
|
+
raise SystemExit(0)
|
|
149
|
+
|
|
150
|
+
# Recovery is idempotent. Never kill an existing tmux to make room: that pane
|
|
151
|
+
# may own an active Codex turn or a child experiment.
|
|
152
|
+
existing_tmux()
|
|
153
|
+
resume_command = (
|
|
154
|
+
"exec "
|
|
155
|
+
+ shlex.quote(executable)
|
|
156
|
+
+ (" --yolo" if yolo else " --ask-for-approval on-request --sandbox workspace-write")
|
|
157
|
+
+ ((" --model " + shlex.quote(model)) if model else "")
|
|
158
|
+
+ " resume "
|
|
159
|
+
+ shlex.quote(session_id)
|
|
160
|
+
)
|
|
161
|
+
create_args = [
|
|
162
|
+
"tmux",
|
|
163
|
+
"new-session",
|
|
164
|
+
"-d",
|
|
165
|
+
"-s",
|
|
166
|
+
tmux_name,
|
|
167
|
+
"-c",
|
|
168
|
+
expected_cwd,
|
|
169
|
+
]
|
|
170
|
+
if pre_launch_command:
|
|
171
|
+
zsh_handler = "; ".join([
|
|
172
|
+
pre_launch_command,
|
|
173
|
+
resume_command,
|
|
174
|
+
])
|
|
175
|
+
bash_handler = "; ".join([
|
|
176
|
+
"source ~/.bashrc >/dev/null 2>&1 || true",
|
|
177
|
+
pre_launch_command,
|
|
178
|
+
resume_command,
|
|
179
|
+
])
|
|
180
|
+
if shutil.which("zsh"):
|
|
181
|
+
create_args.append("exec zsh -ic " + shlex.quote(zsh_handler))
|
|
182
|
+
else:
|
|
183
|
+
create_args.append("exec bash -ic " + shlex.quote(bash_handler))
|
|
184
|
+
else:
|
|
185
|
+
create_args.append(resume_command)
|
|
186
|
+
created = subprocess.run(
|
|
187
|
+
create_args,
|
|
188
|
+
stdout=subprocess.PIPE,
|
|
189
|
+
stderr=subprocess.PIPE,
|
|
190
|
+
text=True,
|
|
191
|
+
)
|
|
192
|
+
if created.returncode != 0:
|
|
193
|
+
fail("tmux 创建失败:" + (created.stderr.strip() or created.stdout.strip()), 19)
|
|
194
|
+
|
|
195
|
+
# zsh startup can legitimately spend a few seconds in conda/plugin handlers
|
|
196
|
+
# before its final exec. Treat that shell as transitional instead of failing a
|
|
197
|
+
# healthy takeover at the old six-second boundary.
|
|
198
|
+
deadline = time.time() + 15.0
|
|
199
|
+
checked = None
|
|
200
|
+
target = ""
|
|
201
|
+
pane_command = ""
|
|
202
|
+
pane_pid = 0
|
|
203
|
+
pane_target = f"{tmux_name}:0.0"
|
|
204
|
+
ready_started_at = time.time()
|
|
205
|
+
while time.time() < deadline:
|
|
206
|
+
checked = subprocess.run(
|
|
207
|
+
[
|
|
208
|
+
"tmux",
|
|
209
|
+
"display-message",
|
|
210
|
+
"-p",
|
|
211
|
+
"-t",
|
|
212
|
+
pane_target,
|
|
213
|
+
"#S:#I.#P\t#{pane_current_command}\t#{pane_pid}",
|
|
214
|
+
],
|
|
215
|
+
stdout=subprocess.PIPE,
|
|
216
|
+
stderr=subprocess.PIPE,
|
|
217
|
+
text=True,
|
|
218
|
+
)
|
|
219
|
+
if checked.returncode != 0:
|
|
220
|
+
fail(
|
|
221
|
+
"tmux 已创建但 Codex 立即退出:"
|
|
222
|
+
+ (checked.stderr.strip() or "请检查远端 Codex CLI")
|
|
223
|
+
+ f";已保留 tmux {tmux_name} 供检查",
|
|
224
|
+
20,
|
|
225
|
+
)
|
|
226
|
+
checked_fields = checked.stdout.strip().split("\t")
|
|
227
|
+
target = checked_fields[0] if checked_fields else ""
|
|
228
|
+
pane_command = checked_fields[1] if len(checked_fields) > 1 else ""
|
|
229
|
+
try:
|
|
230
|
+
pane_pid = int(checked_fields[2]) if len(checked_fields) > 2 else 0
|
|
231
|
+
except Exception:
|
|
232
|
+
pane_pid = 0
|
|
233
|
+
# npm/global Codex installations commonly exec a Node entrypoint, so tmux
|
|
234
|
+
# reports node even though the pane is the intended Codex process.
|
|
235
|
+
if pane_command in ("codex", "node"):
|
|
236
|
+
break
|
|
237
|
+
time.sleep(0.2)
|
|
238
|
+
if pane_command not in ("codex", "node"):
|
|
239
|
+
fail(
|
|
240
|
+
"tmux 已创建但 Codex 未在 15 秒内保持运行"
|
|
241
|
+
+ (f"(pane command: {pane_command})" if pane_command else "")
|
|
242
|
+
+ f";已保留 tmux {tmux_name} 供检查",
|
|
243
|
+
25,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
# The Node process can exist before the TUI has finished restoring the thread.
|
|
247
|
+
# Inputs pasted during that gap are rendered briefly and then discarded by the
|
|
248
|
+
# final screen initialization, so do not report a usable tmux target yet.
|
|
249
|
+
ui_deadline = time.time() + 8.0
|
|
250
|
+
ui_ready = False
|
|
251
|
+
cwd_choice = "unchanged"
|
|
252
|
+
cwd_prompt_answered = False
|
|
253
|
+
preview = ""
|
|
254
|
+
while time.time() < ui_deadline:
|
|
255
|
+
captured = subprocess.run(
|
|
256
|
+
["tmux", "capture-pane", "-p", "-t", pane_target, "-S", "-80"],
|
|
257
|
+
stdout=subprocess.PIPE,
|
|
258
|
+
stderr=subprocess.PIPE,
|
|
259
|
+
text=True,
|
|
260
|
+
)
|
|
261
|
+
preview = captured.stdout
|
|
262
|
+
cwd_prompt = "Choose working directory to resume this session"
|
|
263
|
+
if (
|
|
264
|
+
captured.returncode == 0
|
|
265
|
+
and cwd_prompt in preview
|
|
266
|
+
and not cwd_prompt_answered
|
|
267
|
+
):
|
|
268
|
+
# Preserve the directory of the process being taken over. Rollouts are
|
|
269
|
+
# often created under /tmp and later continued from the real project or
|
|
270
|
+
# worktree, so choose option 2 (Current) instead of the original Session
|
|
271
|
+
# directory.
|
|
272
|
+
answered = subprocess.run(
|
|
273
|
+
["tmux", "send-keys", "-t", pane_target, "Down", "Enter"],
|
|
274
|
+
stdout=subprocess.PIPE,
|
|
275
|
+
stderr=subprocess.PIPE,
|
|
276
|
+
text=True,
|
|
277
|
+
)
|
|
278
|
+
if answered.returncode != 0:
|
|
279
|
+
fail(
|
|
280
|
+
"Codex 请求选择恢复目录,但无法确认当前工作目录:"
|
|
281
|
+
+ (answered.stderr.strip() or "tmux send-keys 失败")
|
|
282
|
+
+ f";已保留 tmux {tmux_name} 供检查",
|
|
283
|
+
27,
|
|
284
|
+
)
|
|
285
|
+
cwd_prompt_answered = True
|
|
286
|
+
cwd_choice = "current"
|
|
287
|
+
time.sleep(0.5)
|
|
288
|
+
continue
|
|
289
|
+
if (
|
|
290
|
+
captured.returncode == 0
|
|
291
|
+
and "OpenAI Codex" in preview
|
|
292
|
+
and cwd_prompt not in preview
|
|
293
|
+
):
|
|
294
|
+
# Let the input editor finish replacing the startup screen.
|
|
295
|
+
time.sleep(0.5)
|
|
296
|
+
ui_ready = True
|
|
297
|
+
break
|
|
298
|
+
time.sleep(0.2)
|
|
299
|
+
if not ui_ready:
|
|
300
|
+
fail(
|
|
301
|
+
"tmux 已创建但 Codex TUI 未在 8 秒内就绪"
|
|
302
|
+
+ f";已保留 tmux {tmux_name} 供检查",
|
|
303
|
+
26,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
captured = subprocess.run(
|
|
307
|
+
["tmux", "capture-pane", "-p", "-S", "-8", "-t", target],
|
|
308
|
+
stdout=subprocess.PIPE,
|
|
309
|
+
stderr=subprocess.DEVNULL,
|
|
310
|
+
text=True,
|
|
311
|
+
)
|
|
312
|
+
preview = captured.stdout.strip()[-1600:] if captured.returncode == 0 else ""
|
|
313
|
+
|
|
314
|
+
print(json.dumps({
|
|
315
|
+
"ok": True,
|
|
316
|
+
"killedPid": None,
|
|
317
|
+
"forced": False,
|
|
318
|
+
"tmux": tmux_name,
|
|
319
|
+
"target": target,
|
|
320
|
+
"paneCommand": pane_command,
|
|
321
|
+
"panePid": pane_pid,
|
|
322
|
+
"readyInMs": int((time.time() - ready_started_at) * 1000),
|
|
323
|
+
"cwdChoice": cwd_choice,
|
|
324
|
+
"preview": preview,
|
|
325
|
+
"cwd": expected_cwd,
|
|
326
|
+
"sessionId": session_id,
|
|
327
|
+
}, ensure_ascii=False, separators=(",", ":")))
|
|
328
328
|
`;
|