@pawlogic/dl 0.4.0-staging.eb79d7d → 0.4.0-staging.ed34cde

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,13 +1,21 @@
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
- # 触发条件:DL_NPM_DIST_TAG 已 bake 到 sandbox env(prod=latest,staging=staging;PR
9
- # preview 留空 → 本脚本直接 exit,沙箱内 dl 锁在 template 烤进去那版,便于 PR 间
10
- # 复现 / 互不污染)。
7
+ # dist-tag 来源,优先级从高到低:
8
+ # 1. PI_DL_DIST_TAG —— hono-pi-e2b 每条命令注入 (e2b-operations.ts),反映
9
+ # **当前**这条 lane。
10
+ # 2. DL_NPM_DIST_TAG —— template build 时按 alias 烤进 wrapper 第二行
11
+ # (prod=latest,staging=staging;PR preview 留空 → 本脚本直接 exit,沙箱内
12
+ # dl 锁在 template 烤进去那版,便于 PR 间复现 / 互不污染)。
13
+ #
14
+ # 1 必须压过 2:project → sandbox_id 这条记录是 prod / staging 共享的 (staging
15
+ # 切片连的是同一个 pi-agent-prod-pg),切换环境时会 resume 到对方那台沙箱,而 2
16
+ # 跟着**建沙箱时**那个模板走 —— prod 建的沙箱被 staging resume 之后仍然会去装
17
+ # @latest。wrapper 是烤死在镜像里的改不动,但本脚本每次 npm install 后会从新包里
18
+ # 拷一份覆盖自己 (见下方"自我热更"),所以存量沙箱两代之内就能吃到这条优先级。
11
19
  #
12
20
  # 鉴权: 不需要。包发到 npmjs.com 的 @pawlogic scope (public),npm 默认 registry
13
21
  # 就是 npmjs.com,匿名 download 直接通,沙箱没有 ~/.npmrc。
@@ -28,19 +36,54 @@ STAMP=/tmp/dl-npm-checkedat
28
36
  LOCK=/tmp/dl-npm-update.lock
29
37
  TTL=300
30
38
  PACKAGE="@pawlogic/dl"
39
+ # 独立下发恢复脚本时,拒绝被尚未包含修复的 npm 包降级。
40
+ UPDATER_REVISION=3
41
+ CLI=false
42
+ [ "${1:-}" = "--cli" ] && CLI=true
43
+ RECOVERED=false
44
+
45
+ # 注入值优先,回落到 wrapper 烤死的值(理由见文件头)。
46
+ #
47
+ # 逐个来源单独校验、非法就继续往下回落,而不是先选后校验 —— 后者会让一个
48
+ # 写坏的 PI_DL_DIST_TAG 把整台沙箱的自更新彻底关掉,而不是退回模板烤死的值。
49
+ # 规则跟 self-update.ts 的 resolveDistTag 保持一致(首字符必须是字母数字)。
50
+ # 这个值会流到 npm install 的参数和 stamp 文件里,不合法一律当没给。
51
+ _valid_tag() {
52
+ case "${1:-}" in
53
+ '' | [!A-Za-z0-9]* | *[!A-Za-z0-9._-]*) return 1 ;;
54
+ *) return 0 ;;
55
+ esac
56
+ }
57
+ TAG=""
58
+ if _valid_tag "${PI_DL_DIST_TAG:-}"; then
59
+ TAG="$PI_DL_DIST_TAG"
60
+ elif _valid_tag "${DL_NPM_DIST_TAG:-}"; then
61
+ TAG="$DL_NPM_DIST_TAG"
62
+ fi
31
63
 
32
64
  # ── 观测上报: fire-and-forget JSON POST,失败 swallow ────────────────────────
33
- # args: outcome duration_ms [error_tail_file]
65
+ # args: outcome duration_ms [error_tail_file] [local_error_code]
34
66
  # error_tail_file 仅 npm_fail 分支传入,读末尾 ~200 字节带上去 —— 之前 npm stderr
35
67
  # 全 swallow,根本看不到为什么失败,fleet 升级率 0.24% 都查不到根因。
36
68
  report_event() {
37
69
  _outcome="$1"
38
70
  _dur="${2:-null}"
39
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
40
83
  [ -z "${CALLBACK_SERVER_URL:-}" ] && return 0
41
84
  [ -z "${DL_PROXY_TOKEN:-}" ] && return 0
42
85
  command -v curl >/dev/null 2>&1 || return 0
43
- _tag="${DL_NPM_DIST_TAG:-unknown}"
86
+ _tag="${TAG:-unknown}"
44
87
  case "$_tag" in
45
88
  latest|staging) ;;
46
89
  *) _tag="unknown" ;;
@@ -90,9 +133,9 @@ report_event() {
90
133
  curl -fsS -m 2 -X POST "${CALLBACK_SERVER_URL%/}/sandbox-rpc/dl/selfupdate-event" \
91
134
  -H 'content-type: application/json' \
92
135
  -H "authorization: Bearer ${DL_PROXY_TOKEN}" \
93
- -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}" \
94
137
  >/dev/null 2>&1 || true
95
- ) &
138
+ ) 9>&- &
96
139
  }
97
140
 
98
141
  # 采样 TTL hit 上报: 10 选 1。awk 比 $RANDOM 更便携 (busybox sh 默认没 $RANDOM)。
@@ -100,8 +143,8 @@ should_sample_ttl() {
100
143
  awk 'BEGIN{srand(); exit !(int(rand()*10)==0)}' 2>/dev/null
101
144
  }
102
145
 
103
- # 环境不全 → no-op。staging / prod 才烤 DL_NPM_DIST_TAG,PR preview / 本地裸跑空。
104
- if [ -z "${DL_NPM_DIST_TAG:-}" ]; then
146
+ # 环境不全 → no-op。staging / prod 两个来源至少有一个有值,PR preview / 本地裸跑空。
147
+ if [ -z "$TAG" ]; then
105
148
  report_event no_tag null
106
149
  exit 0
107
150
  fi
@@ -110,28 +153,92 @@ if ! command -v npm >/dev/null 2>&1; then
110
153
  exit 0
111
154
  fi
112
155
 
113
- # TTL 拦截。stamp 损坏当 0 处理。
114
- now=$(date +%s 2>/dev/null) || exit 0
115
- if [ -f "$STAMP" ]; then
116
- last=$(cat "$STAMP" 2>/dev/null || echo 0)
117
- case "$last" in
118
- ''|*[!0-9]*) last=0 ;;
119
- esac
120
- delta=$((now - last))
121
- if [ "$delta" -ge 0 ] && [ "$delta" -lt "$TTL" ]; then
122
- if should_sample_ttl; then
123
- report_event ttl_hit null
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
+
164
+ # TTL 拦截。stamp 格式 `<epoch> <tag>`,stamp 损坏当 0 处理。
165
+ #
166
+ # tag 也要存:只比时间戳的话,切完 lane 的头 300s 会被 TTL short-circuit 掉,
167
+ # 整个 turn 都还在用另一条 lane 的 dl —— 自愈就不"立刻"了。tag 跟本次解析出
168
+ # 来的不一致直接跳过 TTL 重装。老格式(只有 epoch)解出来的 tag 是空串,同样
169
+ # 判成不一致,升级后第一次调用自然会重装一遍。
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
124
193
  fi
125
- exit 0
126
194
  fi
127
- fi
195
+ }
196
+
197
+ check_ttl
128
198
 
129
199
  # 单飞锁:并发 dl 调用同时进来时只让一个跑 npm install,其他直接退出。
130
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
+
131
233
  run_install() {
234
+ # 首次安装等待锁期间,另一个入口可能已经装好并写入 TTL。
235
+ check_ttl
132
236
  _t0=$(date +%s 2>/dev/null || echo 0)
133
237
  # stderr 写到独立文件 npm_fail 分支带末尾给观测端;stdout 仍丢掉。
134
- _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
135
242
  # 必须带 --prefix=/home/user/.local 跟 HOME=/home/user,跟 sandbox-template
136
243
  # 首次安装那一段对齐 (packages/sandbox-template/scripts/publish-e2b-template.ts
137
244
  # 里 wrapper 写法)。沙箱进程 owner 是 user,无 /usr/lib/node_modules 写权限;
@@ -145,12 +252,22 @@ run_install() {
145
252
  # --force to overwrite"); --force 允许 npm 覆盖。Honeycomb 上 staging
146
253
  # dl-selfupdate dataset 看到的所有 npm_fail outcome (#741 接通后) 都是这条,
147
254
  # 加 --force 就清零。
148
- if HOME=/home/user npm install -g --prefix=/home/user/.local \
149
- --no-audit --no-fund --no-update-notifier --force \
150
- "$PACKAGE@$DL_NPM_DIST_TAG" >/dev/null 2>"$_errlog"; then
255
+ if install_package; then
151
256
  _outcome=ok
152
257
  else
153
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"
154
271
  fi
155
272
  _t1=$(date +%s 2>/dev/null || echo 0)
156
273
  _dur_ms=$(( (_t1 - _t0) * 1000 ))
@@ -159,19 +276,41 @@ run_install() {
159
276
  # npm shim, 下次调用绕过 wrapper, dl 的 DL_NPM_DIST_TAG export 跑不到, 自更新
160
277
  # 链路死掉; ilands 也跟着丢 wrapper(虽然 ilands 自己不需要 env, 但 update.sh
161
278
  # 入口被绕过, 跟 dl 同时挂)。
162
- ln -sf /usr/local/bin/dl /home/user/.local/bin/dl >/dev/null 2>&1 || true
163
- 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
164
290
  # 自我热更: @pawlogic/dl npm 包里 ship 了本脚本的最新版 (packages/dl/sandbox-assets/
165
291
  # dl-npm-update.sh), npm install 完成后覆盖一份到 wrapper 调用的固定路径。
166
292
  # 1 代 lag: 本次仍跑老逻辑, 新逻辑下次 dl 调用生效 —— 比"等 e2b template republish
167
293
  # + 老 snapshot 自然下线"快好几个量级, snapshot resume 的沙箱也能跟上。
168
- # 当前进程文件是 shell 解释器一次性读入内存的, 覆盖自身不会撕裂正在执行的循环。
169
- if [ -f /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh ]; then
170
- cp /home/user/.local/lib/node_modules/@pawlogic/dl/dl-npm-update.sh \
171
- /home/user/.local/lib/dl-npm-update.sh 2>/dev/null || true
172
- 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
173
312
  fi
174
- echo "$now" > "$STAMP"
313
+ echo "$now $TAG" > "$STAMP"
175
314
  if [ "$_outcome" = "npm_fail" ]; then
176
315
  report_event "$_outcome" "$_dur_ms" "$_errlog"
177
316
  else
@@ -181,8 +320,17 @@ run_install() {
181
320
  }
182
321
 
183
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
184
327
  (
185
- 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
186
334
  report_event lock_skip null
187
335
  exit 0
188
336
  fi