@pawlogic/dl 0.4.0-staging.ef43cd7 → 0.4.0-staging.efb7cde

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/dl-npm-update.sh CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/bin/sh
2
2
  # dl 沙箱内自更新 (npm 版本) —— 替代 dl-selfupdate.sh 的 GCS 路径。
3
3
  #
4
- # wrapper 在 exec dl 之前调一次本脚本。任何步骤失败一律 swallow,永远 exit 0,
5
- # 绝不阻塞业务 dl 调用。当次仍用旧 bundle,新 bundle 装完后**下次** dl 调用才生效
6
- # (agent 失败重试天然吃到新版)。
4
+ # wrapper 在 exec dl 之前同步调一次本脚本。更新失败不阻止继续调用已有 bundle。
5
+ # --cli 绕过 TTL,输出 outcome 给 self-update 命令;安装与恢复仍使用同一把锁。
7
6
  #
8
7
  # dist-tag 来源,优先级从高到低:
9
8
  # 1. PI_DL_DIST_TAG —— hono-pi-e2b 每条命令注入 (e2b-operations.ts),反映
@@ -37,6 +36,11 @@ STAMP=/tmp/dl-npm-checkedat
37
36
  LOCK=/tmp/dl-npm-update.lock
38
37
  TTL=300
39
38
  PACKAGE="@pawlogic/dl"
39
+ # 独立下发恢复脚本时,拒绝被尚未包含修复的 npm 包降级。
40
+ UPDATER_REVISION=3
41
+ CLI=false
42
+ [ "${1:-}" = "--cli" ] && CLI=true
43
+ RECOVERED=false
40
44
 
41
45
  # 注入值优先,回落到 wrapper 烤死的值(理由见文件头)。
42
46
  #
@@ -58,13 +62,24 @@ elif _valid_tag "${DL_NPM_DIST_TAG:-}"; then
58
62
  fi
59
63
 
60
64
  # ── 观测上报: fire-and-forget JSON POST,失败 swallow ────────────────────────
61
- # args: outcome duration_ms [error_tail_file]
65
+ # args: outcome duration_ms [error_tail_file] [local_error_code]
62
66
  # error_tail_file 仅 npm_fail 分支传入,读末尾 ~200 字节带上去 —— 之前 npm stderr
63
67
  # 全 swallow,根本看不到为什么失败,fleet 升级率 0.24% 都查不到根因。
64
68
  report_event() {
65
69
  _outcome="$1"
66
70
  _dur="${2:-null}"
67
71
  _errfile="${3:-}"
72
+ _code="${4:-}"
73
+ _syscall=""
74
+ if [ -n "$_errfile" ] && [ -f "$_errfile" ]; then
75
+ _code=$(awk '/^npm (error|ERR!) code [A-Z0-9_]+$/ {print $4; exit}' "$_errfile")
76
+ _syscall=$(awk '/^npm (error|ERR!) syscall [a-z]+$/ {print $4; exit}' "$_errfile")
77
+ fi
78
+ if [ "$CLI" = true ]; then
79
+ printf '%s\n' "$_outcome"
80
+ printf 'code=%s syscall=%s recovered=%s\n' "$_code" "$_syscall" "$RECOVERED" >&2
81
+ return 0
82
+ fi
68
83
  [ -z "${CALLBACK_SERVER_URL:-}" ] && return 0
69
84
  [ -z "${DL_PROXY_TOKEN:-}" ] && return 0
70
85
  command -v curl >/dev/null 2>&1 || return 0
@@ -118,9 +133,9 @@ report_event() {
118
133
  curl -fsS -m 2 -X POST "${CALLBACK_SERVER_URL%/}/sandbox-rpc/dl/selfupdate-event" \
119
134
  -H 'content-type: application/json' \
120
135
  -H "authorization: Bearer ${DL_PROXY_TOKEN}" \
121
- -d "{\"trigger\":\"wrapper\",\"dist_tag\":\"$_tag\",\"outcome\":\"$_outcome\",\"duration_ms\":$_dur,\"installed_version\":\"$_ver\",\"build_short_sha\":null,\"error_tail\":$_err_json,\"project_id\":$_proj_json,\"mode\":$_mode_json,\"user_tier\":$_tier_json,\"user_id\":$_user_json,\"prompt_id\":$_prompt_json,\"tool_call_id\":$_toolcall_json,\"sandbox_id\":$_sandbox_json}" \
136
+ -d "{\"trigger\":\"wrapper\",\"dist_tag\":\"$_tag\",\"outcome\":\"$_outcome\",\"duration_ms\":$_dur,\"installed_version\":\"$_ver\",\"build_short_sha\":null,\"error_tail\":$_err_json,\"error_code\":\"$_code\",\"error_syscall\":\"$_syscall\",\"recovered\":$RECOVERED,\"project_id\":$_proj_json,\"mode\":$_mode_json,\"user_tier\":$_tier_json,\"user_id\":$_user_json,\"prompt_id\":$_prompt_json,\"tool_call_id\":$_toolcall_json,\"sandbox_id\":$_sandbox_json}" \
122
137
  >/dev/null 2>&1 || true
123
- ) &
138
+ ) 9>&- &
124
139
  }
125
140
 
126
141
  # 采样 TTL hit 上报: 10 选 1。awk 比 $RANDOM 更便携 (busybox sh 默认没 $RANDOM)。
@@ -138,42 +153,92 @@ if ! command -v npm >/dev/null 2>&1; then
138
153
  exit 0
139
154
  fi
140
155
 
156
+ # TTL、等待锁和安装结果必须采用同一就绪标准,目录或空文件都不能当成 CLI。
157
+ cli_files_ready() {
158
+ for _entry_name in dl ilands; do
159
+ _entry_file=/home/user/.local/lib/node_modules/@pawlogic/dl/$_entry_name.mjs
160
+ [ -f "$_entry_file" ] && [ -s "$_entry_file" ] && [ -r "$_entry_file" ] || return 1
161
+ done
162
+ }
163
+
141
164
  # TTL 拦截。stamp 格式 `<epoch> <tag>`,stamp 损坏当 0 处理。
142
165
  #
143
166
  # tag 也要存:只比时间戳的话,切完 lane 的头 300s 会被 TTL short-circuit 掉,
144
167
  # 整个 turn 都还在用另一条 lane 的 dl —— 自愈就不"立刻"了。tag 跟本次解析出
145
168
  # 来的不一致直接跳过 TTL 重装。老格式(只有 epoch)解出来的 tag 是空串,同样
146
169
  # 判成不一致,升级后第一次调用自然会重装一遍。
147
- now=$(date +%s 2>/dev/null) || exit 0
148
- last=0
149
- last_tag=""
150
- if [ -f "$STAMP" ]; then
151
- _stamp_line=$(head -n1 "$STAMP" 2>/dev/null || echo "")
152
- last="${_stamp_line%% *}"
153
- case "$_stamp_line" in
154
- *' '*) last_tag="${_stamp_line#* }" ;;
155
- *) last_tag="" ;;
156
- esac
157
- case "$last" in
158
- ''|*[!0-9]*) last=0 ;;
159
- esac
160
- fi
161
- if [ "$last_tag" = "$TAG" ]; then
162
- delta=$((now - last))
163
- if [ "$delta" -ge 0 ] && [ "$delta" -lt "$TTL" ]; then
164
- if should_sample_ttl; then
165
- report_event ttl_hit null
170
+ check_ttl() {
171
+ now=$(date +%s 2>/dev/null) || exit 0
172
+ last=0
173
+ last_tag=""
174
+ if [ -f "$STAMP" ]; then
175
+ _stamp_line=$(head -n1 "$STAMP" 2>/dev/null || echo "")
176
+ last="${_stamp_line%% *}"
177
+ case "$_stamp_line" in
178
+ *' '*) last_tag="${_stamp_line#* }" ;;
179
+ *) last_tag="" ;;
180
+ esac
181
+ case "$last" in
182
+ # 限制为十位以内的规范十进制,防溢出及前导零触发八进制解析。
183
+ ''|*[!0-9]*|???????????*|0?*) last=0 ;;
184
+ esac
185
+ fi
186
+ if [ "$CLI" = false ] && cli_files_ready && [ "$last_tag" = "$TAG" ]; then
187
+ delta=$((now - last))
188
+ if [ "$delta" -ge 0 ] && [ "$delta" -lt "$TTL" ]; then
189
+ if should_sample_ttl; then
190
+ report_event ttl_hit null
191
+ fi
192
+ exit 0
166
193
  fi
167
- exit 0
168
194
  fi
169
- fi
195
+ }
196
+
197
+ check_ttl
170
198
 
171
199
  # 单飞锁:并发 dl 调用同时进来时只让一个跑 npm install,其他直接退出。
172
200
  # flock 不一定有(busybox sh) → mkdir 兜底原子锁。
201
+ install_package() {
202
+ # 在持锁的 shell 内限制 npm 耗时,避免 CLI 超时只杀 shell 却留下安装子进程。
203
+ HOME=/home/user timeout -k 5s 30s npm install -g --prefix=/home/user/.local \
204
+ --no-audit --no-fund --no-update-notifier --force \
205
+ "$PACKAGE@$TAG" >/dev/null 2>"$_errlog"
206
+ _install_status=$?
207
+ case "$_install_status" in
208
+ 124|137) printf 'npm error code ETIMEDOUT\n' >> "$_errlog" ;;
209
+ esac
210
+ return "$_install_status"
211
+ }
212
+
213
+ # 只备份本次 ENOTEMPTY 明确指向的 dl 临时目录,不扫描/删除其它包或工作文件。
214
+ recover_retired_directory() {
215
+ node - "$_errlog" <<'NODE'
216
+ const fs = require('node:fs');
217
+ const path = require('node:path');
218
+ try {
219
+ const log = fs.readFileSync(process.argv[2], 'utf8');
220
+ const field = name => log.match(new RegExp('^npm (?:error|ERR!) ' + name + ' (.+)$', 'm'))?.[1];
221
+ const scope = '/home/user/.local/lib/node_modules/@pawlogic';
222
+ const dest = field('dest');
223
+ if (field('code') !== 'ENOTEMPTY' || field('syscall') !== 'rename'
224
+ || field('path') !== scope + '/dl' || !dest
225
+ || path.dirname(dest) !== scope || !/^\.dl-[A-Za-z0-9]+$/.test(path.basename(dest))
226
+ || fs.realpathSync(scope) !== scope || !fs.lstatSync(dest).isDirectory()) process.exit(1);
227
+ const backup = fs.mkdtempSync('/home/user/.local/lib/dl-update-backup-');
228
+ fs.renameSync(dest, path.join(backup, path.basename(dest)));
229
+ } catch { process.exit(1); }
230
+ NODE
231
+ }
232
+
173
233
  run_install() {
234
+ # 首次安装等待锁期间,另一个入口可能已经装好并写入 TTL。
235
+ check_ttl
174
236
  _t0=$(date +%s 2>/dev/null || echo 0)
175
237
  # stderr 写到独立文件 npm_fail 分支带末尾给观测端;stdout 仍丢掉。
176
- _errlog=$(mktemp 2>/dev/null || echo "/tmp/dl-npm-err.$$")
238
+ if ! _errlog=$(mktemp 2>/dev/null); then
239
+ report_event npm_fail null "" LOCAL_IO_ERROR
240
+ return
241
+ fi
177
242
  # 必须带 --prefix=/home/user/.local 跟 HOME=/home/user,跟 sandbox-template
178
243
  # 首次安装那一段对齐 (packages/sandbox-template/scripts/publish-e2b-template.ts
179
244
  # 里 wrapper 写法)。沙箱进程 owner 是 user,无 /usr/lib/node_modules 写权限;
@@ -187,12 +252,22 @@ run_install() {
187
252
  # --force to overwrite"); --force 允许 npm 覆盖。Honeycomb 上 staging
188
253
  # dl-selfupdate dataset 看到的所有 npm_fail outcome (#741 接通后) 都是这条,
189
254
  # 加 --force 就清零。
190
- if HOME=/home/user npm install -g --prefix=/home/user/.local \
191
- --no-audit --no-fund --no-update-notifier --force \
192
- "$PACKAGE@$TAG" >/dev/null 2>"$_errlog"; then
255
+ if install_package; then
193
256
  _outcome=ok
194
257
  else
195
258
  _outcome=npm_fail
259
+ if recover_retired_directory; then
260
+ if install_package; then
261
+ _outcome=ok
262
+ RECOVERED=true
263
+ fi
264
+ fi
265
+ fi
266
+ # npm exit 0 还不足以证明两个入口完整,缺失时不记为成功。
267
+ if [ "$_outcome" = ok ] && ! cli_files_ready; then
268
+ _outcome=npm_fail
269
+ RECOVERED=false
270
+ printf 'npm error code INCOMPLETE_INSTALL\n' >> "$_errlog"
196
271
  fi
197
272
  _t1=$(date +%s 2>/dev/null || echo 0)
198
273
  _dur_ms=$(( (_t1 - _t0) * 1000 ))
@@ -201,17 +276,39 @@ run_install() {
201
276
  # npm shim, 下次调用绕过 wrapper, dl 的 DL_NPM_DIST_TAG export 跑不到, 自更新
202
277
  # 链路死掉; ilands 也跟着丢 wrapper(虽然 ilands 自己不需要 env, 但 update.sh
203
278
  # 入口被绕过, 跟 dl 同时挂)。
204
- ln -sf /usr/local/bin/dl /home/user/.local/bin/dl >/dev/null 2>&1 || true
205
- ln -sf /usr/local/bin/ilands /home/user/.local/bin/ilands >/dev/null 2>&1 || true
279
+ for _name in dl ilands; do
280
+ _wrapper=/usr/local/bin/$_name
281
+ _entry=/home/user/.local/bin/$_name
282
+ [ -f "$_wrapper" ] && [ -x "$_wrapper" ] && [ ! -L "$_wrapper" ] || continue
283
+ if [ -L "$_entry" ]; then
284
+ [ "$(readlink -f "$_entry")" = "/home/user/.local/lib/node_modules/@pawlogic/dl/$_name.mjs" ] || continue
285
+ elif [ -e "$_entry" ]; then
286
+ continue
287
+ fi
288
+ ln -sfnT "$_wrapper" "$_entry" >/dev/null 2>&1 || true
289
+ done
206
290
  # 自我热更: @pawlogic/dl npm 包里 ship 了本脚本的最新版 (packages/dl/sandbox-assets/
207
291
  # dl-npm-update.sh), npm install 完成后覆盖一份到 wrapper 调用的固定路径。
208
292
  # 1 代 lag: 本次仍跑老逻辑, 新逻辑下次 dl 调用生效 —— 比"等 e2b template republish
209
293
  # + 老 snapshot 自然下线"快好几个量级, snapshot resume 的沙箱也能跟上。
210
- # 当前进程文件是 shell 解释器一次性读入内存的, 覆盖自身不会撕裂正在执行的循环。
211
- if [ -f /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh ]; then
212
- cp /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh \
213
- /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
214
- chmod +x /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
294
+ # 原子替换,避免覆盖当前 shell 仍在读取的 inode;失败时不从旧包回滚恢复脚本。
295
+ _packaged_updater=/home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh
296
+ _packaged_revision=$(awk -F= '/^UPDATER_REVISION=[0-9]+$/ {print $2; exit}' "$_packaged_updater" 2>/dev/null || true)
297
+ _fixed_updater=/home/user/.local/lib/dl-npm-update.sh
298
+ _fixed_revision=$(awk -F= '/^UPDATER_REVISION=[0-9]+$/ {print $2; exit}' "$_fixed_updater" 2>/dev/null || true)
299
+ # 显式 self-update 可能运行旧包脚本,也不能覆盖 Runtime 已下发的更高代。
300
+ if [ "$_outcome" = ok ] && [ ! -L "$_fixed_updater" ] \
301
+ && [ -f "$_packaged_updater" ] && [ ! -L "$_packaged_updater" ] \
302
+ && [ "${_packaged_revision:-0}" -ge "$UPDATER_REVISION" ] 2>/dev/null \
303
+ && [ "${_packaged_revision:-0}" -ge "${_fixed_revision:-0}" ] 2>/dev/null; then
304
+ if _refresh=$(mktemp /home/user/.local/lib/dl-npm-update.XXXXXX 2>/dev/null); then
305
+ if cp "$_packaged_updater" "$_refresh" 2>/dev/null \
306
+ && sh -n "$_refresh" 2>/dev/null \
307
+ && chmod 755 "$_refresh" 2>/dev/null; then
308
+ mv -fT "$_refresh" "$_fixed_updater" 2>/dev/null || true
309
+ fi
310
+ rm -f "$_refresh" 2>/dev/null || true
311
+ fi
215
312
  fi
216
313
  echo "$now $TAG" > "$STAMP"
217
314
  if [ "$_outcome" = "npm_fail" ]; then
@@ -223,8 +320,17 @@ run_install() {
223
320
  }
224
321
 
225
322
  if command -v flock >/dev/null 2>&1; then
323
+ if [ -L "$LOCK" ] || { [ -e "$LOCK" ] && [ ! -f "$LOCK" ]; }; then
324
+ report_event lock_skip null "" LOCK_PATH_INVALID
325
+ exit 0
326
+ fi
226
327
  (
227
- if ! flock -n 9; then
328
+ # 首次安装允许等另一入口完成,已有 bundle 的日常调用仍直接跳过。
329
+ _lock_wait=0
330
+ if ! cli_files_ready; then
331
+ _lock_wait=30
332
+ fi
333
+ if ! flock -w "$_lock_wait" 9; then
228
334
  report_event lock_skip null
229
335
  exit 0
230
336
  fi