@pawlogic/dl 0.42.1 → 0.43.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/dl-npm-update.sh CHANGED
@@ -37,7 +37,7 @@ LOCK=/tmp/dl-npm-update.lock
37
37
  TTL=300
38
38
  PACKAGE="@pawlogic/dl"
39
39
  # 独立下发恢复脚本时,拒绝被尚未包含修复的 npm 包降级。
40
- UPDATER_REVISION=2
40
+ UPDATER_REVISION=3
41
41
  CLI=false
42
42
  [ "${1:-}" = "--cli" ] && CLI=true
43
43
  RECOVERED=false
@@ -62,14 +62,14 @@ elif _valid_tag "${DL_NPM_DIST_TAG:-}"; then
62
62
  fi
63
63
 
64
64
  # ── 观测上报: fire-and-forget JSON POST,失败 swallow ────────────────────────
65
- # args: outcome duration_ms [error_tail_file]
65
+ # args: outcome duration_ms [error_tail_file] [local_error_code]
66
66
  # error_tail_file 仅 npm_fail 分支传入,读末尾 ~200 字节带上去 —— 之前 npm stderr
67
67
  # 全 swallow,根本看不到为什么失败,fleet 升级率 0.24% 都查不到根因。
68
68
  report_event() {
69
69
  _outcome="$1"
70
70
  _dur="${2:-null}"
71
71
  _errfile="${3:-}"
72
- _code=""
72
+ _code="${4:-}"
73
73
  _syscall=""
74
74
  if [ -n "$_errfile" ] && [ -f "$_errfile" ]; then
75
75
  _code=$(awk '/^npm (error|ERR!) code [A-Z0-9_]+$/ {print $4; exit}' "$_errfile")
@@ -153,6 +153,14 @@ if ! command -v npm >/dev/null 2>&1; then
153
153
  exit 0
154
154
  fi
155
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
+
156
164
  # TTL 拦截。stamp 格式 `<epoch> <tag>`,stamp 损坏当 0 处理。
157
165
  #
158
166
  # tag 也要存:只比时间戳的话,切完 lane 的头 300s 会被 TTL short-circuit 掉,
@@ -171,11 +179,11 @@ check_ttl() {
171
179
  *) last_tag="" ;;
172
180
  esac
173
181
  case "$last" in
174
- ''|*[!0-9]*) last=0 ;;
182
+ # 限制为十位以内的规范十进制,防溢出及前导零触发八进制解析。
183
+ ''|*[!0-9]*|???????????*|0?*) last=0 ;;
175
184
  esac
176
185
  fi
177
- if [ "$CLI" = false ] && [ -f /home/user/.local/lib/node_modules/@pawlogic/dl/dl.mjs ] \
178
- && [ -f /home/user/.local/lib/node_modules/@pawlogic/dl/ilands.mjs ] && [ "$last_tag" = "$TAG" ]; then
186
+ if [ "$CLI" = false ] && cli_files_ready && [ "$last_tag" = "$TAG" ]; then
179
187
  delta=$((now - last))
180
188
  if [ "$delta" -ge 0 ] && [ "$delta" -lt "$TTL" ]; then
181
189
  if should_sample_ttl; then
@@ -227,7 +235,10 @@ run_install() {
227
235
  check_ttl
228
236
  _t0=$(date +%s 2>/dev/null || echo 0)
229
237
  # stderr 写到独立文件 npm_fail 分支带末尾给观测端;stdout 仍丢掉。
230
- _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
231
242
  # 必须带 --prefix=/home/user/.local 跟 HOME=/home/user,跟 sandbox-template
232
243
  # 首次安装那一段对齐 (packages/sandbox-template/scripts/publish-e2b-template.ts
233
244
  # 里 wrapper 写法)。沙箱进程 owner 是 user,无 /usr/lib/node_modules 写权限;
@@ -253,8 +264,7 @@ run_install() {
253
264
  fi
254
265
  fi
255
266
  # npm exit 0 还不足以证明两个入口完整,缺失时不记为成功。
256
- if [ "$_outcome" = ok ] && { [ ! -s /home/user/.local/lib/node_modules/@pawlogic/dl/dl.mjs ] \
257
- || [ ! -s /home/user/.local/lib/node_modules/@pawlogic/dl/ilands.mjs ]; }; then
267
+ if [ "$_outcome" = ok ] && ! cli_files_ready; then
258
268
  _outcome=npm_fail
259
269
  RECOVERED=false
260
270
  printf 'npm error code INCOMPLETE_INSTALL\n' >> "$_errlog"
@@ -266,8 +276,17 @@ run_install() {
266
276
  # npm shim, 下次调用绕过 wrapper, dl 的 DL_NPM_DIST_TAG export 跑不到, 自更新
267
277
  # 链路死掉; ilands 也跟着丢 wrapper(虽然 ilands 自己不需要 env, 但 update.sh
268
278
  # 入口被绕过, 跟 dl 同时挂)。
269
- ln -sf /usr/local/bin/dl /home/user/.local/bin/dl >/dev/null 2>&1 || true
270
- 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
271
290
  # 自我热更: @pawlogic/dl npm 包里 ship 了本脚本的最新版 (packages/dl/sandbox-assets/
272
291
  # dl-npm-update.sh), npm install 完成后覆盖一份到 wrapper 调用的固定路径。
273
292
  # 1 代 lag: 本次仍跑老逻辑, 新逻辑下次 dl 调用生效 —— 比"等 e2b template republish
@@ -275,12 +294,21 @@ run_install() {
275
294
  # 原子替换,避免覆盖当前 shell 仍在读取的 inode;失败时不从旧包回滚恢复脚本。
276
295
  _packaged_updater=/home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh
277
296
  _packaged_revision=$(awk -F= '/^UPDATER_REVISION=[0-9]+$/ {print $2; exit}' "$_packaged_updater" 2>/dev/null || true)
278
- if [ "$_outcome" = ok ] && [ "${_packaged_revision:-0}" -ge "$UPDATER_REVISION" ]; then
279
- _refresh=$(mktemp /home/user/.local/lib/dl-npm-update.XXXXXX)
280
- cp /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh \
281
- "$_refresh" 2>/dev/null \
282
- && chmod +x "$_refresh" 2>/dev/null \
283
- && mv -f "$_refresh" /home/user/.local/lib/dl-npm-update.sh 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
284
312
  fi
285
313
  echo "$now $TAG" > "$STAMP"
286
314
  if [ "$_outcome" = "npm_fail" ]; then
@@ -292,11 +320,14 @@ run_install() {
292
320
  }
293
321
 
294
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
295
327
  (
296
328
  # 首次安装允许等另一入口完成,已有 bundle 的日常调用仍直接跳过。
297
329
  _lock_wait=0
298
- if [ ! -f /home/user/.local/lib/node_modules/@pawlogic/dl/dl.mjs ] \
299
- || [ ! -f /home/user/.local/lib/node_modules/@pawlogic/dl/ilands.mjs ]; then
330
+ if ! cli_files_ready; then
300
331
  _lock_wait=30
301
332
  fi
302
333
  if ! flock -w "$_lock_wait" 9; then